1 /* 2 * Authored by Alex Hultman, 2018-2019. 3 * Intellectual property of third-party. 4 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 9 * http://www.apache.org/licenses/LICENSE-2.0 10 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #ifndef BSD_H 19 #define BSD_H 20 21 // top-most wrapper of bsd-like syscalls 22 23 // holds everything you need from the bsd/winsock interfaces, only included by internal libusockets.h 24 // here everything about the syscalls are inline-wrapped and included 25 26 #ifdef _WIN32 27 #ifndef NOMINMAX 28 #define NOMINMAX 29 #endif 30 #include <winsock2.h> 31 #include <ws2tcpip.h> 32 #pragma comment(lib, "ws2_32.lib") 33 #define SETSOCKOPT_PTR_TYPE const char * 34 #define LIBUS_SOCKET_ERROR INVALID_SOCKET 35 #else 36 #ifndef _GNU_SOURCE 37 #define _GNU_SOURCE 38 #endif 39 /* For socklen_t */ 40 #include <sys/socket.h> 41 #define SETSOCKOPT_PTR_TYPE int * 42 #define LIBUS_SOCKET_ERROR -1 43 #endif 44 45 struct bsd_addr_t { 46 struct sockaddr_storage mem; 47 socklen_t len; 48 char *ip; 49 int ip_length; 50 int port; 51 }; 52 53 LIBUS_SOCKET_DESCRIPTOR apple_no_sigpipe(LIBUS_SOCKET_DESCRIPTOR fd); 54 LIBUS_SOCKET_DESCRIPTOR bsd_set_nonblocking(LIBUS_SOCKET_DESCRIPTOR fd); 55 void bsd_socket_nodelay(LIBUS_SOCKET_DESCRIPTOR fd, int enabled); 56 void bsd_socket_flush(LIBUS_SOCKET_DESCRIPTOR fd); 57 LIBUS_SOCKET_DESCRIPTOR bsd_create_socket(int domain, int type, int protocol); 58 59 void bsd_close_socket(LIBUS_SOCKET_DESCRIPTOR fd); 60 void bsd_shutdown_socket(LIBUS_SOCKET_DESCRIPTOR fd); 61 void bsd_shutdown_socket_read(LIBUS_SOCKET_DESCRIPTOR fd); 62 63 void internal_finalize_bsd_addr(struct bsd_addr_t *addr); 64 65 int bsd_local_addr(LIBUS_SOCKET_DESCRIPTOR fd, struct bsd_addr_t *addr); 66 int bsd_remote_addr(LIBUS_SOCKET_DESCRIPTOR fd, struct bsd_addr_t *addr); 67 68 char *bsd_addr_get_ip(struct bsd_addr_t *addr); 69 int bsd_addr_get_ip_length(struct bsd_addr_t *addr); 70 71 int bsd_addr_get_port(struct bsd_addr_t *addr); 72 73 // called by dispatch_ready_poll 74 LIBUS_SOCKET_DESCRIPTOR bsd_accept_socket(LIBUS_SOCKET_DESCRIPTOR fd, struct bsd_addr_t *addr); 75 76 int bsd_recv(LIBUS_SOCKET_DESCRIPTOR fd, void *buf, int length, int flags); 77 int bsd_send(LIBUS_SOCKET_DESCRIPTOR fd, const char *buf, int length, int msg_more); 78 int bsd_would_block(); 79 80 // return LIBUS_SOCKET_ERROR or the fd that represents listen socket 81 // listen both on ipv6 and ipv4 82 LIBUS_SOCKET_DESCRIPTOR bsd_create_listen_socket(const char *host, int port, int options); 83 84 LIBUS_SOCKET_DESCRIPTOR bsd_create_connect_socket(const char *host, int port, const char *source_host, int options); 85 86 #endif // BSD_H 87