]>
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__
40 #include <stdarg.h> /* we declare varargs functions */
42 #include "pcap/funcattrs.h"
44 #include "pcap/socket.h"
48 #include <unistd.h> /* close() */
51 * \brief In Winsock, the close() call cannot be used on a socket;
52 * closesocket() must be used.
53 * We define closesocket() to be a wrapper around close() on UN*X,
54 * so that it can be used on both platforms.
56 #define closesocket(a) close(a)
59 #include "sslutils.h" // for SSL type, whatever that turns out to be
62 * MingW headers include this definition, but only for Windows XP and above.
63 * MSDN states that this function is available for most versions on Windows.
65 #if ((defined(__MINGW32__)) && (_WIN32_WINNT < 0x0501))
66 int WSAAPI
getnameinfo(const struct sockaddr
*,socklen_t
,char*,DWORD
,
71 * \defgroup SockUtils Cross-platform socket utilities (IPv4-IPv6)
75 * \addtogroup SockUtils
80 * \defgroup ExportedStruct Exported Structures and Definitions
84 * \addtogroup ExportedStruct
88 /****************************************************
90 * Exported functions / definitions *
92 ****************************************************/
94 /* 'checkonly' flag, into the rpsock_bufferize() */
95 #define SOCKBUF_CHECKONLY 1
96 /* no 'checkonly' flag, into the rpsock_bufferize() */
97 #define SOCKBUF_BUFFERIZE 0
99 /* no 'server' flag; it opens a client socket */
100 #define SOCKOPEN_CLIENT 0
101 /* 'server' flag; it opens a server socket */
102 #define SOCKOPEN_SERVER 1
105 * Flags for sock_recv().
107 #define SOCK_RECEIVEALL_NO 0x00000000 /* Don't wait to receive all data */
108 #define SOCK_RECEIVEALL_YES 0x00000001 /* Wait to receive all data */
110 #define SOCK_EOF_ISNT_ERROR 0x00000000 /* Return 0 on EOF */
111 #define SOCK_EOF_IS_ERROR 0x00000002 /* Return an error on EOF */
113 #define SOCK_MSG_PEEK 0x00000004 /* Return data but leave it in the socket queue */
124 * \defgroup ExportedFunc Exported Functions
128 * \addtogroup ExportedFunc
132 int sock_init(char *errbuf
, int errbuflen
);
133 void sock_cleanup(void);
134 int sock_geterrcode(void);
135 void sock_vfmterrmsg(char *errbuf
, size_t errbuflen
, int errcode
,
136 PCAP_FORMAT_STRING(const char *fmt
), va_list ap
) PCAP_PRINTFLIKE(4, 0);
137 void sock_fmterrmsg(char *errbuf
, size_t errbuflen
, int errcode
,
138 PCAP_FORMAT_STRING(const char *fmt
), ...) PCAP_PRINTFLIKE(4, 5);
139 void sock_geterrmsg(char *errbuf
, size_t errbuflen
,
140 PCAP_FORMAT_STRING(const char *fmt
), ...) PCAP_PRINTFLIKE(3, 4);
141 struct addrinfo
*sock_initaddress(const char *address
, const char *port
,
142 struct addrinfo
*hints
, char *errbuf
, int errbuflen
);
143 int sock_recv(PCAP_SOCKET sock
, SSL
*, void *buffer
, size_t size
,
144 int receiveall
, char *errbuf
, int errbuflen
);
145 int sock_recv_dgram(PCAP_SOCKET sock
, SSL
*, void *buffer
, size_t size
,
146 char *errbuf
, int errbuflen
);
147 PCAP_SOCKET
sock_open(const char *host
, struct addrinfo
*addrinfo
, int server
,
148 int nconn
, char *errbuf
, int errbuflen
);
149 int sock_close(PCAP_SOCKET sock
, char *errbuf
, int errbuflen
);
151 int sock_send(PCAP_SOCKET sock
, SSL
*, const char *buffer
, size_t size
,
152 char *errbuf
, int errbuflen
);
153 int sock_bufferize(const void *data
, int size
, char *outbuf
, int *offset
, int totsize
, int checkonly
, char *errbuf
, int errbuflen
);
154 int sock_discard(PCAP_SOCKET sock
, SSL
*, int size
, char *errbuf
,
156 int sock_check_hostlist(const char *hostlist
, const char *sep
, struct sockaddr_storage
*from
, char *errbuf
, int errbuflen
);
157 int sock_cmpaddr(struct sockaddr_storage
*first
, struct sockaddr_storage
*second
);
159 int sock_getmyinfo(PCAP_SOCKET sock
, char *address
, int addrlen
, char *port
,
160 int portlen
, int flags
, char *errbuf
, int errbuflen
);
162 int sock_getascii_addrport(const struct sockaddr_storage
*sockaddr
, char *address
, int addrlen
, char *port
, int portlen
, int flags
, char *errbuf
, size_t errbuflen
);
163 int sock_present2network(const char *address
, struct sockaddr_storage
*sockaddr
, int addr_family
, char *errbuf
, int errbuflen
);