1 /* sock.h 2 * - General Socket Function Headers 3 * 4 * Copyright (C) 2014 Michael Smith <msmith@icecast.org>, 5 * Brendan Cully <brendan@xiph.org>, 6 * Karl Heyes <karl@xiph.org>, 7 * Jack Moffitt <jack@icecast.org>, 8 * Ed "oddsock" Zaleski <oddsock@xiph.org> 9 * 10 * This library is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU Library General Public 12 * License as published by the Free Software Foundation; either 13 * version 2 of the License, or (at your option) any later version. 14 * 15 * This library is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 * Library General Public License for more details. 19 * 20 * You should have received a copy of the GNU Library General Public 21 * License along with this library; if not, write to the 22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 23 * Boston, MA 02110-1301, USA. 24 * 25 */ 26 27 #ifndef __SOCK_H 28 #define __SOCK_H 29 30 #include <stdarg.h> 31 32 #ifdef HAVE_WINSOCK2_H 33 #include <winsock2.h> 34 #endif 35 36 #ifdef HAVE_UNISTD_H 37 #include <unistd.h> 38 #elif _WIN32 39 #include <os.h> 40 #endif 41 42 #ifdef HAVE_SYS_UIO_H 43 #include <sys/uio.h> 44 #else 45 #ifndef _SYS_UIO_H 46 struct iovec 47 { 48 void *iov_base; 49 size_t iov_len; 50 }; 51 #endif 52 #endif 53 54 #if !defined(HAVE_INET_ATON) && defined(HAVE_INET_PTON) 55 #define inet_aton(a,b) inet_pton(AF_INET, (a), (b)) 56 #endif 57 58 #ifdef INET6_ADDRSTRLEN 59 #define MAX_ADDR_LEN INET6_ADDRSTRLEN 60 #else 61 #define MAX_ADDR_LEN 46 62 #endif 63 64 #ifndef sock_t 65 #define sock_t int 66 #endif 67 68 /* The following values are based on unix avoiding errno value clashes */ 69 #define SOCK_SUCCESS 0 70 #define SOCK_ERROR (sock_t)-1 71 #define SOCK_TIMEOUT -2 72 73 /* sock connect macro */ 74 #define sock_connect(h, p) sock_connect_wto(h, p, 0) 75 76 #ifdef _mangle 77 # define sock_initialize _mangle(sock_initialize) 78 # define sock_shutdown _mangle(sock_shutdown) 79 # define sock_get_localip _mangle(sock_get_localip) 80 # define sock_error _mangle(sock_error) 81 # define sock_set_error _mangle(sock_set_error) 82 # define sock_recoverable _mangle(sock_recoverable) 83 # define sock_stalled _mangle(sock_stalled) 84 # define sock_valid_socket _mangle(sock_valid_socket) 85 # define sock_set_blocking _mangle(sock_set_blocking) 86 # define sock_set_nolinger _mangle(sock_set_nolinger) 87 # define sock_set_nodelay _mangle(sock_set_nodelay) 88 # define sock_set_keepalive _mangle(sock_set_keepalive) 89 # define sock_close _mangle(sock_close) 90 # define sock_connect_wto _mangle(sock_connect_wto) 91 # define sock_connect_wto_bind _mangle(sock_connect_wto_bind) 92 # define sock_connect_non_blocking _mangle(sock_connect_non_blocking) 93 # define sock_connected _mangle(sock_connected) 94 # define sock_write_bytes _mangle(sock_write_bytes) 95 # define sock_write _mangle(sock_write) 96 # define sock_write_fmt _mangle(sock_write_fmt) 97 # define sock_write_string _mangle(sock_write_string) 98 # define sock_writev _mangle(sock_writev) 99 # define sock_read_bytes _mangle(sock_read_bytes) 100 # define sock_read_line _mangle(sock_read_line) 101 # define sock_get_server_socket _mangle(sock_get_server_socket) 102 # define sock_listen _mangle(sock_listen) 103 # define sock_set_send_buffer _mangle(sock_set_send_buffer) 104 # define sock_accept _mangle(sock_accept) 105 #endif 106 107 /* Misc socket functions */ 108 void sock_initialize(void); 109 void sock_shutdown(void); 110 char *sock_get_localip(char *buff, int len); 111 int sock_error(void); 112 int sock_recoverable(int error); 113 int sock_stalled(int error); 114 int sock_valid_socket(sock_t sock); 115 int sock_active (sock_t sock); 116 int sock_set_blocking(sock_t sock, int block); 117 int sock_set_nolinger(sock_t sock); 118 int sock_set_keepalive(sock_t sock); 119 int sock_set_nodelay(sock_t sock); 120 void sock_set_send_buffer (sock_t sock, int win_size); 121 void sock_set_error(int val); 122 int sock_close(sock_t sock); 123 124 /* Connection related socket functions */ 125 sock_t sock_connect_wto(const char *hostname, int port, int timeout); 126 sock_t sock_connect_wto_bind(const char *hostname, int port, const char *bnd, int timeout); 127 sock_t sock_connect_non_blocking(const char *host, unsigned port); 128 int sock_connected(sock_t sock, int timeout); 129 130 /* Socket write functions */ 131 int sock_write_bytes(sock_t sock, const void *buff, size_t len); 132 int sock_write(sock_t sock, const char *fmt, ...); 133 int sock_write_fmt(sock_t sock, const char *fmt, va_list ap); 134 int sock_write_string(sock_t sock, const char *buff); 135 ssize_t sock_writev (sock_t sock, const struct iovec *iov, size_t count); 136 137 138 /* Socket read functions */ 139 int sock_read_bytes(sock_t sock, char *buff, size_t len); 140 int sock_read_line(sock_t sock, char *string, const int len); 141 142 /* server socket functions */ 143 sock_t sock_get_server_socket(int port, const char *sinterface); 144 int sock_listen(sock_t serversock, int backlog); 145 sock_t sock_accept(sock_t serversock, char *ip, size_t len); 146 147 #ifdef _WIN32 148 int inet_aton(const char *s, struct in_addr *a); 149 #endif 150 151 #endif /* __SOCK_H */ 152