Ruby: ext/socket/constants.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

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
13static VALUE rb_mSockConst;
14
15#include "constdefs.c"
16
17static int
18constant_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
41int
42rsock_family_arg(VALUE domain)
43{
44 /* convert AF_INET, etc. */
45 return constant_arg(domain, rsock_family_to_int, "unknown socket domain");
46}
47
48int
49rsock_socktype_arg(VALUE type)
50{
51 /* convert SOCK_STREAM, etc. */
52 return constant_arg(type, rsock_socktype_to_int, "unknown socket type");
53}
54
55int
56rsock_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
67int
68rsock_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
98int
99rsock_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
129int
130rsock_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 */
139void
140rsock_init_socket_constants(void)
141{
142 /* constants */
143 init_constants();
144}
rsock_family_arg
int rsock_family_arg(VALUE domain)
Definition: constants.c:42
rsock_shutdown_how_arg
int rsock_shutdown_how_arg(VALUE how)
Definition: constants.c:130
rsock_cmsg_type_arg
int rsock_cmsg_type_arg(int family, int level, VALUE type)
Definition: constants.c:99
rsock_init_socket_constants
void rsock_init_socket_constants(void)
Definition: constants.c:140
rsock_optname_arg
int rsock_optname_arg(int family, int level, VALUE optname)
Definition: constants.c:68
rsock_level_arg
int rsock_level_arg(int family, VALUE level)
Definition: constants.c:56
rsock_socktype_arg
int rsock_socktype_arg(VALUE type)
Definition: constants.c:49
constdefs.c
rsock_family_to_int
int rsock_family_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:4468
rsock_so_optname_to_int
int rsock_so_optname_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:5381
rsock_socktype_to_int
int rsock_socktype_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:4863
rsock_ip_level_to_int
int rsock_ip_level_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:5166
rsock_scm_optname_to_int
int rsock_scm_optname_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:6652
rsock_ip_optname_to_int
int rsock_ip_optname_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:5821
rsock_unknown_level_to_int
int rsock_unknown_level_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:5143
rsock_udp_optname_to_int
int rsock_udp_optname_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:6588
rsock_tcp_optname_to_int
int rsock_tcp_optname_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:6367
rsock_shutdown_how_to_int
int rsock_shutdown_how_to_int(const char *str, long len, int *valp)
Definition: constdefs.c:6611
ptr
struct RIMemo * ptr
Definition: debug.c:88
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
RSTRING_LEN
#define RSTRING_LEN(string)
Definition: fbuffer.h:22
RSTRING_PTR
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2917
rb_check_string_type
VALUE rb_check_string_type(VALUE)
Definition: string.c:2462
rb_sym2str
VALUE rb_sym2str(VALUE)
Definition: symbol.c:927
rb_eSocket
VALUE rb_eSocket
Definition: init.c:29
NUM2INT
#define NUM2INT
Definition: int.h:44
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
rubysocket.h
IS_IP_FAMILY
#define IS_IP_FAMILY(af)
Definition: rubysocket.h:187
IPPROTO_TCP
#define IPPROTO_TCP
Definition: constdefs.h:610
IPPROTO_UDP
#define IPPROTO_UDP
Definition: constdefs.h:627
IPPROTO_IP
#define IPPROTO_IP
Definition: constdefs.h:586
NIL_P
#define NIL_P
Definition: special_consts.h:46
VALUE
unsigned long VALUE
Definition: value.h:38
SYMBOL_P
#define SYMBOL_P
Definition: value_type.h:87