17c478bd9Sstevel@tonic-gate 
27c478bd9Sstevel@tonic-gate #ifndef _PORT_SOCKET_H
37c478bd9Sstevel@tonic-gate #define _PORT_SOCKET_H
47c478bd9Sstevel@tonic-gate #if defined(_WIN32)
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate #include <winsock2.h>
77c478bd9Sstevel@tonic-gate #include <ws2tcpip.h>
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /* Some of our own infrastructure where the WinSock stuff was too hairy
107c478bd9Sstevel@tonic-gate    to dump into a clean Unix program...  */
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate typedef WSABUF sg_buf;
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #define SG_ADVANCE(SG, N) \
157c478bd9Sstevel@tonic-gate 	((SG)->len < (N)				\
167c478bd9Sstevel@tonic-gate 	 ? (abort(), 0)					\
177c478bd9Sstevel@tonic-gate 	 : ((SG)->buf += (N), (SG)->len -= (N), 0))
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate #define SG_LEN(SG)		((SG)->len + 0)
207c478bd9Sstevel@tonic-gate #define SG_BUF(SG)		((SG)->buf + 0)
217c478bd9Sstevel@tonic-gate #define SG_SET(SG, B, N)	((SG)->buf = (char *)(B),(SG)->len = (N))
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #define SOCKET_INITIALIZE()     0
247c478bd9Sstevel@tonic-gate #define SOCKET_CLEANUP()
257c478bd9Sstevel@tonic-gate #define SOCKET_ERRNO            (WSAGetLastError())
267c478bd9Sstevel@tonic-gate #define SOCKET_SET_ERRNO(x)     (WSASetLastError (x))
277c478bd9Sstevel@tonic-gate #define SOCKET_NFDS(f)          (0)     /* select()'s first arg is ignored */
287c478bd9Sstevel@tonic-gate #define SOCKET_READ(fd, b, l)   (recv(fd, b, l, 0))
297c478bd9Sstevel@tonic-gate #define SOCKET_WRITE(fd, b, l)  (send(fd, b, l, 0))
307c478bd9Sstevel@tonic-gate #define SOCKET_CONNECT		connect	/* XXX */
317c478bd9Sstevel@tonic-gate #define SOCKET_GETSOCKNAME	getsockname /* XXX */
327c478bd9Sstevel@tonic-gate #define SOCKET_CLOSE		close /* XXX */
337c478bd9Sstevel@tonic-gate #define SOCKET_EINTR            WSAEINTR
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /* Return -1 for error or number of bytes written.
367c478bd9Sstevel@tonic-gate    TMP is a temporary variable; must be declared by the caller, and
377c478bd9Sstevel@tonic-gate    must be used by this macro (to avoid compiler warnings).  */
387c478bd9Sstevel@tonic-gate /* WSASend returns 0 or SOCKET_ERROR.  */
397c478bd9Sstevel@tonic-gate #define SOCKET_WRITEV_TEMP DWORD
407c478bd9Sstevel@tonic-gate #define SOCKET_WRITEV(FD, SG, LEN, TMP)	\
417c478bd9Sstevel@tonic-gate 	(WSASend((FD), (SG), (LEN), &(TMP), 0, 0, 0) ? -1 : (TMP))
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define SHUTDOWN_READ	SD_RECEIVE
447c478bd9Sstevel@tonic-gate #define SHUTDOWN_WRITE	SD_SEND
457c478bd9Sstevel@tonic-gate #define SHUTDOWN_BOTH	SD_BOTH
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #ifndef EINPROGRESS
487c478bd9Sstevel@tonic-gate #define EINPROGRESS WSAEINPROGRESS
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate #ifndef EWOULDBLOCK
517c478bd9Sstevel@tonic-gate #define EWOULDBLOCK WSAEWOULDBLOCK
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate #ifndef ECONNRESET
547c478bd9Sstevel@tonic-gate #define ECONNRESET  WSAECONNRESET
557c478bd9Sstevel@tonic-gate #endif
567c478bd9Sstevel@tonic-gate #ifndef ECONNABORTED
577c478bd9Sstevel@tonic-gate #define ECONNABORTED WSAECONNABORTED
587c478bd9Sstevel@tonic-gate #endif
597c478bd9Sstevel@tonic-gate #ifndef ECONNREFUSED
607c478bd9Sstevel@tonic-gate #define ECONNREFUSED WSAECONNREFUSED
617c478bd9Sstevel@tonic-gate #endif
627c478bd9Sstevel@tonic-gate #ifndef EHOSTUNREACH
637c478bd9Sstevel@tonic-gate #define EHOSTUNREACH WSAEHOSTUNREACH
647c478bd9Sstevel@tonic-gate #endif
657c478bd9Sstevel@tonic-gate #ifndef ETIMEDOUT
667c478bd9Sstevel@tonic-gate #define ETIMEDOUT WSAETIMEDOUT
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate 
69*159d09a2SMark Phalan #elif defined(__palmos__)
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /* If this source file requires it, define struct sockaddr_in
727c478bd9Sstevel@tonic-gate    (and possibly other things related to network I/O).  */
737c478bd9Sstevel@tonic-gate 
74*159d09a2SMark Phalan #include "autoconf.h"
75*159d09a2SMark Phalan #include <netdb.h>
76*159d09a2SMark Phalan typedef int socklen_t;
77*159d09a2SMark Phalan 
78*159d09a2SMark Phalan #else /* UNIX variants */
79*159d09a2SMark Phalan 
807c478bd9Sstevel@tonic-gate #include "autoconf.h"
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #include <sys/types.h>
837c478bd9Sstevel@tonic-gate #include <netinet/in.h>		/* For struct sockaddr_in and in_addr */
847c478bd9Sstevel@tonic-gate #include <arpa/inet.h>		/* For inet_ntoa */
85505d05c7Sgtb #include <netdb.h>
86505d05c7Sgtb 
87505d05c7Sgtb #ifndef HAVE_NETDB_H_H_ERRNO
88505d05c7Sgtb extern int h_errno;		/* In case it's missing, e.g., HP-UX 10.20. */
89505d05c7Sgtb #endif
90505d05c7Sgtb 
917c478bd9Sstevel@tonic-gate #include <sys/param.h>		/* For MAXHOSTNAMELEN */
927c478bd9Sstevel@tonic-gate #include <sys/socket.h>		/* For SOCK_*, AF_*, etc */
937c478bd9Sstevel@tonic-gate #include <sys/time.h>		/* For struct timeval */
947c478bd9Sstevel@tonic-gate #include <net/if.h>		/* For struct ifconf, for localaddr.c */
957c478bd9Sstevel@tonic-gate #ifdef HAVE_SYS_UIO_H
967c478bd9Sstevel@tonic-gate #include <sys/uio.h>		/* For struct iovec, for sg_buf */
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate #ifdef HAVE_SYS_FILIO_H
997c478bd9Sstevel@tonic-gate #include <sys/filio.h>		/* For FIONBIO on Solaris.  */
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /* Either size_t or int or unsigned int is probably right.  Under
1037c478bd9Sstevel@tonic-gate    SunOS 4, it looks like int is desired, according to the accept man
1047c478bd9Sstevel@tonic-gate    page.  */
1057c478bd9Sstevel@tonic-gate #ifndef HAVE_SOCKLEN_T
1067c478bd9Sstevel@tonic-gate typedef int socklen_t;
1077c478bd9Sstevel@tonic-gate #endif
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /* XXX should only be done if sockaddr_storage not found */
1107c478bd9Sstevel@tonic-gate #ifndef HAVE_STRUCT_SOCKADDR_STORAGE
1117c478bd9Sstevel@tonic-gate struct krb5int_sockaddr_storage {
1127c478bd9Sstevel@tonic-gate     struct sockaddr_in s;
1137c478bd9Sstevel@tonic-gate     /* Plenty of slop just in case we get an ipv6 address anyways.  */
1147c478bd9Sstevel@tonic-gate     long extra[16];
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate #define sockaddr_storage krb5int_sockaddr_storage
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * Compatability with WinSock calls on MS-Windows...
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate #define	SOCKET		int
1237c478bd9Sstevel@tonic-gate #define	INVALID_SOCKET	((SOCKET)~0)
1247c478bd9Sstevel@tonic-gate #define	closesocket	close
1257c478bd9Sstevel@tonic-gate #define	ioctlsocket	ioctl
1267c478bd9Sstevel@tonic-gate #define	SOCKET_ERROR	(-1)
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate typedef struct iovec sg_buf;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #define SG_ADVANCE(SG, N) \
1317c478bd9Sstevel@tonic-gate 	((SG)->iov_len < (N)					\
1327c478bd9Sstevel@tonic-gate 	 ? (abort(), 0)						\
1337c478bd9Sstevel@tonic-gate 	 : ((SG)->iov_base = (char *) (SG)->iov_base + (N),	\
1347c478bd9Sstevel@tonic-gate 	    (SG)->iov_len -= (N), 0))
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #define SG_LEN(SG)		((SG)->iov_len + 0)
1377c478bd9Sstevel@tonic-gate #define SG_BUF(SG)		((char*)(SG)->iov_base + 0)
1387c478bd9Sstevel@tonic-gate #define SG_SET(SG, B, L)	((SG)->iov_base = (char*)(B), (SG)->iov_len = (L))
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /* Some of our own infrastructure where the WinSock stuff was too hairy
1417c478bd9Sstevel@tonic-gate    to dump into a clean Unix program...  */
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate #define	SOCKET_INITIALIZE()	(0)	/* No error (or anything else) */
1447c478bd9Sstevel@tonic-gate #define	SOCKET_CLEANUP()	/* nothing */
1457c478bd9Sstevel@tonic-gate #define	SOCKET_ERRNO		errno
1467c478bd9Sstevel@tonic-gate #define	SOCKET_SET_ERRNO(x)	(errno = (x))
1477c478bd9Sstevel@tonic-gate #define SOCKET_NFDS(f)		((f)+1)	/* select() arg for a single fd */
1487c478bd9Sstevel@tonic-gate #define SOCKET_READ		read
1497c478bd9Sstevel@tonic-gate #define SOCKET_WRITE		write
1507c478bd9Sstevel@tonic-gate #define SOCKET_CONNECT		connect
1517c478bd9Sstevel@tonic-gate #define SOCKET_GETSOCKNAME	getsockname
1527c478bd9Sstevel@tonic-gate #define SOCKET_CLOSE		close
1537c478bd9Sstevel@tonic-gate #define SOCKET_EINTR		EINTR
1547c478bd9Sstevel@tonic-gate #define SOCKET_WRITEV_TEMP int
1557c478bd9Sstevel@tonic-gate /* Use TMP to avoid compiler warnings and keep things consistent with
1567c478bd9Sstevel@tonic-gate    Windoze version.  */
1577c478bd9Sstevel@tonic-gate #define SOCKET_WRITEV(FD, SG, LEN, TMP) \
1587c478bd9Sstevel@tonic-gate 	((TMP) = writev((FD), (SG), (LEN)), (TMP))
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate #define SHUTDOWN_READ	0
1617c478bd9Sstevel@tonic-gate #define SHUTDOWN_WRITE	1
1627c478bd9Sstevel@tonic-gate #define SHUTDOWN_BOTH	2
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate #ifndef HAVE_INET_NTOP
1657c478bd9Sstevel@tonic-gate #define inet_ntop(AF,SRC,DST,CNT)					    \
1667c478bd9Sstevel@tonic-gate     ((AF) == AF_INET							    \
1677c478bd9Sstevel@tonic-gate      ? ((CNT) < 16							    \
168505d05c7Sgtb 	? (SOCKET_SET_ERRNO(ENOSPC), (const char *)NULL)		    \
1697c478bd9Sstevel@tonic-gate 	: (sprintf((DST), "%d.%d.%d.%d",				    \
1707c478bd9Sstevel@tonic-gate 		   ((const unsigned char *)(const void *)(SRC))[0] & 0xff,  \
1717c478bd9Sstevel@tonic-gate 		   ((const unsigned char *)(const void *)(SRC))[1] & 0xff,  \
1727c478bd9Sstevel@tonic-gate 		   ((const unsigned char *)(const void *)(SRC))[2] & 0xff,  \
1737c478bd9Sstevel@tonic-gate 		   ((const unsigned char *)(const void *)(SRC))[3] & 0xff), \
1747c478bd9Sstevel@tonic-gate 	   (DST)))							    \
175505d05c7Sgtb      : (SOCKET_SET_ERRNO(EAFNOSUPPORT), (const char *)NULL))
1767c478bd9Sstevel@tonic-gate #define HAVE_INET_NTOP
1777c478bd9Sstevel@tonic-gate #endif
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate #endif /* _WIN32 */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate #if !defined(_WIN32)
1827c478bd9Sstevel@tonic-gate /* UNIX or ...?  */
1837c478bd9Sstevel@tonic-gate # ifdef S_SPLINT_S
1847c478bd9Sstevel@tonic-gate extern int socket (int, int, int) /*@*/;
1857c478bd9Sstevel@tonic-gate # endif
1867c478bd9Sstevel@tonic-gate #endif
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate #endif /*_PORT_SOCKET_H*/
189