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