00001 #ifndef RUBY_SOCKET_H
00002 #define RUBY_SOCKET_H 1
00003
00004 #include "ruby/ruby.h"
00005 #include "ruby/io.h"
00006 #include "ruby/thread.h"
00007 #include "ruby/util.h"
00008 #include "internal.h"
00009 #include <stdio.h>
00010 #include <sys/types.h>
00011 #include <sys/stat.h>
00012
00013 #ifdef HAVE_UNISTD_H
00014 # include <unistd.h>
00015 #endif
00016
00017 #ifdef HAVE_SYS_UIO_H
00018 # include <sys/uio.h>
00019 #endif
00020
00021 #ifdef HAVE_XTI_H
00022 # include <xti.h>
00023 #endif
00024
00025 #ifdef _WIN32
00026 # if defined(_MSC_VER)
00027 # undef HAVE_TYPE_STRUCT_SOCKADDR_DL
00028 # endif
00029 #else
00030 # if defined(__BEOS__) && !defined(__HAIKU__) && !defined(BONE)
00031 # include <net/socket.h>
00032 # else
00033 # include <sys/socket.h>
00034 # endif
00035 # include <netinet/in.h>
00036 # ifdef HAVE_NETINET_IN_SYSTM_H
00037 # include <netinet/in_systm.h>
00038 # endif
00039 # ifdef HAVE_NETINET_TCP_H
00040 # include <netinet/tcp.h>
00041 # endif
00042 # ifdef HAVE_NETINET_UDP_H
00043 # include <netinet/udp.h>
00044 # endif
00045 # ifdef HAVE_ARPA_INET_H
00046 # include <arpa/inet.h>
00047 # endif
00048 # include <netdb.h>
00049 #endif
00050
00051 #ifdef HAVE_NETPACKET_PACKET_H
00052 # include <netpacket/packet.h>
00053 #endif
00054 #ifdef HAVE_NET_ETHERNET_H
00055 # include <net/ethernet.h>
00056 #endif
00057
00058 #include <errno.h>
00059
00060 #ifdef HAVE_SYS_UN_H
00061 # include <sys/un.h>
00062 #endif
00063
00064 #if defined(HAVE_FCNTL)
00065 # ifdef HAVE_SYS_SELECT_H
00066 # include <sys/select.h>
00067 # endif
00068 # ifdef HAVE_SYS_TYPES_H
00069 # include <sys/types.h>
00070 # endif
00071 # ifdef HAVE_SYS_TIME_H
00072 # include <sys/time.h>
00073 # endif
00074 # ifdef HAVE_FCNTL_H
00075 # include <fcntl.h>
00076 # endif
00077 #endif
00078
00079 #ifdef HAVE_IFADDRS_H
00080 # include <ifaddrs.h>
00081 #endif
00082 #ifdef HAVE_SYS_IOCTL_H
00083 # include <sys/ioctl.h>
00084 #endif
00085 #ifdef HAVE_SYS_SOCKIO_H
00086 # include <sys/sockio.h>
00087 #endif
00088 #ifdef HAVE_NET_IF_H
00089 # include <net/if.h>
00090 #endif
00091
00092 #ifdef HAVE_SYS_PARAM_H
00093 # include <sys/param.h>
00094 #endif
00095 #ifdef HAVE_SYS_UCRED_H
00096 # include <sys/ucred.h>
00097 #endif
00098 #ifdef HAVE_UCRED_H
00099 # include <ucred.h>
00100 #endif
00101 #ifdef HAVE_NET_IF_DL_H
00102 # include <net/if_dl.h>
00103 #endif
00104
00105 #ifndef HAVE_TYPE_SOCKLEN_T
00106 typedef int socklen_t;
00107 #endif
00108
00109 #ifdef NEED_IF_INDEXTONAME_DECL
00110 char *if_indextoname(unsigned int, char *);
00111 #endif
00112 #ifdef NEED_IF_NAMETOINDEX_DECL
00113 unsigned int if_nametoindex(const char *);
00114 #endif
00115
00116 #define SOCKLEN_MAX \
00117 (0 < (socklen_t)-1 ? \
00118 ~(socklen_t)0 : \
00119 (((((socklen_t)1) << (sizeof(socklen_t) * CHAR_BIT - 2)) - 1) * 2 + 1))
00120
00121 #ifndef RSTRING_SOCKLEN
00122 # define RSTRING_SOCKLEN (socklen_t)RSTRING_LENINT
00123 #endif
00124
00125 #ifndef EWOULDBLOCK
00126 # define EWOULDBLOCK EAGAIN
00127 #endif
00128
00129
00130
00131
00132
00133
00134
00135
00136 #define pseudo_AF_FTIP pseudo_AF_RTIP
00137
00138 #ifndef HAVE_GETADDRINFO
00139 # include "addrinfo.h"
00140 #endif
00141
00142 #include "sockport.h"
00143
00144 #ifndef NI_MAXHOST
00145 # define NI_MAXHOST 1025
00146 #endif
00147 #ifndef NI_MAXSERV
00148 # define NI_MAXSERV 32
00149 #endif
00150
00151 #ifdef AF_INET6
00152 # define IS_IP_FAMILY(af) ((af) == AF_INET || (af) == AF_INET6)
00153 #else
00154 # define IS_IP_FAMILY(af) ((af) == AF_INET)
00155 #endif
00156
00157 #ifndef IN6_IS_ADDR_UNIQUE_LOCAL
00158 # define IN6_IS_ADDR_UNIQUE_LOCAL(a) (((a)->s6_addr[0] == 0xfc) || ((a)->s6_addr[0] == 0xfd))
00159 #endif
00160
00161 #ifndef HAVE_TYPE_STRUCT_SOCKADDR_STORAGE
00162
00163
00164
00165 # define _SS_MAXSIZE 128
00166 # define _SS_ALIGNSIZE (sizeof(double))
00167 # define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(unsigned char) * 2)
00168 # define _SS_PAD2SIZE (_SS_MAXSIZE - sizeof(unsigned char) * 2 - \
00169 _SS_PAD1SIZE - _SS_ALIGNSIZE)
00170
00171 struct sockaddr_storage {
00172 # ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
00173 unsigned char ss_len;
00174 unsigned char ss_family;
00175 # else
00176 unsigned short ss_family;
00177 # endif
00178 char __ss_pad1[_SS_PAD1SIZE];
00179 double __ss_align;
00180 char __ss_pad2[_SS_PAD2SIZE];
00181 };
00182 #endif
00183
00184 typedef union {
00185 struct sockaddr addr;
00186 struct sockaddr_in in;
00187 #ifdef AF_INET6
00188 struct sockaddr_in6 in6;
00189 #endif
00190 #ifdef HAVE_TYPE_STRUCT_SOCKADDR_UN
00191 struct sockaddr_un un;
00192 #endif
00193 #ifdef HAVE_TYPE_STRUCT_SOCKADDR_DL
00194 struct sockaddr_dl dl;
00195 #endif
00196 struct sockaddr_storage storage;
00197 char place_holder[2048];
00198 } union_sockaddr;
00199
00200 #ifdef __APPLE__
00201
00202
00203
00204
00205
00206 # undef __DARWIN_ALIGNBYTES
00207 # define __DARWIN_ALIGNBYTES (sizeof(unsigned int) - 1)
00208 #endif
00209
00210 #if defined(_AIX)
00211 # ifndef CMSG_SPACE
00212 # define CMSG_SPACE(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + _CMSG_ALIGN(len))
00213 # endif
00214 # ifndef CMSG_LEN
00215 # define CMSG_LEN(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
00216 # endif
00217 #endif
00218
00219 #ifdef __BEOS__
00220 # undef close
00221 # define close closesocket
00222 #endif
00223
00224 #define INET_CLIENT 0
00225 #define INET_SERVER 1
00226 #define INET_SOCKS 2
00227
00228 extern int rsock_do_not_reverse_lookup;
00229 #define FMODE_NOREVLOOKUP 0x100
00230
00231 extern VALUE rb_cBasicSocket;
00232 extern VALUE rb_cIPSocket;
00233 extern VALUE rb_cTCPSocket;
00234 extern VALUE rb_cTCPServer;
00235 extern VALUE rb_cUDPSocket;
00236 #ifdef HAVE_SYS_UN_H
00237 extern VALUE rb_cUNIXSocket;
00238 extern VALUE rb_cUNIXServer;
00239 #endif
00240 extern VALUE rb_cSocket;
00241 extern VALUE rb_cAddrinfo;
00242 extern VALUE rb_cSockOpt;
00243
00244 extern VALUE rb_eSocket;
00245
00246 #ifdef SOCKS
00247 extern VALUE rb_cSOCKSSocket;
00248 # ifdef SOCKS5
00249 # include <socks.h>
00250 # else
00251 void SOCKSinit();
00252 int Rconnect();
00253 # endif
00254 #endif
00255
00256 #include "constdefs.h"
00257
00258 #define BLOCKING_REGION(func, arg) (long)rb_thread_blocking_region((func), (arg), RUBY_UBF_IO, 0)
00259 #define BLOCKING_REGION_FD(func, arg) (long)rb_thread_io_blocking_region((func), (arg), (arg)->fd)
00260
00261 #define SockAddrStringValue(v) rsock_sockaddr_string_value(&(v))
00262 #define SockAddrStringValuePtr(v) rsock_sockaddr_string_value_ptr(&(v))
00263 #define SockAddrStringValueWithAddrinfo(v, rai_ret) rsock_sockaddr_string_value_with_addrinfo(&(v), &(rai_ret))
00264 VALUE rsock_sockaddr_string_value(volatile VALUE *);
00265 char *rsock_sockaddr_string_value_ptr(volatile VALUE *);
00266 VALUE rsock_sockaddr_string_value_with_addrinfo(volatile VALUE *v, VALUE *ai_ret);
00267
00268 VALUE rb_check_sockaddr_string_type(VALUE);
00269
00270 NORETURN(void rsock_raise_socket_error(const char *, int));
00271
00272 int rsock_family_arg(VALUE domain);
00273 int rsock_socktype_arg(VALUE type);
00274 int rsock_level_arg(int family, VALUE level);
00275 int rsock_optname_arg(int family, int level, VALUE optname);
00276 int rsock_cmsg_type_arg(int family, int level, VALUE type);
00277 int rsock_shutdown_how_arg(VALUE how);
00278
00279 int rsock_getfamily(int sockfd);
00280
00281 struct rb_addrinfo {
00282 struct addrinfo *ai;
00283 int allocated_by_malloc;
00284 };
00285 int rb_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct rb_addrinfo **res);
00286 void rb_freeaddrinfo(struct rb_addrinfo *ai);
00287 VALUE rsock_freeaddrinfo(VALUE arg);
00288 int rb_getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags);
00289 struct rb_addrinfo *rsock_addrinfo(VALUE host, VALUE port, int socktype, int flags);
00290 struct rb_addrinfo *rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack);
00291 VALUE rsock_fd_socket_addrinfo(int fd, struct sockaddr *addr, socklen_t len);
00292 VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len);
00293
00294 VALUE rsock_addrinfo_new(struct sockaddr *addr, socklen_t len, int family, int socktype, int protocol, VALUE canonname, VALUE inspectname);
00295 VALUE rsock_addrinfo_inspect_sockaddr(VALUE rai);
00296
00297 VALUE rsock_make_ipaddr(struct sockaddr *addr, socklen_t addrlen);
00298 VALUE rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup);
00299 VALUE rsock_make_hostent(VALUE host, struct rb_addrinfo *addr, VALUE (*ipaddr)(struct sockaddr *, socklen_t));
00300 VALUE rsock_inspect_sockaddr(struct sockaddr *addr, socklen_t socklen, VALUE ret);
00301 socklen_t rsock_sockaddr_len(struct sockaddr *addr);
00302 VALUE rsock_sockaddr_obj(struct sockaddr *addr, socklen_t len);
00303
00304 int rsock_revlookup_flag(VALUE revlookup, int *norevlookup);
00305
00306 #ifdef HAVE_SYS_UN_H
00307 VALUE rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len);
00308 VALUE rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len);
00309 socklen_t rsock_unix_sockaddr_len(VALUE path);
00310 #endif
00311
00312 int rsock_socket(int domain, int type, int proto);
00313 VALUE rsock_init_sock(VALUE sock, int fd);
00314 VALUE rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass);
00315 VALUE rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type);
00316 VALUE rsock_init_unixsock(VALUE sock, VALUE path, int server);
00317
00318 struct rsock_send_arg {
00319 int fd, flags;
00320 VALUE mesg;
00321 struct sockaddr *to;
00322 socklen_t tolen;
00323 };
00324
00325 VALUE rsock_sendto_blocking(void *data);
00326 VALUE rsock_send_blocking(void *data);
00327 VALUE rsock_bsock_send(int argc, VALUE *argv, VALUE sock);
00328
00329 enum sock_recv_type {
00330 RECV_RECV,
00331 RECV_IP,
00332 RECV_UNIX,
00333 RECV_SOCKET
00334 };
00335
00336 VALUE rsock_s_recvfrom_nonblock(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from);
00337 VALUE rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from);
00338
00339 int rsock_connect(int fd, const struct sockaddr *sockaddr, int len, int socks);
00340
00341 VALUE rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len);
00342 VALUE rsock_s_accept_nonblock(VALUE klass, rb_io_t *fptr, struct sockaddr *sockaddr, socklen_t *len);
00343 VALUE rsock_sock_listen(VALUE sock, VALUE log);
00344
00345 VALUE rsock_sockopt_new(int family, int level, int optname, VALUE data);
00346
00347 #if defined(HAVE_SENDMSG)
00348 VALUE rsock_bsock_sendmsg(int argc, VALUE *argv, VALUE sock);
00349 VALUE rsock_bsock_sendmsg_nonblock(int argc, VALUE *argv, VALUE sock);
00350 #else
00351 #define rsock_bsock_sendmsg rb_f_notimplement
00352 #define rsock_bsock_sendmsg_nonblock rb_f_notimplement
00353 #endif
00354
00355 #if defined(HAVE_RECVMSG)
00356 VALUE rsock_bsock_recvmsg(int argc, VALUE *argv, VALUE sock);
00357 VALUE rsock_bsock_recvmsg_nonblock(int argc, VALUE *argv, VALUE sock);
00358 ssize_t rsock_recvmsg(int socket, struct msghdr *message, int flags);
00359 #else
00360 #define rsock_bsock_recvmsg rb_f_notimplement
00361 #define rsock_bsock_recvmsg_nonblock rb_f_notimplement
00362 #endif
00363
00364 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
00365 void rsock_discard_cmsg_resource(struct msghdr *mh, int msg_peek_p);
00366 #endif
00367
00368 void rsock_init_basicsocket(void);
00369 void rsock_init_ipsocket(void);
00370 void rsock_init_tcpsocket(void);
00371 void rsock_init_tcpserver(void);
00372 void rsock_init_sockssocket(void);
00373 void rsock_init_udpsocket(void);
00374 void rsock_init_unixsocket(void);
00375 void rsock_init_unixserver(void);
00376 void rsock_init_socket_constants(void);
00377 void rsock_init_ancdata(void);
00378 void rsock_init_addrinfo(void);
00379 void rsock_init_sockopt(void);
00380 void rsock_init_sockifaddr(void);
00381 void rsock_init_socket_init(void);
00382
00383 NORETURN(void rsock_syserr_fail_host_port(int err, const char *, VALUE, VALUE));
00384 NORETURN(void rsock_syserr_fail_path(int err, const char *, VALUE));
00385 NORETURN(void rsock_syserr_fail_sockaddr(int err, const char *mesg, struct sockaddr *addr, socklen_t len));
00386 NORETURN(void rsock_syserr_fail_raddrinfo(int err, const char *mesg, VALUE rai));
00387 NORETURN(void rsock_syserr_fail_raddrinfo_or_sockaddr(int err, const char *mesg, VALUE addr, VALUE rai));
00388
00389 NORETURN(void rsock_sys_fail_host_port(const char *, VALUE, VALUE));
00390 NORETURN(void rsock_sys_fail_path(const char *, VALUE));
00391 NORETURN(void rsock_sys_fail_sockaddr(const char *, struct sockaddr *addr, socklen_t len));
00392 NORETURN(void rsock_sys_fail_raddrinfo(const char *, VALUE rai));
00393 NORETURN(void rsock_sys_fail_raddrinfo_or_sockaddr(const char *, VALUE addr, VALUE rai));
00394
00395 #endif
00396