]>
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
;
58 * Winsock doesn't have this POSIX type; it's used for the
59 * tv_usec value of struct timeval.
61 typedef long suseconds_t
;
65 #include <string.h> /* for memset() */
66 #include <sys/types.h>
67 #include <sys/socket.h>
68 #include <netdb.h> /* DNS lookup */
69 #include <unistd.h> /* close() */
70 #include <errno.h> /* errno() */
71 #include <netinet/in.h> /* for sockaddr_in, in BSD at least */
72 #include <arpa/inet.h>
76 * \brief In Winsock, a socket handle is of type SOCKET; in UN*X, it's
77 * a file descriptor, and therefore a signed integer.
78 * We define SOCKET to be a signed integer on UN*X, so that it can
79 * be used on both platforms.
86 * \brief In Winsock, the error return if socket() fails is INVALID_SOCKET;
88 * We define INVALID_SOCKET to be -1 on UN*X, so that it can be used on
91 #ifndef INVALID_SOCKET
92 #define INVALID_SOCKET -1
96 * \brief In Winsock, the close() call cannot be used on a socket;
97 * closesocket() must be used.
98 * We define closesocket() to be a wrapper around close() on UN*X,
99 * so that it can be used on both platforms.
101 #define closesocket(a) close(a)
105 * MingW headers include this definition, but only for Windows XP and above.
106 * MSDN states that this function is available for most versions on Windows.
108 #if ((defined(__MINGW32__)) && (_WIN32_WINNT < 0x0501))
109 int WSAAPI
getnameinfo(const struct sockaddr
*,socklen_t
,char*,DWORD
,
114 * \defgroup SockUtils Cross-platform socket utilities (IPv4-IPv6)
118 * \addtogroup SockUtils
123 * \defgroup ExportedStruct Exported Structures and Definitions
127 * \addtogroup ExportedStruct
132 * \brief DEBUG facility: it prints an error message on the screen (stderr)
134 * This macro prints the error on the standard error stream (stderr);
135 * if we are working in debug mode (i.e. there is no NDEBUG defined) and we are in
136 * Microsoft Visual C++, the error message will appear on the MSVC console as well.
138 * When NDEBUG is defined, this macro is empty.
140 * \param msg: the message you want to print.
142 * \param expr: 'false' if you want to abort the program, 'true' it you want
143 * to print the message and continue.
145 * \return No return values.
148 #define SOCK_DEBUG_MESSAGE(msg) ((void)0)
150 #if (defined(_WIN32) && defined(_MSC_VER))
151 #include <crtdbg.h> /* for _CrtDbgReport */
152 /* Use MessageBox(NULL, msg, "warning", MB_OK)' instead of the other calls if you want to debug a Win32 service */
153 /* Remember to activate the 'allow service to interact with desktop' flag of the service */
154 #define SOCK_DEBUG_MESSAGE(msg) { _CrtDbgReport(_CRT_WARN, NULL, 0, NULL, "%s\n", msg); fprintf(stderr, "%s\n", msg); }
156 #define SOCK_DEBUG_MESSAGE(msg) { fprintf(stderr, "%s\n", msg); }
160 /****************************************************
162 * Exported functions / definitions *
164 ****************************************************/
166 /* 'checkonly' flag, into the rpsock_bufferize() */
167 #define SOCKBUF_CHECKONLY 1
168 /* no 'checkonly' flag, into the rpsock_bufferize() */
169 #define SOCKBUF_BUFFERIZE 0
171 /* no 'server' flag; it opens a client socket */
172 #define SOCKOPEN_CLIENT 0
173 /* 'server' flag; it opens a server socket */
174 #define SOCKOPEN_SERVER 1
177 * Flags for sock_recv().
179 #define SOCK_RECEIVEALL_NO 0x00000000 /* Don't wait to receive all data */
180 #define SOCK_RECEIVEALL_YES 0x00000001 /* Wait to receive all data */
182 #define SOCK_EOF_ISNT_ERROR 0x00000000 /* Return 0 on EOF */
183 #define SOCK_EOF_IS_ERROR 0x00000002 /* Return an error on EOF */
185 #define SOCK_MSG_PEEK 0x00000004 /* Return data but leave it in the socket queue */
196 * \defgroup ExportedFunc Exported Functions
200 * \addtogroup ExportedFunc
204 int sock_init(char *errbuf
, int errbuflen
);
205 void sock_cleanup(void);
206 void sock_fmterror(const char *caller
, int errcode
, char *errbuf
, int errbuflen
);
207 void sock_geterror(const char *caller
, char *errbuf
, int errbufsize
);
208 int sock_initaddress(const char *address
, const char *port
,
209 struct addrinfo
*hints
, struct addrinfo
**addrinfo
,
210 char *errbuf
, int errbuflen
);
211 int sock_recv(SOCKET sock
, void *buffer
, size_t size
, int receiveall
,
212 char *errbuf
, int errbuflen
);
213 int sock_recv_dgram(SOCKET sock
, void *buffer
, size_t size
,
214 char *errbuf
, int errbuflen
);
215 SOCKET
sock_open(struct addrinfo
*addrinfo
, int server
, int nconn
, char *errbuf
, int errbuflen
);
216 int sock_close(SOCKET sock
, char *errbuf
, int errbuflen
);
218 int sock_send(SOCKET sock
, const char *buffer
, size_t size
,
219 char *errbuf
, int errbuflen
);
220 int sock_bufferize(const char *buffer
, int size
, char *tempbuf
, int *offset
, int totsize
, int checkonly
, char *errbuf
, int errbuflen
);
221 int sock_discard(SOCKET sock
, int size
, char *errbuf
, int errbuflen
);
222 int sock_check_hostlist(char *hostlist
, const char *sep
, struct sockaddr_storage
*from
, char *errbuf
, int errbuflen
);
223 int sock_cmpaddr(struct sockaddr_storage
*first
, struct sockaddr_storage
*second
);
225 int sock_getmyinfo(SOCKET sock
, char *address
, int addrlen
, char *port
, int portlen
, int flags
, char *errbuf
, int errbuflen
);
227 int sock_getascii_addrport(const struct sockaddr_storage
*sockaddr
, char *address
, int addrlen
, char *port
, int portlen
, int flags
, char *errbuf
, int errbuflen
);
228 int sock_present2network(const char *address
, struct sockaddr_storage
*sockaddr
, int addr_family
, char *errbuf
, int errbuflen
);