1 /* <!-- copyright */
2 /*
3  * aria2 - The high speed download utility
4  *
5  * Copyright (C) 2006 Tatsuhiro Tsujikawa
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * In addition, as a special exception, the copyright holders give
22  * permission to link the code of portions of this program with the
23  * OpenSSL library under certain conditions as described in each
24  * individual source file, and distribute linked combinations
25  * including the two.
26  * You must obey the GNU General Public License in all respects
27  * for all of the code used other than OpenSSL.  If you modify
28  * file(s) with this exception, you may extend this exception to your
29  * version of the file(s), but you are not obligated to do so.  If you
30  * do not wish to do so, delete this exception statement from your
31  * version.  If you delete this exception statement from all source
32  * files in the program, then also delete it here.
33  */
34 /* copyright --> */
35 #ifndef D_A2NETCOMPAT_H
36 #define D_A2NETCOMPAT_H
37 
38 #include "a2io.h"
39 
40 #ifdef __MINGW32__
41 #  ifdef HAVE_WS2TCPIP_H
42 #    include <ws2tcpip.h>
43 #  endif // HAVE_WS2TCPIP_H
44 #endif   // __MINGW32__
45 
46 #ifdef __MINGW32__
47 #  define a2_sockopt_t char*
48 #  ifndef HAVE_GETADDRINFO
49 #    define HAVE_GETADDRINFO
50 #  endif // !HAVE_GETADDRINFO
51 #  undef HAVE_GAI_STRERROR
52 #  undef gai_strerror
53 #else
54 #  define a2_sockopt_t void*
55 #endif // __MINGW32__
56 
57 #ifdef HAVE_NETDB_H
58 #  include <netdb.h>
59 #endif // HAVE_NETDB_H
60 
61 #ifdef HAVE_SYS_SOCKET_H
62 #  include <sys/socket.h>
63 #endif // HAVE_SYS_SOCKET_H
64 
65 #ifdef HAVE_NETINET_IN_H
66 #  include <netinet/in.h>
67 #endif // HAVE_NETINET_IN_H
68 
69 #ifdef HAVE_NETINET_TCP_H
70 #  include <netinet/tcp.h>
71 #endif // HAVE_NETINET_TCP_H
72 
73 #ifdef HAVE_ARPA_INET_H
74 #  include <arpa/inet.h>
75 #endif // HAVE_ARPA_INET_H
76 
77 #ifdef HAVE_NETINET_IN_H
78 #  include <netinet/in.h>
79 #endif // HAVE_NETINET_IN_H
80 
81 #ifdef HAVE_SYS_UIO_H
82 #  include <sys/uio.h>
83 #endif // HAVE_SYS_UIO_H
84 
85 #ifndef HAVE_GETADDRINFO
86 #  include "getaddrinfo.h"
87 #  define HAVE_GAI_STRERROR
88 #endif // HAVE_GETADDRINFO
89 
90 #ifndef HAVE_GAI_STRERROR
91 #  include "gai_strerror.h"
92 #endif // HAVE_GAI_STRERROR
93 
94 #include <string>
95 
96 #ifdef HAVE_WINSOCK2_H
97 #  define sock_t SOCKET
98 #else
99 #  define sock_t int
100 #endif
101 
102 #ifndef AI_ADDRCONFIG
103 #  define AI_ADDRCONFIG 0
104 #endif // !AI_ADDRCONFIG
105 
106 #define DEFAULT_AI_FLAGS AI_ADDRCONFIG
107 
108 #ifdef __MINGW32__
109 #  ifndef SHUT_WR
110 #    define SHUT_WR SD_SEND
111 #  endif // !SHUT_WR
112 #endif   // __MINGW32__
113 
114 union sockaddr_union {
115   sockaddr sa;
116   sockaddr_storage storage;
117   sockaddr_in6 in6;
118   sockaddr_in in;
119 };
120 
121 struct SockAddr {
122   sockaddr_union su;
123   socklen_t suLength;
124 };
125 
126 // Human readable address, family and port.  In other words, addr is
127 // text name, usually obtained from getnameinfo(3).  The family field
128 // is the protocol family if it is known when generating this object.
129 // If it is unknown, this is AF_UNSPEC.
130 struct Endpoint {
131   std::string addr;
132   int family;
133   uint16_t port;
134 };
135 
136 #define A2_DEFAULT_IOV_MAX 128
137 
138 #if defined(IOV_MAX) && IOV_MAX < A2_DEFAULT_IOV_MAX
139 #  define A2_IOV_MAX IOV_MAX
140 #else
141 #  define A2_IOV_MAX A2_DEFAULT_IOV_MAX
142 #endif
143 
144 #ifdef __MINGW32__
145 typedef WSABUF a2iovec;
146 #  define A2IOVEC_BASE buf
147 #  define A2IOVEC_LEN len
148 #else // !__MINGW32__
149 typedef struct iovec a2iovec;
150 #  define A2IOVEC_BASE iov_base
151 #  define A2IOVEC_LEN iov_len
152 #endif // !__MINGW32__
153 
154 #endif // D_A2NETCOMPAT_H
155