xref: /freebsd/contrib/libpcap/pcap/socket.h (revision afdbf109c6a661a729938f68211054a0a50d38ac)
157e22627SCy Schubert /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
257e22627SCy Schubert /*
357e22627SCy Schubert  * Copyright (c) 1993, 1994, 1995, 1996, 1997
457e22627SCy Schubert  *	The Regents of the University of California.  All rights reserved.
557e22627SCy Schubert  *
657e22627SCy Schubert  * Redistribution and use in source and binary forms, with or without
757e22627SCy Schubert  * modification, are permitted provided that the following conditions
857e22627SCy Schubert  * are met:
957e22627SCy Schubert  * 1. Redistributions of source code must retain the above copyright
1057e22627SCy Schubert  *    notice, this list of conditions and the following disclaimer.
1157e22627SCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
1257e22627SCy Schubert  *    notice, this list of conditions and the following disclaimer in the
1357e22627SCy Schubert  *    documentation and/or other materials provided with the distribution.
1457e22627SCy Schubert  * 3. All advertising materials mentioning features or use of this software
1557e22627SCy Schubert  *    must display the following acknowledgement:
1657e22627SCy Schubert  *	This product includes software developed by the Computer Systems
1757e22627SCy Schubert  *	Engineering Group at Lawrence Berkeley Laboratory.
1857e22627SCy Schubert  * 4. Neither the name of the University nor of the Laboratory may be used
1957e22627SCy Schubert  *    to endorse or promote products derived from this software without
2057e22627SCy Schubert  *    specific prior written permission.
2157e22627SCy Schubert  *
2257e22627SCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2357e22627SCy Schubert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2457e22627SCy Schubert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2557e22627SCy Schubert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2657e22627SCy Schubert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2757e22627SCy Schubert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2857e22627SCy Schubert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2957e22627SCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3057e22627SCy Schubert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3157e22627SCy Schubert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3257e22627SCy Schubert  * SUCH DAMAGE.
3357e22627SCy Schubert  */
3457e22627SCy Schubert 
3557e22627SCy Schubert #ifndef lib_pcap_socket_h
3657e22627SCy Schubert #define lib_pcap_socket_h
3757e22627SCy Schubert 
3857e22627SCy Schubert /*
3957e22627SCy Schubert  * Some minor differences between sockets on various platforms.
4057e22627SCy Schubert  * We include whatever sockets are needed for Internet-protocol
4157e22627SCy Schubert  * socket access on UN*X and Windows.
4257e22627SCy Schubert  */
4357e22627SCy Schubert #ifdef _WIN32
4457e22627SCy Schubert   /* Need windef.h for defines used in winsock2.h under MingW32 */
4557e22627SCy Schubert   #ifdef __MINGW32__
4657e22627SCy Schubert     #include <windef.h>
4757e22627SCy Schubert   #endif
4857e22627SCy Schubert   #include <winsock2.h>
4957e22627SCy Schubert   #include <ws2tcpip.h>
5057e22627SCy Schubert 
51*afdbf109SJoseph Mingrone   /*!
52*afdbf109SJoseph Mingrone    * \brief In Winsock, a socket handle is of type SOCKET; in UN*X, it's
53*afdbf109SJoseph Mingrone    * a file descriptor, and therefore a signed integer.
54*afdbf109SJoseph Mingrone    * We define PCAP_SOCKET to be a signed integer on UN*X and a
55*afdbf109SJoseph Mingrone    * SOCKET on Windows, so that it can be used on both platforms.
56*afdbf109SJoseph Mingrone    *
57*afdbf109SJoseph Mingrone    * We used to use SOCKET rather than PCAP_SOCKET, but that collided
58*afdbf109SJoseph Mingrone    * with other software, such as barnyard2, which had their own
59*afdbf109SJoseph Mingrone    * definitions of SOCKET, so we changed it to PCAP_SOCKET.
60*afdbf109SJoseph Mingrone    *
61*afdbf109SJoseph Mingrone    * On Windows, this shouldn't break any APIs, as any code using
62*afdbf109SJoseph Mingrone    * the two active-mode APIs that return a socket handle would
63*afdbf109SJoseph Mingrone    * probably be assigning their return values to a SOCKET, and
64*afdbf109SJoseph Mingrone    * as, on Windows, we're defining PCAP_SOCKET as SOCKET, there
65*afdbf109SJoseph Mingrone    * would be no type clash.
66*afdbf109SJoseph Mingrone    */
67*afdbf109SJoseph Mingrone   #ifndef PCAP_SOCKET
68*afdbf109SJoseph Mingrone     #define PCAP_SOCKET SOCKET
69*afdbf109SJoseph Mingrone   #endif
70*afdbf109SJoseph Mingrone 
7157e22627SCy Schubert   /*
7257e22627SCy Schubert    * Winsock doesn't have this POSIX type; it's used for the
7357e22627SCy Schubert    * tv_usec value of struct timeval.
7457e22627SCy Schubert    */
7557e22627SCy Schubert   typedef long suseconds_t;
7657e22627SCy Schubert #else /* _WIN32 */
7757e22627SCy Schubert   #include <sys/types.h>
7857e22627SCy Schubert   #include <sys/socket.h>
7957e22627SCy Schubert   #include <netdb.h>		/* for struct addrinfo/getaddrinfo() */
8057e22627SCy Schubert   #include <netinet/in.h>	/* for sockaddr_in, in BSD at least */
8157e22627SCy Schubert   #include <arpa/inet.h>
8257e22627SCy Schubert 
8357e22627SCy Schubert   /*!
84*afdbf109SJoseph Mingrone    * \brief In Winsock, a socket handle is of type SOCKET; in UN*Xes,
85*afdbf109SJoseph Mingrone    * it's a file descriptor, and therefore a signed integer.
86*afdbf109SJoseph Mingrone    * We define PCAP_SOCKET to be a signed integer on UN*X and a
87*afdbf109SJoseph Mingrone    * SOCKET on Windows, so that it can be used on both platforms.
88*afdbf109SJoseph Mingrone    *
89*afdbf109SJoseph Mingrone    * We used to use SOCKET rather than PCAP_SOCKET, but that collided
90*afdbf109SJoseph Mingrone    * with other software, such as barnyard2, which had their own
91*afdbf109SJoseph Mingrone    * definitions of SOCKET, so we changed it to PCAP_SOCKET.
92*afdbf109SJoseph Mingrone    *
93*afdbf109SJoseph Mingrone    * On UN*Xes, this might break code that uses one of the two
94*afdbf109SJoseph Mingrone    * active-mode APIs that return a socket handle if those programs
95*afdbf109SJoseph Mingrone    * were written to assign the return values of those APIs to a
96*afdbf109SJoseph Mingrone    * SOCKET, as we're no longer defining SOCKET.  However, as
97*afdbf109SJoseph Mingrone    * those APIs are only provided if libpcap is built with remote
98*afdbf109SJoseph Mingrone    * capture support - which is not the default - and as they're
99*afdbf109SJoseph Mingrone    * somewhat painful to use, there's probably little if any code
100*afdbf109SJoseph Mingrone    * that needs to compile for UN*X and that uses them.  If there
101*afdbf109SJoseph Mingrone    * *is* any such code, it could do
102*afdbf109SJoseph Mingrone    *
103*afdbf109SJoseph Mingrone    *    #ifndef PCAP_SOCKET
104*afdbf109SJoseph Mingrone    *        #ifdef _WIN32
105*afdbf109SJoseph Mingrone    *            #define PCAP_SOCKET SOCKET
106*afdbf109SJoseph Mingrone    *        #else
107*afdbf109SJoseph Mingrone    *            #defube PCAP_SOCKET int
108*afdbf109SJoseph Mingrone    *        #endif
109*afdbf109SJoseph Mingrone    *    #endif
110*afdbf109SJoseph Mingrone    *
111*afdbf109SJoseph Mingrone    * and use PCAP_SOCKET.
11257e22627SCy Schubert    */
113*afdbf109SJoseph Mingrone   #ifndef PCAP_SOCKET
114*afdbf109SJoseph Mingrone     #define PCAP_SOCKET int
11557e22627SCy Schubert   #endif
11657e22627SCy Schubert 
11757e22627SCy Schubert   /*!
11857e22627SCy Schubert    * \brief In Winsock, the error return if socket() fails is INVALID_SOCKET;
11957e22627SCy Schubert    * in UN*X, it's -1.
12057e22627SCy Schubert    * We define INVALID_SOCKET to be -1 on UN*X, so that it can be used on
12157e22627SCy Schubert    * both platforms.
12257e22627SCy Schubert    */
12357e22627SCy Schubert   #ifndef INVALID_SOCKET
12457e22627SCy Schubert     #define INVALID_SOCKET -1
12557e22627SCy Schubert   #endif
12657e22627SCy Schubert #endif /* _WIN32 */
12757e22627SCy Schubert 
12857e22627SCy Schubert #endif /* lib_pcap_socket_h */
129