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