Ruby
2.1.3p242(2014-09-19revision47630)
Main Page
Modules
Data Structures
Files
File List
Globals
ext
socket
rubysocket.h
Go to the documentation of this file.
1
#ifndef RUBY_SOCKET_H
2
#define RUBY_SOCKET_H 1
3
4
#include "
ruby/ruby.h
"
5
#include "
ruby/io.h
"
6
#include "
ruby/thread.h
"
7
#include "
ruby/util.h
"
8
#include "
internal.h
"
9
#include <stdio.h>
10
#include <sys/types.h>
11
#include <sys/stat.h>
12
13
#ifdef HAVE_UNISTD_H
14
# include <
unistd.h
>
15
#endif
16
17
#ifdef HAVE_SYS_UIO_H
18
# include <sys/uio.h>
19
#endif
20
21
#ifdef HAVE_XTI_H
22
# include <xti.h>
23
#endif
24
25
#ifdef _WIN32
26
# if defined(_MSC_VER)
27
# undef HAVE_TYPE_STRUCT_SOCKADDR_DL
28
# endif
29
#else
30
# if defined(__BEOS__) && !defined(__HAIKU__) && !defined(BONE)
31
# include <net/socket.h>
32
# else
33
# include <sys/socket.h>
34
# endif
35
# include <netinet/in.h>
36
# ifdef HAVE_NETINET_IN_SYSTM_H
37
# include <netinet/in_systm.h>
38
# endif
39
# ifdef HAVE_NETINET_TCP_H
40
# include <netinet/tcp.h>
41
# endif
42
# ifdef HAVE_NETINET_UDP_H
43
# include <netinet/udp.h>
44
# endif
45
# ifdef HAVE_ARPA_INET_H
46
# include <arpa/inet.h>
47
# endif
48
# include <netdb.h>
49
#endif
50
51
#ifdef HAVE_NETPACKET_PACKET_H
52
# include <netpacket/packet.h>
53
#endif
54
#ifdef HAVE_NET_ETHERNET_H
55
# include <net/ethernet.h>
56
#endif
57
58
#include <errno.h>
59
60
#ifdef HAVE_SYS_UN_H
61
# include <sys/un.h>
62
#endif
63
64
#if defined(HAVE_FCNTL)
65
# ifdef HAVE_SYS_SELECT_H
66
# include <sys/select.h>
67
# endif
68
# ifdef HAVE_SYS_TYPES_H
69
# include <sys/types.h>
70
# endif
71
# ifdef HAVE_SYS_TIME_H
72
# include <sys/time.h>
73
# endif
74
# ifdef HAVE_FCNTL_H
75
# include <fcntl.h>
76
# endif
77
#endif
78
79
#ifdef HAVE_IFADDRS_H
80
# include <ifaddrs.h>
81
#endif
82
#ifdef HAVE_SYS_IOCTL_H
83
# include <sys/ioctl.h>
84
#endif
85
#ifdef HAVE_SYS_SOCKIO_H
86
# include <sys/sockio.h>
87
#endif
88
#ifdef HAVE_NET_IF_H
89
# include <net/if.h>
90
#endif
91
92
#ifdef HAVE_SYS_PARAM_H
93
# include <sys/param.h>
94
#endif
95
#ifdef HAVE_SYS_UCRED_H
96
# include <sys/ucred.h>
97
#endif
98
#ifdef HAVE_UCRED_H
99
# include <ucred.h>
100
#endif
101
#ifdef HAVE_NET_IF_DL_H
102
# include <net/if_dl.h>
103
#endif
104
105
#ifndef HAVE_TYPE_SOCKLEN_T
106
typedef
int
socklen_t
;
107
#endif
108
109
#ifdef NEED_IF_INDEXTONAME_DECL
110
char
*if_indextoname(
unsigned
int
,
char
*);
111
#endif
112
#ifdef NEED_IF_NAMETOINDEX_DECL
113
unsigned
int
if_nametoindex(
const
char
*);
114
#endif
115
116
#define SOCKLEN_MAX \
117
(0 < (socklen_t)-1 ? \
118
~(socklen_t)0 : \
119
(((((socklen_t)1) << (sizeof(socklen_t) * CHAR_BIT - 2)) - 1) * 2 + 1))
120
121
#ifndef RSTRING_SOCKLEN
122
# define RSTRING_SOCKLEN (socklen_t)RSTRING_LENINT
123
#endif
124
125
#ifndef EWOULDBLOCK
126
# define EWOULDBLOCK EAGAIN
127
#endif
128
129
/*
130
* workaround for NetBSD, OpenBSD and etc.
131
* The problem is since 4.4BSD-Lite.
132
* FreeBSD fix the problem at FreeBSD 2.2.0.
133
* NetBSD fix the problem at NetBSD 3.0 by kern/29624.
134
* OpenBSD fix the problem at OpenBSD 3.8.
135
*/
136
#define pseudo_AF_FTIP pseudo_AF_RTIP
137
138
#ifndef HAVE_GETADDRINFO
139
# include "
addrinfo.h
"
140
#endif
141
142
#include "
sockport.h
"
143
144
#ifndef NI_MAXHOST
145
# define NI_MAXHOST 1025
146
#endif
147
#ifndef NI_MAXSERV
148
# define NI_MAXSERV 32
149
#endif
150
151
#ifdef AF_INET6
152
# define IS_IP_FAMILY(af) ((af) == AF_INET || (af) == AF_INET6)
153
#else
154
# define IS_IP_FAMILY(af) ((af) == AF_INET)
155
#endif
156
157
#ifndef IN6_IS_ADDR_UNIQUE_LOCAL
158
# define IN6_IS_ADDR_UNIQUE_LOCAL(a) (((a)->s6_addr[0] == 0xfc) || ((a)->s6_addr[0] == 0xfd))
159
#endif
160
161
#ifndef HAVE_TYPE_STRUCT_SOCKADDR_STORAGE
162
/*
163
* RFC 2553: protocol-independent placeholder for socket addresses
164
*/
165
# define _SS_MAXSIZE 128
166
# define _SS_ALIGNSIZE (sizeof(double))
167
# define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(unsigned char) * 2)
168
# define _SS_PAD2SIZE (_SS_MAXSIZE - sizeof(unsigned char) * 2 - \
169
_SS_PAD1SIZE - _SS_ALIGNSIZE)
170
171
struct
sockaddr_storage
{
172
# ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
173
unsigned
char
ss_len;
/* address length */
174
unsigned
char
ss_family
;
/* address family */
175
# else
176
unsigned
short
ss_family
;
177
# endif
178
char
__ss_pad1
[
_SS_PAD1SIZE
];
179
double
__ss_align
;
/* force desired structure storage alignment */
180
char
__ss_pad2
[
_SS_PAD2SIZE
];
181
};
182
#endif
183
184
typedef
union
{
185
struct
sockaddr addr;
186
struct
sockaddr_in in;
187
#ifdef AF_INET6
188
struct
sockaddr_in6 in6;
189
#endif
190
#ifdef HAVE_TYPE_STRUCT_SOCKADDR_UN
191
struct
sockaddr_un un;
192
#endif
193
#ifdef HAVE_TYPE_STRUCT_SOCKADDR_DL
194
struct
sockaddr_dl dl;
/* AF_LINK */
195
#endif
196
struct
sockaddr_storage
storage;
197
char
place_holder[2048];
/* sockaddr_storage is not enough for Unix domain sockets on SunOS and Darwin. */
198
}
union_sockaddr
;
199
200
#ifdef __APPLE__
201
/*
202
* CMSG_ macros are broken on 64bit darwin, because __DARWIN_ALIGN
203
* aligns up to __darwin_size_t which is 64bit, but CMSG_DATA is
204
* 32bit-aligned.
205
*/
206
# undef __DARWIN_ALIGNBYTES
207
# define __DARWIN_ALIGNBYTES (sizeof(unsigned int) - 1)
208
#endif
209
210
#if defined(_AIX)
211
# ifndef CMSG_SPACE
212
# define CMSG_SPACE(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + _CMSG_ALIGN(len))
213
# endif
214
# ifndef CMSG_LEN
215
# define CMSG_LEN(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
216
# endif
217
#endif
218
219
#ifdef __BEOS__
220
# undef close
221
# define close closesocket
222
#endif
223
224
#define INET_CLIENT 0
225
#define INET_SERVER 1
226
#define INET_SOCKS 2
227
228
extern
int
rsock_do_not_reverse_lookup
;
229
#define FMODE_NOREVLOOKUP 0x100
230
231
extern
VALUE
rb_cBasicSocket
;
232
extern
VALUE
rb_cIPSocket
;
233
extern
VALUE
rb_cTCPSocket
;
234
extern
VALUE
rb_cTCPServer
;
235
extern
VALUE
rb_cUDPSocket
;
236
#ifdef HAVE_SYS_UN_H
237
extern
VALUE
rb_cUNIXSocket;
238
extern
VALUE
rb_cUNIXServer;
239
#endif
240
extern
VALUE
rb_cSocket
;
241
extern
VALUE
rb_cAddrinfo
;
242
extern
VALUE
rb_cSockOpt
;
243
244
extern
VALUE
rb_eSocket
;
245
246
#ifdef SOCKS
247
extern
VALUE
rb_cSOCKSSocket;
248
# ifdef SOCKS5
249
# include <socks.h>
250
# else
251
void
SOCKSinit();
252
int
Rconnect();
253
# endif
254
#endif
255
256
#include "
constdefs.h
"
257
258
#define BLOCKING_REGION(func, arg) (long)rb_thread_blocking_region((func), (arg), RUBY_UBF_IO, 0)
259
#define BLOCKING_REGION_FD(func, arg) (long)rb_thread_io_blocking_region((func), (arg), (arg)->fd)
260
261
#define SockAddrStringValue(v) rsock_sockaddr_string_value(&(v))
262
#define SockAddrStringValuePtr(v) rsock_sockaddr_string_value_ptr(&(v))
263
#define SockAddrStringValueWithAddrinfo(v, rai_ret) rsock_sockaddr_string_value_with_addrinfo(&(v), &(rai_ret))
264
VALUE
rsock_sockaddr_string_value
(
volatile
VALUE
*);
265
char
*
rsock_sockaddr_string_value_ptr
(
volatile
VALUE
*);
266
VALUE
rsock_sockaddr_string_value_with_addrinfo
(
volatile
VALUE
*v,
VALUE
*ai_ret);
267
268
VALUE
rb_check_sockaddr_string_type
(
VALUE
);
269
270
NORETURN
(
void
rsock_raise_socket_error
(
const
char
*,
int
));
271
272
int
rsock_family_arg
(
VALUE
domain);
273
int
rsock_socktype_arg
(
VALUE
type
);
274
int
rsock_level_arg
(
int
family,
VALUE
level);
275
int
rsock_optname_arg
(
int
family,
int
level,
VALUE
optname);
276
int
rsock_cmsg_type_arg
(
int
family,
int
level,
VALUE
type
);
277
int
rsock_shutdown_how_arg
(
VALUE
how);
278
279
int
rsock_getfamily
(
int
sockfd);
280
281
struct
rb_addrinfo
{
282
struct
addrinfo
*
ai
;
283
int
allocated_by_malloc
;
284
};
285
int
rb_getaddrinfo
(
const
char
*node,
const
char
*service,
const
struct
addrinfo
*hints,
struct
rb_addrinfo
**res);
286
void
rb_freeaddrinfo
(
struct
rb_addrinfo
*ai);
287
VALUE
rsock_freeaddrinfo
(
VALUE
arg
);
288
int
rb_getnameinfo
(
const
struct
sockaddr *sa,
socklen_t
salen,
char
*host,
size_t
hostlen,
char
*serv,
size_t
servlen,
int
flags);
289
struct
rb_addrinfo
*
rsock_addrinfo
(
VALUE
host,
VALUE
port,
int
socktype,
int
flags);
290
struct
rb_addrinfo
*
rsock_getaddrinfo
(
VALUE
host,
VALUE
port,
struct
addrinfo
*hints,
int
socktype_hack);
291
VALUE
rsock_fd_socket_addrinfo
(
int
fd,
struct
sockaddr *addr,
socklen_t
len);
292
VALUE
rsock_io_socket_addrinfo
(
VALUE
io,
struct
sockaddr *addr,
socklen_t
len);
293
294
VALUE
rsock_addrinfo_new
(
struct
sockaddr *addr,
socklen_t
len,
int
family,
int
socktype,
int
protocol,
VALUE
canonname,
VALUE
inspectname);
295
VALUE
rsock_addrinfo_inspect_sockaddr
(
VALUE
rai);
296
297
VALUE
rsock_make_ipaddr
(
struct
sockaddr *addr,
socklen_t
addrlen);
298
VALUE
rsock_ipaddr
(
struct
sockaddr *sockaddr,
socklen_t
sockaddrlen,
int
norevlookup);
299
VALUE
rsock_make_hostent
(
VALUE
host,
struct
rb_addrinfo
*addr,
VALUE
(*ipaddr)(
struct
sockaddr *,
socklen_t
));
300
VALUE
rsock_inspect_sockaddr
(
struct
sockaddr *addr,
socklen_t
socklen,
VALUE
ret);
301
socklen_t
rsock_sockaddr_len
(
struct
sockaddr *addr);
302
VALUE
rsock_sockaddr_obj
(
struct
sockaddr *addr,
socklen_t
len);
303
304
int
rsock_revlookup_flag
(
VALUE
revlookup,
int
*norevlookup);
305
306
#ifdef HAVE_SYS_UN_H
307
VALUE
rsock_unixpath_str(
struct
sockaddr_un *sockaddr,
socklen_t
len);
308
VALUE
rsock_unixaddr(
struct
sockaddr_un *sockaddr,
socklen_t
len);
309
socklen_t
rsock_unix_sockaddr_len(
VALUE
path);
310
#endif
311
312
int
rsock_socket
(
int
domain,
int
type
,
int
proto
);
313
VALUE
rsock_init_sock
(
VALUE
sock,
int
fd);
314
VALUE
rsock_sock_s_socketpair
(
int
argc
,
VALUE
*
argv
,
VALUE
klass);
315
VALUE
rsock_init_inetsock
(
VALUE
sock,
VALUE
remote_host,
VALUE
remote_serv,
VALUE
local_host,
VALUE
local_serv,
int
type
);
316
VALUE
rsock_init_unixsock
(
VALUE
sock,
VALUE
path,
int
server);
317
318
struct
rsock_send_arg
{
319
int
fd
,
flags
;
320
VALUE
mesg
;
321
struct
sockaddr *
to
;
322
socklen_t
tolen
;
323
};
324
325
VALUE
rsock_sendto_blocking
(
void
*data);
326
VALUE
rsock_send_blocking
(
void
*data);
327
VALUE
rsock_bsock_send
(
int
argc
,
VALUE
*
argv
,
VALUE
sock);
328
329
enum
sock_recv_type
{
330
RECV_RECV
,
/* BasicSocket#recv(no from) */
331
RECV_IP
,
/* IPSocket#recvfrom */
332
RECV_UNIX
,
/* UNIXSocket#recvfrom */
333
RECV_SOCKET
/* Socket#recvfrom */
334
};
335
336
VALUE
rsock_s_recvfrom_nonblock
(
VALUE
sock,
int
argc
,
VALUE
*
argv
,
enum
sock_recv_type
from);
337
VALUE
rsock_s_recvfrom
(
VALUE
sock,
int
argc
,
VALUE
*
argv
,
enum
sock_recv_type
from);
338
339
int
rsock_connect
(
int
fd,
const
struct
sockaddr *sockaddr,
int
len,
int
socks);
340
341
VALUE
rsock_s_accept
(
VALUE
klass,
int
fd,
struct
sockaddr *sockaddr,
socklen_t
*len);
342
VALUE
rsock_s_accept_nonblock
(
VALUE
klass,
rb_io_t
*fptr,
struct
sockaddr *sockaddr,
socklen_t
*len);
343
VALUE
rsock_sock_listen
(
VALUE
sock,
VALUE
log);
344
345
VALUE
rsock_sockopt_new
(
int
family,
int
level,
int
optname,
VALUE
data);
346
347
#if defined(HAVE_SENDMSG)
348
VALUE
rsock_bsock_sendmsg
(
int
argc
,
VALUE
*
argv
,
VALUE
sock);
349
VALUE
rsock_bsock_sendmsg_nonblock
(
int
argc
,
VALUE
*
argv
,
VALUE
sock);
350
#else
351
#define rsock_bsock_sendmsg rb_f_notimplement
352
#define rsock_bsock_sendmsg_nonblock rb_f_notimplement
353
#endif
354
355
#if defined(HAVE_RECVMSG)
356
VALUE
rsock_bsock_recvmsg
(
int
argc
,
VALUE
*
argv
,
VALUE
sock);
357
VALUE
rsock_bsock_recvmsg_nonblock
(
int
argc
,
VALUE
*
argv
,
VALUE
sock);
358
ssize_t rsock_recvmsg(
int
socket,
struct
msghdr
*message,
int
flags);
359
#else
360
#define rsock_bsock_recvmsg rb_f_notimplement
361
#define rsock_bsock_recvmsg_nonblock rb_f_notimplement
362
#endif
363
364
#ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
365
void
rsock_discard_cmsg_resource(
struct
msghdr
*mh,
int
msg_peek_p);
366
#endif
367
368
void
rsock_init_basicsocket
(
void
);
369
void
rsock_init_ipsocket
(
void
);
370
void
rsock_init_tcpsocket
(
void
);
371
void
rsock_init_tcpserver
(
void
);
372
void
rsock_init_sockssocket
(
void
);
373
void
rsock_init_udpsocket
(
void
);
374
void
rsock_init_unixsocket
(
void
);
375
void
rsock_init_unixserver
(
void
);
376
void
rsock_init_socket_constants
(
void
);
377
void
rsock_init_ancdata
(
void
);
378
void
rsock_init_addrinfo
(
void
);
379
void
rsock_init_sockopt
(
void
);
380
void
rsock_init_sockifaddr
(
void
);
381
void
rsock_init_socket_init
(
void
);
382
383
NORETURN
(
void
rsock_syserr_fail_host_port
(
int
err
,
const
char
*,
VALUE
,
VALUE
));
384
NORETURN
(
void
rsock_syserr_fail_path
(
int
err
,
const
char
*,
VALUE
));
385
NORETURN
(
void
rsock_syserr_fail_sockaddr
(
int
err
,
const
char
*mesg,
struct
sockaddr *addr,
socklen_t
len));
386
NORETURN
(
void
rsock_syserr_fail_raddrinfo
(
int
err
,
const
char
*mesg,
VALUE
rai));
387
NORETURN
(
void
rsock_syserr_fail_raddrinfo_or_sockaddr
(
int
err
,
const
char
*mesg,
VALUE
addr,
VALUE
rai));
388
389
NORETURN
(
void
rsock_sys_fail_host_port
(
const
char
*,
VALUE
,
VALUE
));
390
NORETURN
(
void
rsock_sys_fail_path
(
const
char
*,
VALUE
));
391
NORETURN
(
void
rsock_sys_fail_sockaddr
(
const
char
*,
struct
sockaddr *addr,
socklen_t
len));
392
NORETURN
(
void
rsock_sys_fail_raddrinfo
(
const
char
*,
VALUE
rai));
393
NORETURN
(
void
rsock_sys_fail_raddrinfo_or_sockaddr
(
const
char
*,
VALUE
addr,
VALUE
rai));
394
395
#endif
396
Generated on Mon Sep 22 2014 11:05:29 for Ruby by
1.8.2