Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

constants.c
Go to the documentation of this file.
1 /************************************************
2
3 constants.c -
4
5 created at: Thu Mar 31 12:21:29 JST 1994
6
7 Copyright (C) 1993-2007 Yukihiro Matsumoto
8
9 ************************************************/
10
11 #include "rubysocket.h"
12
13 static VALUE rb_mSockConst;
14
15 #include "constdefs.c"
16
17 static int
18 constant_arg(VALUE arg, int (*str_to_int)(const char*, long, int*), const char *errmsg)
19 {
20 VALUE tmp;
21 char *ptr;
22 int ret;
23
24 if (SYMBOL_P(arg)) {
25 arg = rb_sym2str(arg);
26 goto str;
27 }
28 else if (!NIL_P(tmp = rb_check_string_type(arg))) {
29 arg = tmp;
30 str:
31 ptr = RSTRING_PTR(arg);
32 if (str_to_int(ptr, RSTRING_LEN(arg), &ret) == -1)
33 rb_raise(rb_eSocket, "%s: %s", errmsg, ptr);
34 }
35 else {
36 ret = NUM2INT(arg);
37 }
38 return ret;
39 }
40
41 int
42 rsock_family_arg(VALUE domain)
43 {
44 /* convert AF_INET, etc. */
45 return constant_arg(domain, rsock_family_to_int, "unknown socket domain");
46 }
47
48 int
49 rsock_socktype_arg(VALUE type)
50 {
51 /* convert SOCK_STREAM, etc. */
52 return constant_arg(type, rsock_socktype_to_int, "unknown socket type");
53 }
54
55 int
56 rsock_level_arg(int family, VALUE level)
57 {
58 /* convert SOL_SOCKET, IPPROTO_TCP, etc. */
59 if (IS_IP_FAMILY(family)) {
60 return constant_arg(level, rsock_ip_level_to_int, "unknown protocol level");
61 }
62 else {
63 return constant_arg(level, rsock_unknown_level_to_int, "unknown protocol level");
64 }
65 }
66
67 int
68 rsock_optname_arg(int family, int level, VALUE optname)
69 {
70 if (IS_IP_FAMILY(family)) {
71 switch (level) {
72 case SOL_SOCKET:
73 return constant_arg(optname, rsock_so_optname_to_int, "unknown socket level option name");
74 case IPPROTO_IP:
75 return constant_arg(optname, rsock_ip_optname_to_int, "unknown IP level option name");
76 #ifdef IPPROTO_IPV6
77 case IPPROTO_IPV6:
78 return constant_arg(optname, rsock_ipv6_optname_to_int, "unknown IPv6 level option name");
79 #endif
80 case IPPROTO_TCP:
81 return constant_arg(optname, rsock_tcp_optname_to_int, "unknown TCP level option name");
82 case IPPROTO_UDP:
83 return constant_arg(optname, rsock_udp_optname_to_int, "unknown UDP level option name");
84 default:
85 return NUM2INT(optname);
86 }
87 }
88 else {
89 switch (level) {
90 case SOL_SOCKET:
91 return constant_arg(optname, rsock_so_optname_to_int, "unknown socket level option name");
92 default:
93 return NUM2INT(optname);
94 }
95 }
96 }
97
98 int
99 rsock_cmsg_type_arg(int family, int level, VALUE type)
100 {
101 if (IS_IP_FAMILY(family)) {
102 switch (level) {
103 case SOL_SOCKET:
104 return constant_arg(type, rsock_scm_optname_to_int, "unknown UNIX control message");
105 case IPPROTO_IP:
106 return constant_arg(type, rsock_ip_optname_to_int, "unknown IP control message");
107 #ifdef IPPROTO_IPV6
108 case IPPROTO_IPV6:
109 return constant_arg(type, rsock_ipv6_optname_to_int, "unknown IPv6 control message");
110 #endif
111 case IPPROTO_TCP:
112 return constant_arg(type, rsock_tcp_optname_to_int, "unknown TCP control message");
113 case IPPROTO_UDP:
114 return constant_arg(type, rsock_udp_optname_to_int, "unknown UDP control message");
115 default:
116 return NUM2INT(type);
117 }
118 }
119 else {
120 switch (level) {
121 case SOL_SOCKET:
122 return constant_arg(type, rsock_scm_optname_to_int, "unknown UNIX control message");
123 default:
124 return NUM2INT(type);
125 }
126 }
127 }
128
129 int
130 rsock_shutdown_how_arg(VALUE how)
131 {
132 /* convert SHUT_RD, SHUT_WR, SHUT_RDWR. */
133 return constant_arg(how, rsock_shutdown_how_to_int, "unknown shutdown argument");
134 }
135
136 /*
137 * Socket::Constants module
138 */
139 void
140 rsock_init_socket_constants(void)
141 {
142 /* constants */
143 init_constants();
144 }
rsock_tcp_optname_to_int
int rsock_tcp_optname_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:6367
IPPROTO_UDP
#define IPPROTO_UDP
Definition: constdefs.h:627
IPPROTO_TCP
#define IPPROTO_TCP
Definition: constdefs.h:610
RSTRING_PTR
#define RSTRING_PTR(str)
Definition: ruby.h:1009
VALUE
unsigned long VALUE
Definition: ruby.h:102
IPPROTO_IP
#define IPPROTO_IP
Definition: constdefs.h:586
rsock_socktype_to_int
int rsock_socktype_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:4863
rsock_shutdown_how_arg
int rsock_shutdown_how_arg(VALUE how)
Definition: constants.c:130
rb_check_string_type
VALUE rb_check_string_type(VALUE)
Definition: string.c:2314
ptr
struct RIMemo * ptr
Definition: debug.c:74
IS_IP_FAMILY
#define IS_IP_FAMILY(af)
Definition: rubysocket.h:162
rsock_ip_optname_to_int
int rsock_ip_optname_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:5821
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2669
rsock_family_to_int
int rsock_family_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:4468
rsock_cmsg_type_arg
int rsock_cmsg_type_arg(int family, int level, VALUE type)
Definition: constants.c:99
rsock_optname_arg
int rsock_optname_arg(int family, int level, VALUE optname)
Definition: constants.c:68
rb_eSocket
VALUE rb_eSocket
Definition: init.c:29
rsock_init_socket_constants
void rsock_init_socket_constants(void)
Definition: constants.c:140
rsock_udp_optname_to_int
int rsock_udp_optname_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:6588
rsock_socktype_arg
int rsock_socktype_arg(VALUE type)
Definition: constants.c:49
arg
VALUE arg
Definition: rb_mjit_min_header-2.7.0.h:5601
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
rsock_scm_optname_to_int
int rsock_scm_optname_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:6652
NIL_P
#define NIL_P(v)
Definition: ruby.h:482
rsock_level_arg
int rsock_level_arg(int family, VALUE level)
Definition: constants.c:56
rsock_so_optname_to_int
int rsock_so_optname_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:5381
constdefs.c
rsock_unknown_level_to_int
int rsock_unknown_level_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:5143
SYMBOL_P
#define SYMBOL_P(x)
Definition: ruby.h:413
rubysocket.h
rb_sym2str
VALUE rb_sym2str(VALUE)
Definition: symbol.c:784
rsock_ip_level_to_int
int rsock_ip_level_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:5166
NUM2INT
#define NUM2INT(x)
Definition: ruby.h:715
rsock_family_arg
int rsock_family_arg(VALUE domain)
Definition: constants.c:42
RSTRING_LEN
#define RSTRING_LEN(str)
Definition: ruby.h:1005
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:39
rsock_shutdown_how_to_int
int rsock_shutdown_how_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:6611