Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame^] | 3 | * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #ifndef __PJ_COMPAT_SOCKET_H__ |
| 20 | #define __PJ_COMPAT_SOCKET_H__ |
| 21 | |
| 22 | /** |
| 23 | * @file socket.h |
| 24 | * @brief Provides all socket related functions,data types, error codes, etc. |
| 25 | */ |
| 26 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 27 | #if defined(PJ_HAS_WINSOCK2_H) && PJ_HAS_WINSOCK2_H != 0 |
| 28 | # include <winsock2.h> |
| 29 | #endif |
| 30 | |
Benny Prijono | c5b6dbf | 2006-09-10 16:33:48 +0000 | [diff] [blame] | 31 | #if defined(PJ_HAS_WINSOCK_H) && PJ_HAS_WINSOCK_H != 0 |
| 32 | # include <winsock.h> |
| 33 | #endif |
| 34 | |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 35 | /* |
| 36 | * IPv6 for Visual Studio's |
| 37 | * |
| 38 | * = Visual Studio 6 = |
| 39 | * |
| 40 | * Visual Studio 6 does not ship with IPv6 support, so you MUST |
| 41 | * download and install IPv6 Tehnology Preview (IPv6Kit) from: |
| 42 | * http://msdn.microsoft.com/downloads/sdks/platform/tpipv6/ReadMe.asp |
| 43 | * Then put IPv6Kit\inc in your Visual Studio include path. |
| 44 | * |
| 45 | * In addition, by default IPv6Kit does not want to install on |
| 46 | * Windows 2000 SP4. Please see: |
| 47 | * http://msdn.microsoft.com/downloads/sdks/platform/tpipv6/faq.asp |
| 48 | * on how to install IPv6Kit on Win2K SP4. |
| 49 | * |
| 50 | * |
| 51 | * = Visual Studio 2003, 2005 (including Express) = |
| 52 | * |
| 53 | * These VS uses Microsoft Platform SDK for Windows Server 2003 SP1, and |
| 54 | * it has built-in IPv6 support. |
| 55 | */ |
| 56 | #if defined(_MSC_VER) && defined(PJ_HAS_IPV6) && PJ_HAS_IPV6!=0 |
| 57 | # ifndef s_addr |
| 58 | # define s_addr S_un.S_addr |
| 59 | # endif |
| 60 | |
| 61 | # include <ws2tcpip.h> |
| 62 | |
| 63 | # ifndef IPPROTO_IPV6 |
| 64 | /* Need to download and install IPv6Kit for this platform. |
| 65 | * Please see the comments above about Visual Studio 6. |
| 66 | */ |
| 67 | # include <tpipv6.h> |
| 68 | # endif |
| 69 | |
Benny Prijono | 80025db | 2007-12-02 15:36:46 +0000 | [diff] [blame] | 70 | # define PJ_SOCK_HAS_GETADDRINFO 1 |
Benny Prijono | c16c6e3 | 2007-11-18 14:53:47 +0000 | [diff] [blame] | 71 | #endif /* _MSC_VER */ |
| 72 | |
| 73 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 74 | #if defined(PJ_HAS_SYS_TYPES_H) && PJ_HAS_SYS_TYPES_H != 0 |
| 75 | # include <sys/types.h> |
| 76 | #endif |
| 77 | |
| 78 | #if defined(PJ_HAS_SYS_SOCKET_H) && PJ_HAS_SYS_SOCKET_H != 0 |
| 79 | # include <sys/socket.h> |
| 80 | #endif |
| 81 | |
| 82 | #if defined(PJ_HAS_LINUX_SOCKET_H) && PJ_HAS_LINUX_SOCKET_H != 0 |
| 83 | # include <linux/socket.h> |
| 84 | #endif |
| 85 | |
| 86 | #if defined(PJ_HAS_SYS_SELECT_H) && PJ_HAS_SYS_SELECT_H != 0 |
| 87 | # include <sys/select.h> |
| 88 | #endif |
| 89 | |
| 90 | #if defined(PJ_HAS_NETINET_IN_H) && PJ_HAS_NETINET_IN_H != 0 |
| 91 | # include <netinet/in.h> |
| 92 | #endif |
| 93 | |
Benny Prijono | 87a0089 | 2007-02-01 00:33:12 +0000 | [diff] [blame] | 94 | #if defined(PJ_HAS_NETINET_IP_H) && PJ_HAS_NETINET_IP_H != 0 |
| 95 | /* To pull in IPTOS_* constants */ |
| 96 | # include <netinet/ip.h> |
| 97 | #endif |
| 98 | |
Benny Prijono | 0ae59ae | 2007-11-17 10:27:34 +0000 | [diff] [blame] | 99 | #if defined(PJ_HAS_NET_IF_H) && PJ_HAS_NET_IF_H != 0 |
| 100 | /* For interface enumeration in ip_helper */ |
| 101 | # include <net/if.h> |
| 102 | #endif |
| 103 | |
Benny Prijono | 1d65f70 | 2007-12-03 04:03:17 +0000 | [diff] [blame] | 104 | #if defined(PJ_HAS_IFADDRS_H) && PJ_HAS_IFADDRS_H != 0 |
| 105 | /* Interface enum with getifaddrs() which works with IPv6 */ |
| 106 | # include <ifaddrs.h> |
| 107 | #endif |
| 108 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 109 | #if defined(PJ_HAS_ARPA_INET_H) && PJ_HAS_ARPA_INET_H != 0 |
| 110 | # include <arpa/inet.h> |
| 111 | #endif |
| 112 | |
| 113 | #if defined(PJ_HAS_SYS_IOCTL_H) && PJ_HAS_SYS_IOCTL_H != 0 |
| 114 | # include <sys/ioctl.h> /* FBIONBIO */ |
| 115 | #endif |
| 116 | |
| 117 | #if defined(PJ_HAS_ERRNO_H) && PJ_HAS_ERRNO_H != 0 |
| 118 | # include <errno.h> |
| 119 | #endif |
| 120 | |
| 121 | #if defined(PJ_HAS_NETDB_H) && PJ_HAS_NETDB_H != 0 |
| 122 | # include <netdb.h> |
| 123 | #endif |
| 124 | |
| 125 | #if defined(PJ_HAS_UNISTD_H) && PJ_HAS_UNISTD_H != 0 |
| 126 | # include <unistd.h> |
| 127 | #endif |
| 128 | |
| 129 | |
| 130 | /* |
| 131 | * Define common errors. |
| 132 | */ |
Benny Prijono | ace0093 | 2006-02-14 21:04:47 +0000 | [diff] [blame] | 133 | #if (defined(PJ_WIN32) && PJ_WIN32!=0) || \ |
| 134 | (defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE!=0) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 135 | # define OSERR_EWOULDBLOCK WSAEWOULDBLOCK |
| 136 | # define OSERR_EINPROGRESS WSAEINPROGRESS |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 137 | # define OSERR_ECONNRESET WSAECONNRESET |
Benny Prijono | 9f0a523 | 2006-06-26 10:03:09 +0000 | [diff] [blame] | 138 | # define OSERR_ENOTCONN WSAENOTCONN |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 139 | #elif defined(PJ_SYMBIAN) && PJ_SYMBIAN!=0 |
| 140 | # define OSERR_EWOULDBLOCK -1 |
| 141 | # define OSERR_EINPROGRESS -1 |
| 142 | # define OSERR_ECONNRESET -1 |
| 143 | # define OSERR_ENOTCONN -1 |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 144 | #else |
| 145 | # define OSERR_EWOULDBLOCK EWOULDBLOCK |
| 146 | # define OSERR_EINPROGRESS EINPROGRESS |
Benny Prijono | fa73e3e | 2006-01-05 23:35:46 +0000 | [diff] [blame] | 147 | # define OSERR_ECONNRESET ECONNRESET |
Benny Prijono | 9f0a523 | 2006-06-26 10:03:09 +0000 | [diff] [blame] | 148 | # define OSERR_ENOTCONN ENOTCONN |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 149 | #endif |
| 150 | |
| 151 | |
| 152 | /* |
Benny Prijono | 7ebdb3e | 2007-12-03 00:40:58 +0000 | [diff] [blame] | 153 | * And undefine these.. |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 154 | */ |
| 155 | #undef s_addr |
Benny Prijono | 7ebdb3e | 2007-12-03 00:40:58 +0000 | [diff] [blame] | 156 | #undef s6_addr |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 157 | |
| 158 | /* |
| 159 | * Linux kernel specifics |
| 160 | */ |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 161 | #if defined(PJ_LINUX_KERNEL) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 162 | # include <linux/net.h> |
| 163 | # include <asm/ioctls.h> /* FIONBIO */ |
| 164 | # include <linux/syscalls.h> /* sys_select() */ |
| 165 | # include <asm/uaccess.h> /* set/get_fs() */ |
| 166 | |
| 167 | typedef int socklen_t; |
| 168 | # define getsockopt sys_getsockopt |
| 169 | |
| 170 | /* |
| 171 | * Wrapper for select() in Linux kernel. |
| 172 | */ |
| 173 | PJ_INLINE(int) select(int n, fd_set *inp, fd_set *outp, fd_set *exp, |
| 174 | struct timeval *tvp) |
| 175 | { |
| 176 | int count; |
| 177 | mm_segment_t oldfs = get_fs(); |
| 178 | set_fs(KERNEL_DS); |
| 179 | count = sys_select(n, inp, outp, exp, tvp); |
| 180 | set_fs(oldfs); |
| 181 | return count; |
| 182 | } |
| 183 | #endif /* PJ_LINUX_KERNEL */ |
| 184 | |
| 185 | |
| 186 | /* |
Benny Prijono | c5b6dbf | 2006-09-10 16:33:48 +0000 | [diff] [blame] | 187 | * This will finally be obsoleted, since it should be declared in |
| 188 | * os_auto.h |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 189 | */ |
Benny Prijono | c5b6dbf | 2006-09-10 16:33:48 +0000 | [diff] [blame] | 190 | #if !defined(PJ_HAS_SOCKLEN_T) || PJ_HAS_SOCKLEN_T==0 |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 191 | typedef int socklen_t; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 192 | #endif |
| 193 | |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 194 | /* Regarding sin_len member of sockaddr_in: |
| 195 | * BSD systems (including MacOS X requires that the sin_len member of |
| 196 | * sockaddr_in be set to sizeof(sockaddr_in), while other systems (Windows |
| 197 | * and Linux included) do not. |
| 198 | * |
| 199 | * To maintain compatibility between systems, PJLIB will automatically |
| 200 | * set this field before invoking native OS socket API, and it will |
| 201 | * always reset the field to zero before returning pj_sockaddr_in to |
| 202 | * application (such as in pj_getsockname() and pj_recvfrom()). |
| 203 | * |
| 204 | * Application MUST always set this field to zero. |
| 205 | * |
| 206 | * This way we can avoid hard to find problem such as when the socket |
| 207 | * address is used as hash table key. |
| 208 | */ |
| 209 | #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0 |
| 210 | # define PJ_SOCKADDR_SET_LEN(addr,len) (((pj_addr_hdr*)(addr))->sa_zero_len=(len)) |
| 211 | # define PJ_SOCKADDR_RESET_LEN(addr) (((pj_addr_hdr*)(addr))->sa_zero_len=0) |
| 212 | #else |
| 213 | # define PJ_SOCKADDR_SET_LEN(addr,len) |
| 214 | # define PJ_SOCKADDR_RESET_LEN(addr) |
| 215 | #endif |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 216 | |
| 217 | #endif /* __PJ_COMPAT_SOCKET_H__ */ |
| 218 | |