forked from syuu1228/ruby-netif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netif.c
200 lines (170 loc) · 5.01 KB
/
netif.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "netif.h"
#include <stdio.h>
#include <unistd.h>
#include <net/if.h>
#include <ruby.h>
VALUE rb_cNetif;
VALUE netif_initialize(VALUE self, VALUE ifname)
{
rb_iv_set(self, "@ifname", ifname);
return self;
}
VALUE netif_exists(VALUE self, VALUE ifname)
{
int exist = ifexist(StringValuePtr(ifname));
return exist ? Qtrue : Qfalse;
}
VALUE netif_enable_promisc(VALUE self)
{
VALUE ifname = rb_iv_get(self, "@ifname");
setifpromisc(StringValuePtr(ifname), 1);
return Qnil;
}
VALUE netif_disable_promisc(VALUE self)
{
VALUE ifname = rb_iv_get(self, "@ifname");
setifpromisc(StringValuePtr(ifname), 0);
return Qnil;
}
VALUE netif_test_promisc(VALUE self)
{
VALUE ifname = rb_iv_get(self, "@ifname");
int enable = getifpromisc(StringValuePtr(ifname));
return enable ? Qtrue : Qfalse;
}
VALUE netif_up(VALUE self)
{
VALUE ifname = rb_iv_get(self, "@ifname");
setifup(StringValuePtr(ifname), 1);
return Qnil;
}
VALUE netif_down(VALUE self)
{
VALUE ifname = rb_iv_get(self, "@ifname");
setifup(StringValuePtr(ifname), 0);
return Qnil;
}
VALUE netif_test_up(VALUE self)
{
VALUE ifname = rb_iv_get(self, "@ifname");
int enable = getifup(StringValuePtr(ifname));
return enable ? Qtrue : Qfalse;
}
VALUE netif_set_mtu(VALUE self, VALUE mtu)
{
VALUE ifname = rb_iv_get(self, "@ifname");
setifmtu(StringValuePtr(ifname), FIX2INT(mtu));
return Qnil;
}
VALUE netif_get_mtu(VALUE self)
{
VALUE ifname = rb_iv_get(self, "@ifname");
int mtu = getifmtu(StringValuePtr(ifname));
return INT2FIX(mtu);
}
VALUE netif_get_netmask(VALUE self)
{
VALUE ifname = rb_iv_get(self, "@ifname");
char buf[128];
getifnetmask(StringValuePtr(ifname), buf, sizeof(buf));
return rb_str_new2(buf);
}
VALUE netif_get_addr(VALUE self)
{
VALUE ifname = rb_iv_get(self, "@ifname");
char buf[128];
getifaddr(StringValuePtr(ifname), buf, sizeof(buf));
return rb_str_new2(buf);
}
VALUE netif_get_broadaddr(VALUE self)
{
VALUE ifname = rb_iv_get(self, "@ifname");
char buf[128];
getifbroadaddr(StringValuePtr(ifname), buf, sizeof(buf));
return rb_str_new2(buf);
}
VALUE netif_set_addr(VALUE self, VALUE addr, VALUE mask)
{
VALUE ifname = rb_iv_get(self, "@ifname");
setifaddr(StringValuePtr(ifname), StringValuePtr(addr),
StringValuePtr(mask));
return Qnil;
}
VALUE netif_add_arp(VALUE self, VALUE host, VALUE addr)
{
VALUE ifname = rb_iv_get(self, "@ifname");
addifarp(StringValuePtr(ifname), StringValuePtr(host),
StringValuePtr(addr));
return Qnil;
}
VALUE netif_del_arp(VALUE self, VALUE host)
{
VALUE ifname = rb_iv_get(self, "@ifname");
delifarp(StringValuePtr(ifname), StringValuePtr(host));
return Qnil;
}
VALUE netif_get_arp(VALUE self, VALUE host)
{
VALUE ifname = rb_iv_get(self, "@ifname");
char buf[128];
getifarp(StringValuePtr(ifname), StringValuePtr(host), buf,
sizeof(buf));
return rb_str_new2(buf);
}
VALUE netif_set_hwaddr(VALUE self, VALUE addr)
{
VALUE ifname = rb_iv_get(self, "@ifname");
setifhwaddr(StringValuePtr(ifname), StringValuePtr(addr));
return Qnil;
}
VALUE netif_get_hwaddr(VALUE self)
{
VALUE ifname = rb_iv_get(self, "@ifname");
char buf[128];
getifhwaddr(StringValuePtr(ifname), buf, sizeof(buf));
return rb_str_new2(buf);
}
VALUE netif_set_defaultgw(VALUE self, VALUE addr)
{
VALUE ifname = rb_iv_get(self, "@ifname");
char buf[128];
if (!getifdefaultgw(StringValuePtr(ifname), buf, sizeof(buf)))
delifdefaultgw(StringValuePtr(ifname), buf);
if (addr != Qnil)
setifdefaultgw(StringValuePtr(ifname), StringValuePtr(addr));
return Qnil;
}
VALUE netif_get_defaultgw(VALUE self)
{
VALUE ifname = rb_iv_get(self, "@ifname");
char buf[128];
if (!getifdefaultgw(StringValuePtr(ifname), buf, sizeof(buf)))
return rb_str_new2(buf);
else
return Qnil;
}
void Init_netif(void)
{
rb_cNetif = rb_define_class("Netif", rb_cObject);
rb_define_method(rb_cNetif, "initialize", netif_initialize, 1);
rb_define_singleton_method(rb_cNetif, "exists?", netif_exists, 1);
rb_define_method(rb_cNetif, "enable_promisc", netif_enable_promisc, 0);
rb_define_method(rb_cNetif, "disable_promisc", netif_disable_promisc, 0);
rb_define_method(rb_cNetif, "promisc?", netif_test_promisc, 0);
rb_define_method(rb_cNetif, "up", netif_up, 0);
rb_define_method(rb_cNetif, "down", netif_down, 0);
rb_define_method(rb_cNetif, "up?", netif_test_up, 0);
rb_define_method(rb_cNetif, "mtu=", netif_set_mtu, 1);
rb_define_method(rb_cNetif, "mtu", netif_get_mtu, 0);
rb_define_method(rb_cNetif, "netmask", netif_get_netmask, 0);
rb_define_method(rb_cNetif, "addr", netif_get_addr, 0);
rb_define_method(rb_cNetif, "broadaddr", netif_get_broadaddr, 0);
rb_define_method(rb_cNetif, "set_addr", netif_set_addr, 2);
rb_define_method(rb_cNetif, "add_arp", netif_add_arp, 2);
rb_define_method(rb_cNetif, "del_arp", netif_del_arp, 1);
rb_define_method(rb_cNetif, "get_arp", netif_get_arp, 1);
rb_define_method(rb_cNetif, "hwaddr=", netif_set_hwaddr, 1);
rb_define_method(rb_cNetif, "hwaddr", netif_get_hwaddr, 0);
rb_define_method(rb_cNetif, "defaultgw=", netif_set_defaultgw, 1);
rb_define_method(rb_cNetif, "defaultgw", netif_get_defaultgw, 0);
}