10Sstevel@tonic-gate 20Sstevel@tonic-gate #ifndef _PORT_SOCKET_H 30Sstevel@tonic-gate #define _PORT_SOCKET_H 40Sstevel@tonic-gate #if defined(_WIN32) 50Sstevel@tonic-gate 60Sstevel@tonic-gate #include <winsock2.h> 70Sstevel@tonic-gate #include <ws2tcpip.h> 80Sstevel@tonic-gate 90Sstevel@tonic-gate /* Some of our own infrastructure where the WinSock stuff was too hairy 100Sstevel@tonic-gate to dump into a clean Unix program... */ 110Sstevel@tonic-gate 120Sstevel@tonic-gate typedef WSABUF sg_buf; 130Sstevel@tonic-gate 140Sstevel@tonic-gate #define SG_ADVANCE(SG, N) \ 150Sstevel@tonic-gate ((SG)->len < (N) \ 160Sstevel@tonic-gate ? (abort(), 0) \ 170Sstevel@tonic-gate : ((SG)->buf += (N), (SG)->len -= (N), 0)) 180Sstevel@tonic-gate 190Sstevel@tonic-gate #define SG_LEN(SG) ((SG)->len + 0) 200Sstevel@tonic-gate #define SG_BUF(SG) ((SG)->buf + 0) 210Sstevel@tonic-gate #define SG_SET(SG, B, N) ((SG)->buf = (char *)(B),(SG)->len = (N)) 220Sstevel@tonic-gate 230Sstevel@tonic-gate #define SOCKET_INITIALIZE() 0 240Sstevel@tonic-gate #define SOCKET_CLEANUP() 250Sstevel@tonic-gate #define SOCKET_ERRNO (WSAGetLastError()) 260Sstevel@tonic-gate #define SOCKET_SET_ERRNO(x) (WSASetLastError (x)) 270Sstevel@tonic-gate #define SOCKET_NFDS(f) (0) /* select()'s first arg is ignored */ 280Sstevel@tonic-gate #define SOCKET_READ(fd, b, l) (recv(fd, b, l, 0)) 290Sstevel@tonic-gate #define SOCKET_WRITE(fd, b, l) (send(fd, b, l, 0)) 300Sstevel@tonic-gate #define SOCKET_CONNECT connect /* XXX */ 310Sstevel@tonic-gate #define SOCKET_GETSOCKNAME getsockname /* XXX */ 320Sstevel@tonic-gate #define SOCKET_CLOSE close /* XXX */ 330Sstevel@tonic-gate #define SOCKET_EINTR WSAEINTR 340Sstevel@tonic-gate 350Sstevel@tonic-gate /* Return -1 for error or number of bytes written. 360Sstevel@tonic-gate TMP is a temporary variable; must be declared by the caller, and 370Sstevel@tonic-gate must be used by this macro (to avoid compiler warnings). */ 380Sstevel@tonic-gate /* WSASend returns 0 or SOCKET_ERROR. */ 390Sstevel@tonic-gate #define SOCKET_WRITEV_TEMP DWORD 400Sstevel@tonic-gate #define SOCKET_WRITEV(FD, SG, LEN, TMP) \ 410Sstevel@tonic-gate (WSASend((FD), (SG), (LEN), &(TMP), 0, 0, 0) ? -1 : (TMP)) 420Sstevel@tonic-gate 430Sstevel@tonic-gate #define SHUTDOWN_READ SD_RECEIVE 440Sstevel@tonic-gate #define SHUTDOWN_WRITE SD_SEND 450Sstevel@tonic-gate #define SHUTDOWN_BOTH SD_BOTH 460Sstevel@tonic-gate 470Sstevel@tonic-gate #ifndef EINPROGRESS 480Sstevel@tonic-gate #define EINPROGRESS WSAEINPROGRESS 490Sstevel@tonic-gate #endif 500Sstevel@tonic-gate #ifndef EWOULDBLOCK 510Sstevel@tonic-gate #define EWOULDBLOCK WSAEWOULDBLOCK 520Sstevel@tonic-gate #endif 530Sstevel@tonic-gate #ifndef ECONNRESET 540Sstevel@tonic-gate #define ECONNRESET WSAECONNRESET 550Sstevel@tonic-gate #endif 560Sstevel@tonic-gate #ifndef ECONNABORTED 570Sstevel@tonic-gate #define ECONNABORTED WSAECONNABORTED 580Sstevel@tonic-gate #endif 590Sstevel@tonic-gate #ifndef ECONNREFUSED 600Sstevel@tonic-gate #define ECONNREFUSED WSAECONNREFUSED 610Sstevel@tonic-gate #endif 620Sstevel@tonic-gate #ifndef EHOSTUNREACH 630Sstevel@tonic-gate #define EHOSTUNREACH WSAEHOSTUNREACH 640Sstevel@tonic-gate #endif 650Sstevel@tonic-gate #ifndef ETIMEDOUT 660Sstevel@tonic-gate #define ETIMEDOUT WSAETIMEDOUT 670Sstevel@tonic-gate #endif 680Sstevel@tonic-gate 69*7934SMark.Phalan@Sun.COM #elif defined(__palmos__) 700Sstevel@tonic-gate 710Sstevel@tonic-gate /* If this source file requires it, define struct sockaddr_in 720Sstevel@tonic-gate (and possibly other things related to network I/O). */ 730Sstevel@tonic-gate 740Sstevel@tonic-gate #include "autoconf.h" 75*7934SMark.Phalan@Sun.COM #include <netdb.h> 76*7934SMark.Phalan@Sun.COM typedef int socklen_t; 77*7934SMark.Phalan@Sun.COM 78*7934SMark.Phalan@Sun.COM #else /* UNIX variants */ 79*7934SMark.Phalan@Sun.COM 80*7934SMark.Phalan@Sun.COM #include "autoconf.h" 810Sstevel@tonic-gate 820Sstevel@tonic-gate #include <sys/types.h> 830Sstevel@tonic-gate #include <netinet/in.h> /* For struct sockaddr_in and in_addr */ 840Sstevel@tonic-gate #include <arpa/inet.h> /* For inet_ntoa */ 85781Sgtb #include <netdb.h> 86781Sgtb 87781Sgtb #ifndef HAVE_NETDB_H_H_ERRNO 88781Sgtb extern int h_errno; /* In case it's missing, e.g., HP-UX 10.20. */ 89781Sgtb #endif 90781Sgtb 910Sstevel@tonic-gate #include <sys/param.h> /* For MAXHOSTNAMELEN */ 920Sstevel@tonic-gate #include <sys/socket.h> /* For SOCK_*, AF_*, etc */ 930Sstevel@tonic-gate #include <sys/time.h> /* For struct timeval */ 940Sstevel@tonic-gate #include <net/if.h> /* For struct ifconf, for localaddr.c */ 950Sstevel@tonic-gate #ifdef HAVE_SYS_UIO_H 960Sstevel@tonic-gate #include <sys/uio.h> /* For struct iovec, for sg_buf */ 970Sstevel@tonic-gate #endif 980Sstevel@tonic-gate #ifdef HAVE_SYS_FILIO_H 990Sstevel@tonic-gate #include <sys/filio.h> /* For FIONBIO on Solaris. */ 1000Sstevel@tonic-gate #endif 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate /* Either size_t or int or unsigned int is probably right. Under 1030Sstevel@tonic-gate SunOS 4, it looks like int is desired, according to the accept man 1040Sstevel@tonic-gate page. */ 1050Sstevel@tonic-gate #ifndef HAVE_SOCKLEN_T 1060Sstevel@tonic-gate typedef int socklen_t; 1070Sstevel@tonic-gate #endif 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate /* XXX should only be done if sockaddr_storage not found */ 1100Sstevel@tonic-gate #ifndef HAVE_STRUCT_SOCKADDR_STORAGE 1110Sstevel@tonic-gate struct krb5int_sockaddr_storage { 1120Sstevel@tonic-gate struct sockaddr_in s; 1130Sstevel@tonic-gate /* Plenty of slop just in case we get an ipv6 address anyways. */ 1140Sstevel@tonic-gate long extra[16]; 1150Sstevel@tonic-gate }; 1160Sstevel@tonic-gate #define sockaddr_storage krb5int_sockaddr_storage 1170Sstevel@tonic-gate #endif 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* 1200Sstevel@tonic-gate * Compatability with WinSock calls on MS-Windows... 1210Sstevel@tonic-gate */ 1220Sstevel@tonic-gate #define SOCKET int 1230Sstevel@tonic-gate #define INVALID_SOCKET ((SOCKET)~0) 1240Sstevel@tonic-gate #define closesocket close 1250Sstevel@tonic-gate #define ioctlsocket ioctl 1260Sstevel@tonic-gate #define SOCKET_ERROR (-1) 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate typedef struct iovec sg_buf; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate #define SG_ADVANCE(SG, N) \ 1310Sstevel@tonic-gate ((SG)->iov_len < (N) \ 1320Sstevel@tonic-gate ? (abort(), 0) \ 1330Sstevel@tonic-gate : ((SG)->iov_base = (char *) (SG)->iov_base + (N), \ 1340Sstevel@tonic-gate (SG)->iov_len -= (N), 0)) 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate #define SG_LEN(SG) ((SG)->iov_len + 0) 1370Sstevel@tonic-gate #define SG_BUF(SG) ((char*)(SG)->iov_base + 0) 1380Sstevel@tonic-gate #define SG_SET(SG, B, L) ((SG)->iov_base = (char*)(B), (SG)->iov_len = (L)) 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate /* Some of our own infrastructure where the WinSock stuff was too hairy 1410Sstevel@tonic-gate to dump into a clean Unix program... */ 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate #define SOCKET_INITIALIZE() (0) /* No error (or anything else) */ 1440Sstevel@tonic-gate #define SOCKET_CLEANUP() /* nothing */ 1450Sstevel@tonic-gate #define SOCKET_ERRNO errno 1460Sstevel@tonic-gate #define SOCKET_SET_ERRNO(x) (errno = (x)) 1470Sstevel@tonic-gate #define SOCKET_NFDS(f) ((f)+1) /* select() arg for a single fd */ 1480Sstevel@tonic-gate #define SOCKET_READ read 1490Sstevel@tonic-gate #define SOCKET_WRITE write 1500Sstevel@tonic-gate #define SOCKET_CONNECT connect 1510Sstevel@tonic-gate #define SOCKET_GETSOCKNAME getsockname 1520Sstevel@tonic-gate #define SOCKET_CLOSE close 1530Sstevel@tonic-gate #define SOCKET_EINTR EINTR 1540Sstevel@tonic-gate #define SOCKET_WRITEV_TEMP int 1550Sstevel@tonic-gate /* Use TMP to avoid compiler warnings and keep things consistent with 1560Sstevel@tonic-gate Windoze version. */ 1570Sstevel@tonic-gate #define SOCKET_WRITEV(FD, SG, LEN, TMP) \ 1580Sstevel@tonic-gate ((TMP) = writev((FD), (SG), (LEN)), (TMP)) 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate #define SHUTDOWN_READ 0 1610Sstevel@tonic-gate #define SHUTDOWN_WRITE 1 1620Sstevel@tonic-gate #define SHUTDOWN_BOTH 2 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate #ifndef HAVE_INET_NTOP 1650Sstevel@tonic-gate #define inet_ntop(AF,SRC,DST,CNT) \ 1660Sstevel@tonic-gate ((AF) == AF_INET \ 1670Sstevel@tonic-gate ? ((CNT) < 16 \ 168781Sgtb ? (SOCKET_SET_ERRNO(ENOSPC), (const char *)NULL) \ 1690Sstevel@tonic-gate : (sprintf((DST), "%d.%d.%d.%d", \ 1700Sstevel@tonic-gate ((const unsigned char *)(const void *)(SRC))[0] & 0xff, \ 1710Sstevel@tonic-gate ((const unsigned char *)(const void *)(SRC))[1] & 0xff, \ 1720Sstevel@tonic-gate ((const unsigned char *)(const void *)(SRC))[2] & 0xff, \ 1730Sstevel@tonic-gate ((const unsigned char *)(const void *)(SRC))[3] & 0xff), \ 1740Sstevel@tonic-gate (DST))) \ 175781Sgtb : (SOCKET_SET_ERRNO(EAFNOSUPPORT), (const char *)NULL)) 1760Sstevel@tonic-gate #define HAVE_INET_NTOP 1770Sstevel@tonic-gate #endif 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate #endif /* _WIN32 */ 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate #if !defined(_WIN32) 1820Sstevel@tonic-gate /* UNIX or ...? */ 1830Sstevel@tonic-gate # ifdef S_SPLINT_S 1840Sstevel@tonic-gate extern int socket (int, int, int) /*@*/; 1850Sstevel@tonic-gate # endif 1860Sstevel@tonic-gate #endif 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate #endif /*_PORT_SOCKET_H*/ 189