summaryrefslogtreecommitdiff
path: root/ext/socket/option.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-01-19 01:11:01 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-01-19 01:11:01 +0000
commit6282b3b5c307396472e4ece5a227b83a5f716bdc (patch)
treea192e3d2dbbd0c3fc58d5505bad0d33863a96fc0 /ext/socket/option.c
parentd1d7f12c89cf683947740727fa37b6ce7fe512d4 (diff)
socket/option.c: socket option variations
* ext/socket/option.c (NUM2SOCKOPT, sockopt_value): extract to wrap socket option variations. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44647 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket/option.c')
-rw-r--r--ext/socket/option.c34
1 files changed, 14 insertions, 20 deletions
diff --git a/ext/socket/option.c b/ext/socket/option.c
index 0b01065..dbed7d9 100644
--- a/ext/socket/option.c
+++ b/ext/socket/option.c
@@ -2,6 +2,16 @@
VALUE rb_cSockOpt;
+#if defined(__NetBSD__) || defined(__OpenBSD__)
+typedef unsigned char rb_sockopt_t;
+# define NUM2SOCKOPT(value) NUM2CHR(rb_to_int(value))
+# define sockopt_value(obj) sockopt_byte(obj)
+# else
+typedef int rb_sockopt_t;
+# define NUM2SOCKOPT(value) NUM2INT(rb_to_int(value))
+# define sockopt_value(obj) sockopt_int(obj)
+#endif
+
static VALUE
constant_to_sym(int constant, ID (*intern_const)(int))
{
@@ -353,11 +363,7 @@ static VALUE
sockopt_s_ipv4_multicast_loop(VALUE klass, VALUE value)
{
#if defined(IPPROTO_IP) && defined(IP_MULTICAST_LOOP)
-# if defined(__NetBSD__) || defined(__OpenBSD__)
- unsigned char i = NUM2CHR(rb_to_int(value));
-# else
- int i = NUM2INT(rb_to_int(value));
-# endif
+ rb_sockopt_t i = NUM2SOCKOPT(value);
return rsock_sockopt_new(AF_INET, IPPROTO_IP, IP_MULTICAST_LOOP,
rb_str_new((char*)&i, sizeof(i)));
#else
@@ -383,11 +389,7 @@ sockopt_ipv4_multicast_loop(VALUE self)
#if defined(IPPROTO_IP) && defined(IP_MULTICAST_LOOP)
if (family == AF_INET && level == IPPROTO_IP && optname == IP_MULTICAST_LOOP) {
-# if defined(__NetBSD__) || defined(__OpenBSD__)
- return sockopt_byte(self);
-# else
- return sockopt_int(self);
-# endif
+ return sockopt_value(self);
}
#endif
rb_raise(rb_eTypeError, "ipv4_multicast_loop socket option expected");
@@ -416,11 +418,7 @@ static VALUE
sockopt_s_ipv4_multicast_ttl(VALUE klass, VALUE value)
{
#if defined(IPPROTO_IP) && defined(IP_MULTICAST_TTL)
-# if defined(__NetBSD__) || defined(__OpenBSD__)
- unsigned char i = NUM2CHR(rb_to_int(value));
-# else
- int i = NUM2INT(rb_to_int(value));
-# endif
+ rb_sockopt_t i = NUM2SOCKOPT(value);
return rsock_sockopt_new(AF_INET, IPPROTO_IP, IP_MULTICAST_TTL,
rb_str_new((char*)&i, sizeof(i)));
#else
@@ -446,11 +444,7 @@ sockopt_ipv4_multicast_ttl(VALUE self)
#if defined(IPPROTO_IP) && defined(IP_MULTICAST_TTL)
if (family == AF_INET && level == IPPROTO_IP && optname == IP_MULTICAST_TTL) {
-# if defined(__NetBSD__) || defined(__OpenBSD__)
- return sockopt_byte(self);
-# else
- return sockopt_int(self);
-# endif
+ return sockopt_value(self);
}
#endif
rb_raise(rb_eTypeError, "ipv4_multicast_ttl socket option expected");