2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
#if !defined(hysock_h)
20
/******************************************************\
21
Portable socket library implementation.
22
\******************************************************/
26
#include <sys/types.h>
30
#include <sys/socket.h>
31
#include <netinet/in.h>
34
#include <netinet/tcp.h>
35
#else /* !defined(ZOS) */
36
#include <netinet/tcp_var.h>
37
#endif /* !defined(ZOS) */
44
#if !defined(MACOSX) && !defined(__CYGWIN__)
45
#include <sys/socketvar.h>
47
#else /* !defined(ZOS) */
48
#include <arpa/inet.h>
50
#endif /* !defined(ZOS) */
54
// #include "hyport.h"
56
#define HYSOCKDEBUG(x, err) printf(x, err)
57
#define HYSOCKDEBUGH(x, err) printf(x, err)
58
#define HYSOCKDEBUGPRINT(x) printf(x)
60
#define HYSOCKDEBUG(x, err)
61
#define HYSOCKDEBUGH(x, err)
62
#define HYSOCKDEBUGPRINT(x)
64
/* the following defines are used to determine how gethostby*_r calls should be handled.*/
65
/* HOSTENT_DATA_R: if the HOSTENT_DATA structure is used */
66
#define HOSTENT_DATA_R (defined(AIX))
67
/* GLIBC_R: uses the GLIBC versions */
68
#define GLIBC_R (defined(LINUX))
69
/* ORIGINAL_R: the original gethostby* call is thread safe */
70
#define ORIGINAL_R defined(FREEBSD)
71
/* TOFIX: FreeBSD man page says:
72
* Though these functions are thread-safe, still it is recommended to use
73
* the getaddrinfo(3) family of functions, instead.
75
/* NO_R: gethostby*_r calls do not exist and the normal gethostby* calls are not threadsafe */
76
#define NO_R (defined(MACOSX) || defined(__CYGWIN__))
77
/* OTHER_R: everything else */
78
#define OTHER_R ((!HOSTENT_DATA_R)&&(!GLIBC_R)&&(!ORIGINAL_R)&&(!NO_R))
79
/* Converts (seconds, microseconds) to milliseconds */
80
#define TO_MILLIS(sec, microsec) (sec * 1000 + (microsec + 999) / 1000)
82
typedef int OSSOCKET; /* as returned by socket() */
83
typedef struct sockaddr_in OSSOCKADDR; /* as used by bind() and friends */
84
typedef struct hostent OSHOSTENT;
85
typedef fd_set OSFDSET;
86
typedef struct timeval OSTIMEVAL;
87
typedef struct linger OSLINGER;
88
typedef struct sockaddr OSADDR;
89
typedef struct sockaddr_in6 OSSOCKADDR_IN6; /* IPv6 */
90
typedef struct ipv6_mreq OSIPMREQ6;
91
typedef struct addrinfo OSADDRINFO; /* IPv6 */
92
typedef struct sockaddr_storage OSSOCKADDR_STORAGE; /* IPv6 */
96
#define OSSOCK_ANY 0 /* for getaddrinfo hints */
97
#define OSSOCK_STREAM SOCK_STREAM /* stream socket */
98
#define OSSOCK_DGRAM SOCK_DGRAM /* datagram socket */
99
#define OSSOCK_RAW SOCK_RAW /* raw-protocol interface */
100
#define OSSOCK_RDM SOCK_RDM /* reliably-delivered message */
101
#define OSSOCK_SEQPACKET SOCK_SEQPACKET /* sequenced packet stream */
102
#if defined(HOSTENT_DATA_R)
103
typedef struct hostent_data OSHOSTENT_DATA;
105
typedef struct ip_mreq OSIPMREQ;
106
#define OSSOMAXCONN SOMAXCONN
107
#define OS_BADSOCKET -1 /* Provide bad socket constant */
108
/* defines for socket levels */
109
#define OS_SOL_SOCKET SOL_SOCKET
110
#define OS_IPPROTO_TCP IPPROTO_TCP
111
#define OS_IPPROTO_IP IPPROTO_IP
112
#if defined(IPv6_FUNCTION_SUPPORT)
113
#define OS_IPPROTO_IPV6 IPPROTO_IPV6
115
/* defines for socket options */
116
#define OS_SO_LINGER SO_LINGER
117
#define OS_SO_KEEPALIVE SO_KEEPALIVE
118
#define OS_TCP_NODELAY TCP_NODELAY
119
#define OS_SO_REUSEADDR SO_REUSEADDR
120
#define OS_SO_SNDBUF SO_SNDBUF
121
#define OS_SO_RCVBUF SO_RCVBUF
122
#define OS_SO_BROADCAST SO_BROADCAST /* Support datagram broadcasts */
123
#define OS_SO_OOBINLINE SO_OOBINLINE
124
#define OS_IP_TOS IP_TOS
125
/* defines for socket options, multicast */
126
#define OS_MCAST_TTL IP_MULTICAST_TTL
127
#define OS_MCAST_ADD_MEMBERSHIP IP_ADD_MEMBERSHIP
128
#define OS_MCAST_DROP_MEMBERSHIP IP_DROP_MEMBERSHIP
129
#define OS_MCAST_INTERFACE IP_MULTICAST_IF
130
#define OS_MCAST_LOOP IP_MULTICAST_LOOP
131
#if defined(IPv6_FUNCTION_SUPPORT)
132
#define OS_MCAST_INTERFACE_2 IPV6_MULTICAST_IF
133
#if defined(FREEBSD) || defined(MACOSX) || defined(ZOS)
134
#define OS_IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
135
#define OS_IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
137
#define OS_IPV6_ADD_MEMBERSHIP IPV6_ADD_MEMBERSHIP
138
#define OS_IPV6_DROP_MEMBERSHIP IPV6_DROP_MEMBERSHIP
141
/* defines for the unix error constants. These may be overriden for specific platforms. */
142
#define HYPORT_ERROR_SOCKET_UNIX_CONNRESET ECONNRESET
143
#define HYPORT_ERROR_SOCKET_UNIX_EAGAIN EAGAIN
144
#define HYPORT_ERROR_SOCKET_UNIX_EAFNOSUPPORT EAFNOSUPPORT
145
#define HYPORT_ERROR_SOCKET_UNIX_EBADF EBADF
146
#define HYPORT_ERROR_SOCKET_UNIX_ECONNRESET ECONNRESET
147
#define HYPORT_ERROR_SOCKET_UNIX_EINVAL EINVAL
148
#define HYPORT_ERROR_SOCKET_UNIX_EINTR EINTR
149
#define HYPORT_ERROR_SOCKET_UNIX_EFAULT EFAULT
150
#define HYPORT_ERROR_SOCKET_UNIX_ENOPROTOOPT ENOPROTOOPT
151
#define HYPORT_ERROR_SOCKET_UNIX_ENOTCONN ENOTCONN
152
#define HYPORT_ERROR_SOCKET_UNIX_EPROTONOSUPPORT EPROTONOSUPPORT
153
#define HYPORT_ERROR_SOCKET_UNIX_HOSTNOTFOUND HOST_NOT_FOUND
154
#define HYPORT_ERROR_SOCKET_UNIX_ENOBUFS ENOBUFS
155
#define HYPORT_ERROR_SOCKET_UNIX_NODATA NO_DATA
156
#define HYPORT_ERROR_SOCKET_UNIX_NORECOVERY NO_RECOVERY
157
#define HYPORT_ERROR_SOCKET_UNIX_ENOTSOCK ENOTSOCK
158
#define HYPORT_ERROR_SOCKET_UNIX_TRYAGAIN TRY_AGAIN
159
#define HYPORT_ERROR_SOCKET_UNIX_EOPNOTSUP EOPNOTSUPP
160
#define HYPORT_ERROR_SOCKET_UNIX_ETIMEDOUT ETIMEDOUT
161
#define HYPORT_ERROR_SOCKET_UNIX_CONNREFUSED ECONNREFUSED
162
#define HYPORT_ERROR_SOCKET_UNIX_EINPROGRESS EINPROGRESS
163
#define HYPORT_ERROR_SOCKET_UNIX_ENETUNREACH ENETUNREACH
164
#define HYPORT_ERROR_SOCKET_UNIX_EACCES EACCES
165
/* platform constants */
166
#define HYSOCK_MAXCONN OSSOMAXCONN;
167
#define HYSOCK_BADSOCKET OS_BADSOCKET /* Provide bad socket constant */
168
/* defines added for IPv6 */
169
#define OS_AF_INET4 AF_INET
170
#define OS_AF_UNSPEC AF_UNSPEC
171
#define OS_PF_UNSPEC PF_UNSPEC
172
#define OS_PF_INET4 PF_INET
173
#define OS_INET4_ADDRESS_LENGTH INET_ADDRSTRLEN
174
#if defined(NI_MAXHOST)
175
#define OSNIMAXHOST NI_MAXHOST
176
#define OSNIMAXSERV NI_MAXSERV
178
#define OSNIMAXHOST 1025
179
#define OSNIMAXSERV 32
182
#if defined(AF_INET6)
183
#define OS_AF_INET6 AF_INET6
184
#define OS_PF_INET6 PF_INET6
185
#define OS_INET6_ADDRESS_LENGTH INET6_ADDRSTRLEN
187
#define OS_AF_INET6 -1
188
#define OS_PF_INET6 -1
189
#define OS_INET6_ADDRESS_LENGTH 46
191
/*platform structs */
192
typedef struct hysocket_struct
197
/* The sockets are now structs */
198
#if defined(NO_LVALUE_CASTING)
199
#define SOCKET_CAST(x) ((struct hysocket_struct*) x)->sock
201
#define SOCKET_CAST(x) ((struct hysocket_struct*) x)->sock
203
typedef struct hysockaddr_struct
205
#if defined(IPv6_FUNCTION_SUPPORT)
206
OSSOCKADDR_STORAGE addr;
211
typedef struct hyhostent_struct
215
typedef struct hyfdset_struct
219
typedef struct hytimeval_struct
223
typedef struct hylinger_struct
227
typedef struct hyipmreq_struct
231
#define GET_HOST_BUFFER_SIZE 512
232
/* The gethostBuffer is allocated bufferSize + EXTRA_SPACE, while gethostby*_r is only aware of bufferSize
233
* because there seems to be a bug on Linux 386 where gethostbyname_r writes past the end of the
234
* buffer. This bug has not been observed on other platforms, but EXTRA_SPACE is added anyway as a precaution.*/
235
#define EXTRA_SPACE 128
236
/*size is 16 because an IP string is "xxx.xxx.xxx.xxx" + 1 null character */
238
/** new structure for IPv6 mulitcast requests */
239
typedef struct hyipv6_mreq_struct
241
#if defined(IPv6_FUNCTION_SUPPORT)
246
} hyipv6_mreq_struct;
247
/* structure for returning either and IPV4 or IPV6 ip address */
248
typedef struct hyipAddress_struct
252
#if defined(IPv6_FUNCTION_SUPPORT)
253
U_8 bytes[sizeof (struct in6_addr)];
255
U_8 bytes[sizeof (struct in_addr)];
257
struct in_addr inAddr;
258
#if defined(IPv6_FUNCTION_SUPPORT)
259
struct in6_addr in6Addr;
264
} hyipAddress_struct;
265
/* structure for returning network interface information */
266
typedef struct hyNetworkInterface_struct
270
U_32 numberAddresses;
272
struct hyipAddress_struct *addresses;
273
} hyNetworkInterface_struct;
274
/* array of network interface structures */
275
typedef struct hyNetworkInterfaceArray_struct
278
struct hyNetworkInterface_struct *elements;
279
} hyNetworkInterfaceArray_struct;
280
/** new structure for IPv6 addrinfo will either point to a hostent or
281
an addr info depending on the IPv6 support for this OS */
282
typedef struct hyaddrinfo_struct