]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * s_serv.c: Server related functions. | |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
7 | * Copyright (C) 2002-2005 ircd-ratbox development team | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
22 | * USA | |
23 | * | |
54015b5f | 24 | * $Id: s_serv.c 3446 2007-05-14 22:21:16Z jilles $ |
212380e3 AC |
25 | */ |
26 | ||
27 | #include "stdinc.h" | |
28 | ||
29 | #ifdef HAVE_LIBCRYPTO | |
30 | #include <openssl/rsa.h> | |
31 | #endif | |
32 | ||
33 | #include "tools.h" | |
34 | #include "s_serv.h" | |
35 | #include "class.h" | |
36 | #include "client.h" | |
37 | #include "common.h" | |
38 | #include "event.h" | |
39 | #include "hash.h" | |
40 | #include "irc_string.h" | |
41 | #include "sprintf_irc.h" | |
42 | #include "ircd.h" | |
43 | #include "ircd_defs.h" | |
44 | #include "numeric.h" | |
45 | #include "packet.h" | |
46 | #include "res.h" | |
47 | #include "commio.h" | |
48 | #include "s_conf.h" | |
49 | #include "s_newconf.h" | |
50 | #include "s_log.h" | |
51 | #include "s_stats.h" | |
52 | #include "s_user.h" | |
53 | #include "scache.h" | |
54 | #include "send.h" | |
55 | #include "client.h" | |
56 | #include "memory.h" | |
57 | #include "channel.h" /* chcap_usage_counts stuff... */ | |
58 | #include "hook.h" | |
59 | #include "msg.h" | |
54015b5f | 60 | #include "reject.h" |
212380e3 AC |
61 | |
62 | extern char *crypt(); | |
63 | ||
64 | #ifndef INADDR_NONE | |
65 | #define INADDR_NONE ((unsigned int) 0xffffffff) | |
66 | #endif | |
67 | ||
68 | #ifndef HAVE_SOCKETPAIR | |
69 | static int inet_socketpair(int d, int type, int protocol, int sv[2]); | |
70 | #endif | |
71 | ||
72 | int MaxConnectionCount = 1; | |
73 | int MaxClientCount = 1; | |
74 | int refresh_user_links = 0; | |
75 | ||
76 | static char buf[BUFSIZE]; | |
77 | ||
78 | static void start_io(struct Client *server); | |
79 | ||
80 | static SlinkRplHnd slink_error; | |
81 | static SlinkRplHnd slink_zipstats; | |
82 | /* | |
83 | * list of recognized server capabilities. "TS" is not on the list | |
84 | * because all servers that we talk to already do TS, and the kludged | |
85 | * extra argument to "PASS" takes care of checking that. -orabidoo | |
86 | */ | |
87 | struct Capability captab[] = { | |
88 | /* name cap */ | |
89 | { "QS", CAP_QS }, | |
90 | { "EX", CAP_EX }, | |
91 | { "CHW", CAP_CHW}, | |
92 | { "IE", CAP_IE}, | |
93 | { "KLN", CAP_KLN}, | |
94 | { "GLN", CAP_GLN}, | |
95 | { "KNOCK", CAP_KNOCK}, | |
96 | { "ZIP", CAP_ZIP}, | |
97 | { "TB", CAP_TB}, | |
98 | { "UNKLN", CAP_UNKLN}, | |
99 | { "CLUSTER", CAP_CLUSTER}, | |
100 | { "ENCAP", CAP_ENCAP }, | |
101 | { "SERVICES", CAP_SERVICE }, | |
102 | { "RSFNC", CAP_RSFNC }, | |
103 | { "SAVE", CAP_SAVE }, | |
104 | { "EUID", CAP_EUID }, | |
105 | {0, 0} | |
106 | }; | |
107 | ||
108 | struct SlinkRplDef slinkrpltab[] = { | |
109 | {SLINKRPL_ERROR, slink_error, SLINKRPL_FLAG_DATA}, | |
110 | {SLINKRPL_ZIPSTATS, slink_zipstats, SLINKRPL_FLAG_DATA}, | |
111 | {0, 0, 0}, | |
112 | }; | |
113 | ||
114 | static int fork_server(struct Client *client_p); | |
115 | ||
116 | static CNCB serv_connect_callback; | |
117 | ||
118 | void | |
119 | slink_error(unsigned int rpl, unsigned int len, unsigned char *data, struct Client *server_p) | |
120 | { | |
121 | char squitreason[256]; | |
122 | ||
123 | s_assert(rpl == SLINKRPL_ERROR); | |
124 | ||
125 | s_assert(len < 256); | |
126 | data[len - 1] = '\0'; | |
127 | ||
128 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "SlinkError for %s: %s", server_p->name, data); | |
129 | snprintf(squitreason, sizeof squitreason, "servlink error: %s", data); | |
130 | exit_client(server_p, server_p, &me, squitreason); | |
131 | } | |
132 | ||
133 | void | |
134 | slink_zipstats(unsigned int rpl, unsigned int len, unsigned char *data, struct Client *server_p) | |
135 | { | |
136 | struct ZipStats zipstats; | |
137 | u_int32_t in = 0, in_wire = 0, out = 0, out_wire = 0; | |
138 | int i = 0; | |
139 | ||
140 | s_assert(rpl == SLINKRPL_ZIPSTATS); | |
141 | s_assert(len == 16); | |
142 | s_assert(IsCapable(server_p, CAP_ZIP)); | |
143 | ||
144 | /* Yes, it needs to be done this way, no we cannot let the compiler | |
145 | * work with the pointer to the structure. This works around a GCC | |
146 | * bug on SPARC that affects all versions at the time of this writing. | |
147 | * I will feed you to the creatures living in RMS's beard if you do | |
148 | * not leave this as is, without being sure that you are not causing | |
149 | * regression for most of our installed SPARC base. | |
150 | * -jmallett, 04/27/2002 | |
151 | */ | |
152 | memcpy(&zipstats, &server_p->localClient->zipstats, sizeof(struct ZipStats)); | |
153 | ||
154 | in |= (data[i++] << 24); | |
155 | in |= (data[i++] << 16); | |
156 | in |= (data[i++] << 8); | |
157 | in |= (data[i++]); | |
158 | ||
159 | in_wire |= (data[i++] << 24); | |
160 | in_wire |= (data[i++] << 16); | |
161 | in_wire |= (data[i++] << 8); | |
162 | in_wire |= (data[i++]); | |
163 | ||
164 | out |= (data[i++] << 24); | |
165 | out |= (data[i++] << 16); | |
166 | out |= (data[i++] << 8); | |
167 | out |= (data[i++]); | |
168 | ||
169 | out_wire |= (data[i++] << 24); | |
170 | out_wire |= (data[i++] << 16); | |
171 | out_wire |= (data[i++] << 8); | |
172 | out_wire |= (data[i++]); | |
173 | ||
174 | zipstats.in += in; | |
175 | zipstats.inK += zipstats.in >> 10; | |
176 | zipstats.in &= 0x03ff; | |
177 | ||
178 | zipstats.in_wire += in_wire; | |
179 | zipstats.inK_wire += zipstats.in_wire >> 10; | |
180 | zipstats.in_wire &= 0x03ff; | |
181 | ||
182 | zipstats.out += out; | |
183 | zipstats.outK += zipstats.out >> 10; | |
184 | zipstats.out &= 0x03ff; | |
185 | ||
186 | zipstats.out_wire += out_wire; | |
187 | zipstats.outK_wire += zipstats.out_wire >> 10; | |
188 | zipstats.out_wire &= 0x03ff; | |
189 | ||
190 | if(zipstats.inK > 0) | |
191 | zipstats.in_ratio = | |
192 | (((double) (zipstats.inK - zipstats.inK_wire) / | |
193 | (double) zipstats.inK) * 100.00); | |
194 | else | |
195 | zipstats.in_ratio = 0; | |
196 | ||
197 | if(zipstats.outK > 0) | |
198 | zipstats.out_ratio = | |
199 | (((double) (zipstats.outK - zipstats.outK_wire) / | |
200 | (double) zipstats.outK) * 100.00); | |
201 | else | |
202 | zipstats.out_ratio = 0; | |
203 | ||
204 | memcpy(&server_p->localClient->zipstats, &zipstats, sizeof(struct ZipStats)); | |
205 | } | |
206 | ||
207 | void | |
208 | collect_zipstats(void *unused) | |
209 | { | |
210 | dlink_node *ptr; | |
211 | struct Client *target_p; | |
212 | ||
213 | DLINK_FOREACH(ptr, serv_list.head) | |
214 | { | |
215 | target_p = ptr->data; | |
216 | if(IsCapable(target_p, CAP_ZIP)) | |
217 | { | |
218 | /* only bother if we haven't already got something queued... */ | |
219 | if(!target_p->localClient->slinkq) | |
220 | { | |
221 | target_p->localClient->slinkq = MyMalloc(1); /* sigh.. */ | |
222 | target_p->localClient->slinkq[0] = SLINKCMD_ZIPSTATS; | |
223 | target_p->localClient->slinkq_ofs = 0; | |
224 | target_p->localClient->slinkq_len = 1; | |
225 | send_queued_slink_write(target_p->localClient->ctrlfd, target_p); | |
226 | } | |
227 | } | |
228 | } | |
229 | } | |
230 | ||
231 | /* | |
232 | * hunt_server - Do the basic thing in delivering the message (command) | |
233 | * across the relays to the specific server (server) for | |
234 | * actions. | |
235 | * | |
236 | * Note: The command is a format string and *MUST* be | |
237 | * of prefixed style (e.g. ":%s COMMAND %s ..."). | |
238 | * Command can have only max 8 parameters. | |
239 | * | |
240 | * server parv[server] is the parameter identifying the | |
241 | * target server. | |
242 | * | |
243 | * *WARNING* | |
244 | * parv[server] is replaced with the pointer to the | |
245 | * real servername from the matched client (I'm lazy | |
246 | * now --msa). | |
247 | * | |
248 | * returns: (see #defines) | |
249 | */ | |
250 | int | |
251 | hunt_server(struct Client *client_p, struct Client *source_p, | |
252 | const char *command, int server, int parc, const char *parv[]) | |
253 | { | |
254 | struct Client *target_p; | |
255 | int wilds; | |
256 | dlink_node *ptr; | |
257 | const char *old; | |
258 | char *new; | |
259 | ||
260 | /* | |
261 | * Assume it's me, if no server | |
262 | */ | |
263 | if(parc <= server || EmptyString(parv[server]) || | |
4d7a1ee5 | 264 | match(parv[server], me.name) || (strcmp(parv[server], me.id) == 0)) |
212380e3 AC |
265 | return (HUNTED_ISME); |
266 | ||
267 | new = LOCAL_COPY(parv[server]); | |
268 | ||
269 | /* | |
270 | * These are to pickup matches that would cause the following | |
271 | * message to go in the wrong direction while doing quick fast | |
272 | * non-matching lookups. | |
273 | */ | |
274 | if(MyClient(source_p)) | |
275 | target_p = find_named_client(new); | |
276 | else | |
277 | target_p = find_client(new); | |
278 | ||
279 | if(target_p) | |
280 | if(target_p->from == source_p->from && !MyConnect(target_p)) | |
281 | target_p = NULL; | |
282 | ||
212380e3 AC |
283 | collapse(new); |
284 | wilds = (strchr(new, '?') || strchr(new, '*')); | |
285 | ||
286 | /* | |
287 | * Again, if there are no wild cards involved in the server | |
288 | * name, use the hash lookup | |
289 | */ | |
290 | if(!target_p) | |
291 | { | |
292 | if(!wilds) | |
293 | { | |
294 | if(MyClient(source_p) || !IsDigit(parv[server][0])) | |
295 | sendto_one_numeric(source_p, ERR_NOSUCHSERVER, | |
296 | form_str(ERR_NOSUCHSERVER), | |
297 | parv[server]); | |
298 | return (HUNTED_NOSUCH); | |
299 | } | |
300 | else | |
301 | { | |
302 | target_p = NULL; | |
303 | ||
304 | DLINK_FOREACH(ptr, global_client_list.head) | |
305 | { | |
306 | if(match(new, ((struct Client *) (ptr->data))->name)) | |
307 | { | |
308 | target_p = ptr->data; | |
309 | break; | |
310 | } | |
311 | } | |
312 | } | |
313 | } | |
314 | ||
315 | if(target_p) | |
316 | { | |
317 | if(!IsRegistered(target_p)) | |
318 | { | |
319 | sendto_one_numeric(source_p, ERR_NOSUCHSERVER, | |
320 | form_str(ERR_NOSUCHSERVER), | |
321 | parv[server]); | |
322 | return HUNTED_NOSUCH; | |
323 | } | |
324 | ||
325 | if(IsMe(target_p) || MyClient(target_p)) | |
326 | return HUNTED_ISME; | |
327 | ||
328 | old = parv[server]; | |
329 | parv[server] = get_id(target_p, target_p); | |
330 | ||
331 | sendto_one(target_p, command, get_id(source_p, target_p), | |
332 | parv[1], parv[2], parv[3], parv[4], parv[5], parv[6], parv[7], parv[8]); | |
333 | parv[server] = old; | |
334 | return (HUNTED_PASS); | |
335 | } | |
336 | ||
337 | if(!IsDigit(parv[server][0])) | |
338 | sendto_one_numeric(source_p, ERR_NOSUCHSERVER, | |
339 | form_str(ERR_NOSUCHSERVER), parv[server]); | |
340 | return (HUNTED_NOSUCH); | |
341 | } | |
342 | ||
343 | /* | |
344 | * try_connections - scan through configuration and try new connections. | |
345 | * Returns the calendar time when the next call to this | |
346 | * function should be made latest. (No harm done if this | |
347 | * is called earlier or later...) | |
348 | */ | |
349 | void | |
350 | try_connections(void *unused) | |
351 | { | |
352 | struct Client *client_p; | |
353 | struct server_conf *server_p = NULL; | |
354 | struct server_conf *tmp_p; | |
355 | struct Class *cltmp; | |
356 | dlink_node *ptr; | |
357 | int connecting = FALSE; | |
358 | int confrq = 0; | |
359 | time_t next = 0; | |
360 | ||
361 | DLINK_FOREACH(ptr, server_conf_list.head) | |
362 | { | |
363 | tmp_p = ptr->data; | |
364 | ||
365 | if(ServerConfIllegal(tmp_p) || !ServerConfAutoconn(tmp_p)) | |
366 | continue; | |
367 | ||
368 | cltmp = tmp_p->class; | |
369 | ||
370 | /* | |
371 | * Skip this entry if the use of it is still on hold until | |
372 | * future. Otherwise handle this entry (and set it on hold | |
373 | * until next time). Will reset only hold times, if already | |
374 | * made one successfull connection... [this algorithm is | |
375 | * a bit fuzzy... -- msa >;) ] | |
376 | */ | |
377 | if(tmp_p->hold > CurrentTime) | |
378 | { | |
379 | if(next > tmp_p->hold || next == 0) | |
380 | next = tmp_p->hold; | |
381 | continue; | |
382 | } | |
383 | ||
384 | confrq = get_con_freq(cltmp); | |
385 | tmp_p->hold = CurrentTime + confrq; | |
386 | ||
387 | /* | |
388 | * Found a CONNECT config with port specified, scan clients | |
389 | * and see if this server is already connected? | |
390 | */ | |
391 | client_p = find_server(NULL, tmp_p->name); | |
392 | ||
393 | if(!client_p && (CurrUsers(cltmp) < MaxUsers(cltmp)) && !connecting) | |
394 | { | |
395 | server_p = tmp_p; | |
396 | ||
397 | /* We connect only one at time... */ | |
398 | connecting = TRUE; | |
399 | } | |
400 | ||
401 | if((next > tmp_p->hold) || (next == 0)) | |
402 | next = tmp_p->hold; | |
403 | } | |
404 | ||
405 | /* TODO: change this to set active flag to 0 when added to event! --Habeeb */ | |
406 | if(GlobalSetOptions.autoconn == 0) | |
407 | return; | |
408 | ||
409 | if(!connecting) | |
410 | return; | |
411 | ||
412 | /* move this connect entry to end.. */ | |
413 | dlinkDelete(&server_p->node, &server_conf_list); | |
414 | dlinkAddTail(server_p, &server_p->node, &server_conf_list); | |
415 | ||
416 | /* | |
417 | * We used to only print this if serv_connect() actually | |
418 | * suceeded, but since comm_tcp_connect() can call the callback | |
419 | * immediately if there is an error, we were getting error messages | |
420 | * in the wrong order. SO, we just print out the activated line, | |
421 | * and let serv_connect() / serv_connect_callback() print an | |
422 | * error afterwards if it fails. | |
423 | * -- adrian | |
424 | */ | |
425 | #ifndef HIDE_SERVERS_IPS | |
426 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
427 | "Connection to %s[%s] activated.", | |
428 | server_p->name, server_p->host); | |
429 | #else | |
430 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
431 | "Connection to %s activated", | |
432 | server_p->name); | |
433 | #endif | |
434 | ||
435 | serv_connect(server_p, 0); | |
436 | } | |
437 | ||
438 | int | |
439 | check_server(const char *name, struct Client *client_p) | |
440 | { | |
441 | struct server_conf *server_p = NULL; | |
442 | struct server_conf *tmp_p; | |
443 | dlink_node *ptr; | |
444 | int error = -1; | |
445 | ||
446 | s_assert(NULL != client_p); | |
447 | if(client_p == NULL) | |
448 | return error; | |
449 | ||
450 | if(!(client_p->localClient->passwd)) | |
451 | return -2; | |
452 | ||
453 | if(strlen(name) > HOSTLEN) | |
454 | return -4; | |
455 | ||
456 | DLINK_FOREACH(ptr, server_conf_list.head) | |
457 | { | |
458 | tmp_p = ptr->data; | |
459 | ||
460 | if(ServerConfIllegal(tmp_p)) | |
461 | continue; | |
462 | ||
463 | if(!match(tmp_p->name, name)) | |
464 | continue; | |
465 | ||
466 | error = -3; | |
467 | ||
468 | /* XXX: Fix me for IPv6 */ | |
469 | /* XXX sockhost is the IPv4 ip as a string */ | |
470 | if(match(tmp_p->host, client_p->host) || | |
471 | match(tmp_p->host, client_p->sockhost)) | |
472 | { | |
473 | error = -2; | |
474 | ||
475 | if(ServerConfEncrypted(tmp_p)) | |
476 | { | |
477 | if(!strcmp(tmp_p->passwd, crypt(client_p->localClient->passwd, | |
478 | tmp_p->passwd))) | |
479 | { | |
480 | server_p = tmp_p; | |
481 | break; | |
482 | } | |
483 | } | |
484 | else if(!strcmp(tmp_p->passwd, client_p->localClient->passwd)) | |
485 | { | |
486 | server_p = tmp_p; | |
487 | break; | |
488 | } | |
489 | } | |
490 | } | |
491 | ||
492 | if(server_p == NULL) | |
493 | return error; | |
494 | ||
495 | attach_server_conf(client_p, server_p); | |
496 | ||
497 | /* clear ZIP/TB if they support but we dont want them */ | |
498 | #ifdef HAVE_LIBZ | |
499 | if(!ServerConfCompressed(server_p)) | |
500 | #endif | |
501 | ClearCap(client_p, CAP_ZIP); | |
502 | ||
503 | if(!ServerConfTb(server_p)) | |
504 | ClearCap(client_p, CAP_TB); | |
505 | ||
506 | return 0; | |
507 | } | |
508 | ||
509 | /* | |
510 | * send_capabilities | |
511 | * | |
512 | * inputs - Client pointer to send to | |
513 | * - int flag of capabilities that this server has | |
514 | * output - NONE | |
515 | * side effects - send the CAPAB line to a server -orabidoo | |
516 | * | |
517 | */ | |
518 | void | |
519 | send_capabilities(struct Client *client_p, int cap_can_send) | |
520 | { | |
521 | struct Capability *cap; | |
522 | char msgbuf[BUFSIZE]; | |
523 | char *t; | |
524 | int tl; | |
525 | ||
526 | t = msgbuf; | |
527 | ||
528 | for (cap = captab; cap->name; ++cap) | |
529 | { | |
530 | if(cap->cap & cap_can_send) | |
531 | { | |
532 | tl = ircsprintf(t, "%s ", cap->name); | |
533 | t += tl; | |
534 | } | |
535 | } | |
536 | ||
537 | t--; | |
538 | *t = '\0'; | |
539 | ||
540 | sendto_one(client_p, "CAPAB :%s", msgbuf); | |
541 | } | |
542 | ||
543 | /* burst_modes_TS5() | |
544 | * | |
545 | * input - client to burst to, channel name, list to burst, mode flag | |
546 | * output - | |
547 | * side effects - client is sent a list of +b, or +e, or +I modes | |
548 | */ | |
549 | static void | |
550 | burst_modes_TS5(struct Client *client_p, char *chname, dlink_list *list, char flag) | |
551 | { | |
552 | dlink_node *ptr; | |
553 | struct Ban *banptr; | |
554 | char mbuf[MODEBUFLEN]; | |
555 | char pbuf[BUFSIZE]; | |
556 | int tlen; | |
557 | int mlen; | |
558 | int cur_len; | |
559 | char *mp; | |
560 | char *pp; | |
561 | int count = 0; | |
562 | ||
563 | mlen = ircsprintf(buf, ":%s MODE %s +", me.name, chname); | |
564 | cur_len = mlen; | |
565 | ||
566 | mp = mbuf; | |
567 | pp = pbuf; | |
568 | ||
569 | DLINK_FOREACH(ptr, list->head) | |
570 | { | |
571 | banptr = ptr->data; | |
572 | tlen = strlen(banptr->banstr) + 3; | |
573 | ||
574 | /* uh oh */ | |
575 | if(tlen > MODEBUFLEN) | |
576 | continue; | |
577 | ||
578 | if((count >= MAXMODEPARAMS) || ((cur_len + tlen + 2) > (BUFSIZE - 3))) | |
579 | { | |
580 | sendto_one(client_p, "%s%s %s", buf, mbuf, pbuf); | |
581 | ||
582 | mp = mbuf; | |
583 | pp = pbuf; | |
584 | cur_len = mlen; | |
585 | count = 0; | |
586 | } | |
587 | ||
588 | *mp++ = flag; | |
589 | *mp = '\0'; | |
590 | pp += ircsprintf(pp, "%s ", banptr->banstr); | |
591 | cur_len += tlen; | |
592 | count++; | |
593 | } | |
594 | ||
595 | if(count != 0) | |
596 | sendto_one(client_p, "%s%s %s", buf, mbuf, pbuf); | |
597 | } | |
598 | ||
599 | /* burst_modes_TS6() | |
600 | * | |
601 | * input - client to burst to, channel name, list to burst, mode flag | |
602 | * output - | |
603 | * side effects - client is sent a list of +b, +e, or +I modes | |
604 | */ | |
605 | static void | |
606 | burst_modes_TS6(struct Client *client_p, struct Channel *chptr, | |
607 | dlink_list *list, char flag) | |
608 | { | |
609 | dlink_node *ptr; | |
610 | struct Ban *banptr; | |
611 | char *t; | |
612 | int tlen; | |
613 | int mlen; | |
614 | int cur_len; | |
615 | ||
616 | cur_len = mlen = ircsprintf(buf, ":%s BMASK %ld %s %c :", | |
617 | me.id, (long) chptr->channelts, chptr->chname, flag); | |
618 | t = buf + mlen; | |
619 | ||
620 | DLINK_FOREACH(ptr, list->head) | |
621 | { | |
622 | banptr = ptr->data; | |
623 | ||
624 | tlen = strlen(banptr->banstr) + 1; | |
625 | ||
626 | /* uh oh */ | |
627 | if(cur_len + tlen > BUFSIZE - 3) | |
628 | { | |
629 | /* the one we're trying to send doesnt fit at all! */ | |
630 | if(cur_len == mlen) | |
631 | { | |
632 | s_assert(0); | |
633 | continue; | |
634 | } | |
635 | ||
636 | /* chop off trailing space and send.. */ | |
637 | *(t-1) = '\0'; | |
638 | sendto_one(client_p, "%s", buf); | |
639 | cur_len = mlen; | |
640 | t = buf + mlen; | |
641 | } | |
642 | ||
643 | ircsprintf(t, "%s ", banptr->banstr); | |
644 | t += tlen; | |
645 | cur_len += tlen; | |
646 | } | |
647 | ||
648 | /* cant ever exit the loop above without having modified buf, | |
649 | * chop off trailing space and send. | |
650 | */ | |
651 | *(t-1) = '\0'; | |
652 | sendto_one(client_p, "%s", buf); | |
653 | } | |
654 | ||
655 | /* | |
656 | * burst_TS5 | |
657 | * | |
658 | * inputs - client (server) to send nick towards | |
659 | * - client to send nick for | |
660 | * output - NONE | |
661 | * side effects - NICK message is sent towards given client_p | |
662 | */ | |
663 | static void | |
664 | burst_TS5(struct Client *client_p) | |
665 | { | |
666 | static char ubuf[12]; | |
667 | struct Client *target_p; | |
668 | struct Channel *chptr; | |
669 | struct membership *msptr; | |
670 | hook_data_client hclientinfo; | |
671 | hook_data_channel hchaninfo; | |
672 | dlink_node *ptr; | |
673 | dlink_node *uptr; | |
674 | char *t; | |
675 | int tlen, mlen; | |
676 | int cur_len = 0; | |
677 | ||
678 | hclientinfo.client = hchaninfo.client = client_p; | |
679 | ||
680 | DLINK_FOREACH(ptr, global_client_list.head) | |
681 | { | |
682 | target_p = ptr->data; | |
683 | ||
684 | if(!IsPerson(target_p)) | |
685 | continue; | |
686 | ||
687 | send_umode(NULL, target_p, 0, 0, ubuf); | |
688 | if(!*ubuf) | |
689 | { | |
690 | ubuf[0] = '+'; | |
691 | ubuf[1] = '\0'; | |
692 | } | |
693 | ||
694 | sendto_one(client_p, "NICK %s %d %ld %s %s %s %s :%s", | |
695 | target_p->name, target_p->hopcount + 1, | |
696 | (long) target_p->tsinfo, ubuf, | |
697 | target_p->username, target_p->host, | |
698 | target_p->user->server, target_p->info); | |
699 | ||
700 | if(IsDynSpoof(target_p)) | |
701 | sendto_one(client_p, ":%s ENCAP * REALHOST %s", | |
702 | target_p->name, target_p->orighost); | |
703 | if(!EmptyString(target_p->user->suser)) | |
704 | sendto_one(client_p, ":%s ENCAP * LOGIN %s", | |
705 | target_p->name, target_p->user->suser); | |
706 | ||
707 | if(ConfigFileEntry.burst_away && !EmptyString(target_p->user->away)) | |
708 | sendto_one(client_p, ":%s AWAY :%s", | |
709 | target_p->name, target_p->user->away); | |
710 | ||
711 | hclientinfo.target = target_p; | |
712 | call_hook(h_burst_client, &hclientinfo); | |
713 | } | |
714 | ||
715 | DLINK_FOREACH(ptr, global_channel_list.head) | |
716 | { | |
717 | chptr = ptr->data; | |
718 | ||
719 | if(*chptr->chname != '#') | |
720 | continue; | |
721 | ||
722 | cur_len = mlen = ircsprintf(buf, ":%s SJOIN %ld %s %s :", me.name, | |
723 | (long) chptr->channelts, chptr->chname, | |
724 | channel_modes(chptr, client_p)); | |
725 | ||
726 | t = buf + mlen; | |
727 | ||
728 | DLINK_FOREACH(uptr, chptr->members.head) | |
729 | { | |
730 | msptr = uptr->data; | |
731 | ||
732 | tlen = strlen(msptr->client_p->name) + 1; | |
733 | if(is_chanop(msptr)) | |
734 | tlen++; | |
735 | if(is_voiced(msptr)) | |
736 | tlen++; | |
737 | ||
738 | if(cur_len + tlen >= BUFSIZE - 3) | |
739 | { | |
740 | t--; | |
741 | *t = '\0'; | |
742 | sendto_one(client_p, "%s", buf); | |
743 | cur_len = mlen; | |
744 | t = buf + mlen; | |
745 | } | |
746 | ||
747 | ircsprintf(t, "%s%s ", find_channel_status(msptr, 1), | |
748 | msptr->client_p->name); | |
749 | ||
750 | cur_len += tlen; | |
751 | t += tlen; | |
752 | } | |
753 | ||
754 | if (dlink_list_length(&chptr->members) > 0) | |
755 | { | |
756 | /* remove trailing space */ | |
757 | t--; | |
758 | *t = '\0'; | |
759 | } | |
760 | sendto_one(client_p, "%s", buf); | |
761 | ||
762 | burst_modes_TS5(client_p, chptr->chname, &chptr->banlist, 'b'); | |
763 | ||
764 | if(IsCapable(client_p, CAP_EX)) | |
765 | burst_modes_TS5(client_p, chptr->chname, &chptr->exceptlist, 'e'); | |
766 | ||
767 | if(IsCapable(client_p, CAP_IE)) | |
768 | burst_modes_TS5(client_p, chptr->chname, &chptr->invexlist, 'I'); | |
769 | ||
770 | burst_modes_TS5(client_p, chptr->chname, &chptr->quietlist, 'q'); | |
771 | ||
772 | if(IsCapable(client_p, CAP_TB) && chptr->topic != NULL) | |
773 | sendto_one(client_p, ":%s TB %s %ld %s%s:%s", | |
774 | me.name, chptr->chname, (long) chptr->topic_time, | |
775 | ConfigChannel.burst_topicwho ? chptr->topic_info : "", | |
776 | ConfigChannel.burst_topicwho ? " " : "", | |
777 | chptr->topic); | |
778 | ||
779 | hchaninfo.chptr = chptr; | |
780 | call_hook(h_burst_channel, &hchaninfo); | |
781 | } | |
782 | ||
783 | hclientinfo.target = NULL; | |
784 | call_hook(h_burst_finished, &hclientinfo); | |
785 | } | |
786 | ||
787 | /* | |
788 | * burst_TS6 | |
789 | * | |
790 | * inputs - client (server) to send nick towards | |
791 | * - client to send nick for | |
792 | * output - NONE | |
793 | * side effects - NICK message is sent towards given client_p | |
794 | */ | |
795 | static void | |
796 | burst_TS6(struct Client *client_p) | |
797 | { | |
798 | static char ubuf[12]; | |
799 | struct Client *target_p; | |
800 | struct Channel *chptr; | |
801 | struct membership *msptr; | |
802 | hook_data_client hclientinfo; | |
803 | hook_data_channel hchaninfo; | |
804 | dlink_node *ptr; | |
805 | dlink_node *uptr; | |
806 | char *t; | |
807 | int tlen, mlen; | |
808 | int cur_len = 0; | |
809 | ||
810 | hclientinfo.client = hchaninfo.client = client_p; | |
811 | ||
812 | DLINK_FOREACH(ptr, global_client_list.head) | |
813 | { | |
814 | target_p = ptr->data; | |
815 | ||
816 | if(!IsPerson(target_p)) | |
817 | continue; | |
818 | ||
819 | send_umode(NULL, target_p, 0, 0, ubuf); | |
820 | if(!*ubuf) | |
821 | { | |
822 | ubuf[0] = '+'; | |
823 | ubuf[1] = '\0'; | |
824 | } | |
825 | ||
826 | if(has_id(target_p) && IsCapable(client_p, CAP_EUID)) | |
827 | sendto_one(client_p, ":%s EUID %s %d %ld %s %s %s %s %s %s %s :%s", | |
828 | target_p->servptr->id, target_p->name, | |
829 | target_p->hopcount + 1, | |
830 | (long) target_p->tsinfo, ubuf, | |
831 | target_p->username, target_p->host, | |
832 | IsIPSpoof(target_p) ? "0" : target_p->sockhost, | |
833 | target_p->id, | |
834 | IsDynSpoof(target_p) ? target_p->orighost : "*", | |
835 | EmptyString(target_p->user->suser) ? "*" : target_p->user->suser, | |
836 | target_p->info); | |
837 | else if(has_id(target_p)) | |
838 | sendto_one(client_p, ":%s UID %s %d %ld %s %s %s %s %s :%s", | |
839 | target_p->servptr->id, target_p->name, | |
840 | target_p->hopcount + 1, | |
841 | (long) target_p->tsinfo, ubuf, | |
842 | target_p->username, target_p->host, | |
843 | IsIPSpoof(target_p) ? "0" : target_p->sockhost, | |
844 | target_p->id, target_p->info); | |
845 | else | |
846 | sendto_one(client_p, "NICK %s %d %ld %s %s %s %s :%s", | |
847 | target_p->name, | |
848 | target_p->hopcount + 1, | |
849 | (long) target_p->tsinfo, | |
850 | ubuf, | |
851 | target_p->username, target_p->host, | |
852 | target_p->user->server, target_p->info); | |
853 | ||
854 | if(!has_id(target_p) || !IsCapable(client_p, CAP_EUID)) | |
855 | { | |
856 | if(IsDynSpoof(target_p)) | |
857 | sendto_one(client_p, ":%s ENCAP * REALHOST %s", | |
858 | use_id(target_p), target_p->orighost); | |
859 | if(!EmptyString(target_p->user->suser)) | |
860 | sendto_one(client_p, ":%s ENCAP * LOGIN %s", | |
861 | use_id(target_p), target_p->user->suser); | |
862 | } | |
863 | ||
864 | if(ConfigFileEntry.burst_away && !EmptyString(target_p->user->away)) | |
865 | sendto_one(client_p, ":%s AWAY :%s", | |
866 | use_id(target_p), | |
867 | target_p->user->away); | |
868 | ||
869 | hclientinfo.target = target_p; | |
870 | call_hook(h_burst_client, &hclientinfo); | |
871 | } | |
872 | ||
873 | DLINK_FOREACH(ptr, global_channel_list.head) | |
874 | { | |
875 | chptr = ptr->data; | |
876 | ||
877 | if(*chptr->chname != '#') | |
878 | continue; | |
879 | ||
880 | cur_len = mlen = ircsprintf(buf, ":%s SJOIN %ld %s %s :", me.id, | |
881 | (long) chptr->channelts, chptr->chname, | |
882 | channel_modes(chptr, client_p)); | |
883 | ||
884 | t = buf + mlen; | |
885 | ||
886 | DLINK_FOREACH(uptr, chptr->members.head) | |
887 | { | |
888 | msptr = uptr->data; | |
889 | ||
890 | tlen = strlen(use_id(msptr->client_p)) + 1; | |
891 | if(is_chanop(msptr)) | |
892 | tlen++; | |
893 | if(is_voiced(msptr)) | |
894 | tlen++; | |
895 | ||
896 | if(cur_len + tlen >= BUFSIZE - 3) | |
897 | { | |
898 | *(t-1) = '\0'; | |
899 | sendto_one(client_p, "%s", buf); | |
900 | cur_len = mlen; | |
901 | t = buf + mlen; | |
902 | } | |
903 | ||
904 | ircsprintf(t, "%s%s ", find_channel_status(msptr, 1), | |
905 | use_id(msptr->client_p)); | |
906 | ||
907 | cur_len += tlen; | |
908 | t += tlen; | |
909 | } | |
910 | ||
911 | if (dlink_list_length(&chptr->members) > 0) | |
912 | { | |
913 | /* remove trailing space */ | |
914 | *(t-1) = '\0'; | |
915 | } | |
916 | sendto_one(client_p, "%s", buf); | |
917 | ||
918 | if(dlink_list_length(&chptr->banlist) > 0) | |
919 | burst_modes_TS6(client_p, chptr, &chptr->banlist, 'b'); | |
920 | ||
921 | if(IsCapable(client_p, CAP_EX) && | |
922 | dlink_list_length(&chptr->exceptlist) > 0) | |
923 | burst_modes_TS6(client_p, chptr, &chptr->exceptlist, 'e'); | |
924 | ||
925 | if(IsCapable(client_p, CAP_IE) && | |
926 | dlink_list_length(&chptr->invexlist) > 0) | |
927 | burst_modes_TS6(client_p, chptr, &chptr->invexlist, 'I'); | |
928 | ||
929 | if(dlink_list_length(&chptr->quietlist) > 0) | |
930 | burst_modes_TS6(client_p, chptr, &chptr->quietlist, 'q'); | |
931 | ||
932 | if(IsCapable(client_p, CAP_TB) && chptr->topic != NULL) | |
933 | sendto_one(client_p, ":%s TB %s %ld %s%s:%s", | |
934 | me.id, chptr->chname, (long) chptr->topic_time, | |
935 | ConfigChannel.burst_topicwho ? chptr->topic_info : "", | |
936 | ConfigChannel.burst_topicwho ? " " : "", | |
937 | chptr->topic); | |
938 | ||
939 | hchaninfo.chptr = chptr; | |
940 | call_hook(h_burst_channel, &hchaninfo); | |
941 | } | |
942 | ||
943 | hclientinfo.target = NULL; | |
944 | call_hook(h_burst_finished, &hclientinfo); | |
945 | } | |
946 | ||
947 | /* | |
948 | * show_capabilities - show current server capabilities | |
949 | * | |
950 | * inputs - pointer to an struct Client | |
951 | * output - pointer to static string | |
952 | * side effects - build up string representing capabilities of server listed | |
953 | */ | |
954 | const char * | |
955 | show_capabilities(struct Client *target_p) | |
956 | { | |
957 | static char msgbuf[BUFSIZE]; | |
958 | struct Capability *cap; | |
959 | char *t; | |
960 | int tl; | |
961 | ||
962 | t = msgbuf; | |
963 | tl = ircsprintf(msgbuf, "TS "); | |
964 | t += tl; | |
965 | ||
966 | if(!IsServer(target_p) || !target_p->serv->caps) /* short circuit if no caps */ | |
967 | { | |
968 | msgbuf[2] = '\0'; | |
969 | return msgbuf; | |
970 | } | |
971 | ||
972 | for (cap = captab; cap->cap; ++cap) | |
973 | { | |
974 | if(cap->cap & target_p->serv->caps) | |
975 | { | |
976 | tl = ircsprintf(t, "%s ", cap->name); | |
977 | t += tl; | |
978 | } | |
979 | } | |
980 | ||
981 | t--; | |
982 | *t = '\0'; | |
983 | ||
984 | return msgbuf; | |
985 | } | |
986 | ||
987 | /* | |
988 | * server_estab | |
989 | * | |
990 | * inputs - pointer to a struct Client | |
991 | * output - | |
992 | * side effects - | |
993 | */ | |
994 | int | |
995 | server_estab(struct Client *client_p) | |
996 | { | |
997 | struct Client *target_p; | |
998 | struct server_conf *server_p; | |
999 | hook_data_client hdata; | |
1000 | char *host; | |
1001 | dlink_node *ptr; | |
1002 | ||
1003 | s_assert(NULL != client_p); | |
1004 | if(client_p == NULL) | |
1005 | return -1; | |
1006 | ClearAccess(client_p); | |
1007 | ||
1008 | host = client_p->name; | |
1009 | ||
1010 | if((server_p = client_p->localClient->att_sconf) == NULL) | |
1011 | { | |
1012 | /* This shouldn't happen, better tell the ops... -A1kmm */ | |
1013 | sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL, | |
1014 | "Warning: Lost connect{} block for server %s!", host); | |
1015 | return exit_client(client_p, client_p, client_p, "Lost connect{} block!"); | |
1016 | } | |
1017 | ||
1018 | /* We shouldn't have to check this, it should already done before | |
1019 | * server_estab is called. -A1kmm | |
1020 | */ | |
1021 | if(client_p->localClient->passwd) | |
1022 | { | |
1023 | memset(client_p->localClient->passwd, 0, strlen(client_p->localClient->passwd)); | |
1024 | MyFree(client_p->localClient->passwd); | |
1025 | client_p->localClient->passwd = NULL; | |
1026 | } | |
1027 | ||
1028 | /* Its got identd , since its a server */ | |
1029 | SetGotId(client_p); | |
1030 | ||
1031 | /* If there is something in the serv_list, it might be this | |
1032 | * connecting server.. | |
1033 | */ | |
1034 | if(!ServerInfo.hub && serv_list.head) | |
1035 | { | |
1036 | if(client_p != serv_list.head->data || serv_list.head->next) | |
1037 | { | |
1038 | ServerStats->is_ref++; | |
1039 | sendto_one(client_p, "ERROR :I'm a leaf not a hub"); | |
1040 | return exit_client(client_p, client_p, client_p, "I'm a leaf"); | |
1041 | } | |
1042 | } | |
1043 | ||
1044 | if(IsUnknown(client_p)) | |
1045 | { | |
1046 | /* | |
1047 | * jdc -- 1. Use EmptyString(), not [0] index reference. | |
1048 | * 2. Check ->spasswd, not ->passwd. | |
1049 | */ | |
1050 | if(!EmptyString(server_p->spasswd)) | |
1051 | { | |
1052 | /* kludge, if we're not using TS6, dont ever send | |
1053 | * ourselves as being TS6 capable. | |
1054 | */ | |
1055 | if(ServerInfo.use_ts6) | |
1056 | sendto_one(client_p, "PASS %s TS %d :%s", | |
1057 | server_p->spasswd, TS_CURRENT, me.id); | |
1058 | else | |
1059 | sendto_one(client_p, "PASS %s :TS", | |
1060 | server_p->spasswd); | |
1061 | } | |
1062 | ||
1063 | /* pass info to new server */ | |
1064 | send_capabilities(client_p, default_server_capabs | |
1065 | | (ServerConfCompressed(server_p) ? CAP_ZIP_SUPPORTED : 0) | |
1066 | | (ServerConfTb(server_p) ? CAP_TB : 0)); | |
1067 | ||
1068 | sendto_one(client_p, "SERVER %s 1 :%s%s", | |
1069 | me.name, | |
1070 | ConfigServerHide.hidden ? "(H) " : "", | |
1071 | (me.info[0]) ? (me.info) : "IRCers United"); | |
1072 | } | |
1073 | ||
1074 | if(!comm_set_buffers(client_p->localClient->fd, READBUF_SIZE)) | |
1075 | report_error(SETBUF_ERROR_MSG, | |
1076 | get_server_name(client_p, SHOW_IP), | |
1077 | log_client_name(client_p, SHOW_IP), errno); | |
1078 | ||
1079 | /* Hand the server off to servlink now */ | |
1080 | if(IsCapable(client_p, CAP_ZIP)) | |
1081 | { | |
1082 | if(fork_server(client_p) < 0) | |
1083 | { | |
1084 | sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL, | |
1085 | "Warning: fork failed for server %s -- check servlink_path (%s)", | |
1086 | get_server_name(client_p, HIDE_IP), | |
1087 | ConfigFileEntry.servlink_path); | |
1088 | return exit_client(client_p, client_p, client_p, "Fork failed"); | |
1089 | } | |
1090 | start_io(client_p); | |
1091 | SetServlink(client_p); | |
1092 | } | |
1093 | ||
1094 | sendto_one(client_p, "SVINFO %d %d 0 :%ld", TS_CURRENT, TS_MIN, CurrentTime); | |
1095 | ||
1096 | client_p->servptr = &me; | |
1097 | ||
1098 | if(IsAnyDead(client_p)) | |
1099 | return CLIENT_EXITED; | |
1100 | ||
1101 | SetServer(client_p); | |
1102 | ||
1103 | /* Update the capability combination usage counts */ | |
1104 | set_chcap_usage_counts(client_p); | |
1105 | ||
1106 | dlinkAdd(client_p, &client_p->lnode, &me.serv->servers); | |
54015b5f | 1107 | del_unknown_ip(client_p); |
212380e3 AC |
1108 | dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &serv_list); |
1109 | dlinkAddTailAlloc(client_p, &global_serv_list); | |
1110 | ||
1111 | if(has_id(client_p)) | |
1112 | add_to_id_hash(client_p->id, client_p); | |
1113 | ||
1114 | add_to_client_hash(client_p->name, client_p); | |
1115 | /* doesnt duplicate client_p->serv if allocated this struct already */ | |
1116 | make_server(client_p); | |
1117 | client_p->serv->up = me.name; | |
1118 | client_p->serv->upid = me.id; | |
1119 | ||
1120 | client_p->serv->caps = client_p->localClient->caps; | |
1121 | ||
1122 | if(client_p->localClient->fullcaps) | |
1123 | { | |
1124 | DupString(client_p->serv->fullcaps, client_p->localClient->fullcaps); | |
1125 | MyFree(client_p->localClient->fullcaps); | |
1126 | client_p->localClient->fullcaps = NULL; | |
1127 | } | |
1128 | ||
1129 | /* add it to scache */ | |
1130 | find_or_add(client_p->name); | |
1131 | client_p->localClient->firsttime = CurrentTime; | |
1132 | /* fixing eob timings.. -gnp */ | |
1133 | ||
1134 | /* Show the real host/IP to admins */ | |
1135 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
1136 | "Link with %s established: (%s) link", | |
1137 | get_server_name(client_p, SHOW_IP), | |
1138 | show_capabilities(client_p)); | |
1139 | ||
1140 | ilog(L_SERVER, "Link with %s established: (%s) link", | |
1141 | log_client_name(client_p, SHOW_IP), show_capabilities(client_p)); | |
1142 | ||
1143 | hdata.client = &me; | |
1144 | hdata.target = client_p; | |
1145 | call_hook(h_server_introduced, &hdata); | |
1146 | ||
1147 | if(HasServlink(client_p)) | |
1148 | { | |
1149 | /* we won't overflow FD_DESC_SZ here, as it can hold | |
1150 | * client_p->name + 64 | |
1151 | */ | |
1152 | comm_note(client_p->localClient->fd, "slink data: %s", client_p->name); | |
1153 | comm_note(client_p->localClient->ctrlfd, "slink ctrl: %s", client_p->name); | |
1154 | } | |
1155 | else | |
1156 | comm_note(client_p->localClient->fd, "Server: %s", client_p->name); | |
1157 | ||
1158 | /* | |
1159 | ** Old sendto_serv_but_one() call removed because we now | |
1160 | ** need to send different names to different servers | |
1161 | ** (domain name matching) Send new server to other servers. | |
1162 | */ | |
1163 | DLINK_FOREACH(ptr, serv_list.head) | |
1164 | { | |
1165 | target_p = ptr->data; | |
1166 | ||
1167 | if(target_p == client_p) | |
1168 | continue; | |
1169 | ||
1170 | if(has_id(target_p) && has_id(client_p)) | |
1171 | { | |
1172 | sendto_one(target_p, ":%s SID %s 2 %s :%s%s", | |
1173 | me.id, client_p->name, client_p->id, | |
1174 | IsHidden(client_p) ? "(H) " : "", client_p->info); | |
1175 | ||
1176 | if(IsCapable(target_p, CAP_ENCAP) && | |
1177 | !EmptyString(client_p->serv->fullcaps)) | |
1178 | sendto_one(target_p, ":%s ENCAP * GCAP :%s", | |
1179 | client_p->id, client_p->serv->fullcaps); | |
1180 | } | |
1181 | else | |
1182 | { | |
1183 | sendto_one(target_p, ":%s SERVER %s 2 :%s%s", | |
1184 | me.name, client_p->name, | |
1185 | IsHidden(client_p) ? "(H) " : "", client_p->info); | |
1186 | ||
1187 | if(IsCapable(target_p, CAP_ENCAP) && | |
1188 | !EmptyString(client_p->serv->fullcaps)) | |
1189 | sendto_one(target_p, ":%s ENCAP * GCAP :%s", | |
1190 | client_p->name, client_p->serv->fullcaps); | |
1191 | } | |
1192 | } | |
1193 | ||
1194 | /* | |
1195 | ** Pass on my client information to the new server | |
1196 | ** | |
1197 | ** First, pass only servers (idea is that if the link gets | |
1198 | ** cancelled beacause the server was already there, | |
1199 | ** there are no NICK's to be cancelled...). Of course, | |
1200 | ** if cancellation occurs, all this info is sent anyway, | |
1201 | ** and I guess the link dies when a read is attempted...? --msa | |
1202 | ** | |
1203 | ** Note: Link cancellation to occur at this point means | |
1204 | ** that at least two servers from my fragment are building | |
1205 | ** up connection this other fragment at the same time, it's | |
1206 | ** a race condition, not the normal way of operation... | |
1207 | ** | |
1208 | ** ALSO NOTE: using the get_client_name for server names-- | |
1209 | ** see previous *WARNING*!!! (Also, original inpath | |
1210 | ** is destroyed...) | |
1211 | */ | |
1212 | DLINK_FOREACH(ptr, global_serv_list.head) | |
1213 | { | |
1214 | target_p = ptr->data; | |
1215 | ||
1216 | /* target_p->from == target_p for target_p == client_p */ | |
1217 | if(IsMe(target_p) || target_p->from == client_p) | |
1218 | continue; | |
1219 | ||
1220 | /* presumption, if target has an id, so does its uplink */ | |
1221 | if(has_id(client_p) && has_id(target_p)) | |
1222 | sendto_one(client_p, ":%s SID %s %d %s :%s%s", | |
1223 | target_p->serv->upid, target_p->name, | |
1224 | target_p->hopcount + 1, target_p->id, | |
1225 | IsHidden(target_p) ? "(H) " : "", target_p->info); | |
1226 | else | |
1227 | sendto_one(client_p, ":%s SERVER %s %d :%s%s", | |
1228 | target_p->serv->up, | |
1229 | target_p->name, target_p->hopcount + 1, | |
1230 | IsHidden(target_p) ? "(H) " : "", target_p->info); | |
1231 | ||
1232 | if(IsCapable(client_p, CAP_ENCAP) && | |
1233 | !EmptyString(target_p->serv->fullcaps)) | |
1234 | sendto_one(client_p, ":%s ENCAP * GCAP :%s", | |
1235 | get_id(target_p, client_p), | |
1236 | target_p->serv->fullcaps); | |
1237 | } | |
1238 | ||
1239 | if(has_id(client_p)) | |
1240 | burst_TS6(client_p); | |
1241 | else | |
1242 | burst_TS5(client_p); | |
1243 | ||
1244 | /* Always send a PING after connect burst is done */ | |
1245 | sendto_one(client_p, "PING :%s", get_id(&me, client_p)); | |
1246 | ||
1247 | free_pre_client(client_p); | |
1248 | ||
1249 | return 0; | |
1250 | } | |
1251 | ||
1252 | static void | |
1253 | start_io(struct Client *server) | |
1254 | { | |
1255 | unsigned char *iobuf; | |
1256 | int c = 0; | |
1257 | int linecount = 0; | |
1258 | int linelen; | |
1259 | ||
1260 | iobuf = MyMalloc(256); /* XXX: This seems arbitrary. Perhaps make it IRCD_BUFSIZE? --nenolod */ | |
1261 | ||
1262 | if(IsCapable(server, CAP_ZIP)) | |
1263 | { | |
1264 | /* ziplink */ | |
1265 | iobuf[c++] = SLINKCMD_SET_ZIP_OUT_LEVEL; | |
1266 | iobuf[c++] = 0; /* | */ | |
1267 | iobuf[c++] = 1; /* \ len is 1 */ | |
1268 | iobuf[c++] = ConfigFileEntry.compression_level; | |
1269 | iobuf[c++] = SLINKCMD_START_ZIP_IN; | |
1270 | iobuf[c++] = SLINKCMD_START_ZIP_OUT; | |
1271 | } | |
1272 | ||
1273 | while (MyConnect(server)) | |
1274 | { | |
1275 | linecount++; | |
1276 | ||
1277 | iobuf = MyRealloc(iobuf, (c + READBUF_SIZE + 64)); | |
1278 | ||
1279 | /* store data in c+3 to allow for SLINKCMD_INJECT_RECVQ and len u16 */ | |
1280 | linelen = linebuf_get(&server->localClient->buf_recvq, (char *) (iobuf + c + 3), READBUF_SIZE, LINEBUF_PARTIAL, LINEBUF_RAW); /* include partial lines */ | |
1281 | ||
1282 | if(linelen) | |
1283 | { | |
1284 | iobuf[c++] = SLINKCMD_INJECT_RECVQ; | |
1285 | iobuf[c++] = (linelen >> 8); | |
1286 | iobuf[c++] = (linelen & 0xff); | |
1287 | c += linelen; | |
1288 | } | |
1289 | else | |
1290 | break; | |
1291 | } | |
1292 | ||
1293 | while (MyConnect(server)) | |
1294 | { | |
1295 | linecount++; | |
1296 | ||
1297 | iobuf = MyRealloc(iobuf, (c + BUF_DATA_SIZE + 64)); | |
1298 | ||
1299 | /* store data in c+3 to allow for SLINKCMD_INJECT_RECVQ and len u16 */ | |
1300 | linelen = linebuf_get(&server->localClient->buf_sendq, | |
1301 | (char *) (iobuf + c + 3), READBUF_SIZE, | |
1302 | LINEBUF_PARTIAL, LINEBUF_PARSED); /* include partial lines */ | |
1303 | ||
1304 | if(linelen) | |
1305 | { | |
1306 | iobuf[c++] = SLINKCMD_INJECT_SENDQ; | |
1307 | iobuf[c++] = (linelen >> 8); | |
1308 | iobuf[c++] = (linelen & 0xff); | |
1309 | c += linelen; | |
1310 | } | |
1311 | else | |
1312 | break; | |
1313 | } | |
1314 | ||
1315 | /* start io */ | |
1316 | iobuf[c++] = SLINKCMD_INIT; | |
1317 | ||
1318 | server->localClient->slinkq = iobuf; | |
1319 | server->localClient->slinkq_ofs = 0; | |
1320 | server->localClient->slinkq_len = c; | |
1321 | ||
1322 | /* schedule a write */ | |
1323 | send_queued_slink_write(server->localClient->ctrlfd, server); | |
1324 | } | |
1325 | ||
1326 | /* | |
1327 | * fork_server | |
1328 | * | |
1329 | * inputs - struct Client *server | |
1330 | * output - success: 0 / failure: -1 | |
1331 | * side effect - fork, and exec SERVLINK to handle this connection | |
1332 | */ | |
1333 | static int | |
1334 | fork_server(struct Client *server) | |
1335 | { | |
1336 | int ret; | |
1337 | int i; | |
1338 | int ctrl_fds[2]; | |
1339 | int data_fds[2]; | |
1340 | ||
1341 | char fd_str[4][6]; | |
1342 | char *kid_argv[7]; | |
1343 | char slink[] = "-slink"; | |
1344 | ||
1345 | ||
1346 | /* ctrl */ | |
1347 | #ifdef HAVE_SOCKETPAIR | |
1348 | if(socketpair(AF_UNIX, SOCK_STREAM, 0, ctrl_fds) < 0) | |
1349 | #else | |
1350 | if(inet_socketpair(AF_INET,SOCK_STREAM, 0, ctrl_fds) < 0) | |
1351 | #endif | |
1352 | goto fork_error; | |
1353 | ||
1354 | ||
1355 | ||
1356 | /* data */ | |
1357 | #ifdef HAVE_SOCKETPAIR | |
1358 | if(socketpair(AF_UNIX, SOCK_STREAM, 0, data_fds) < 0) | |
1359 | #else | |
1360 | if(inet_socketpair(AF_INET,SOCK_STREAM, 0, data_fds) < 0) | |
1361 | #endif | |
1362 | goto fork_error; | |
1363 | ||
1364 | ||
1365 | #ifdef __CYGWIN__ | |
1366 | if((ret = vfork()) < 0) | |
1367 | #else | |
1368 | if((ret = fork()) < 0) | |
1369 | #endif | |
1370 | goto fork_error; | |
1371 | else if(ret == 0) | |
1372 | { | |
6fcb8629 AC |
1373 | int maxconn = comm_get_maxconnections(); |
1374 | ||
212380e3 | 1375 | /* set our fds as non blocking and close everything else */ |
6fcb8629 | 1376 | for (i = 0; i < maxconn; i++) |
212380e3 AC |
1377 | { |
1378 | ||
1379 | ||
1380 | if((i == ctrl_fds[1]) || (i == data_fds[1]) || (i == server->localClient->fd)) | |
1381 | { | |
1382 | comm_set_nb(i); | |
1383 | } | |
1384 | else | |
1385 | { | |
1386 | #ifdef __CYGWIN__ | |
1387 | if(i > 2) /* don't close std* */ | |
1388 | #endif | |
1389 | close(i); | |
1390 | } | |
1391 | } | |
1392 | ||
1393 | ircsnprintf(fd_str[0], sizeof(fd_str[0]), "%d", ctrl_fds[1]); | |
1394 | ircsnprintf(fd_str[1], sizeof(fd_str[1]), "%d", data_fds[1]); | |
1395 | ircsnprintf(fd_str[2], sizeof(fd_str[2]), "%d", server->localClient->fd); | |
1396 | kid_argv[0] = slink; | |
1397 | kid_argv[1] = fd_str[0]; | |
1398 | kid_argv[2] = fd_str[1]; | |
1399 | kid_argv[3] = fd_str[2]; | |
1400 | kid_argv[4] = NULL; | |
1401 | ||
1402 | /* exec servlink program */ | |
1403 | execv(ConfigFileEntry.servlink_path, kid_argv); | |
1404 | ||
1405 | /* We're still here, abort. */ | |
1406 | _exit(1); | |
1407 | } | |
1408 | else | |
1409 | { | |
1410 | comm_close(server->localClient->fd); | |
1411 | ||
1412 | /* close the childs end of the pipes */ | |
1413 | close(ctrl_fds[1]); | |
1414 | close(data_fds[1]); | |
1415 | ||
1416 | s_assert(server->localClient); | |
1417 | server->localClient->ctrlfd = ctrl_fds[0]; | |
1418 | server->localClient->fd = data_fds[0]; | |
1419 | ||
1420 | if(!comm_set_nb(server->localClient->fd)) | |
1421 | { | |
1422 | report_error(NONB_ERROR_MSG, | |
1423 | get_server_name(server, SHOW_IP), | |
1424 | log_client_name(server, SHOW_IP), | |
1425 | errno); | |
1426 | } | |
1427 | ||
1428 | if(!comm_set_nb(server->localClient->ctrlfd)) | |
1429 | { | |
1430 | report_error(NONB_ERROR_MSG, | |
1431 | get_server_name(server, SHOW_IP), | |
1432 | log_client_name(server, SHOW_IP), | |
1433 | errno); | |
1434 | } | |
1435 | ||
1436 | comm_open(server->localClient->ctrlfd, FD_SOCKET, NULL); | |
1437 | comm_open(server->localClient->fd, FD_SOCKET, NULL); | |
1438 | ||
1439 | read_ctrl_packet(server->localClient->ctrlfd, server); | |
1440 | read_packet(server->localClient->fd, server); | |
1441 | } | |
1442 | ||
1443 | return 0; | |
1444 | ||
1445 | fork_error: | |
1446 | /* this is ugly, but nicer than repeating | |
1447 | * about 50 close() statements everywhre... */ | |
1448 | close(data_fds[0]); | |
1449 | close(data_fds[1]); | |
1450 | close(ctrl_fds[0]); | |
1451 | close(ctrl_fds[1]); | |
1452 | return -1; | |
1453 | } | |
1454 | ||
1455 | /* | |
1456 | * New server connection code | |
1457 | * Based upon the stuff floating about in s_bsd.c | |
1458 | * -- adrian | |
1459 | */ | |
1460 | ||
1461 | /* | |
1462 | * serv_connect() - initiate a server connection | |
1463 | * | |
1464 | * inputs - pointer to conf | |
1465 | * - pointer to client doing the connet | |
1466 | * output - | |
1467 | * side effects - | |
1468 | * | |
1469 | * This code initiates a connection to a server. It first checks to make | |
1470 | * sure the given server exists. If this is the case, it creates a socket, | |
1471 | * creates a client, saves the socket information in the client, and | |
1472 | * initiates a connection to the server through comm_connect_tcp(). The | |
1473 | * completion of this goes through serv_completed_connection(). | |
1474 | * | |
1475 | * We return 1 if the connection is attempted, since we don't know whether | |
1476 | * it suceeded or not, and 0 if it fails in here somewhere. | |
1477 | */ | |
1478 | int | |
1479 | serv_connect(struct server_conf *server_p, struct Client *by) | |
1480 | { | |
1481 | struct Client *client_p; | |
1482 | struct irc_sockaddr_storage myipnum; | |
1483 | int fd; | |
1484 | ||
1485 | s_assert(server_p != NULL); | |
1486 | if(server_p == NULL) | |
1487 | return 0; | |
1488 | ||
1489 | /* | |
1490 | * Make sure this server isn't already connected | |
1491 | */ | |
1492 | if((client_p = find_server(NULL, server_p->name))) | |
1493 | { | |
1494 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
1495 | "Server %s already present from %s", | |
1496 | server_p->name, get_server_name(client_p, SHOW_IP)); | |
1497 | if(by && IsPerson(by) && !MyClient(by)) | |
1498 | sendto_one_notice(by, ":Server %s already present from %s", | |
1499 | server_p->name, get_server_name(client_p, SHOW_IP)); | |
1500 | return 0; | |
1501 | } | |
1502 | ||
1503 | /* create a socket for the server connection */ | |
1504 | if((fd = comm_socket(server_p->aftype, SOCK_STREAM, 0, NULL)) < 0) | |
1505 | { | |
1506 | /* Eek, failure to create the socket */ | |
1507 | report_error("opening stream socket to %s: %s", | |
1508 | server_p->name, server_p->name, errno); | |
1509 | return 0; | |
1510 | } | |
1511 | ||
1512 | /* servernames are always guaranteed under HOSTLEN chars */ | |
1513 | comm_note(fd, "Server: %s", server_p->name); | |
1514 | ||
1515 | /* Create a local client */ | |
1516 | client_p = make_client(NULL); | |
1517 | ||
1518 | /* Copy in the server, hostname, fd | |
1519 | * The sockhost may be a hostname, this will be corrected later | |
1520 | * -- jilles | |
1521 | */ | |
1522 | strlcpy(client_p->name, server_p->name, sizeof(client_p->name)); | |
1523 | strlcpy(client_p->host, server_p->host, sizeof(client_p->host)); | |
1524 | strlcpy(client_p->sockhost, server_p->host, sizeof(client_p->sockhost)); | |
1525 | client_p->localClient->fd = fd; | |
1526 | ||
1527 | /* | |
1528 | * Set up the initial server evilness, ripped straight from | |
1529 | * connect_server(), so don't blame me for it being evil. | |
1530 | * -- adrian | |
1531 | */ | |
1532 | ||
1533 | if(!comm_set_buffers(client_p->localClient->fd, READBUF_SIZE)) | |
1534 | { | |
1535 | report_error(SETBUF_ERROR_MSG, | |
1536 | get_server_name(client_p, SHOW_IP), | |
1537 | log_client_name(client_p, SHOW_IP), | |
1538 | errno); | |
1539 | } | |
1540 | ||
1541 | /* | |
1542 | * Attach config entries to client here rather than in | |
1543 | * serv_connect_callback(). This to avoid null pointer references. | |
1544 | */ | |
1545 | attach_server_conf(client_p, server_p); | |
1546 | ||
1547 | /* | |
1548 | * at this point we have a connection in progress and C/N lines | |
1549 | * attached to the client, the socket info should be saved in the | |
1550 | * client and it should either be resolved or have a valid address. | |
1551 | * | |
1552 | * The socket has been connected or connect is in progress. | |
1553 | */ | |
1554 | make_server(client_p); | |
1555 | if(by && IsPerson(by)) | |
1556 | { | |
1557 | strcpy(client_p->serv->by, by->name); | |
1558 | if(client_p->serv->user) | |
1559 | free_user(client_p->serv->user, NULL); | |
1560 | client_p->serv->user = by->user; | |
1561 | by->user->refcnt++; | |
1562 | } | |
1563 | else | |
1564 | { | |
1565 | strcpy(client_p->serv->by, "AutoConn."); | |
1566 | if(client_p->serv->user) | |
1567 | free_user(client_p->serv->user, NULL); | |
1568 | client_p->serv->user = NULL; | |
1569 | } | |
1570 | client_p->serv->up = me.name; | |
1571 | client_p->serv->upid = me.id; | |
1572 | SetConnecting(client_p); | |
1573 | dlinkAddTail(client_p, &client_p->node, &global_client_list); | |
1574 | ||
1575 | /* log */ | |
1576 | ilog(L_SERVER, "Connecting to %s[%s] port %d (%s)", server_p->name, server_p->host, server_p->port, | |
1577 | #ifdef IPV6 | |
1578 | server_p->aftype == AF_INET6 ? "IPv6" : | |
1579 | #endif | |
1580 | (server_p->aftype == AF_INET ? "IPv4" : "?")); | |
1581 | ||
1582 | if(ServerConfVhosted(server_p)) | |
1583 | { | |
1584 | memcpy(&myipnum, &server_p->my_ipnum, sizeof(myipnum)); | |
1585 | ((struct sockaddr_in *)&myipnum)->sin_port = 0; | |
1586 | myipnum.ss_family = server_p->aftype; | |
1587 | ||
1588 | } | |
1589 | else if(server_p->aftype == AF_INET && ServerInfo.specific_ipv4_vhost) | |
1590 | { | |
1591 | memcpy(&myipnum, &ServerInfo.ip, sizeof(myipnum)); | |
1592 | ((struct sockaddr_in *)&myipnum)->sin_port = 0; | |
1593 | myipnum.ss_family = AF_INET; | |
1594 | SET_SS_LEN(myipnum, sizeof(struct sockaddr_in)); | |
1595 | } | |
1596 | ||
1597 | #ifdef IPV6 | |
1598 | else if((server_p->aftype == AF_INET6) && ServerInfo.specific_ipv6_vhost) | |
1599 | { | |
1600 | memcpy(&myipnum, &ServerInfo.ip6, sizeof(myipnum)); | |
1601 | ((struct sockaddr_in6 *)&myipnum)->sin6_port = 0; | |
1602 | myipnum.ss_family = AF_INET6; | |
1603 | SET_SS_LEN(myipnum, sizeof(struct sockaddr_in6)); | |
1604 | } | |
1605 | #endif | |
1606 | else | |
1607 | { | |
1608 | comm_connect_tcp(client_p->localClient->fd, server_p->host, | |
1609 | server_p->port, NULL, 0, serv_connect_callback, | |
1610 | client_p, server_p->aftype, | |
1611 | ConfigFileEntry.connect_timeout); | |
1612 | return 1; | |
1613 | } | |
1614 | ||
1615 | comm_connect_tcp(client_p->localClient->fd, server_p->host, | |
1616 | server_p->port, (struct sockaddr *) &myipnum, | |
1617 | GET_SS_LEN(myipnum), serv_connect_callback, client_p, | |
1618 | myipnum.ss_family, ConfigFileEntry.connect_timeout); | |
1619 | ||
1620 | return 1; | |
1621 | } | |
1622 | ||
1623 | /* | |
1624 | * serv_connect_callback() - complete a server connection. | |
1625 | * | |
1626 | * This routine is called after the server connection attempt has | |
1627 | * completed. If unsucessful, an error is sent to ops and the client | |
1628 | * is closed. If sucessful, it goes through the initialisation/check | |
1629 | * procedures, the capabilities are sent, and the socket is then | |
1630 | * marked for reading. | |
1631 | */ | |
1632 | static void | |
1633 | serv_connect_callback(int fd, int status, void *data) | |
1634 | { | |
1635 | struct Client *client_p = data; | |
1636 | struct server_conf *server_p; | |
1637 | char *errstr; | |
c961476e | 1638 | fde_t *F = comm_locate_fd(fd); |
212380e3 AC |
1639 | |
1640 | /* First, make sure its a real client! */ | |
1641 | s_assert(client_p != NULL); | |
1642 | s_assert(client_p->localClient->fd == fd); | |
1643 | ||
1644 | if(client_p == NULL) | |
1645 | return; | |
1646 | ||
1647 | /* while we were waiting for the callback, its possible this already | |
1648 | * linked in.. --fl | |
1649 | */ | |
1650 | if(find_server(NULL, client_p->name) != NULL) | |
1651 | { | |
1652 | exit_client(client_p, client_p, &me, "Server Exists"); | |
1653 | return; | |
1654 | } | |
1655 | ||
1656 | /* Next, for backward purposes, record the ip of the server */ | |
c961476e | 1657 | memcpy(&client_p->localClient->ip, &F->connect.hostaddr, sizeof client_p->localClient->ip); |
212380e3 | 1658 | /* Set sockhost properly now -- jilles */ |
c961476e | 1659 | inetntop_sock((struct sockaddr *)&F->connect.hostaddr, |
212380e3 AC |
1660 | client_p->sockhost, sizeof client_p->sockhost); |
1661 | ||
1662 | /* Check the status */ | |
1663 | if(status != COMM_OK) | |
1664 | { | |
1665 | /* COMM_ERR_TIMEOUT wont have an errno associated with it, | |
1666 | * the others will.. --fl | |
1667 | */ | |
1668 | if(status == COMM_ERR_TIMEOUT) | |
1669 | { | |
1670 | sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL, | |
1671 | "Error connecting to %s[%s]: %s", | |
1672 | client_p->name, | |
1673 | #ifdef HIDE_SERVERS_IPS | |
1674 | "255.255.255.255", | |
1675 | #else | |
1676 | client_p->host, | |
1677 | #endif | |
1678 | comm_errstr(status)); | |
1679 | ilog(L_SERVER, "Error connecting to %s[%s]: %s", | |
1680 | client_p->name, client_p->sockhost, | |
1681 | comm_errstr(status)); | |
1682 | } | |
1683 | else | |
1684 | { | |
1685 | errstr = strerror(comm_get_sockerr(fd)); | |
1686 | sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL, | |
1687 | "Error connecting to %s[%s]: %s (%s)", | |
1688 | client_p->name, | |
1689 | #ifdef HIDE_SERVERS_IPS | |
1690 | "255.255.255.255", | |
1691 | #else | |
1692 | client_p->host, | |
1693 | #endif | |
1694 | comm_errstr(status), errstr); | |
1695 | ilog(L_SERVER, "Error connecting to %s[%s]: %s (%s)", | |
1696 | client_p->name, client_p->sockhost, | |
1697 | comm_errstr(status), errstr); | |
1698 | } | |
1699 | ||
1700 | exit_client(client_p, client_p, &me, comm_errstr(status)); | |
1701 | return; | |
1702 | } | |
1703 | ||
1704 | /* COMM_OK, so continue the connection procedure */ | |
1705 | /* Get the C/N lines */ | |
1706 | if((server_p = client_p->localClient->att_sconf) == NULL) | |
1707 | { | |
1708 | sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL, "Lost connect{} block for %s", | |
1709 | get_server_name(client_p, HIDE_IP)); | |
1710 | exit_client(client_p, client_p, &me, "Lost connect{} block"); | |
1711 | return; | |
1712 | } | |
1713 | ||
1714 | /* Next, send the initial handshake */ | |
1715 | SetHandshake(client_p); | |
1716 | ||
1717 | /* kludge, if we're not using TS6, dont ever send | |
1718 | * ourselves as being TS6 capable. | |
1719 | */ | |
1720 | if(!EmptyString(server_p->spasswd)) | |
1721 | { | |
1722 | if(ServerInfo.use_ts6) | |
1723 | sendto_one(client_p, "PASS %s TS %d :%s", | |
1724 | server_p->spasswd, TS_CURRENT, me.id); | |
1725 | else | |
1726 | sendto_one(client_p, "PASS %s :TS", | |
1727 | server_p->spasswd); | |
1728 | } | |
1729 | ||
1730 | /* pass my info to the new server */ | |
1731 | send_capabilities(client_p, default_server_capabs | |
1732 | | (ServerConfCompressed(server_p) ? CAP_ZIP_SUPPORTED : 0) | |
1733 | | (ServerConfTb(server_p) ? CAP_TB : 0)); | |
1734 | ||
1735 | sendto_one(client_p, "SERVER %s 1 :%s%s", | |
1736 | me.name, | |
1737 | ConfigServerHide.hidden ? "(H) " : "", me.info); | |
1738 | ||
1739 | /* | |
1740 | * If we've been marked dead because a send failed, just exit | |
1741 | * here now and save everyone the trouble of us ever existing. | |
1742 | */ | |
1743 | if(IsAnyDead(client_p)) | |
1744 | { | |
1745 | sendto_realops_snomask(SNO_GENERAL, is_remote_connect(client_p) ? L_NETWIDE : L_ALL, | |
1746 | "%s went dead during handshake", client_p->name); | |
1747 | exit_client(client_p, client_p, &me, "Went dead during handshake"); | |
1748 | return; | |
1749 | } | |
1750 | ||
1751 | /* don't move to serv_list yet -- we haven't sent a burst! */ | |
1752 | ||
1753 | /* If we get here, we're ok, so lets start reading some data */ | |
1754 | read_packet(fd, client_p); | |
1755 | } | |
1756 | ||
1757 | #ifndef HAVE_SOCKETPAIR | |
1758 | static int | |
1759 | inet_socketpair(int d, int type, int protocol, int sv[2]) | |
1760 | { | |
1761 | struct sockaddr_in addr1, addr2, addr3; | |
1762 | int addr3_len = sizeof(addr3); | |
1763 | int fd, rc; | |
1764 | int port_no = 20000; | |
1765 | ||
1766 | if(d != AF_INET || type != SOCK_STREAM || protocol) | |
1767 | { | |
1768 | errno = EAFNOSUPPORT; | |
1769 | return -1; | |
1770 | } | |
1771 | if(((sv[0] = socket(AF_INET, SOCK_STREAM, 0)) < 0) || ((sv[1] = socket(AF_INET, SOCK_STREAM, 0)) < 0)) | |
1772 | return -1; | |
1773 | ||
1774 | addr1.sin_port = htons(port_no); | |
1775 | addr1.sin_family = AF_INET; | |
1776 | addr1.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
1777 | while ((rc = bind (sv[0], (struct sockaddr *) &addr1, sizeof (addr1))) < 0 && errno == EADDRINUSE) | |
1778 | addr1.sin_port = htons(++port_no); | |
1779 | ||
1780 | if(rc < 0) | |
1781 | return -1; | |
1782 | ||
1783 | if(listen(sv[0], 1) < 0) | |
1784 | { | |
1785 | close(sv[0]); | |
1786 | close(sv[1]); | |
1787 | return -1; | |
1788 | } | |
1789 | ||
1790 | addr2.sin_port = htons(port_no); | |
1791 | addr2.sin_family = AF_INET; | |
1792 | addr2.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
1793 | if(connect (sv[1], (struct sockaddr *) &addr2, sizeof (addr2)) < 0) | |
1794 | { | |
1795 | close(sv[0]); | |
1796 | close(sv[1]); | |
1797 | return -1; | |
1798 | } | |
1799 | ||
1800 | if((fd = accept(sv[1], (struct sockaddr *) &addr3, &addr3_len)) < 0) | |
1801 | { | |
1802 | close(sv[0]); | |
1803 | close(sv[1]); | |
1804 | return -1; | |
1805 | } | |
1806 | close(sv[0]); | |
1807 | sv[0] = fd; | |
1808 | ||
1809 | return(0); | |
1810 | ||
1811 | } | |
1812 | #endif |