]>
The Tcpdump Group git mirrors - libpcap/blob - sockutils.h
2 * Copyright (c) 2002 - 2003
3 * NetGroup, Politecnico di Torino (Italy)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #ifndef __SOCKUTILS_H__
34 #define __SOCKUTILS_H__
41 /* Need windef.h for defines used in winsock2.h under MingW32 */
49 * Winsock doesn't have this UN*X type; it's used in the UN*X
52 * XXX - do we need to worry about UN*Xes so old that *they*
53 * don't have it, either?
55 typedef int socklen_t
;
59 #include <string.h> /* for memset() */
60 #include <sys/types.h>
61 #include <sys/socket.h>
62 #include <netdb.h> /* DNS lookup */
63 #include <unistd.h> /* close() */
64 #include <errno.h> /* errno() */
65 #include <netinet/in.h> /* for sockaddr_in, in BSD at least */
66 #include <arpa/inet.h>
70 * \brief In Winsock, a socket handle is of type SOCKET; in UN*X, it's
71 * a file descriptor, and therefore a signed integer.
72 * We define SOCKET to be a signed integer on UN*X, so that it can
73 * be used on both platforms.
80 * \brief In Winsock, the error return if socket() fails is INVALID_SOCKET;
82 * We define INVALID_SOCKET to be -1 on UN*X, so that it can be used on
85 #ifndef INVALID_SOCKET
86 #define INVALID_SOCKET -1
90 * \brief In Winsock, the close() call cannot be used on a socket;
91 * closesocket() must be used.
92 * We define closesocket() to be a wrapper around close() on UN*X,
93 * so that it can be used on both platforms.
95 #define closesocket(a) close(a)
99 * MingW headers include this definition, but only for Windows XP and above.
100 * MSDN states that this function is available for most versions on Windows.
102 #if ((defined(__MINGW32__)) && (_WIN32_WINNT < 0x0501))
103 int WSAAPI
getnameinfo(const struct sockaddr
*,socklen_t
,char*,DWORD
,
108 * \defgroup SockUtils Cross-platform socket utilities (IPv4-IPv6)
112 * \addtogroup SockUtils
117 * \defgroup ExportedStruct Exported Structures and Definitions
121 * \addtogroup ExportedStruct
126 * \brief DEBUG facility: it prints an error message on the screen (stderr)
128 * This macro prints the error on the standard error stream (stderr);
129 * if we are working in debug mode (i.e. there is no NDEBUG defined) and we are in
130 * Microsoft Visual C++, the error message will appear on the MSVC console as well.
132 * When NDEBUG is defined, this macro is empty.
134 * \param msg: the message you want to print.
136 * \param expr: 'false' if you want to abort the program, 'true' it you want
137 * to print the message and continue.
139 * \return No return values.
142 #define SOCK_DEBUG_MESSAGE(msg) ((void)0)
144 #if (defined(_WIN32) && defined(_MSC_VER))
145 #include <crtdbg.h> /* for _CrtDbgReport */
146 /* Use MessageBox(NULL, msg, "warning", MB_OK)' instead of the other calls if you want to debug a Win32 service */
147 /* Remember to activate the 'allow service to interact with desktop' flag of the service */
148 #define SOCK_DEBUG_MESSAGE(msg) { _CrtDbgReport(_CRT_WARN, NULL, 0, NULL, "%s\n", msg); fprintf(stderr, "%s\n", msg); }
150 #define SOCK_DEBUG_MESSAGE(msg) { fprintf(stderr, "%s\n", msg); }
154 /****************************************************
156 * Exported functions / definitions *
158 ****************************************************/
160 /* 'checkonly' flag, into the rpsock_bufferize() */
161 #define SOCKBUF_CHECKONLY 1
162 /* no 'checkonly' flag, into the rpsock_bufferize() */
163 #define SOCKBUF_BUFFERIZE 0
165 /* no 'server' flag; it opens a client socket */
166 #define SOCKOPEN_CLIENT 0
167 /* 'server' flag; it opens a server socket */
168 #define SOCKOPEN_SERVER 1
171 * Flags for sock_recv().
173 #define SOCK_RECEIVEALL_NO 0x00000000 /* Don't wait to receive all data */
174 #define SOCK_RECEIVEALL_YES 0x00000001 /* Wait to receive all data */
176 #define SOCK_EOF_ISNT_ERROR 0x00000000 /* Return 0 on EOF */
177 #define SOCK_EOF_IS_ERROR 0x00000002 /* Return an error on EOF */
188 * \defgroup ExportedFunc Exported Functions
192 * \addtogroup ExportedFunc
196 int sock_init(char *errbuf
, int errbuflen
);
197 void sock_cleanup(void);
198 void sock_fmterror(const char *caller
, int errcode
, char *errbuf
, int errbuflen
);
199 void sock_geterror(const char *caller
, char *errbuf
, int errbufsize
);
200 int sock_initaddress(const char *address
, const char *port
,
201 struct addrinfo
*hints
, struct addrinfo
**addrinfo
,
202 char *errbuf
, int errbuflen
);
203 int sock_recv(SOCKET sock
, void *buffer
, size_t size
, int receiveall
,
204 char *errbuf
, int errbuflen
);
205 int sock_recv_dgram(SOCKET sock
, void *buffer
, size_t size
,
206 char *errbuf
, int errbuflen
);
207 SOCKET
sock_open(struct addrinfo
*addrinfo
, int server
, int nconn
, char *errbuf
, int errbuflen
);
208 int sock_close(SOCKET sock
, char *errbuf
, int errbuflen
);
210 int sock_send(SOCKET sock
, const char *buffer
, size_t size
,
211 char *errbuf
, int errbuflen
);
212 int sock_bufferize(const char *buffer
, int size
, char *tempbuf
, int *offset
, int totsize
, int checkonly
, char *errbuf
, int errbuflen
);
213 int sock_discard(SOCKET sock
, int size
, char *errbuf
, int errbuflen
);
214 int sock_check_hostlist(char *hostlist
, const char *sep
, struct sockaddr_storage
*from
, char *errbuf
, int errbuflen
);
215 int sock_cmpaddr(struct sockaddr_storage
*first
, struct sockaddr_storage
*second
);
217 int sock_getmyinfo(SOCKET sock
, char *address
, int addrlen
, char *port
, int portlen
, int flags
, char *errbuf
, int errbuflen
);
219 int sock_getascii_addrport(const struct sockaddr_storage
*sockaddr
, char *address
, int addrlen
, char *port
, int portlen
, int flags
, char *errbuf
, int errbuflen
);
220 int sock_present2network(const char *address
, struct sockaddr_storage
*sockaddr
, int addr_family
, char *errbuf
, int errbuflen
);