/[cvs]/eggdrop1.9/src/scriptnet.c
ViewVC logotype

Contents of /eggdrop1.9/src/scriptnet.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Sun Nov 3 23:16:09 2002 UTC (16 years, 10 months ago) by stdarg
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +0 -0 lines
File MIME type: text/x-chdr
FILE REMOVED
* Moved md5 code to lib/eggdrop so I can use it for password hashing
* Passwords are stored as standard hex encoding of md5(salt + password)
* Started moving script interface functions to modules/script

1 /*
2 * scriptnet.c -- script support for network functions
3 *
4 */
5
6 #include <stdio.h> /* NULL */
7 #include <eggdrop/eggdrop.h>
8
9 /* From main.c */
10 extern egg_timeval_t egg_timeval_now;
11
12 typedef struct script_net_info {
13 struct script_net_info *prev, *next;
14 int idx;
15 char *peer_ip, *peer_host, *peer_ident;
16 int peer_port;
17 script_callback_t *on_connect, *on_eof, *on_newclient;
18 script_callback_t *on_read, *on_written;
19 script_callback_t *on_delete;
20
21 /* Some stats about this connection. */
22 egg_timeval_t connected_at;
23 egg_timeval_t last_input_at;
24 egg_timeval_t last_output_at;
25
26 unsigned int bytes_in, bytes_out;
27 unsigned int bytes_left;
28 } script_net_info_t;
29
30 /* We keep a list of script-controlled sockbufs. */
31 static script_net_info_t *script_net_info_head = NULL;
32
33 static void script_net_info_add(script_net_info_t *info);
34 static void script_net_info_remove(script_net_info_t *info);
35
36 /* Script interface prototypes. */
37 int script_net_takeover(int idx);
38 static int script_net_open(const char *host, int port, int timeout);
39 static int script_net_close(int idx);
40 static int script_net_write(int idx, const char *text, int len);
41 static int script_net_linemode(int idx, int onoff);
42 static int script_net_handler(int idx, const char *event, script_callback_t *handler);
43 static int script_net_info(script_var_t *retval, int idx, char *what);
44 static int script_net_throttle_set(void *client_data, int idx, int speed);
45
46 static script_command_t script_cmds[] = {
47 {"", "net_takeover", script_net_takeover, NULL, 1, "i", "idx", SCRIPT_INTEGER, 0},
48 {"", "net_open", script_net_open, NULL, 2, "sii", "host port ?timeout?", SCRIPT_INTEGER, SCRIPT_VAR_ARGS},
49 {"", "net_close", script_net_close, NULL, 1, "i", "idx", SCRIPT_INTEGER, 0},
50 {"", "net_write", script_net_write, NULL, 2, "isi", "idx text ?len?", SCRIPT_INTEGER, SCRIPT_VAR_ARGS},
51 {"", "net_handler", script_net_handler, NULL, 2, "isc", "idx event callback", SCRIPT_INTEGER, SCRIPT_VAR_ARGS},
52 {"", "net_linemode", script_net_linemode, NULL, 2, "ii", "idx on-off", SCRIPT_INTEGER, 0},
53 {"", "net_info", script_net_info, NULL, 2, "is", "idx what", 0, SCRIPT_PASS_RETVAL},
54 {"", "net_throttle", throttle_on, NULL, 1, "i", "idx", SCRIPT_INTEGER, 0},
55 {"", "net_throttle_in", script_net_throttle_set, (void *)0, 2, "ii", "idx speed", SCRIPT_INTEGER, SCRIPT_PASS_CDATA},
56 {"", "net_throttle_out", script_net_throttle_set, (void *)1, 2, "ii", "idx speed", SCRIPT_INTEGER, SCRIPT_PASS_CDATA},
57 {0}
58 };
59
60
61 /* Sockbuf handler prototypes. */
62 static int on_connect(void *client_data, int idx, const char *peer_ip, int peer_port);
63 static int on_eof(void *client_data, int idx, int err, const char *errmsg);
64 static int on_newclient(void *client_data, int idx, int newidx, const char *peer_ip, int peer_port);
65 static int on_read(void *client_data, int idx, char *data, int len);
66 static int on_written(void *client_data, int idx, int len, int remaining);
67 static int on_delete(void *client_data, int idx);
68
69 static sockbuf_handler_t sock_handler = {
70 "script",
71 on_connect, on_eof, on_newclient,
72 on_read, on_written,
73 on_delete
74 };
75
76 /* Create script commands. */
77 void script_net_init()
78 {
79 script_create_commands(script_cmds);
80 }
81
82 /* Put an idx under script control. */
83 int script_net_takeover(int idx)
84 {
85 script_net_info_t *info;
86
87 info = calloc(1, sizeof(*info));
88 info->idx = idx;
89 script_net_info_add(info);
90 sockbuf_set_handler(info->idx, &sock_handler, info);
91 return(0);
92 }
93
94 /* Look up info based on the idx. */
95 static script_net_info_t *script_net_info_lookup(int idx)
96 {
97 script_net_info_t *info;
98
99 for (info = script_net_info_head; info; info = info->next) {
100 if (info->idx == idx) break;
101 }
102 return(info);
103 }
104
105 static void script_net_info_add(script_net_info_t *info)
106 {
107 info->next = script_net_info_head;
108 info->prev = NULL;
109 if (script_net_info_head) script_net_info_head->prev = info;
110 script_net_info_head = info;
111 }
112
113 static void script_net_info_remove(script_net_info_t *info)
114 {
115 if (info->prev) info->prev->next = info->next;
116 else script_net_info_head = info->next;
117
118 if (info->next) info->next->prev = info->prev;
119 }
120
121 /* Script interface functions. */
122 static int script_net_open(const char *host, int port, int timeout)
123 {
124 int idx;
125
126 idx = egg_connect(host, port, timeout);
127 script_net_takeover(idx);
128 return(idx);
129 }
130
131 static int script_net_close(int idx)
132 {
133 return sockbuf_delete(idx);
134 }
135
136 static int script_net_write(int idx, const char *text, int len)
137 {
138 if (len <= 0) len = strlen(text);
139
140 return sockbuf_write(idx, text, len);
141 }
142
143 static int script_net_linemode(int idx, int onoff)
144 {
145 if (onoff) linemode_on(idx);
146 else linemode_off(idx);
147 return(0);
148 }
149
150 static int script_net_handler(int idx, const char *event, script_callback_t *callback)
151 {
152 script_net_info_t *info;
153 script_callback_t **replace = NULL;
154 const char *newsyntax;
155
156 /* See if it's already script-controlled. */
157 info = script_net_info_lookup(idx);
158 if (!info) {
159 /* Nope, take it over. */
160 script_net_takeover(idx);
161 info = script_net_info_lookup(idx);
162 if (!info) return(-1);
163 }
164
165 if (!strcasecmp(event, "connect")) {
166 replace = &info->on_connect;
167 newsyntax = "isi";
168 }
169 else if (!strcasecmp(event, "eof")) {
170 replace = &info->on_eof;
171 newsyntax = "iis";
172 }
173 else if (!strcasecmp(event, "newclient")) {
174 replace = &info->on_newclient;
175 newsyntax = "iisi";
176 }
177 else if (!strcasecmp(event, "read")) {
178 replace = &info->on_read;
179 newsyntax = "ibi";
180 }
181 else if (!strcasecmp(event, "written")) {
182 replace = &info->on_written;
183 newsyntax = "iii";
184 }
185 else if (!strcasecmp(event, "delete")) {
186 replace = &info->on_delete;
187 newsyntax = "i";
188 }
189 else {
190 /* Invalid event type. */
191 return(-2);
192 }
193
194 if (*replace) (*replace)->del(*replace);
195 if (callback) callback->syntax = strdup(newsyntax);
196 *replace = callback;
197 return(0);
198 }
199
200 static int script_net_info(script_var_t *retval, int idx, char *what)
201 {
202 script_net_info_t *info = script_net_info_lookup(idx);
203
204 retval->type = SCRIPT_INTEGER;
205 if (!info) return(0);
206
207 if (!strcasecmp(what, "bytesleft")) retval->value = (void *)info->bytes_left;
208 return(0);
209 }
210
211 static int script_net_throttle_set(void *client_data, int idx, int speed)
212 {
213 return throttle_set(idx, (int) client_data, speed);
214 }
215
216 /* Sockbuf handler functions. */
217 static int on_connect(void *client_data, int idx, const char *peer_ip, int peer_port)
218 {
219 script_net_info_t *info = client_data;
220
221 str_redup(&info->peer_ip, peer_ip);
222 info->peer_port = peer_port;
223 memcpy(&info->connected_at, &egg_timeval_now, sizeof(egg_timeval_now));
224
225 if (info->on_connect) info->on_connect->callback(info->on_connect, idx, peer_ip, peer_port);
226 return(0);
227 }
228
229 static int on_eof(void *client_data, int idx, int err, const char *errmsg)
230 {
231 script_net_info_t *info = client_data;
232
233 if (info->on_eof) info->on_eof->callback(info->on_eof, idx, err, errmsg);
234 sockbuf_delete(idx);
235 return(0);
236 }
237
238 static int on_newclient(void *client_data, int idx, int newidx, const char *peer_ip, int peer_port)
239 {
240 script_net_info_t *info = client_data;
241
242 if (info->on_newclient) info->on_newclient->callback(info->on_newclient, idx, newidx, peer_ip, peer_port);
243 return(0);
244 }
245
246 static int on_read(void *client_data, int idx, char *data, int len)
247 {
248 script_net_info_t *info = client_data;
249 byte_array_t bytes;
250
251 info->bytes_in += len;
252 memcpy(&info->last_input_at, &egg_timeval_now, sizeof(egg_timeval_now));
253
254 if (info->on_read) {
255 bytes.bytes = data;
256 bytes.len = len;
257 bytes.do_free = 0;
258 info->on_read->callback(info->on_read, idx, &bytes, len);
259 }
260 return(0);
261 }
262
263 static int on_written(void *client_data, int idx, int len, int remaining)
264 {
265 script_net_info_t *info = client_data;
266
267 memcpy(&info->last_output_at, &egg_timeval_now, sizeof(egg_timeval_now));
268
269 info->bytes_out += len;
270 info->bytes_left = remaining;
271
272 if (info->on_written) info->on_written->callback(info->on_written, idx, len, remaining);
273 return(0);
274 }
275
276 static int on_delete(void *client_data, int idx)
277 {
278 script_net_info_t *info = client_data;
279
280 if (info->on_connect) info->on_connect->del(info->on_connect);
281 if (info->on_eof) info->on_eof->del(info->on_eof);
282 if (info->on_newclient) info->on_newclient->del(info->on_newclient);
283 if (info->on_read) info->on_read->del(info->on_read);
284 if (info->on_written) info->on_written->del(info->on_written);
285
286 if (info->on_delete) {
287 info->on_delete->callback(info->on_delete, idx);
288 info->on_delete->del(info->on_delete);
289 }
290 script_net_info_remove(info);
291 free(info);
292 return(0);
293 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23