|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftNIO open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +// Xcode's Archive builds with Xcode's Package support struggle with empty .c files |
| 16 | +// (https://bugs.swift.org/browse/SR-12939). |
| 17 | +void CNIOBSD_i_do_nothing_just_working_around_a_darwin_toolchain_bug(void) {} |
| 18 | + |
| 19 | +#if defined(__OpenBSD__) |
| 20 | + |
| 21 | +#include <CNIOBSD.h> |
| 22 | +#include <pthread.h> |
| 23 | +#include <assert.h> |
| 24 | +#include <unistd.h> |
| 25 | + |
| 26 | +int CNIOBSD_pthread_set_name_np(pthread_t thread, const char *name) { |
| 27 | + pthread_set_name_np(thread, name); |
| 28 | + return 0; |
| 29 | +} |
| 30 | + |
| 31 | +int CNIOBSD_pthread_get_name_np(pthread_t thread, char *name, size_t len) { |
| 32 | + pthread_get_name_np(thread, name, len); |
| 33 | + return 0; |
| 34 | +} |
| 35 | + |
| 36 | +int CNIOBSD_sendmmsg(int sockfd, CNIOBSD_mmsghdr *msgvec, unsigned int vlen, int flags) { |
| 37 | + // This is technically undefined behaviour, but it's basically fine because these types are the same size, and we |
| 38 | + // don't think the compiler is inclined to blow anything up here. |
| 39 | + // This comment is from CNIOLinux, but I haven't reverified this applies for OpenBSD. |
| 40 | + return sendmmsg(sockfd, (struct mmsghdr *)msgvec, vlen, flags); |
| 41 | +} |
| 42 | + |
| 43 | +int CNIOBSD_recvmmsg(int sockfd, CNIOBSD_mmsghdr *msgvec, unsigned int vlen, int flags, struct timespec *timeout) { |
| 44 | + // This is technically undefined behaviour, but it's basically fine because these types are the same size, and we |
| 45 | + // don't think the compiler is inclined to blow anything up here. |
| 46 | + // This comment is from CNIOLinux, but I haven't reverified this applies for OpenBSD. |
| 47 | + return recvmmsg(sockfd, (struct mmsghdr *)msgvec, vlen, flags, timeout); |
| 48 | +} |
| 49 | + |
| 50 | +int CNIOBSD_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { |
| 51 | + return accept4(sockfd, addr, addrlen, flags); |
| 52 | +} |
| 53 | + |
| 54 | +struct cmsghdr *CNIOBSD_CMSG_FIRSTHDR(const struct msghdr *mhdr) { |
| 55 | + assert(mhdr != NULL); |
| 56 | + return CMSG_FIRSTHDR(mhdr); |
| 57 | +} |
| 58 | + |
| 59 | +struct cmsghdr *CNIOBSD_CMSG_NXTHDR(struct msghdr *mhdr, struct cmsghdr *cmsg) { |
| 60 | + assert(mhdr != NULL); |
| 61 | + assert(cmsg != NULL); |
| 62 | + return CMSG_NXTHDR(mhdr, cmsg); |
| 63 | +} |
| 64 | + |
| 65 | +const void *CNIOBSD_CMSG_DATA(const struct cmsghdr *cmsg) { |
| 66 | + assert(cmsg != NULL); |
| 67 | + return CMSG_DATA(cmsg); |
| 68 | +} |
| 69 | + |
| 70 | +void *CNIOBSD_CMSG_DATA_MUTABLE(struct cmsghdr *cmsg) { |
| 71 | + assert(cmsg != NULL); |
| 72 | + return CMSG_DATA(cmsg); |
| 73 | +} |
| 74 | + |
| 75 | +size_t CNIOBSD_CMSG_LEN(size_t payloadSizeBytes) { |
| 76 | + return CMSG_LEN(payloadSizeBytes); |
| 77 | +} |
| 78 | + |
| 79 | +size_t CNIOBSD_CMSG_SPACE(size_t payloadSizeBytes) { |
| 80 | + return CMSG_SPACE(payloadSizeBytes); |
| 81 | +} |
| 82 | + |
| 83 | +const int CNIOBSD_SO_TIMESTAMP = SO_TIMESTAMP; |
| 84 | +const int CNIOBSD_SO_RCVTIMEO = SO_RCVTIMEO; |
| 85 | + |
| 86 | +bool supports_udp_sockopt(int opt, int value) { |
| 87 | + int fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 88 | + if (fd == -1) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + int rc = setsockopt(fd, IPPROTO_UDP, opt, &value, sizeof(value)); |
| 92 | + close(fd); |
| 93 | + return rc == 0; |
| 94 | +} |
| 95 | + |
| 96 | +bool CNIOBSD_supports_udp_segment() { |
| 97 | + #ifndef UDP_SEGMENT |
| 98 | + return false; |
| 99 | + #else |
| 100 | + return supports_udp_sockopt(UDP_SEGMENT, 512); |
| 101 | + #endif |
| 102 | +} |
| 103 | + |
| 104 | +bool CNIOBSD_supports_udp_gro() { |
| 105 | + #ifndef UDP_GRO |
| 106 | + return false; |
| 107 | + #else |
| 108 | + return supports_udp_sockopt(UDP_GRO, 1); |
| 109 | + #endif |
| 110 | +} |
| 111 | + |
| 112 | +int CNIOBSD_system_info(struct utsname* uname_data) { |
| 113 | + return uname(uname_data); |
| 114 | +} |
| 115 | + |
| 116 | +const char* CNIOBSD_dirent_dname(struct dirent* ent) { |
| 117 | + return ent->d_name; |
| 118 | +} |
| 119 | + |
| 120 | +const unsigned long CNIOBSD_UTIME_OMIT = UTIME_OMIT; |
| 121 | +const unsigned long CNIOBSD_UTIME_NOW = UTIME_NOW; |
| 122 | + |
| 123 | +#ifdef UDP_MAX_SEGMENTS |
| 124 | +const long CNIOBSD_UDP_MAX_SEGMENTS = UDP_MAX_SEGMENTS; |
| 125 | +#endif |
| 126 | +const long CNIOBSD_UDP_MAX_SEGMENTS = -1; |
| 127 | + |
| 128 | +FTS *CNIOBSD_fts_open(char * const *path_argv, int options, int (*compar)(const FTSENT **, const FTSENT **)) { |
| 129 | + return fts_open(path_argv, options, compar); |
| 130 | +} |
| 131 | +#endif |
0 commit comments