Ruby 1.9.2p136(2010-12-25revision30365)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# ext/socket/constants.c

Go to the documentation of this file.

    00001 /************************************************
    00002
    00003   constants.c -
    00004
    00005   created at: Thu Mar 31 12:21:29 JST 1994
    00006
    00007   Copyright (C) 1993-2007 Yukihiro Matsumoto
    00008
    00009 ************************************************/
    00010
    00011 #include "rubysocket.h"
    00012
    00013 static VALUE rb_mSockConst;
    00014
    00015 #include "constdefs.c"
    00016
    00017 static int
    00018 constant_arg(VALUE arg, int (*str_to_int)(const char*, long, int*), const char *errmsg)
    00019 {
    00020     VALUE tmp;
    00021     char *ptr;
    00022     int ret;
    00023
    00024     if (SYMBOL_P(arg)) {
    00025         arg = rb_sym_to_s(arg);
    00026         goto str;
    00027     }
    00028     else if (!NIL_P(tmp = rb_check_string_type(arg))) {
    00029         arg = tmp;
    00030       str:
    00031         rb_check_safe_obj(arg);
    00032         ptr = RSTRING_PTR(arg);
    00033         if (str_to_int(ptr, RSTRING_LEN(arg), &ret) == -1)
    00034             rb_raise(rb_eSocket, "%s: %s", errmsg, ptr);
    00035     }
    00036     else {
    00037         ret = NUM2INT(arg);
    00038     }
    00039     return ret;
    00040 }
    00041
    00042 int
    00043 rsock_family_arg(VALUE domain)
    00044 {
    00045     /* convert AF_INET, etc. */
    00046     return constant_arg(domain, rsock_family_to_int, "unknown socket domain");
    00047 }
    00048
    00049 int
    00050 rsock_socktype_arg(VALUE type)
    00051 {
    00052     /* convert SOCK_STREAM, etc. */
    00053     return constant_arg(type, rsock_socktype_to_int, "unknown socket type");
    00054 }
    00055
    00056 int
    00057 rsock_level_arg(int family, VALUE level)
    00058 {
    00059     /* convert SOL_SOCKET, IPPROTO_TCP, etc. */
    00060     if (IS_IP_FAMILY(family)) {
    00061         return constant_arg(level, rsock_ip_level_to_int, "unknown protocol level");
    00062     }
    00063     else {
    00064         return constant_arg(level, rsock_unknown_level_to_int, "unknown protocol level");
    00065     }
    00066 }
    00067
    00068 int
    00069 rsock_optname_arg(int family, int level, VALUE optname)
    00070 {
    00071     if (IS_IP_FAMILY(family)) {
    00072         switch (level) {
    00073           case SOL_SOCKET:
    00074             return constant_arg(optname, rsock_so_optname_to_int, "unknown socket level option name");
    00075           case IPPROTO_IP:
    00076             return constant_arg(optname, rsock_ip_optname_to_int, "unknown IP level option name");
    00077 #ifdef IPPROTO_IPV6
    00078           case IPPROTO_IPV6:
    00079             return constant_arg(optname, rsock_ipv6_optname_to_int, "unknown IPv6 level option name");
    00080 #endif
    00081           case IPPROTO_TCP:
    00082             return constant_arg(optname, rsock_tcp_optname_to_int, "unknown TCP level option name");
    00083           case IPPROTO_UDP:
    00084             return constant_arg(optname, rsock_udp_optname_to_int, "unknown UDP level option name");
    00085           default:
    00086             return NUM2INT(optname);
    00087         }
    00088     }
    00089     else {
    00090         switch (level) {
    00091           case SOL_SOCKET:
    00092             return constant_arg(optname, rsock_so_optname_to_int, "unknown socket level option name");
    00093           default:
    00094             return NUM2INT(optname);
    00095         }
    00096     }
    00097 }
    00098
    00099 int
    00100 rsock_cmsg_type_arg(int family, int level, VALUE type)
    00101 {
    00102     if (IS_IP_FAMILY(family)) {
    00103         switch (level) {
    00104           case SOL_SOCKET:
    00105             return constant_arg(type, rsock_scm_optname_to_int, "unknown UNIX control message");
    00106           case IPPROTO_IP:
    00107             return constant_arg(type, rsock_ip_optname_to_int, "unknown IP control message");
    00108 #ifdef INET6
    00109           case IPPROTO_IPV6:
    00110             return constant_arg(type, rsock_ipv6_optname_to_int, "unknown IPv6 control message");
    00111 #endif
    00112           case IPPROTO_TCP:
    00113             return constant_arg(type, rsock_tcp_optname_to_int, "unknown TCP control message");
    00114           case IPPROTO_UDP:
    00115             return constant_arg(type, rsock_udp_optname_to_int, "unknown UDP control message");
    00116           default:
    00117             return NUM2INT(type);
    00118         }
    00119     }
    00120     else {
    00121         switch (level) {
    00122           case SOL_SOCKET:
    00123             return constant_arg(type, rsock_scm_optname_to_int, "unknown UNIX control message");
    00124           default:
    00125             return NUM2INT(type);
    00126         }
    00127     }
    00128 }
    00129
    00130 int
    00131 rsock_shutdown_how_arg(VALUE how)
    00132 {
    00133     /* convert SHUT_RD, SHUT_WR, SHUT_RDWR. */
    00134     return constant_arg(how, rsock_shutdown_how_to_int, "unknown shutdown argument");
    00135 }
    00136
    00137 /*
    00138  * Socket::Constants module
    00139  */
    00140 void
    00141 rsock_init_socket_constants(void)
    00142 {
    00143     /* constants */
    00144     init_constants();
    00145 }
    00146