blob: e132e647f92dc9459f7486ba18eea1f804772626 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00004 *
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 Prijono5dcb38d2005-11-21 01:55:47 +000027#if defined(PJ_HAS_WINSOCK2_H) && PJ_HAS_WINSOCK2_H != 0
28# include <winsock2.h>
29#endif
30
Benny Prijonoc5b6dbf2006-09-10 16:33:48 +000031#if defined(PJ_HAS_WINSOCK_H) && PJ_HAS_WINSOCK_H != 0
32# include <winsock.h>
33#endif
34
Benny Prijonoe2746132008-09-27 13:16:35 +000035#if defined(PJ_HAS_WS2TCPIP_H) && PJ_HAS_WS2TCPIP_H != 0
36# include <ws2tcpip.h>
37#endif
38
39
Benny Prijonoc16c6e32007-11-18 14:53:47 +000040/*
41 * IPv6 for Visual Studio's
42 *
43 * = Visual Studio 6 =
44 *
45 * Visual Studio 6 does not ship with IPv6 support, so you MUST
46 * download and install IPv6 Tehnology Preview (IPv6Kit) from:
47 * http://msdn.microsoft.com/downloads/sdks/platform/tpipv6/ReadMe.asp
48 * Then put IPv6Kit\inc in your Visual Studio include path.
49 *
50 * In addition, by default IPv6Kit does not want to install on
51 * Windows 2000 SP4. Please see:
52 * http://msdn.microsoft.com/downloads/sdks/platform/tpipv6/faq.asp
53 * on how to install IPv6Kit on Win2K SP4.
54 *
55 *
56 * = Visual Studio 2003, 2005 (including Express) =
57 *
58 * These VS uses Microsoft Platform SDK for Windows Server 2003 SP1, and
59 * it has built-in IPv6 support.
60 */
Benny Prijono513d1352008-09-27 09:35:34 +000061#if defined(_MSC_VER) && defined(PJ_HAS_IPV6) && PJ_HAS_IPV6!=0
Benny Prijonoc16c6e32007-11-18 14:53:47 +000062# ifndef s_addr
63# define s_addr S_un.S_addr
64# endif
65
Benny Prijono513d1352008-09-27 09:35:34 +000066# if !defined(IPPROTO_IPV6)
Benny Prijonoc16c6e32007-11-18 14:53:47 +000067 /* Need to download and install IPv6Kit for this platform.
68 * Please see the comments above about Visual Studio 6.
69 */
70# include <tpipv6.h>
71# endif
72
Benny Prijono80025db2007-12-02 15:36:46 +000073# define PJ_SOCK_HAS_GETADDRINFO 1
Benny Prijonoc16c6e32007-11-18 14:53:47 +000074#endif /* _MSC_VER */
75
Benny Prijono5dcb38d2005-11-21 01:55:47 +000076#if defined(PJ_HAS_SYS_TYPES_H) && PJ_HAS_SYS_TYPES_H != 0
77# include <sys/types.h>
78#endif
79
80#if defined(PJ_HAS_SYS_SOCKET_H) && PJ_HAS_SYS_SOCKET_H != 0
81# include <sys/socket.h>
82#endif
83
84#if defined(PJ_HAS_LINUX_SOCKET_H) && PJ_HAS_LINUX_SOCKET_H != 0
85# include <linux/socket.h>
86#endif
87
88#if defined(PJ_HAS_SYS_SELECT_H) && PJ_HAS_SYS_SELECT_H != 0
89# include <sys/select.h>
90#endif
91
92#if defined(PJ_HAS_NETINET_IN_H) && PJ_HAS_NETINET_IN_H != 0
93# include <netinet/in.h>
94#endif
95
Benny Prijono37fc9de2008-08-04 14:45:02 +000096#if defined(PJ_HAS_NETINET_IN_SYSTM_H) && PJ_HAS_NETINET_IN_SYSTM_H != 0
97/* Required to include netinet/ip.h in FreeBSD 7.0 */
98# include <netinet/in_systm.h>
99#endif
100
Benny Prijono87a00892007-02-01 00:33:12 +0000101#if defined(PJ_HAS_NETINET_IP_H) && PJ_HAS_NETINET_IP_H != 0
102/* To pull in IPTOS_* constants */
103# include <netinet/ip.h>
104#endif
105
Benny Prijono0ae59ae2007-11-17 10:27:34 +0000106#if defined(PJ_HAS_NET_IF_H) && PJ_HAS_NET_IF_H != 0
107/* For interface enumeration in ip_helper */
108# include <net/if.h>
109#endif
110
Benny Prijono1d65f702007-12-03 04:03:17 +0000111#if defined(PJ_HAS_IFADDRS_H) && PJ_HAS_IFADDRS_H != 0
112/* Interface enum with getifaddrs() which works with IPv6 */
113# include <ifaddrs.h>
114#endif
115
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000116#if defined(PJ_HAS_ARPA_INET_H) && PJ_HAS_ARPA_INET_H != 0
117# include <arpa/inet.h>
118#endif
119
120#if defined(PJ_HAS_SYS_IOCTL_H) && PJ_HAS_SYS_IOCTL_H != 0
121# include <sys/ioctl.h> /* FBIONBIO */
122#endif
123
124#if defined(PJ_HAS_ERRNO_H) && PJ_HAS_ERRNO_H != 0
125# include <errno.h>
126#endif
127
128#if defined(PJ_HAS_NETDB_H) && PJ_HAS_NETDB_H != 0
129# include <netdb.h>
130#endif
131
132#if defined(PJ_HAS_UNISTD_H) && PJ_HAS_UNISTD_H != 0
133# include <unistd.h>
134#endif
135
136
137/*
138 * Define common errors.
139 */
Benny Prijonoace00932006-02-14 21:04:47 +0000140#if (defined(PJ_WIN32) && PJ_WIN32!=0) || \
141 (defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE!=0)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000142# define OSERR_EWOULDBLOCK WSAEWOULDBLOCK
143# define OSERR_EINPROGRESS WSAEINPROGRESS
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000144# define OSERR_ECONNRESET WSAECONNRESET
Benny Prijono9f0a5232006-06-26 10:03:09 +0000145# define OSERR_ENOTCONN WSAENOTCONN
Benny Prijonof260e462007-04-30 21:03:32 +0000146#elif defined(PJ_SYMBIAN) && PJ_SYMBIAN!=0
147# define OSERR_EWOULDBLOCK -1
148# define OSERR_EINPROGRESS -1
149# define OSERR_ECONNRESET -1
150# define OSERR_ENOTCONN -1
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000151#else
152# define OSERR_EWOULDBLOCK EWOULDBLOCK
153# define OSERR_EINPROGRESS EINPROGRESS
Benny Prijonofa73e3e2006-01-05 23:35:46 +0000154# define OSERR_ECONNRESET ECONNRESET
Benny Prijono9f0a5232006-06-26 10:03:09 +0000155# define OSERR_ENOTCONN ENOTCONN
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000156#endif
157
158
159/*
Benny Prijono7ebdb3e2007-12-03 00:40:58 +0000160 * And undefine these..
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000161 */
162#undef s_addr
Benny Prijono7ebdb3e2007-12-03 00:40:58 +0000163#undef s6_addr
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000164
165/*
166 * Linux kernel specifics
167 */
Benny Prijono9cf138e2006-01-19 03:58:29 +0000168#if defined(PJ_LINUX_KERNEL)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000169# include <linux/net.h>
170# include <asm/ioctls.h> /* FIONBIO */
171# include <linux/syscalls.h> /* sys_select() */
172# include <asm/uaccess.h> /* set/get_fs() */
173
174 typedef int socklen_t;
175# define getsockopt sys_getsockopt
176
177 /*
178 * Wrapper for select() in Linux kernel.
179 */
180 PJ_INLINE(int) select(int n, fd_set *inp, fd_set *outp, fd_set *exp,
181 struct timeval *tvp)
182 {
183 int count;
184 mm_segment_t oldfs = get_fs();
185 set_fs(KERNEL_DS);
186 count = sys_select(n, inp, outp, exp, tvp);
187 set_fs(oldfs);
188 return count;
189 }
190#endif /* PJ_LINUX_KERNEL */
191
192
193/*
Benny Prijonoc5b6dbf2006-09-10 16:33:48 +0000194 * This will finally be obsoleted, since it should be declared in
195 * os_auto.h
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000196 */
Benny Prijonoc5b6dbf2006-09-10 16:33:48 +0000197#if !defined(PJ_HAS_SOCKLEN_T) || PJ_HAS_SOCKLEN_T==0
Benny Prijono9cf138e2006-01-19 03:58:29 +0000198 typedef int socklen_t;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000199#endif
200
Benny Prijono62b86eb2007-12-01 08:52:57 +0000201/* Regarding sin_len member of sockaddr_in:
202 * BSD systems (including MacOS X requires that the sin_len member of
203 * sockaddr_in be set to sizeof(sockaddr_in), while other systems (Windows
204 * and Linux included) do not.
205 *
206 * To maintain compatibility between systems, PJLIB will automatically
207 * set this field before invoking native OS socket API, and it will
208 * always reset the field to zero before returning pj_sockaddr_in to
209 * application (such as in pj_getsockname() and pj_recvfrom()).
210 *
211 * Application MUST always set this field to zero.
212 *
213 * This way we can avoid hard to find problem such as when the socket
214 * address is used as hash table key.
215 */
216#if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
217# define PJ_SOCKADDR_SET_LEN(addr,len) (((pj_addr_hdr*)(addr))->sa_zero_len=(len))
218# define PJ_SOCKADDR_RESET_LEN(addr) (((pj_addr_hdr*)(addr))->sa_zero_len=0)
219#else
220# define PJ_SOCKADDR_SET_LEN(addr,len)
221# define PJ_SOCKADDR_RESET_LEN(addr)
222#endif
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000223
224#endif /* __PJ_COMPAT_SOCKET_H__ */
225