]> The Tcpdump Group git mirrors - libpcap/blob - sockutils.h
update configure and cmake
[libpcap] / sockutils.h
1 /*
2 * Copyright (c) 2002 - 2003
3 * NetGroup, Politecnico di Torino (Italy)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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.
18 *
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.
30 *
31 */
32
33 #ifndef __SOCKUTILS_H__
34 #define __SOCKUTILS_H__
35
36 #ifdef _MSC_VER
37 #pragma once
38 #endif
39
40 #ifdef _WIN32
41 /* Need windef.h for defines used in winsock2.h under MingW32 */
42 #ifdef __MINGW32__
43 #include <windef.h>
44 #endif
45 #include <winsock2.h>
46 #include <ws2tcpip.h>
47
48 /*
49 * Winsock doesn't have this UN*X type; it's used in the UN*X
50 * sockets API.
51 *
52 * XXX - do we need to worry about UN*Xes so old that *they*
53 * don't have it, either?
54 */
55 typedef int socklen_t;
56
57 /*
58 * Winsock doesn't have this POSIX type; it's used for the
59 * tv_usec value of struct timeval.
60 */
61 typedef long suseconds_t;
62 #else
63 /* UN*X */
64 #include <stdio.h>
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>
73 #include <net/if.h>
74
75 /*!
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.
80 */
81 #ifndef SOCKET
82 #define SOCKET int
83 #endif
84
85 /*!
86 * \brief In Winsock, the error return if socket() fails is INVALID_SOCKET;
87 * in UN*X, it's -1.
88 * We define INVALID_SOCKET to be -1 on UN*X, so that it can be used on
89 * both platforms.
90 */
91 #ifndef INVALID_SOCKET
92 #define INVALID_SOCKET -1
93 #endif
94
95 /*!
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.
100 */
101 #define closesocket(a) close(a)
102 #endif
103
104 /*
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.
107 */
108 #if ((defined(__MINGW32__)) && (_WIN32_WINNT < 0x0501))
109 int WSAAPI getnameinfo(const struct sockaddr*,socklen_t,char*,DWORD,
110 char*,DWORD,int);
111 #endif
112
113 /*
114 * \defgroup SockUtils Cross-platform socket utilities (IPv4-IPv6)
115 */
116
117 /*
118 * \addtogroup SockUtils
119 * \{
120 */
121
122 /*
123 * \defgroup ExportedStruct Exported Structures and Definitions
124 */
125
126 /*
127 * \addtogroup ExportedStruct
128 * \{
129 */
130
131 /*
132 * \brief DEBUG facility: it prints an error message on the screen (stderr)
133 *
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.
137 *
138 * When NDEBUG is defined, this macro is empty.
139 *
140 * \param msg: the message you want to print.
141 *
142 * \param expr: 'false' if you want to abort the program, 'true' it you want
143 * to print the message and continue.
144 *
145 * \return No return values.
146 */
147 #ifdef NDEBUG
148 #define SOCK_DEBUG_MESSAGE(msg) ((void)0)
149 #else
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); }
155 #else
156 #define SOCK_DEBUG_MESSAGE(msg) { fprintf(stderr, "%s\n", msg); }
157 #endif
158 #endif
159
160 /****************************************************
161 * *
162 * Exported functions / definitions *
163 * *
164 ****************************************************/
165
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
170
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
175
176 /*
177 * Flags for sock_recv().
178 */
179 #define SOCK_RECEIVEALL_NO 0x00000000 /* Don't wait to receive all data */
180 #define SOCK_RECEIVEALL_YES 0x00000001 /* Wait to receive all data */
181
182 #define SOCK_EOF_ISNT_ERROR 0x00000000 /* Return 0 on EOF */
183 #define SOCK_EOF_IS_ERROR 0x00000002 /* Return an error on EOF */
184
185 #define SOCK_MSG_PEEK 0x00000004 /* Return data but leave it in the socket queue */
186
187 /*
188 * \}
189 */
190
191 #ifdef __cplusplus
192 extern "C" {
193 #endif
194
195 /*
196 * \defgroup ExportedFunc Exported Functions
197 */
198
199 /*
200 * \addtogroup ExportedFunc
201 * \{
202 */
203
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);
217
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);
224
225 int sock_getmyinfo(SOCKET sock, char *address, int addrlen, char *port, int portlen, int flags, char *errbuf, int errbuflen);
226
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);
229
230 #ifdef __cplusplus
231 }
232 #endif
233
234 /*
235 * \}
236 */
237
238 /*
239 * \}
240 */
241
242 #endif