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