2009-01-16 23:11:27 -05:00
|
|
|
#ifndef RUBY_SOCKET_H
|
|
|
|
#define RUBY_SOCKET_H 1
|
|
|
|
|
|
|
|
#include "ruby/ruby.h"
|
|
|
|
#include "ruby/io.h"
|
2012-07-10 09:57:11 -04:00
|
|
|
#include "ruby/thread.h"
|
2009-01-16 23:11:27 -05:00
|
|
|
#include "ruby/util.h"
|
2011-06-21 08:31:17 -04:00
|
|
|
#include "internal.h"
|
2009-01-16 23:11:27 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
2009-02-18 18:47:21 -05:00
|
|
|
#include <sys/stat.h>
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_UIO_H
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_XTI_H
|
|
|
|
#include <xti.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#if defined(__BEOS__) && !defined(__HAIKU__) && !defined(BONE)
|
|
|
|
# include <net/socket.h>
|
|
|
|
#else
|
|
|
|
# include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#ifdef HAVE_NETINET_IN_SYSTM_H
|
|
|
|
# include <netinet/in_systm.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_TCP_H
|
|
|
|
# include <netinet/tcp.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_UDP_H
|
|
|
|
# include <netinet/udp.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
# include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
#include <sys/un.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_FCNTL)
|
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
2009-01-20 06:06:22 -05:00
|
|
|
|
2009-01-20 06:19:09 -05:00
|
|
|
#ifdef HAVE_IFADDRS_H
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SOCKIO_H
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NET_IF_H
|
|
|
|
#include <net/if.h>
|
|
|
|
#endif
|
|
|
|
|
2009-02-08 10:37:55 -05:00
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_UCRED_H
|
|
|
|
#include <sys/ucred.h>
|
|
|
|
#endif
|
2009-02-12 07:43:04 -05:00
|
|
|
#ifdef HAVE_UCRED_H
|
|
|
|
#include <ucred.h>
|
|
|
|
#endif
|
2009-02-08 10:37:55 -05:00
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
#ifndef EWOULDBLOCK
|
|
|
|
#define EWOULDBLOCK EAGAIN
|
|
|
|
#endif
|
2009-01-20 06:06:22 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* workaround for NetBSD, OpenBSD and etc.
|
|
|
|
* The problem is since 4.4BSD-Lite.
|
|
|
|
* FreeBSD fix the problem at FreeBSD 2.2.0.
|
|
|
|
* NetBSD fix the problem at NetBSD 3.0 by kern/29624.
|
|
|
|
* OpenBSD fix the problem at OpenBSD 3.8.
|
|
|
|
*/
|
|
|
|
#define pseudo_AF_FTIP pseudo_AF_RTIP
|
|
|
|
|
2009-12-31 04:19:05 -05:00
|
|
|
#ifndef HAVE_GETADDRINFO
|
2009-12-30 15:56:41 -05:00
|
|
|
#include "addrinfo.h"
|
2009-12-31 04:19:05 -05:00
|
|
|
#endif
|
2009-01-16 23:11:27 -05:00
|
|
|
#include "sockport.h"
|
|
|
|
|
|
|
|
#ifndef NI_MAXHOST
|
|
|
|
# define NI_MAXHOST 1025
|
|
|
|
#endif
|
|
|
|
#ifndef NI_MAXSERV
|
|
|
|
# define NI_MAXSERV 32
|
|
|
|
#endif
|
|
|
|
|
2009-01-17 13:14:34 -05:00
|
|
|
#ifdef AF_INET6
|
|
|
|
# define IS_IP_FAMILY(af) ((af) == AF_INET || (af) == AF_INET6)
|
|
|
|
#else
|
|
|
|
# define IS_IP_FAMILY(af) ((af) == AF_INET)
|
|
|
|
#endif
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
#ifndef HAVE_SOCKADDR_STORAGE
|
|
|
|
/*
|
|
|
|
* RFC 2553: protocol-independent placeholder for socket addresses
|
|
|
|
*/
|
|
|
|
#define _SS_MAXSIZE 128
|
|
|
|
#define _SS_ALIGNSIZE (sizeof(double))
|
|
|
|
#define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(unsigned char) * 2)
|
|
|
|
#define _SS_PAD2SIZE (_SS_MAXSIZE - sizeof(unsigned char) * 2 - \
|
|
|
|
_SS_PAD1SIZE - _SS_ALIGNSIZE)
|
|
|
|
|
|
|
|
struct sockaddr_storage {
|
|
|
|
#ifdef HAVE_SA_LEN
|
|
|
|
unsigned char ss_len; /* address length */
|
|
|
|
unsigned char ss_family; /* address family */
|
|
|
|
#else
|
|
|
|
unsigned short ss_family;
|
|
|
|
#endif
|
|
|
|
char __ss_pad1[_SS_PAD1SIZE];
|
|
|
|
double __ss_align; /* force desired structure storage alignment */
|
|
|
|
char __ss_pad2[_SS_PAD2SIZE];
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2012-07-09 11:52:49 -04:00
|
|
|
#ifdef __APPLE__
|
2010-09-13 06:56:52 -04:00
|
|
|
/*
|
|
|
|
* CMSG_ macros are broken on 64bit darwin, because __DARWIN_ALIGN
|
|
|
|
* aligns up to __darwin_size_t which is 64bit, but CMSG_DATA is
|
|
|
|
* 32bit-aligned.
|
|
|
|
*/
|
|
|
|
#undef __DARWIN_ALIGNBYTES
|
|
|
|
#define __DARWIN_ALIGNBYTES (sizeof(unsigned int) - 1)
|
|
|
|
#endif
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
#if defined(_AIX)
|
|
|
|
#ifndef CMSG_SPACE
|
|
|
|
# define CMSG_SPACE(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + _CMSG_ALIGN(len))
|
|
|
|
#endif
|
|
|
|
#ifndef CMSG_LEN
|
|
|
|
# define CMSG_LEN(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __BEOS__
|
|
|
|
#undef close
|
|
|
|
#define close closesocket
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define INET_CLIENT 0
|
|
|
|
#define INET_SERVER 1
|
|
|
|
#define INET_SOCKS 2
|
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
extern int rsock_do_not_reverse_lookup;
|
2009-01-16 23:11:27 -05:00
|
|
|
#define FMODE_NOREVLOOKUP 0x100
|
|
|
|
|
|
|
|
extern VALUE rb_cBasicSocket;
|
|
|
|
extern VALUE rb_cIPSocket;
|
|
|
|
extern VALUE rb_cTCPSocket;
|
|
|
|
extern VALUE rb_cTCPServer;
|
|
|
|
extern VALUE rb_cUDPSocket;
|
2009-01-18 21:22:57 -05:00
|
|
|
#ifdef HAVE_SYS_UN_H
|
2009-01-16 23:11:27 -05:00
|
|
|
extern VALUE rb_cUNIXSocket;
|
|
|
|
extern VALUE rb_cUNIXServer;
|
|
|
|
#endif
|
|
|
|
extern VALUE rb_cSocket;
|
2009-02-05 06:01:43 -05:00
|
|
|
extern VALUE rb_cAddrinfo;
|
2009-02-01 20:53:02 -05:00
|
|
|
extern VALUE rb_cSockOpt;
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
extern VALUE rb_eSocket;
|
|
|
|
|
|
|
|
#ifdef SOCKS
|
|
|
|
extern VALUE rb_cSOCKSSocket;
|
|
|
|
#ifdef SOCKS5
|
|
|
|
#include <socks.h>
|
|
|
|
#else
|
|
|
|
void SOCKSinit();
|
|
|
|
int Rconnect();
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2009-01-17 05:19:03 -05:00
|
|
|
#include "constdefs.h"
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
#define BLOCKING_REGION(func, arg) (long)rb_thread_blocking_region((func), (arg), RUBY_UBF_IO, 0)
|
2011-02-12 00:44:23 -05:00
|
|
|
#define BLOCKING_REGION_FD(func, arg) (long)rb_thread_io_blocking_region((func), (arg), (arg)->fd)
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
#define SockAddrStringValue(v) rsock_sockaddr_string_value(&(v))
|
|
|
|
#define SockAddrStringValuePtr(v) rsock_sockaddr_string_value_ptr(&(v))
|
|
|
|
VALUE rsock_sockaddr_string_value(volatile VALUE *);
|
|
|
|
char *rsock_sockaddr_string_value_ptr(volatile VALUE *);
|
2009-01-17 11:39:03 -05:00
|
|
|
VALUE rb_check_sockaddr_string_type(VALUE);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
NORETURN(void rsock_raise_socket_error(const char *, int));
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
int rsock_family_arg(VALUE domain);
|
|
|
|
int rsock_socktype_arg(VALUE type);
|
|
|
|
int rsock_level_arg(int family, VALUE level);
|
|
|
|
int rsock_optname_arg(int family, int level, VALUE optname);
|
|
|
|
int rsock_cmsg_type_arg(int family, int level, VALUE type);
|
|
|
|
int rsock_shutdown_how_arg(VALUE how);
|
2009-02-09 08:56:43 -05:00
|
|
|
|
2009-03-01 06:58:13 -05:00
|
|
|
int rsock_getfamily(int sockfd);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
int rb_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
|
|
|
|
int rb_getnameinfo(const struct sockaddr *sa, socklen_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags);
|
2009-03-01 01:48:22 -05:00
|
|
|
struct addrinfo *rsock_addrinfo(VALUE host, VALUE port, int socktype, int flags);
|
|
|
|
struct addrinfo *rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack);
|
2009-03-01 01:30:41 -05:00
|
|
|
VALUE rsock_fd_socket_addrinfo(int fd, struct sockaddr *addr, socklen_t len);
|
|
|
|
VALUE rsock_io_socket_addrinfo(VALUE io, struct sockaddr *addr, socklen_t len);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
VALUE rsock_addrinfo_new(struct sockaddr *addr, socklen_t len, int family, int socktype, int protocol, VALUE canonname, VALUE inspectname);
|
2009-01-20 06:19:09 -05:00
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
VALUE rsock_make_ipaddr(struct sockaddr *addr);
|
|
|
|
VALUE rsock_ipaddr(struct sockaddr *sockaddr, int norevlookup);
|
|
|
|
VALUE rsock_make_hostent(VALUE host, struct addrinfo *addr, VALUE (*ipaddr)(struct sockaddr *, size_t));
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2010-03-21 11:51:07 -04:00
|
|
|
int rsock_revlookup_flag(VALUE revlookup, int *norevlookup);
|
|
|
|
|
2009-01-18 21:22:57 -05:00
|
|
|
#ifdef HAVE_SYS_UN_H
|
2012-04-25 23:34:12 -04:00
|
|
|
VALUE rsock_unixpath_str(struct sockaddr_un *sockaddr, socklen_t len);
|
2009-03-01 01:30:41 -05:00
|
|
|
VALUE rsock_unixaddr(struct sockaddr_un *sockaddr, socklen_t len);
|
2009-01-18 21:22:57 -05:00
|
|
|
#endif
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2009-03-01 01:48:22 -05:00
|
|
|
int rsock_socket(int domain, int type, int proto);
|
2009-03-01 01:30:41 -05:00
|
|
|
VALUE rsock_init_sock(VALUE sock, int fd);
|
2009-03-01 01:48:22 -05:00
|
|
|
VALUE rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass);
|
2009-03-01 01:30:41 -05:00
|
|
|
VALUE rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type);
|
|
|
|
VALUE rsock_init_unixsock(VALUE sock, VALUE path, int server);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
struct rsock_send_arg {
|
2009-01-16 23:11:27 -05:00
|
|
|
int fd, flags;
|
|
|
|
VALUE mesg;
|
|
|
|
struct sockaddr *to;
|
|
|
|
socklen_t tolen;
|
|
|
|
};
|
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
VALUE rsock_sendto_blocking(void *data);
|
|
|
|
VALUE rsock_send_blocking(void *data);
|
|
|
|
VALUE rsock_bsock_send(int argc, VALUE *argv, VALUE sock);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
enum sock_recv_type {
|
|
|
|
RECV_RECV, /* BasicSocket#recv(no from) */
|
|
|
|
RECV_IP, /* IPSocket#recvfrom */
|
|
|
|
RECV_UNIX, /* UNIXSocket#recvfrom */
|
|
|
|
RECV_SOCKET /* Socket#recvfrom */
|
|
|
|
};
|
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
VALUE rsock_s_recvfrom_nonblock(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from);
|
|
|
|
VALUE rsock_s_recvfrom(VALUE sock, int argc, VALUE *argv, enum sock_recv_type from);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2009-03-01 01:48:22 -05:00
|
|
|
int rsock_connect(int fd, const struct sockaddr *sockaddr, int len, int socks);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
VALUE rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len);
|
|
|
|
VALUE rsock_s_accept_nonblock(VALUE klass, rb_io_t *fptr, struct sockaddr *sockaddr, socklen_t *len);
|
2010-03-21 06:50:52 -04:00
|
|
|
VALUE rsock_sock_listen(VALUE sock, VALUE log);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
2009-03-01 01:30:41 -05:00
|
|
|
VALUE rsock_sockopt_new(int family, int level, int optname, VALUE data);
|
2009-02-01 20:53:02 -05:00
|
|
|
|
2010-03-21 06:50:52 -04:00
|
|
|
#if defined(HAVE_SENDMSG)
|
|
|
|
VALUE rsock_bsock_sendmsg(int argc, VALUE *argv, VALUE sock);
|
|
|
|
VALUE rsock_bsock_sendmsg_nonblock(int argc, VALUE *argv, VALUE sock);
|
2010-04-28 03:16:30 -04:00
|
|
|
#else
|
|
|
|
#define rsock_bsock_sendmsg rb_f_notimplement
|
|
|
|
#define rsock_bsock_sendmsg_nonblock rb_f_notimplement
|
2010-03-21 06:50:52 -04:00
|
|
|
#endif
|
|
|
|
#if defined(HAVE_RECVMSG)
|
|
|
|
VALUE rsock_bsock_recvmsg(int argc, VALUE *argv, VALUE sock);
|
|
|
|
VALUE rsock_bsock_recvmsg_nonblock(int argc, VALUE *argv, VALUE sock);
|
2011-11-06 01:51:43 -04:00
|
|
|
ssize_t rsock_recvmsg(int socket, struct msghdr *message, int flags);
|
2010-04-28 03:16:30 -04:00
|
|
|
#else
|
|
|
|
#define rsock_bsock_recvmsg rb_f_notimplement
|
|
|
|
#define rsock_bsock_recvmsg_nonblock rb_f_notimplement
|
2010-03-21 06:50:52 -04:00
|
|
|
#endif
|
|
|
|
|
2009-02-25 09:03:42 -05:00
|
|
|
#ifdef HAVE_ST_MSG_CONTROL
|
2011-07-23 04:13:37 -04:00
|
|
|
void rsock_discard_cmsg_resource(struct msghdr *mh, int msg_peek_p);
|
2009-02-25 09:03:42 -05:00
|
|
|
#endif
|
|
|
|
|
2010-03-22 12:15:21 -04:00
|
|
|
void rsock_init_basicsocket(void);
|
|
|
|
void rsock_init_ipsocket(void);
|
|
|
|
void rsock_init_tcpsocket(void);
|
|
|
|
void rsock_init_tcpserver(void);
|
|
|
|
void rsock_init_sockssocket(void);
|
|
|
|
void rsock_init_udpsocket(void);
|
|
|
|
void rsock_init_unixsocket(void);
|
|
|
|
void rsock_init_unixserver(void);
|
|
|
|
void rsock_init_socket_constants(void);
|
|
|
|
void rsock_init_ancdata(void);
|
|
|
|
void rsock_init_addrinfo(void);
|
|
|
|
void rsock_init_sockopt(void);
|
|
|
|
void rsock_init_socket_init(void);
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
#endif
|