1 
2 #ifndef _PORT_SOCKET_H
3 #define _PORT_SOCKET_H
4 #if defined(_WIN32)
5 
6 #include <winsock2.h>
7 #include <ws2tcpip.h>
8 
9 /* Some of our own infrastructure where the WinSock stuff was too hairy
10    to dump into a clean Unix program...  */
11 
12 typedef WSABUF sg_buf;
13 
14 #define SG_ADVANCE(SG, N) \
15 	((SG)->len < (N)				\
16 	 ? (abort(), 0)					\
17 	 : ((SG)->buf += (N), (SG)->len -= (N), 0))
18 
19 #define SG_LEN(SG)		((SG)->len + 0)
20 #define SG_BUF(SG)		((SG)->buf + 0)
21 #define SG_SET(SG, B, N)	((SG)->buf = (char *)(B),(SG)->len = (N))
22 
23 #define SOCKET_INITIALIZE()     0
24 #define SOCKET_CLEANUP()
25 #define SOCKET_ERRNO            (WSAGetLastError())
26 #define SOCKET_SET_ERRNO(x)     (WSASetLastError (x))
27 #define SOCKET_NFDS(f)          (0)     /* select()'s first arg is ignored */
28 #define SOCKET_READ(fd, b, l)   (recv(fd, b, l, 0))
29 #define SOCKET_WRITE(fd, b, l)  (send(fd, b, l, 0))
30 #define SOCKET_CONNECT		connect	/* XXX */
31 #define SOCKET_GETSOCKNAME	getsockname /* XXX */
32 #define SOCKET_CLOSE		close /* XXX */
33 #define SOCKET_EINTR            WSAEINTR
34 
35 /* Return -1 for error or number of bytes written.
36    TMP is a temporary variable; must be declared by the caller, and
37    must be used by this macro (to avoid compiler warnings).  */
38 /* WSASend returns 0 or SOCKET_ERROR.  */
39 #define SOCKET_WRITEV_TEMP DWORD
40 #define SOCKET_WRITEV(FD, SG, LEN, TMP)	\
41 	(WSASend((FD), (SG), (LEN), &(TMP), 0, 0, 0) ? -1 : (TMP))
42 
43 #define SHUTDOWN_READ	SD_RECEIVE
44 #define SHUTDOWN_WRITE	SD_SEND
45 #define SHUTDOWN_BOTH	SD_BOTH
46 
47 #ifndef EINPROGRESS
48 #define EINPROGRESS WSAEINPROGRESS
49 #endif
50 #ifndef EWOULDBLOCK
51 #define EWOULDBLOCK WSAEWOULDBLOCK
52 #endif
53 #ifndef ECONNRESET
54 #define ECONNRESET  WSAECONNRESET
55 #endif
56 #ifndef ECONNABORTED
57 #define ECONNABORTED WSAECONNABORTED
58 #endif
59 #ifndef ECONNREFUSED
60 #define ECONNREFUSED WSAECONNREFUSED
61 #endif
62 #ifndef EHOSTUNREACH
63 #define EHOSTUNREACH WSAEHOSTUNREACH
64 #endif
65 #ifndef ETIMEDOUT
66 #define ETIMEDOUT WSAETIMEDOUT
67 #endif
68 
69 #elif defined(__palmos__)
70 
71 /* If this source file requires it, define struct sockaddr_in
72    (and possibly other things related to network I/O).  */
73 
74 #include "autoconf.h"
75 #include <netdb.h>
76 typedef int socklen_t;
77 
78 #else /* UNIX variants */
79 
80 #include "autoconf.h"
81 
82 #include <sys/types.h>
83 #include <netinet/in.h>		/* For struct sockaddr_in and in_addr */
84 #include <arpa/inet.h>		/* For inet_ntoa */
85 #include <netdb.h>
86 
87 #ifndef HAVE_NETDB_H_H_ERRNO
88 extern int h_errno;		/* In case it's missing, e.g., HP-UX 10.20. */
89 #endif
90 
91 #include <sys/param.h>		/* For MAXHOSTNAMELEN */
92 #include <sys/socket.h>		/* For SOCK_*, AF_*, etc */
93 #include <sys/time.h>		/* For struct timeval */
94 #include <net/if.h>		/* For struct ifconf, for localaddr.c */
95 #ifdef HAVE_SYS_UIO_H
96 #include <sys/uio.h>		/* For struct iovec, for sg_buf */
97 #endif
98 #ifdef HAVE_SYS_FILIO_H
99 #include <sys/filio.h>		/* For FIONBIO on Solaris.  */
100 #endif
101 
102 /* Either size_t or int or unsigned int is probably right.  Under
103    SunOS 4, it looks like int is desired, according to the accept man
104    page.  */
105 #ifndef HAVE_SOCKLEN_T
106 typedef int socklen_t;
107 #endif
108 
109 /* XXX should only be done if sockaddr_storage not found */
110 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE
111 struct krb5int_sockaddr_storage {
112     struct sockaddr_in s;
113     /* Plenty of slop just in case we get an ipv6 address anyways.  */
114     long extra[16];
115 };
116 #define sockaddr_storage krb5int_sockaddr_storage
117 #endif
118 
119 /*
120  * Compatability with WinSock calls on MS-Windows...
121  */
122 #define	SOCKET		int
123 #define	INVALID_SOCKET	((SOCKET)~0)
124 #define	closesocket	close
125 #define	ioctlsocket	ioctl
126 #define	SOCKET_ERROR	(-1)
127 
128 typedef struct iovec sg_buf;
129 
130 #define SG_ADVANCE(SG, N) \
131 	((SG)->iov_len < (N)					\
132 	 ? (abort(), 0)						\
133 	 : ((SG)->iov_base = (char *) (SG)->iov_base + (N),	\
134 	    (SG)->iov_len -= (N), 0))
135 
136 #define SG_LEN(SG)		((SG)->iov_len + 0)
137 #define SG_BUF(SG)		((char*)(SG)->iov_base + 0)
138 #define SG_SET(SG, B, L)	((SG)->iov_base = (char*)(B), (SG)->iov_len = (L))
139 
140 /* Some of our own infrastructure where the WinSock stuff was too hairy
141    to dump into a clean Unix program...  */
142 
143 #define	SOCKET_INITIALIZE()	(0)	/* No error (or anything else) */
144 #define	SOCKET_CLEANUP()	/* nothing */
145 #define	SOCKET_ERRNO		errno
146 #define	SOCKET_SET_ERRNO(x)	(errno = (x))
147 #define SOCKET_NFDS(f)		((f)+1)	/* select() arg for a single fd */
148 #define SOCKET_READ		read
149 #define SOCKET_WRITE		write
150 #define SOCKET_CONNECT		connect
151 #define SOCKET_GETSOCKNAME	getsockname
152 #define SOCKET_CLOSE		close
153 #define SOCKET_EINTR		EINTR
154 #define SOCKET_WRITEV_TEMP int
155 /* Use TMP to avoid compiler warnings and keep things consistent with
156    Windoze version.  */
157 #define SOCKET_WRITEV(FD, SG, LEN, TMP) \
158 	((TMP) = writev((FD), (SG), (LEN)), (TMP))
159 
160 #define SHUTDOWN_READ	0
161 #define SHUTDOWN_WRITE	1
162 #define SHUTDOWN_BOTH	2
163 
164 #ifndef HAVE_INET_NTOP
165 #define inet_ntop(AF,SRC,DST,CNT)					    \
166     ((AF) == AF_INET							    \
167      ? ((CNT) < 16							    \
168 	? (SOCKET_SET_ERRNO(ENOSPC), (const char *)NULL)		    \
169 	: (sprintf((DST), "%d.%d.%d.%d",				    \
170 		   ((const unsigned char *)(const void *)(SRC))[0] & 0xff,  \
171 		   ((const unsigned char *)(const void *)(SRC))[1] & 0xff,  \
172 		   ((const unsigned char *)(const void *)(SRC))[2] & 0xff,  \
173 		   ((const unsigned char *)(const void *)(SRC))[3] & 0xff), \
174 	   (DST)))							    \
175      : (SOCKET_SET_ERRNO(EAFNOSUPPORT), (const char *)NULL))
176 #define HAVE_INET_NTOP
177 #endif
178 
179 #endif /* _WIN32 */
180 
181 #if !defined(_WIN32)
182 /* UNIX or ...?  */
183 # ifdef S_SPLINT_S
184 extern int socket (int, int, int) /*@*/;
185 # endif
186 #endif
187 
188 #endif /*_PORT_SOCKET_H*/
189