Ruby  2.0.0p247(2013-06-27revision41674)
ext/socket/rubysocket.h
Go to the documentation of this file.
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 #ifndef _WIN32
00026 #if defined(__BEOS__) && !defined(__HAIKU__) && !defined(BONE)
00027 # include <net/socket.h>
00028 #else
00029 # include <sys/socket.h>
00030 #endif
00031 #include <netinet/in.h>
00032 #ifdef HAVE_NETINET_IN_SYSTM_H
00033 # include <netinet/in_systm.h>
00034 #endif
00035 #ifdef HAVE_NETINET_TCP_H
00036 # include <netinet/tcp.h>
00037 #endif
00038 #ifdef HAVE_NETINET_UDP_H
00039 # include <netinet/udp.h>
00040 #endif
00041 #ifdef HAVE_ARPA_INET_H
00042 # include <arpa/inet.h>
00043 #endif
00044 #include <netdb.h>
00045 #endif
00046 #include <errno.h>
00047 #ifdef HAVE_SYS_UN_H
00048 #include <sys/un.h>
00049 #endif
00050 
00051 #if defined(HAVE_FCNTL)
00052 #ifdef HAVE_SYS_SELECT_H
00053 #include <sys/select.h>
00054 #endif
00055 #ifdef HAVE_SYS_TYPES_H
00056 #include <sys/types.h>
00057 #endif
00058 #ifdef HAVE_SYS_TIME_H
00059 #include <sys/time.h>
00060 #endif
00061 #ifdef HAVE_FCNTL_H
00062 #include <fcntl.h>
00063 #endif
00064 #endif
00065 
00066 #ifdef HAVE_IFADDRS_H
00067 #include <ifaddrs.h>
00068 #endif
00069 #ifdef HAVE_SYS_IOCTL_H
00070 #include <sys/ioctl.h>
00071 #endif
00072 #ifdef HAVE_SYS_SOCKIO_H
00073 #include <sys/sockio.h>
00074 #endif
00075 #ifdef HAVE_NET_IF_H
00076 #include <net/if.h>
00077 #endif
00078 
00079 #ifdef HAVE_SYS_PARAM_H
00080 #include <sys/param.h>
00081 #endif
00082 #ifdef HAVE_SYS_UCRED_H
00083 #include <sys/ucred.h>
00084 #endif
00085 #ifdef HAVE_UCRED_H
00086 #include <ucred.h>
00087 #endif
00088 
00089 #ifndef EWOULDBLOCK
00090 #define EWOULDBLOCK EAGAIN
00091 #endif
00092 
00093 /*
00094  * workaround for NetBSD, OpenBSD and etc.
00095  * The problem is since 4.4BSD-Lite.
00096  * FreeBSD fix the problem at FreeBSD 2.2.0.
00097  * NetBSD fix the problem at NetBSD 3.0 by kern/29624.
00098  * OpenBSD fix the problem at OpenBSD 3.8.
00099  */
00100 #define pseudo_AF_FTIP pseudo_AF_RTIP
00101 
00102 #ifndef HAVE_GETADDRINFO
00103 #include "addrinfo.h"
00104 #endif
00105 #include "sockport.h"
00106 
00107 #ifndef NI_MAXHOST
00108 # define NI_MAXHOST 1025
00109 #endif
00110 #ifndef NI_MAXSERV
00111 # define NI_MAXSERV 32
00112 #endif
00113 
00114 #ifdef AF_INET6
00115 # define IS_IP_FAMILY(af) ((af) == AF_INET || (af) == AF_INET6)
00116 #else
00117 # define IS_IP_FAMILY(af) ((af) == AF_INET)
00118 #endif
00119 
00120 #ifndef IN6_IS_ADDR_UNIQUE_LOCAL
00121 # define IN6_IS_ADDR_UNIQUE_LOCAL(a) (((a)->s6_addr[0] == 0xfc) || ((a)->s6_addr[0] == 0xfd))
00122 #endif
00123 
00124 #ifndef HAVE_SOCKADDR_STORAGE
00125 /*
00126  * RFC 2553: protocol-independent placeholder for socket addresses
00127  */
00128 #define _SS_MAXSIZE     128
00129 #define _SS_ALIGNSIZE   (sizeof(double))
00130 #define _SS_PAD1SIZE    (_SS_ALIGNSIZE - sizeof(unsigned char) * 2)
00131 #define _SS_PAD2SIZE    (_SS_MAXSIZE - sizeof(unsigned char) * 2 - \
00132                                 _SS_PAD1SIZE - _SS_ALIGNSIZE)
00133 
00134 struct sockaddr_storage {
00135 #ifdef HAVE_SA_LEN
00136         unsigned char ss_len;           /* address length */
00137         unsigned char ss_family;        /* address family */
00138 #else
00139         unsigned short ss_family;
00140 #endif
00141         char    __ss_pad1[_SS_PAD1SIZE];
00142         double  __ss_align;     /* force desired structure storage alignment */
00143         char    __ss_pad2[_SS_PAD2SIZE];
00144 };
00145 #endif
00146 
00147 #ifdef __APPLE__
00148 /*
00149  * CMSG_ macros are broken on 64bit darwin, because __DARWIN_ALIGN
00150  * aligns up to __darwin_size_t which is 64bit, but CMSG_DATA is
00151  * 32bit-aligned.
00152  */
00153 #undef __DARWIN_ALIGNBYTES
00154 #define __DARWIN_ALIGNBYTES (sizeof(unsigned int) - 1)
00155 #endif
00156 
00157 #if defined(_AIX)
00158 #ifndef CMSG_SPACE
00159 # define CMSG_SPACE(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + _CMSG_ALIGN(len))
00160 #endif
00161 #ifndef CMSG_LEN
00162 # define CMSG_LEN(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
00163 #endif
00164 #endif
00165 
00166 #ifdef __BEOS__
00167 #undef close
00168 #define close closesocket
00169 #endif
00170 
00171 #define INET_CLIENT 0
00172 #define INET_SERVER 1
00173 #define INET_SOCKS  2
00174 
00175 extern int rsock_do_not_reverse_lookup;
00176 #define FMODE_NOREVLOOKUP 0x100
00177 
00178 extern VALUE rb_cBasicSocket;
00179 extern VALUE rb_cIPSocket;
00180 extern VALUE rb_cTCPSocket;
00181 extern VALUE rb_cTCPServer;
00182 extern VALUE rb_cUDPSocket;
00183 #ifdef HAVE_SYS_UN_H
00184 extern VALUE rb_cUNIXSocket;
00185 extern VALUE rb_cUNIXServer;
00186 #endif
00187 extern VALUE rb_cSocket;
00188 extern VALUE rb_cAddrinfo;
00189 extern VALUE rb_cSockOpt;
00190 
00191 extern VALUE rb_eSocket;
00192 
00193 #ifdef SOCKS
00194 extern VALUE rb_cSOCKSSocket;
00195 #ifdef SOCKS5
00196 #include <socks.h>
00197 #else
00198 void SOCKSinit();
00199 int Rconnect();
00200 #endif
00201 #endif
00202 
00203 #include "constdefs.h"
00204 
00205 #define BLOCKING_REGION(func, arg) (long)rb_thread_blocking_region((func), (arg), RUBY_UBF_IO, 0)
00206 #define BLOCKING_REGION_FD(func, arg) (long)rb_thread_io_blocking_region((func), (arg), (arg)->fd)
00207 
00208 #define SockAddrStringValue(v) rsock_sockaddr_string_value(&(v))
00209 #define SockAddrStringValuePtr(v) rsock_sockaddr_string_value_ptr(&(v))
00210 VALUE rsock_sockaddr_string_value(volatile VALUE *);
00211 char *rsock_sockaddr_string_value_ptr(volatile VALUE *);
00212 VALUE rb_check_sockaddr_string_type(VALUE);
00213 
00214 NORETURN(void rsock_raise_socket_error(const char *, int));
00215 
00216 int rsock_family_arg(VALUE domain);
00217 int rsock_socktype_arg(VALUE type);
00218 int rsock_level_arg(int family, VALUE level);
00219 int rsock_optname_arg(int family, int level, VALUE optname);
00220 int rsock_cmsg_type_arg(int family, int level, VALUE type);
00221 int rsock_shutdown_how_arg(VALUE how);
00222 
00223 int rsock_getfamily(int sockfd);
00224 
00225 int rb_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
00226 int rb_getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags);
00227 struct addrinfo *rsock_addrinfo(VALUE host, VALUE port, int socktype, int flags);
00228 struct addrinfo *rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack);
00229 VALUE rsock_fd_socket_addrinfo(int fd, struct sockaddr *addr, socklen_t len);
00230 VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len);
00231 
00232 VALUE rsock_addrinfo_new(struct sockaddr *addr, socklen_t len, int family, int socktype, int protocol, VALUE canonname, VALUE inspectname);
00233 
00234 VALUE rsock_make_ipaddr(struct sockaddr *addr);
00235 VALUE rsock_ipaddr(struct sockaddr *sockaddr, int norevlookup);
00236 VALUE rsock_make_hostent(VALUE host, struct addrinfo *addr, VALUE (*ipaddr)(struct sockaddr *, size_t));
00237 
00238 int rsock_revlookup_flag(VALUE revlookup, int *norevlookup);
00239 
00240 #ifdef HAVE_SYS_UN_H
00241 VALUE rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len);
00242 VALUE rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len);
00243 socklen_t rsock_unix_sockaddr_len(VALUE path);
00244 #endif
00245 
00246 int rsock_socket(int domain, int type, int proto);
00247 VALUE rsock_init_sock(VALUE sock, int fd);
00248 VALUE rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass);
00249 VALUE rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type);
00250 VALUE rsock_init_unixsock(VALUE sock, VALUE path, int server);
00251 
00252 struct rsock_send_arg {
00253     int fd, flags;
00254     VALUE mesg;
00255     struct sockaddr *to;
00256     socklen_t tolen;
00257 };
00258 
00259 VALUE rsock_sendto_blocking(void *data);
00260 VALUE rsock_send_blocking(void *data);
00261 VALUE rsock_bsock_send(int argc, VALUE *argv, VALUE sock);
00262 
00263 enum sock_recv_type {
00264     RECV_RECV,                  /* BasicSocket#recv(no from) */
00265     RECV_IP,                    /* IPSocket#recvfrom */
00266     RECV_UNIX,                  /* UNIXSocket#recvfrom */
00267     RECV_SOCKET                 /* Socket#recvfrom */
00268 };
00269 
00270 VALUE rsock_s_recvfrom_nonblock(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from);
00271 VALUE rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from);
00272 
00273 int rsock_connect(int fd, const struct sockaddr *sockaddr, int len, int socks);
00274 
00275 VALUE rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len);
00276 VALUE rsock_s_accept_nonblock(VALUE klass, rb_io_t *fptr, struct sockaddr *sockaddr, socklen_t *len);
00277 VALUE rsock_sock_listen(VALUE sock, VALUE log);
00278 
00279 VALUE rsock_sockopt_new(int family, int level, int optname, VALUE data);
00280 
00281 #if defined(HAVE_SENDMSG)
00282 VALUE rsock_bsock_sendmsg(int argc, VALUE *argv, VALUE sock);
00283 VALUE rsock_bsock_sendmsg_nonblock(int argc, VALUE *argv, VALUE sock);
00284 #else
00285 #define rsock_bsock_sendmsg rb_f_notimplement
00286 #define rsock_bsock_sendmsg_nonblock rb_f_notimplement
00287 #endif
00288 #if defined(HAVE_RECVMSG)
00289 VALUE rsock_bsock_recvmsg(int argc, VALUE *argv, VALUE sock);
00290 VALUE rsock_bsock_recvmsg_nonblock(int argc, VALUE *argv, VALUE sock);
00291 ssize_t rsock_recvmsg(int socket, struct msghdr *message, int flags);
00292 #else
00293 #define rsock_bsock_recvmsg rb_f_notimplement
00294 #define rsock_bsock_recvmsg_nonblock rb_f_notimplement
00295 #endif
00296 
00297 #ifdef HAVE_ST_MSG_CONTROL
00298 void rsock_discard_cmsg_resource(struct msghdr *mh, int msg_peek_p);
00299 #endif
00300 
00301 void rsock_init_basicsocket(void);
00302 void rsock_init_ipsocket(void);
00303 void rsock_init_tcpsocket(void);
00304 void rsock_init_tcpserver(void);
00305 void rsock_init_sockssocket(void);
00306 void rsock_init_udpsocket(void);
00307 void rsock_init_unixsocket(void);
00308 void rsock_init_unixserver(void);
00309 void rsock_init_socket_constants(void);
00310 void rsock_init_ancdata(void);
00311 void rsock_init_addrinfo(void);
00312 void rsock_init_sockopt(void);
00313 void rsock_init_socket_init(void);
00314 
00315 #endif
00316