| 1 | /* Define required structures and constants if they're needed. |
|---|
| 2 | * Most of these are in Posix 1001.g, but getnameinfo isn't (Though |
|---|
| 3 | * it's in at least one RFC. Anyways, all this gives us IP version |
|---|
| 4 | * independance. |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | #ifndef __MYSOCKET_H |
|---|
| 8 | #define __MYSOCKET_H |
|---|
| 9 | |
|---|
| 10 | #include "copyrite.h" |
|---|
| 11 | #include "config.h" |
|---|
| 12 | #include "confmagic.h" |
|---|
| 13 | |
|---|
| 14 | #ifdef WIN32 |
|---|
| 15 | #ifndef FD_SETSIZE |
|---|
| 16 | #define FD_SETSIZE 256 |
|---|
| 17 | #endif |
|---|
| 18 | #include <winsock.h> |
|---|
| 19 | #include <io.h> |
|---|
| 20 | #undef EINTR /* Clashes with errno.h */ |
|---|
| 21 | #define EINTR WSAEINTR |
|---|
| 22 | #define EWOULDBLOCK WSAEWOULDBLOCK |
|---|
| 23 | #define EINPROGRESS WSAEINPROGRESS |
|---|
| 24 | #define ETIMEDOUT WSAETIMEDOUT |
|---|
| 25 | #define EAFNOSUPPORT WSAEAFNOSUPPORT |
|---|
| 26 | #define ENOSPC 28 |
|---|
| 27 | #define MAXHOSTNAMELEN 32 |
|---|
| 28 | #pragma comment( lib, "wsock32.lib") |
|---|
| 29 | #pragma comment( lib, "winmm.lib") |
|---|
| 30 | #pragma comment( lib, "advapi32.lib") |
|---|
| 31 | #endif |
|---|
| 32 | |
|---|
| 33 | /* This number taken from Stevens. It's the size of the largest possible |
|---|
| 34 | * sockaddr_* struct. Since that includes unix-domain sockets, this |
|---|
| 35 | * gives us lots of buffer space. */ |
|---|
| 36 | #ifndef MAXSOCKADDR |
|---|
| 37 | #define MAXSOCKADDR 128 |
|---|
| 38 | #endif |
|---|
| 39 | |
|---|
| 40 | /** Information about a host. |
|---|
| 41 | */ |
|---|
| 42 | struct hostname_info { |
|---|
| 43 | const char *hostname; /**< Host's name */ |
|---|
| 44 | const char *port; /**< Host's source port */ |
|---|
| 45 | }; |
|---|
| 46 | |
|---|
| 47 | /** A union for sockaddr manipulation */ |
|---|
| 48 | union sockaddr_u { |
|---|
| 49 | struct sockaddr addr; /**< A sockaddr structure */ |
|---|
| 50 | char data[MAXSOCKADDR]; /**< A byte array representation */ |
|---|
| 51 | }; |
|---|
| 52 | |
|---|
| 53 | /* What htons expects. Is this even used anymore? */ |
|---|
| 54 | typedef unsigned short Port_t; |
|---|
| 55 | |
|---|
| 56 | struct hostname_info *hostname_convert(struct sockaddr *host, int len); |
|---|
| 57 | struct hostname_info *ip_convert(struct sockaddr *host, int len); |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | /* Open a socket for listening */ |
|---|
| 61 | int make_socket |
|---|
| 62 | (Port_t port, int socktype, union sockaddr_u *addr, socklen_t * len, |
|---|
| 63 | const char *host); |
|---|
| 64 | /* Connect somewhere using TCP */ |
|---|
| 65 | int make_socket_conn(const char *host, int socktype, |
|---|
| 66 | struct sockaddr *myiterface, socklen_t myilen, Port_t port, |
|---|
| 67 | bool nonb); |
|---|
| 68 | int wait_for_connect(int, int); |
|---|
| 69 | void make_nonblocking(int s); |
|---|
| 70 | void make_blocking(int s); |
|---|
| 71 | void set_keepalive(int s); |
|---|
| 72 | /* Win32 uses closesocket() to close a socket, and so will we */ |
|---|
| 73 | #ifndef WIN32 |
|---|
| 74 | #define closesocket(s) close(s) |
|---|
| 75 | #else |
|---|
| 76 | extern BOOL GetErrorMessage(const DWORD dwError, LPTSTR lpszError, const UINT |
|---|
| 77 | nMaxError); |
|---|
| 78 | #endif |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | #ifndef HAS_GETHOSTBYNAME2 |
|---|
| 82 | #define gethostbyname2(host, type) gethostbyname((host)) |
|---|
| 83 | #endif |
|---|
| 84 | |
|---|
| 85 | #ifndef HAS_INET_PTON |
|---|
| 86 | int inet_pton(int, const char *, void *); |
|---|
| 87 | const char *inet_ntop(int, const void *, char *, size_t); |
|---|
| 88 | #endif |
|---|
| 89 | |
|---|
| 90 | #ifndef HAS_GETADDRINFO |
|---|
| 91 | /** addrinfo structure for systems without it. |
|---|
| 92 | * Everything here really belongs in <netdb.h>. |
|---|
| 93 | * These defines are separate for now, to avoid having to modify the |
|---|
| 94 | * system's header. |
|---|
| 95 | */ |
|---|
| 96 | |
|---|
| 97 | struct addrinfo { |
|---|
| 98 | int ai_flags; /**< AI_PASSIVE, AI_CANONNAME */ |
|---|
| 99 | int ai_family; /**< PF_xxx */ |
|---|
| 100 | int ai_socktype; /**< SOCK_xxx */ |
|---|
| 101 | int ai_protocol; /**< IPPROTO_xxx for IPv4 and IPv6 */ |
|---|
| 102 | size_t ai_addrlen; /**< length of ai_addr */ |
|---|
| 103 | char *ai_canonname; /**< canonical name for host */ |
|---|
| 104 | struct sockaddr *ai_addr; /**< binary address */ |
|---|
| 105 | struct addrinfo *ai_next; /**< next structure in linked list */ |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | struct addrinfo; |
|---|
| 109 | /* following for getaddrinfo() */ |
|---|
| 110 | #define AI_PASSIVE 1 /* socket is intended for bind() + listen() */ |
|---|
| 111 | #define AI_CANONNAME 2 /* return canonical name */ |
|---|
| 112 | |
|---|
| 113 | /* error returns */ |
|---|
| 114 | #define EAI_ADDRFAMILY 1 /* address family for host not supported */ |
|---|
| 115 | #define EAI_AGAIN 2 /* temporary failure in name resolution */ |
|---|
| 116 | #define EAI_BADFLAGS 3 /* invalid value for ai_flags */ |
|---|
| 117 | #define EAI_FAIL 4 /* non-recoverable failure in name resolution */ |
|---|
| 118 | #define EAI_FAMILY 5 /* ai_family not supported */ |
|---|
| 119 | #define EAI_MEMORY 6 /* memory allocation failure */ |
|---|
| 120 | #define EAI_NODATA 7 /* no address associated with host */ |
|---|
| 121 | #define EAI_NONAME 8 /* host nor service provided, or not known */ |
|---|
| 122 | #define EAI_SERVICE 9 /* service not supported for ai_socktype */ |
|---|
| 123 | #define EAI_SOCKTYPE 10 /* ai_socktype not supported */ |
|---|
| 124 | #define EAI_SYSTEM 11 /* system error returned in errno */ |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | int getaddrinfo(const char *hostname, const char *servname, |
|---|
| 128 | const struct addrinfo *hintsp, struct addrinfo **result); |
|---|
| 129 | /* If we don't have getaddrinfo, we won't have these either... */ |
|---|
| 130 | |
|---|
| 131 | void freeaddrinfo(struct addrinfo *old); |
|---|
| 132 | |
|---|
| 133 | #endif /* HAS_GETADDRINFO */ |
|---|
| 134 | |
|---|
| 135 | #ifndef HAS_GAI_STRERROR |
|---|
| 136 | const char *gai_strerror(int errval); |
|---|
| 137 | #endif |
|---|
| 138 | |
|---|
| 139 | #ifndef HAS_GETNAMEINFO /* following for getnameinfo() */ |
|---|
| 140 | #ifndef __APPLE__ |
|---|
| 141 | /* Apple has these in netdb.h */ |
|---|
| 142 | #define NI_MAXHOST 1025 /* max hostname returned */ |
|---|
| 143 | #define NI_MAXSERV 32 /* max service name returned */ |
|---|
| 144 | |
|---|
| 145 | #define NI_NOFQDN 1 /* do not return FQDN */ |
|---|
| 146 | #define NI_NUMERICHOST 2 /* return numeric form of hostname */ |
|---|
| 147 | #define NI_NAMEREQD 4 /* return error if hostname not found */ |
|---|
| 148 | #define NI_NUMERICSERV 8 /* return numeric form of service name */ |
|---|
| 149 | #define NI_DGRAM 16 /* datagram service for getservbyname() */ |
|---|
| 150 | #endif |
|---|
| 151 | |
|---|
| 152 | int getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, |
|---|
| 153 | size_t hostlen, char *serv, size_t servlen, int flags); |
|---|
| 154 | #endif /* HAS_GETNAMEINFO */ |
|---|
| 155 | |
|---|
| 156 | #endif /* MYSOCKET_H */ |
|---|