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