Class: Rlibiptc::Iptables

Inherits:
Object
  • Object
show all
Defined in:
ext/rlibiptc/rlibiptc.c

Instance Method Summary collapse

Constructor Details

#initialize(tablename) ⇒ Object



188
189
190
191
# File 'ext/rlibiptc/rlibiptc.c', line 188

VALUE method_init(VALUE self, VALUE tablename) {
    rb_iv_set(self, "@table", tablename);
    return self;
}

Instance Method Details

#chainsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'ext/rlibiptc/rlibiptc.c', line 50

VALUE method_chains(VALUE self) {
    open_iptables(self);

    VALUE arr;

    arr = rb_ary_new();

    const char *chain = NULL;

    for(chain = iptc_first_chain(my_handle); chain; chain = iptc_next_chain(my_handle)) {
        rb_ary_push(arr, rb_str_new2(chain));
    }

    close_iptables();

    return arr;
}

#create(chain_name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'ext/rlibiptc/rlibiptc.c', line 104

VALUE method_create(VALUE self, VALUE chain_name) {

    const char *chain = StringValueCStr(chain_name);

    open_iptables(self);

    if(iptc_create_chain(chain, my_handle) == 0) {
        close_iptables();
        rb_raise(rb_eStandardError, "%s", iptc_strerror(errno));
    }

    iptc_commit(my_handle);

    close_iptables();

    return self;
}

#delete(chain_name) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'ext/rlibiptc/rlibiptc.c', line 122

VALUE method_delete(VALUE self, VALUE chain_name) {

    const char *chain = StringValueCStr(chain_name);

    open_iptables(self);

    if(iptc_delete_chain(chain, my_handle) == 0) {
        close_iptables();
        rb_raise(rb_eStandardError, "%s", iptc_strerror(errno));
    }

    iptc_commit(my_handle);

    close_iptables();

    return self;
}

#flush(chain_name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/rlibiptc/rlibiptc.c', line 68

VALUE method_flush(VALUE self, VALUE chain_name) {

    const char *chain = StringValueCStr(chain_name);

    open_iptables(self);

    if(iptc_flush_entries(chain, my_handle) == 0) {
        close_iptables();
        rb_raise(rb_eStandardError, "%s", iptc_strerror(errno));
    }

    iptc_commit(my_handle);

    close_iptables();

    return self;
}

#rename(chain_name, new_name) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'ext/rlibiptc/rlibiptc.c', line 140

VALUE method_rename(VALUE self, VALUE chain_name, VALUE new_name) {

    const char *chain = StringValueCStr(chain_name);
    const char *name = StringValueCStr(new_name);

    open_iptables(self);

    if(iptc_rename_chain(chain, name, my_handle) == 0) {
        close_iptables();
        rb_raise(rb_eStandardError, "%s", iptc_strerror(errno));
    }

    iptc_commit(my_handle);

    close_iptables();

    return self;
}

#rules(chain_name) ⇒ Object



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
# File 'ext/rlibiptc/rlibiptc.c', line 159

VALUE method_rules(VALUE self, VALUE chain_name) {
    const char *chain = StringValueCStr(chain_name);

    const struct ipt_entry *e;

    open_iptables(self);

    VALUE arr;

    arr = rb_ary_new();

    int i = 0;

    for(e = iptc_first_rule(chain, my_handle); e; e = iptc_next_rule(e,my_handle)) {
        VALUE entry;
        entry = rb_hash_new();
        rb_hash_aset(entry, ID2SYM(rb_intern("line")), INT2NUM(i++));
        rb_hash_aset(entry, ID2SYM(rb_intern("bytes")), LONG2NUM(e->counters.bcnt));
        rb_hash_aset(entry, ID2SYM(rb_intern("packets")), LONG2NUM(e->counters.pcnt));

        rb_ary_push(arr, entry);
    }

    close_iptables();

    return arr;

}

#tableObject



46
47
48
# File 'ext/rlibiptc/rlibiptc.c', line 46

VALUE method_table(VALUE self) {
    return rb_iv_get(self, "@table");
}

#zero(chain_name) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'ext/rlibiptc/rlibiptc.c', line 86

VALUE method_zero(VALUE self, VALUE chain_name) {

    const char *chain = StringValueCStr(chain_name);

    open_iptables(self);

    if(iptc_zero_entries(chain, my_handle) == 0) {
        close_iptables();
        rb_raise(rb_eStandardError, "%s", iptc_strerror(errno));
    }

    iptc_commit(my_handle);

    close_iptables();

    return self;
}