2009-01-16 23:11:27 -05:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
constants.c -
|
|
|
|
|
|
|
|
created at: Thu Mar 31 12:21:29 JST 1994
|
|
|
|
|
|
|
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "rubysocket.h"
|
|
|
|
|
2009-02-16 11:55:33 -05:00
|
|
|
static VALUE rb_mSockConst;
|
|
|
|
|
2009-01-17 05:19:03 -05:00
|
|
|
#include "constdefs.c"
|
2009-01-16 23:11:27 -05:00
|
|
|
|
|
|
|
static int
|
2010-04-28 04:14:13 -04:00
|
|
|
constant_arg(VALUE arg, int (*str_to_int)(const char*, long, int*), const char *errmsg)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
VALUE tmp;
|
|
|
|
char *ptr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (SYMBOL_P(arg)) {
|
|
|
|
arg = rb_sym_to_s(arg);
|
|
|
|
goto str;
|
|
|
|
}
|
|
|
|
else if (!NIL_P(tmp = rb_check_string_type(arg))) {
|
|
|
|
arg = tmp;
|
|
|
|
str:
|
|
|
|
rb_check_safe_obj(arg);
|
|
|
|
ptr = RSTRING_PTR(arg);
|
|
|
|
if (str_to_int(ptr, RSTRING_LEN(arg), &ret) == -1)
|
|
|
|
rb_raise(rb_eSocket, "%s: %s", errmsg, ptr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = NUM2INT(arg);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_family_arg(VALUE domain)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
/* convert AF_INET, etc. */
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(domain, rsock_family_to_int, "unknown socket domain");
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_socktype_arg(VALUE type)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
/* convert SOCK_STREAM, etc. */
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(type, rsock_socktype_to_int, "unknown socket type");
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_level_arg(int family, VALUE level)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
/* convert SOL_SOCKET, IPPROTO_TCP, etc. */
|
2009-02-09 09:23:53 -05:00
|
|
|
if (IS_IP_FAMILY(family)) {
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(level, rsock_ip_level_to_int, "unknown protocol level");
|
2009-02-09 09:23:53 -05:00
|
|
|
}
|
|
|
|
else {
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(level, rsock_unknown_level_to_int, "unknown protocol level");
|
2009-02-09 09:23:53 -05:00
|
|
|
}
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_optname_arg(int family, int level, VALUE optname)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
2009-02-09 09:23:53 -05:00
|
|
|
if (IS_IP_FAMILY(family)) {
|
|
|
|
switch (level) {
|
|
|
|
case SOL_SOCKET:
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(optname, rsock_so_optname_to_int, "unknown socket level option name");
|
2009-02-09 09:23:53 -05:00
|
|
|
case IPPROTO_IP:
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(optname, rsock_ip_optname_to_int, "unknown IP level option name");
|
2009-01-16 23:11:27 -05:00
|
|
|
#ifdef IPPROTO_IPV6
|
2009-02-09 09:23:53 -05:00
|
|
|
case IPPROTO_IPV6:
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(optname, rsock_ipv6_optname_to_int, "unknown IPv6 level option name");
|
2009-01-16 23:11:27 -05:00
|
|
|
#endif
|
2009-02-09 09:23:53 -05:00
|
|
|
case IPPROTO_TCP:
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(optname, rsock_tcp_optname_to_int, "unknown TCP level option name");
|
2009-02-09 09:23:53 -05:00
|
|
|
case IPPROTO_UDP:
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(optname, rsock_udp_optname_to_int, "unknown UDP level option name");
|
2009-02-09 09:23:53 -05:00
|
|
|
default:
|
|
|
|
return NUM2INT(optname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch (level) {
|
|
|
|
case SOL_SOCKET:
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(optname, rsock_so_optname_to_int, "unknown socket level option name");
|
2009-02-09 09:23:53 -05:00
|
|
|
default:
|
|
|
|
return NUM2INT(optname);
|
|
|
|
}
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-01 21:51:02 -05:00
|
|
|
int
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_cmsg_type_arg(int family, int level, VALUE type)
|
2009-02-01 21:51:02 -05:00
|
|
|
{
|
2009-02-09 09:23:53 -05:00
|
|
|
if (IS_IP_FAMILY(family)) {
|
|
|
|
switch (level) {
|
|
|
|
case SOL_SOCKET:
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(type, rsock_scm_optname_to_int, "unknown UNIX control message");
|
2009-02-09 09:23:53 -05:00
|
|
|
case IPPROTO_IP:
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(type, rsock_ip_optname_to_int, "unknown IP control message");
|
2013-04-08 11:28:43 -04:00
|
|
|
#ifdef IPPROTO_IPV6
|
2009-02-09 09:23:53 -05:00
|
|
|
case IPPROTO_IPV6:
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(type, rsock_ipv6_optname_to_int, "unknown IPv6 control message");
|
2009-02-02 00:23:23 -05:00
|
|
|
#endif
|
2009-02-09 09:23:53 -05:00
|
|
|
case IPPROTO_TCP:
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(type, rsock_tcp_optname_to_int, "unknown TCP control message");
|
2009-02-09 09:23:53 -05:00
|
|
|
case IPPROTO_UDP:
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(type, rsock_udp_optname_to_int, "unknown UDP control message");
|
2009-02-09 09:23:53 -05:00
|
|
|
default:
|
|
|
|
return NUM2INT(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch (level) {
|
|
|
|
case SOL_SOCKET:
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(type, rsock_scm_optname_to_int, "unknown UNIX control message");
|
2009-02-09 09:23:53 -05:00
|
|
|
default:
|
|
|
|
return NUM2INT(type);
|
|
|
|
}
|
2009-02-01 21:51:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-09 09:23:53 -05:00
|
|
|
int
|
2009-03-01 01:30:41 -05:00
|
|
|
rsock_shutdown_how_arg(VALUE how)
|
2009-02-09 09:23:53 -05:00
|
|
|
{
|
|
|
|
/* convert SHUT_RD, SHUT_WR, SHUT_RDWR. */
|
2009-03-01 01:30:41 -05:00
|
|
|
return constant_arg(how, rsock_shutdown_how_to_int, "unknown shutdown argument");
|
2009-02-09 09:23:53 -05:00
|
|
|
}
|
|
|
|
|
2009-01-16 23:11:27 -05:00
|
|
|
/*
|
|
|
|
* Socket::Constants module
|
|
|
|
*/
|
|
|
|
void
|
2010-03-22 12:15:21 -04:00
|
|
|
rsock_init_socket_constants(void)
|
2009-01-16 23:11:27 -05:00
|
|
|
{
|
|
|
|
/* constants */
|
2009-02-16 11:55:33 -05:00
|
|
|
init_constants();
|
2009-01-16 23:11:27 -05:00
|
|
|
}
|