2 * charybdis: an advanced ircd.
3 * client.c: Controls clients.
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 * Copyright (C) 2007 William Pitcock
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 * $Id: client.c 3514 2007-06-06 16:25:21Z nenolod $
34 #include "irc_string.h"
35 #include "sprintf_irc.h"
42 #include "s_newconf.h"
55 #include "blacklist.h"
58 #include "irc_dictionary.h"
60 #define DEBUG_EXITED_CLIENTS
62 static void check_pings_list(rb_dlink_list
* list
);
63 static void check_unknowns_list(rb_dlink_list
* list
);
64 static void free_exited_clients(void *unused
);
65 static void exit_aborted_clients(void *unused
);
67 static int exit_remote_client(struct Client
*, struct Client
*, struct Client
*,const char *);
68 static int exit_remote_server(struct Client
*, struct Client
*, struct Client
*,const char *);
69 static int exit_local_client(struct Client
*, struct Client
*, struct Client
*,const char *);
70 static int exit_unknown_client(struct Client
*, struct Client
*, struct Client
*,const char *);
71 static int exit_local_server(struct Client
*, struct Client
*, struct Client
*,const char *);
72 static int qs_server(struct Client
*, struct Client
*, struct Client
*, const char *comment
);
74 static EVH check_pings
;
76 extern rb_bh
*client_heap
;
77 extern rb_bh
*lclient_heap
;
78 extern rb_bh
*pclient_heap
;
79 static rb_bh
*away_heap
= NULL
;
81 extern char current_uid
[IDLEN
];
83 struct Dictionary
*nd_dict
= NULL
;
92 rb_dlink_list dead_list
;
93 #ifdef DEBUG_EXITED_CLIENTS
94 static rb_dlink_list dead_remote_list
;
100 struct Client
*client
;
101 char notice
[REASONLEN
];
104 static rb_dlink_list abort_list
;
112 * side effects - initialize client free memory
118 * start off the check ping event .. -- adrian
119 * Every 30 seconds is plenty -- db
121 client_heap
= rb_bh_create(sizeof(struct Client
), CLIENT_HEAP_SIZE
, "client_heap");
122 lclient_heap
= rb_bh_create(sizeof(struct LocalUser
), LCLIENT_HEAP_SIZE
, "lclient_heap");
123 pclient_heap
= rb_bh_create(sizeof(struct PreClient
), PCLIENT_HEAP_SIZE
, "pclient_heap");
124 away_heap
= rb_bh_create(AWAYLEN
, AWAY_HEAP_SIZE
, "away_heap");
126 rb_event_addish("check_pings", check_pings
, NULL
, 30);
127 rb_event_addish("free_exited_clients", &free_exited_clients
, NULL
, 4);
128 rb_event_addish("exit_aborted_clients", exit_aborted_clients
, NULL
, 1);
129 rb_event_add("flood_recalc", flood_recalc
, NULL
, 1);
131 nd_dict
= irc_dictionary_create(irccmp
);
136 * make_client - create a new Client struct and set it to initial state.
138 * from == NULL, create local client (a client connected
141 * from, create remote client (behind a socket
142 * associated with the client defined by
143 * 'from'). ('from' is a local client!!).
146 make_client(struct Client
*from
)
148 struct Client
*client_p
= NULL
;
149 struct LocalUser
*localClient
;
151 client_p
= rb_bh_alloc(client_heap
);
155 client_p
->from
= client_p
; /* 'from' of local client is self! */
157 localClient
= (struct LocalUser
*) rb_bh_alloc(lclient_heap
);
158 SetMyConnect(client_p
);
159 client_p
->localClient
= localClient
;
161 client_p
->localClient
->lasttime
= client_p
->localClient
->firsttime
= rb_current_time();
163 client_p
->localClient
->F
= NULL
;
164 client_p
->localClient
->ctrlF
= NULL
;
166 client_p
->preClient
= (struct PreClient
*) rb_bh_alloc(pclient_heap
);
168 /* as good a place as any... */
169 rb_dlinkAdd(client_p
, &client_p
->localClient
->tnode
, &unknown_list
);
172 { /* from is not NULL */
173 client_p
->localClient
= NULL
;
174 client_p
->preClient
= NULL
;
175 client_p
->from
= from
; /* 'from' of local client is self! */
178 SetUnknown(client_p
);
179 strcpy(client_p
->username
, "unknown");
185 free_pre_client(struct Client
*client_p
)
187 struct Blacklist
*blptr
;
189 s_assert(NULL
!= client_p
);
191 if(client_p
->preClient
== NULL
)
194 blptr
= client_p
->preClient
->dnsbl_listed
;
196 unref_blacklist(blptr
);
197 abort_blacklist_queries(client_p
);
198 rb_bh_free(pclient_heap
, client_p
->preClient
);
199 client_p
->preClient
= NULL
;
203 free_local_client(struct Client
*client_p
)
205 s_assert(NULL
!= client_p
);
206 s_assert(&me
!= client_p
);
208 if(client_p
->localClient
== NULL
)
212 * clean up extra sockets from P-lines which have been discarded.
214 if(client_p
->localClient
->listener
)
216 s_assert(0 < client_p
->localClient
->listener
->ref_count
);
217 if(0 == --client_p
->localClient
->listener
->ref_count
218 && !client_p
->localClient
->listener
->active
)
219 free_listener(client_p
->localClient
->listener
);
220 client_p
->localClient
->listener
= 0;
223 if(client_p
->localClient
->F
)
224 rb_close(client_p
->localClient
->F
);
226 if(client_p
->localClient
->passwd
)
228 memset(client_p
->localClient
->passwd
, 0,
229 strlen(client_p
->localClient
->passwd
));
230 rb_free(client_p
->localClient
->passwd
);
233 rb_free(client_p
->localClient
->challenge
);
234 rb_free(client_p
->localClient
->fullcaps
);
235 rb_free(client_p
->localClient
->opername
);
236 rb_free(client_p
->localClient
->mangledhost
);
238 rb_bh_free(lclient_heap
, client_p
->localClient
);
239 client_p
->localClient
= NULL
;
243 free_client(struct Client
*client_p
)
245 s_assert(NULL
!= client_p
);
246 s_assert(&me
!= client_p
);
247 free_local_client(client_p
);
248 free_pre_client(client_p
);
249 rb_bh_free(client_heap
, client_p
);
253 * check_pings - go through the local client list and check activity
254 * kill off stuff that should die
256 * inputs - NOT USED (from event)
257 * output - next time_t when check_pings() should be called again
261 * A PING can be sent to clients as necessary.
263 * Client/Server ping outs are handled.
267 * Addon from adrian. We used to call this after nextping seconds,
268 * however I've changed it to run once a second. This is only for
269 * PING timeouts, not K/etc-line checks (thanks dianora!). Having it
270 * run once a second makes life a lot easier - when a new client connects
271 * and they need a ping in 4 seconds, if nextping was set to 20 seconds
272 * we end up waiting 20 seconds. This is stupid. :-)
273 * I will optimise (hah!) check_pings() once I've finished working on
274 * tidying up other network IO evilnesses.
279 check_pings(void *notused
)
281 check_pings_list(&lclient_list
);
282 check_pings_list(&serv_list
);
283 check_unknowns_list(&unknown_list
);
289 * inputs - pointer to list to check
294 check_pings_list(rb_dlink_list
* list
)
296 char scratch
[32]; /* way too generous but... */
297 struct Client
*client_p
; /* current local client_p being examined */
298 int ping
= 0; /* ping time value from client */
299 rb_dlink_node
*ptr
, *next_ptr
;
301 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
303 client_p
= ptr
->data
;
305 if(!MyConnect(client_p
) || IsDead(client_p
))
308 ping
= get_client_ping(client_p
);
310 if(ping
< (rb_current_time() - client_p
->localClient
->lasttime
))
313 * If the client/server hasnt talked to us in 2*ping seconds
314 * and it has a ping time, then close its connection.
316 if(((rb_current_time() - client_p
->localClient
->lasttime
) >= (2 * ping
)
317 && (client_p
->flags
& FLAGS_PINGSENT
)))
319 if(IsServer(client_p
))
321 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
322 "No response from %s, closing link",
323 get_server_name(client_p
, HIDE_IP
));
325 "No response from %s, closing link",
326 log_client_name(client_p
, HIDE_IP
));
328 (void) rb_snprintf(scratch
, sizeof(scratch
),
329 "Ping timeout: %d seconds",
330 (int) (rb_current_time() - client_p
->localClient
->lasttime
));
332 exit_client(client_p
, client_p
, &me
, scratch
);
335 else if((client_p
->flags
& FLAGS_PINGSENT
) == 0)
338 * if we havent PINGed the connection and we havent
339 * heard from it in a while, PING it to make sure
342 client_p
->flags
|= FLAGS_PINGSENT
;
343 /* not nice but does the job */
344 client_p
->localClient
->lasttime
= rb_current_time() - ping
;
345 sendto_one(client_p
, "PING :%s", me
.name
);
354 * check_unknowns_list
356 * inputs - pointer to list of unknown clients
358 * side effects - unknown clients get marked for termination after n seconds
361 check_unknowns_list(rb_dlink_list
* list
)
363 rb_dlink_node
*ptr
, *next_ptr
;
364 struct Client
*client_p
;
367 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
369 client_p
= ptr
->data
;
371 if(IsDead(client_p
) || IsClosing(client_p
))
375 * Check UNKNOWN connections - if they have been in this state
376 * for > 30s, close them.
379 timeout
= IsAnyServer(client_p
) ? ConfigFileEntry
.connect_timeout
: 30;
380 if((rb_current_time() - client_p
->localClient
->firsttime
) > timeout
)
382 if(IsAnyServer(client_p
))
384 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
385 "No response from %s, closing link",
386 get_server_name(client_p
, HIDE_IP
));
388 "No response from %s, closing link",
389 log_client_name(client_p
, HIDE_IP
));
391 exit_client(client_p
, client_p
, &me
, "Connection timed out");
397 notify_banned_client(struct Client
*client_p
, struct ConfItem
*aconf
, int ban
)
399 static const char conn_closed
[] = "Connection closed";
400 static const char d_lined
[] = "D-lined";
401 static const char k_lined
[] = "K-lined";
402 static const char g_lined
[] = "G-lined";
403 const char *reason
= NULL
;
404 const char *exit_reason
= conn_closed
;
406 if(ConfigFileEntry
.kline_with_reason
&& !EmptyString(aconf
->passwd
))
408 reason
= aconf
->passwd
;
409 exit_reason
= aconf
->passwd
;
413 switch (aconf
->status
)
427 if(ban
== D_LINED
&& !IsPerson(client_p
))
428 sendto_one(client_p
, "NOTICE DLINE :*** You have been D-lined");
430 sendto_one(client_p
, form_str(ERR_YOUREBANNEDCREEP
),
431 me
.name
, client_p
->name
, reason
);
433 exit_client(client_p
, client_p
, &me
,
434 EmptyString(ConfigFileEntry
.kline_reason
) ? exit_reason
:
435 ConfigFileEntry
.kline_reason
);
442 * side effects - Check all connections for a pending k/d/gline against the
443 * client, exit the client if found.
446 check_banned_lines(void)
448 struct Client
*client_p
; /* current local client_p being examined */
449 struct ConfItem
*aconf
= NULL
;
450 rb_dlink_node
*ptr
, *next_ptr
;
452 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
454 client_p
= ptr
->data
;
459 /* if there is a returned struct ConfItem then kill it */
460 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
, client_p
->localClient
->ip
.ss_family
)))
462 if(aconf
->status
& CONF_EXEMPTDLINE
)
465 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
466 "DLINE active for %s",
467 get_client_name(client_p
, HIDE_IP
));
469 notify_banned_client(client_p
, aconf
, D_LINED
);
470 continue; /* and go examine next fd/client_p */
473 if(!IsPerson(client_p
))
476 if((aconf
= find_kline(client_p
)) != NULL
)
478 if(IsExemptKline(client_p
))
480 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
481 "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
482 get_client_name(client_p
, HIDE_IP
),
483 aconf
->user
, aconf
->host
);
487 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
488 "KLINE active for %s",
489 get_client_name(client_p
, HIDE_IP
));
490 notify_banned_client(client_p
, aconf
, K_LINED
);
493 else if((aconf
= find_gline(client_p
)) != NULL
)
495 if(IsExemptKline(client_p
))
497 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
498 "GLINE over-ruled for %s, client is kline_exempt [%s@%s]",
499 get_client_name(client_p
, HIDE_IP
),
500 aconf
->user
, aconf
->host
);
504 if(IsExemptGline(client_p
))
506 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
507 "GLINE over-ruled for %s, client is gline_exempt [%s@%s]",
508 get_client_name(client_p
, HIDE_IP
),
509 aconf
->user
, aconf
->host
);
513 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
514 "GLINE active for %s",
515 get_client_name(client_p
, HIDE_IP
));
517 notify_banned_client(client_p
, aconf
, G_LINED
);
520 else if((aconf
= find_xline(client_p
->info
, 1)) != NULL
)
522 if(IsExemptKline(client_p
))
524 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
525 "XLINE over-ruled for %s, client is kline_exempt [%s]",
526 get_client_name(client_p
, HIDE_IP
),
531 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "XLINE active for %s",
532 get_client_name(client_p
, HIDE_IP
));
534 (void) exit_client(client_p
, client_p
, &me
, "Bad user info");
539 /* also check the unknowns list for new dlines */
540 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, unknown_list
.head
)
542 client_p
= ptr
->data
;
544 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
,client_p
->localClient
->ip
.ss_family
)))
546 if(aconf
->status
& CONF_EXEMPTDLINE
)
549 notify_banned_client(client_p
, aconf
, D_LINED
);
555 /* check_klines_event()
559 * side effects - check_klines() is called, kline_queued unset
562 check_klines_event(void *unused
)
572 * side effects - all clients will be checked for klines
577 struct Client
*client_p
;
578 struct ConfItem
*aconf
;
580 rb_dlink_node
*next_ptr
;
582 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
584 client_p
= ptr
->data
;
586 if(IsMe(client_p
) || !IsPerson(client_p
))
589 if((aconf
= find_kline(client_p
)) != NULL
)
591 if(IsExemptKline(client_p
))
593 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
594 "KLINE over-ruled for %s, client is kline_exempt",
595 get_client_name(client_p
, HIDE_IP
));
599 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
600 "KLINE active for %s",
601 get_client_name(client_p
, HIDE_IP
));
603 notify_banned_client(client_p
, aconf
, K_LINED
);
613 * side effects - all clients will be checked for glines
618 struct Client
*client_p
;
619 struct ConfItem
*aconf
;
621 rb_dlink_node
*next_ptr
;
623 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
625 client_p
= ptr
->data
;
627 if(IsMe(client_p
) || !IsPerson(client_p
))
630 if((aconf
= find_gline(client_p
)) != NULL
)
632 if(IsExemptKline(client_p
))
634 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
635 "GLINE over-ruled for %s, client is kline_exempt",
636 get_client_name(client_p
, HIDE_IP
));
640 if(IsExemptGline(client_p
))
642 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
643 "GLINE over-ruled for %s, client is gline_exempt",
644 get_client_name(client_p
, HIDE_IP
));
648 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
649 "GLINE active for %s",
650 get_client_name(client_p
, HIDE_IP
));
652 notify_banned_client(client_p
, aconf
, K_LINED
);
662 * side effects - all clients will be checked for dlines
667 struct Client
*client_p
;
668 struct ConfItem
*aconf
;
670 rb_dlink_node
*next_ptr
;
672 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
674 client_p
= ptr
->data
;
679 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
,client_p
->localClient
->ip
.ss_family
)) != NULL
)
681 if(aconf
->status
& CONF_EXEMPTDLINE
)
684 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
685 "DLINE active for %s",
686 get_client_name(client_p
, HIDE_IP
));
688 notify_banned_client(client_p
, aconf
, D_LINED
);
693 /* dlines need to be checked against unknowns too */
694 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, unknown_list
.head
)
696 client_p
= ptr
->data
;
698 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
,client_p
->localClient
->ip
.ss_family
)) != NULL
)
700 if(aconf
->status
& CONF_EXEMPTDLINE
)
703 notify_banned_client(client_p
, aconf
, D_LINED
);
712 * side effects - all clients will be checked for xlines
717 struct Client
*client_p
;
718 struct ConfItem
*aconf
;
720 rb_dlink_node
*next_ptr
;
722 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
724 client_p
= ptr
->data
;
726 if(IsMe(client_p
) || !IsPerson(client_p
))
729 if((aconf
= find_xline(client_p
->info
, 1)) != NULL
)
731 if(IsExemptKline(client_p
))
733 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
734 "XLINE over-ruled for %s, client is kline_exempt",
735 get_client_name(client_p
, HIDE_IP
));
739 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "XLINE active for %s",
740 get_client_name(client_p
, HIDE_IP
));
742 (void) exit_client(client_p
, client_p
, &me
, "Bad user info");
749 * update_client_exit_stats
751 * input - pointer to client
756 update_client_exit_stats(struct Client
*client_p
)
758 if(IsServer(client_p
))
760 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
761 "Server %s split from %s",
762 client_p
->name
, client_p
->servptr
->name
);
763 if(HasSentEob(client_p
))
766 else if(IsClient(client_p
))
771 if(IsInvisible(client_p
))
775 if(splitchecking
&& !splitmode
)
776 check_splitmode(NULL
);
780 * release_client_state
782 * input - pointer to client to release
787 release_client_state(struct Client
*client_p
)
789 if(client_p
->user
!= NULL
)
791 free_user(client_p
->user
, client_p
); /* try this here */
795 if(client_p
->serv
->user
!= NULL
)
796 free_user(client_p
->serv
->user
, client_p
);
797 if(client_p
->serv
->fullcaps
)
798 rb_free(client_p
->serv
->fullcaps
);
799 rb_free(client_p
->serv
);
804 * remove_client_from_list
805 * inputs - point to client to remove
807 * side effects - taken the code from ExitOneClient() for this
808 * and placed it here. - avalon
811 remove_client_from_list(struct Client
*client_p
)
813 s_assert(NULL
!= client_p
);
818 /* A client made with make_client()
819 * is on the unknown_list until removed.
820 * If it =does= happen to exit before its removed from that list
821 * and its =not= on the global_client_list, it will core here.
822 * short circuit that case now -db
824 if(client_p
->node
.prev
== NULL
&& client_p
->node
.next
== NULL
)
827 rb_dlinkDelete(&client_p
->node
, &global_client_list
);
829 update_client_exit_stats(client_p
);
834 * find_person - find person by (nick)name.
835 * inputs - pointer to name
836 * output - return client pointer
840 find_person(const char *name
)
842 struct Client
*c2ptr
;
844 c2ptr
= find_client(name
);
846 if(c2ptr
&& IsPerson(c2ptr
))
852 find_named_person(const char *name
)
854 struct Client
*c2ptr
;
856 c2ptr
= find_named_client(name
);
858 if(c2ptr
&& IsPerson(c2ptr
))
865 * find_chasing - find the client structure for a nick name (user)
866 * using history mechanism if necessary. If the client is not found,
867 * an error message (NO SUCH NICK) is generated. If the client was found
868 * through the history, chasing will be 1 and otherwise 0.
871 find_chasing(struct Client
*source_p
, const char *user
, int *chasing
)
875 if(MyClient(source_p
))
876 who
= find_named_person(user
);
878 who
= find_person(user
);
883 if(who
|| IsDigit(*user
))
886 if(!(who
= get_history(user
, (long) KILLCHASETIMELIMIT
)))
888 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
,
889 form_str(ERR_NOSUCHNICK
), user
);
898 * get_client_name - Return the name of the client
899 * for various tracking and
900 * admin purposes. The main purpose of this function is to
901 * return the "socket host" name of the client, if that
902 * differs from the advertised name (other than case).
903 * But, this can be used to any client structure.
906 * Watch out the allocation of "nbuf", if either source_p->name
907 * or source_p->sockhost gets changed into pointers instead of
908 * directly allocated within the structure...
911 * Function return either a pointer to the structure (source_p) or
912 * to internal buffer (nbuf). *NEVER* use the returned pointer
913 * to modify what it points!!!
917 get_client_name(struct Client
*client
, int showip
)
919 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
921 s_assert(NULL
!= client
);
925 if(MyConnect(client
))
927 if(!irccmp(client
->name
, client
->host
))
930 if(ConfigFileEntry
.hide_spoof_ips
&&
931 showip
== SHOW_IP
&& IsIPSpoof(client
))
933 #ifdef HIDE_SERVERS_IPS
934 if(IsAnyServer(client
))
938 /* And finally, let's get the host information, ip or name */
942 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
943 client
->name
, client
->username
,
947 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@255.255.255.255]",
948 client
->name
, client
->username
);
951 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
952 client
->name
, client
->username
, client
->host
);
957 /* As pointed out by Adel Mezibra
958 * Neph|l|m@EFnet. Was missing a return here.
964 get_server_name(struct Client
*target_p
, int showip
)
966 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
971 if(!MyConnect(target_p
) || !irccmp(target_p
->name
, target_p
->host
))
972 return target_p
->name
;
974 #ifdef HIDE_SERVERS_IPS
975 if(EmptyString(target_p
->name
))
977 rb_snprintf(nbuf
, sizeof(nbuf
), "[%s@255.255.255.255]",
982 return target_p
->name
;
988 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
989 target_p
->name
, target_p
->username
,
994 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@255.255.255.255]",
995 target_p
->name
, target_p
->username
);
998 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
999 target_p
->name
, target_p
->username
,
1006 /* log_client_name()
1008 * This version is the same as get_client_name, but doesnt contain the
1009 * code that will hide IPs always. This should be used for logfiles.
1012 log_client_name(struct Client
*target_p
, int showip
)
1014 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
1016 if(target_p
== NULL
)
1019 if(MyConnect(target_p
))
1021 if(irccmp(target_p
->name
, target_p
->host
) == 0)
1022 return target_p
->name
;
1027 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]", target_p
->name
,
1028 target_p
->username
, target_p
->sockhost
);
1032 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@255.255.255.255]",
1033 target_p
->name
, target_p
->username
);
1036 rb_snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]", target_p
->name
,
1037 target_p
->username
, target_p
->host
);
1043 return target_p
->name
;
1046 /* is_remote_connect - Returns whether a server was /connect'ed by a remote
1047 * oper (send notices netwide) */
1049 is_remote_connect(struct Client
*client_p
)
1051 struct Client
*oper
;
1053 if (client_p
->serv
== NULL
)
1055 oper
= find_named_person(client_p
->serv
->by
);
1056 return oper
!= NULL
&& IsOper(oper
) && !MyConnect(oper
);
1060 free_exited_clients(void *unused
)
1062 rb_dlink_node
*ptr
, *next
;
1063 struct Client
*target_p
;
1065 RB_DLINK_FOREACH_SAFE(ptr
, next
, dead_list
.head
)
1067 target_p
= ptr
->data
;
1069 #ifdef DEBUG_EXITED_CLIENTS
1071 struct abort_client
*abt
;
1072 rb_dlink_node
*aptr
;
1075 RB_DLINK_FOREACH(aptr
, abort_list
.head
)
1078 if(abt
->client
== target_p
)
1081 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1082 "On abort_list: %s stat: %u flags: %u/%u handler: %c",
1083 target_p
->name
, (unsigned int) target_p
->status
,
1084 target_p
->flags
, target_p
->flags2
, target_p
->handler
);
1085 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1086 "Please report this to the charybdis developers!");
1093 rb_dlinkDestroy(ptr
, &dead_list
);
1099 if(ptr
->data
== NULL
)
1101 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1102 "Warning: null client on dead_list!");
1103 rb_dlinkDestroy(ptr
, &dead_list
);
1106 release_client_state(target_p
);
1107 free_client(target_p
);
1108 rb_dlinkDestroy(ptr
, &dead_list
);
1111 #ifdef DEBUG_EXITED_CLIENTS
1112 RB_DLINK_FOREACH_SAFE(ptr
, next
, dead_remote_list
.head
)
1114 target_p
= ptr
->data
;
1116 if(ptr
->data
== NULL
)
1118 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1119 "Warning: null client on dead_list!");
1120 rb_dlinkDestroy(ptr
, &dead_list
);
1123 release_client_state(target_p
);
1124 free_client(target_p
);
1125 rb_dlinkDestroy(ptr
, &dead_remote_list
);
1132 ** Recursively send QUITs and SQUITs for source_p and all its dependent clients
1133 ** and servers to those servers that need them. A server needs the client
1134 ** QUITs if it can't figure them out from the SQUIT (ie pre-TS4) or if it
1135 ** isn't getting the SQUIT because of @#(*&@)# hostmasking. With TS4, once
1136 ** a link gets a SQUIT, it doesn't need any QUIT/SQUITs for clients depending
1137 ** on that one -orabidoo
1140 recurse_send_quits(struct Client
*client_p
, struct Client
*source_p
,
1141 struct Client
*to
, const char *comment1
,
1142 const char *comment
)
1144 struct Client
*target_p
;
1145 rb_dlink_node
*ptr
, *ptr_next
;
1146 /* If this server can handle quit storm (QS) removal
1147 * of dependents, just send the SQUIT
1150 if(IsCapable(to
, CAP_QS
))
1152 sendto_one(to
, "SQUIT %s :%s",
1153 get_id(source_p
, to
), comment
);
1157 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1159 target_p
= ptr
->data
;
1160 sendto_one(to
, ":%s QUIT :%s", target_p
->name
, comment1
);
1162 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->servers
.head
)
1164 target_p
= ptr
->data
;
1165 recurse_send_quits(client_p
, target_p
, to
, comment1
, comment
);
1167 sendto_one(to
, "SQUIT %s :%s", source_p
->name
, comment
);
1172 ** Remove all clients that depend on source_p; assumes all (S)QUITs have
1173 ** already been sent. we make sure to exit a server's dependent clients
1174 ** and servers before the server itself; exit_one_client takes care of
1175 ** actually removing things off llists. tweaked from +CSr31 -orabidoo
1178 * added sanity test code.... source_p->serv might be NULL...
1181 recurse_remove_clients(struct Client
*source_p
, const char *comment
)
1183 struct Client
*target_p
;
1184 rb_dlink_node
*ptr
, *ptr_next
;
1189 if(source_p
->serv
== NULL
) /* oooops. uh this is actually a major bug */
1192 /* this is very ugly, but it saves cpu :P */
1193 if(ConfigFileEntry
.nick_delay
> 0)
1195 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1197 target_p
= ptr
->data
;
1198 target_p
->flags
|= FLAGS_KILLED
;
1199 add_nd_entry(target_p
->name
);
1201 if(!IsDead(target_p
) && !IsClosing(target_p
))
1202 exit_remote_client(NULL
, target_p
, &me
, comment
);
1207 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1209 target_p
= ptr
->data
;
1210 target_p
->flags
|= FLAGS_KILLED
;
1212 if(!IsDead(target_p
) && !IsClosing(target_p
))
1213 exit_remote_client(NULL
, target_p
, &me
, comment
);
1217 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->servers
.head
)
1219 target_p
= ptr
->data
;
1220 recurse_remove_clients(target_p
, comment
);
1221 qs_server(NULL
, target_p
, &me
, comment
);
1226 ** Remove *everything* that depends on source_p, from all lists, and sending
1227 ** all necessary QUITs and SQUITs. source_p itself is still on the lists,
1228 ** and its SQUITs have been sent except for the upstream one -orabidoo
1231 remove_dependents(struct Client
*client_p
,
1232 struct Client
*source_p
,
1233 struct Client
*from
, const char *comment
, const char *comment1
)
1236 rb_dlink_node
*ptr
, *next
;
1238 RB_DLINK_FOREACH_SAFE(ptr
, next
, serv_list
.head
)
1242 if(IsMe(to
) || to
== source_p
->from
||
1243 (to
== client_p
&& IsCapable(to
, CAP_QS
)))
1246 recurse_send_quits(client_p
, source_p
, to
, comment1
, comment
);
1249 recurse_remove_clients(source_p
, comment1
);
1253 exit_aborted_clients(void *unused
)
1255 struct abort_client
*abt
;
1256 rb_dlink_node
*ptr
, *next
;
1257 RB_DLINK_FOREACH_SAFE(ptr
, next
, abort_list
.head
)
1261 #ifdef DEBUG_EXITED_CLIENTS
1263 if(rb_dlinkFind(abt
->client
, &dead_list
))
1266 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1267 "On dead_list: %s stat: %u flags: %u/%u handler: %c",
1268 abt
->client
->name
, (unsigned int) abt
->client
->status
,
1269 abt
->client
->flags
, abt
->client
->flags2
, abt
->client
->handler
);
1270 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1271 "Please report this to the charybdis developers!");
1277 s_assert(*((unsigned long*)abt
->client
) != 0xdeadbeef); /* This is lame but its a debug thing */
1278 rb_dlinkDelete(ptr
, &abort_list
);
1280 if(IsAnyServer(abt
->client
))
1281 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1282 "Closing link to %s: %s",
1283 get_server_name(abt
->client
, HIDE_IP
), abt
->notice
);
1285 /* its no longer on abort list - we *must* remove
1286 * FLAGS_CLOSING otherwise exit_client() will not run --fl
1288 abt
->client
->flags
&= ~FLAGS_CLOSING
;
1289 exit_client(abt
->client
, abt
->client
, &me
, abt
->notice
);
1296 * dead_link - Adds client to a list of clients that need an exit_client()
1300 dead_link(struct Client
*client_p
)
1302 struct abort_client
*abt
;
1304 s_assert(!IsMe(client_p
));
1305 if(IsDead(client_p
) || IsClosing(client_p
) || IsMe(client_p
))
1308 abt
= (struct abort_client
*) rb_malloc(sizeof(struct abort_client
));
1310 if(client_p
->flags
& FLAGS_SENDQEX
)
1311 strlcpy(abt
->notice
, "Max SendQ exceeded", sizeof(abt
->notice
));
1313 rb_snprintf(abt
->notice
, sizeof(abt
->notice
), "Write error: %s", strerror(errno
));
1315 abt
->client
= client_p
;
1316 SetIOError(client_p
);
1318 SetClosing(client_p
);
1319 rb_dlinkAdd(abt
, &abt
->node
, &abort_list
);
1323 /* This does the remove of the user from channels..local or remote */
1325 exit_generic_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1326 const char *comment
)
1328 rb_dlink_node
*ptr
, *next_ptr
;
1330 if(IsOper(source_p
))
1331 rb_dlinkFindDestroy(source_p
, &oper_list
);
1333 sendto_common_channels_local(source_p
, ":%s!%s@%s QUIT :%s",
1335 source_p
->username
, source_p
->host
, comment
);
1337 remove_user_from_channels(source_p
);
1339 /* Should not be in any channels now */
1340 s_assert(source_p
->user
->channel
.head
== NULL
);
1342 /* Clean up invitefield */
1343 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, source_p
->user
->invited
.head
)
1345 del_invite(ptr
->data
, source_p
);
1348 /* Clean up allow lists */
1349 del_all_accepts(source_p
);
1351 add_history(source_p
, 0);
1352 off_history(source_p
);
1354 monitor_signoff(source_p
);
1356 if(has_id(source_p
))
1357 del_from_id_hash(source_p
->id
, source_p
);
1359 del_from_hostname_hash(source_p
->orighost
, source_p
);
1360 del_from_client_hash(source_p
->name
, source_p
);
1361 remove_client_from_list(source_p
);
1365 * Assumes IsPerson(source_p) && !MyConnect(source_p)
1369 exit_remote_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1370 const char *comment
)
1372 exit_generic_client(client_p
, source_p
, from
, comment
);
1374 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1376 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->users
);
1379 if((source_p
->flags
& FLAGS_KILLED
) == 0)
1381 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
1382 ":%s QUIT :%s", use_id(source_p
), comment
);
1383 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
,
1384 ":%s QUIT :%s", source_p
->name
, comment
);
1388 #ifdef DEBUG_EXITED_CLIENTS
1389 rb_dlinkAddAlloc(source_p
, &dead_remote_list
);
1391 rb_dlinkAddAlloc(source_p
, &dead_list
);
1393 return(CLIENT_EXITED
);
1397 * This assumes IsUnknown(source_p) == TRUE and MyConnect(source_p) == TRUE
1401 exit_unknown_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1402 const char *comment
)
1404 delete_auth_queries(source_p
);
1405 if (source_p
->localClient
->dnsquery
)
1407 delete_resolver_queries(source_p
->localClient
->dnsquery
);
1408 rb_free(source_p
->localClient
->dnsquery
);
1410 del_unknown_ip(source_p
);
1411 rb_dlinkDelete(&source_p
->localClient
->tnode
, &unknown_list
);
1413 if(!IsIOError(source_p
))
1414 sendto_one(source_p
, "ERROR :Closing Link: %s (%s)",
1415 source_p
->user
!= NULL
? source_p
->host
: "127.0.0.1",
1418 close_connection(source_p
);
1420 if(has_id(source_p
))
1421 del_from_id_hash(source_p
->id
, source_p
);
1423 del_from_hostname_hash(source_p
->host
, source_p
);
1424 del_from_client_hash(source_p
->name
, source_p
);
1425 remove_client_from_list(source_p
);
1427 rb_dlinkAddAlloc(source_p
, &dead_list
);
1429 /* Note that we don't need to add unknowns to the dead_list */
1430 return(CLIENT_EXITED
);
1434 exit_remote_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1435 const char *comment
)
1437 static char comment1
[(HOSTLEN
*2)+2];
1438 static char newcomment
[BUFSIZE
];
1439 struct Client
*target_p
;
1441 if(ConfigServerHide
.flatten_links
)
1442 strcpy(comment1
, "*.net *.split");
1445 strcpy(comment1
, source_p
->servptr
->name
);
1446 strcat(comment1
, " ");
1447 strcat(comment1
, source_p
->name
);
1450 rb_snprintf(newcomment
, sizeof(newcomment
), "by %s: %s",
1451 from
->name
, comment
);
1453 if(source_p
->serv
!= NULL
)
1454 remove_dependents(client_p
, source_p
, from
, IsPerson(from
) ? newcomment
: comment
, comment1
);
1456 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1457 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1461 rb_dlinkFindDestroy(source_p
, &global_serv_list
);
1462 target_p
= source_p
->from
;
1464 if(target_p
!= NULL
&& IsServer(target_p
) && target_p
!= client_p
&&
1465 !IsMe(target_p
) && (source_p
->flags
& FLAGS_KILLED
) == 0)
1467 sendto_one(target_p
, ":%s SQUIT %s :%s",
1468 get_id(from
, target_p
), get_id(source_p
, target_p
),
1472 if(has_id(source_p
))
1473 del_from_id_hash(source_p
->id
, source_p
);
1475 del_from_client_hash(source_p
->name
, source_p
);
1476 remove_client_from_list(source_p
);
1477 scache_split(source_p
->serv
->nameinfo
);
1480 #ifdef DEBUG_EXITED_CLIENTS
1481 rb_dlinkAddAlloc(source_p
, &dead_remote_list
);
1483 rb_dlinkAddAlloc(source_p
, &dead_list
);
1489 qs_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1490 const char *comment
)
1492 struct Client
*target_p
;
1494 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1495 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1499 rb_dlinkFindDestroy(source_p
, &global_serv_list
);
1500 target_p
= source_p
->from
;
1502 if(has_id(source_p
))
1503 del_from_id_hash(source_p
->id
, source_p
);
1505 del_from_client_hash(source_p
->name
, source_p
);
1506 remove_client_from_list(source_p
);
1507 scache_split(source_p
->serv
->nameinfo
);
1510 rb_dlinkAddAlloc(source_p
, &dead_list
);
1515 exit_local_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1516 const char *comment
)
1518 static char comment1
[(HOSTLEN
*2)+2];
1519 static char newcomment
[BUFSIZE
];
1520 unsigned int sendk
, recvk
;
1522 rb_dlinkDelete(&source_p
->localClient
->tnode
, &serv_list
);
1523 rb_dlinkFindDestroy(source_p
, &global_serv_list
);
1525 unset_chcap_usage_counts(source_p
);
1526 sendk
= source_p
->localClient
->sendK
;
1527 recvk
= source_p
->localClient
->receiveK
;
1529 /* Always show source here, so the server notices show
1530 * which side initiated the split -- jilles
1532 rb_snprintf(newcomment
, sizeof(newcomment
), "by %s: %s",
1533 from
== source_p
? me
.name
: from
->name
, comment
);
1534 if (!IsIOError(source_p
))
1535 sendto_one(source_p
, "SQUIT %s :%s", use_id(source_p
),
1537 if(client_p
!= NULL
&& source_p
!= client_p
&& !IsIOError(source_p
))
1539 sendto_one(source_p
, "ERROR :Closing Link: 127.0.0.1 %s (%s)",
1540 source_p
->name
, comment
);
1543 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1544 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1549 close_connection(source_p
);
1551 if(ConfigServerHide
.flatten_links
)
1552 strcpy(comment1
, "*.net *.split");
1555 strcpy(comment1
, source_p
->servptr
->name
);
1556 strcat(comment1
, " ");
1557 strcat(comment1
, source_p
->name
);
1560 if(source_p
->serv
!= NULL
)
1561 remove_dependents(client_p
, source_p
, from
, IsPerson(from
) ? newcomment
: comment
, comment1
);
1563 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s was connected"
1564 " for %ld seconds. %d/%d sendK/recvK.",
1565 source_p
->name
, (long) rb_current_time() - source_p
->localClient
->firsttime
, sendk
, recvk
);
1567 ilog(L_SERVER
, "%s was connected for %ld seconds. %d/%d sendK/recvK.",
1568 source_p
->name
, (long) rb_current_time() - source_p
->localClient
->firsttime
, sendk
, recvk
);
1570 if(has_id(source_p
))
1571 del_from_id_hash(source_p
->id
, source_p
);
1573 del_from_client_hash(source_p
->name
, source_p
);
1574 remove_client_from_list(source_p
);
1575 scache_split(source_p
->serv
->nameinfo
);
1578 rb_dlinkAddAlloc(source_p
, &dead_list
);
1584 * This assumes IsPerson(source_p) == TRUE && MyConnect(source_p) == TRUE
1588 exit_local_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1589 const char *comment
)
1591 unsigned long on_for
;
1593 exit_generic_client(client_p
, source_p
, from
, comment
);
1594 clear_monitor(source_p
);
1596 s_assert(IsPerson(source_p
));
1597 rb_dlinkDelete(&source_p
->localClient
->tnode
, &lclient_list
);
1598 rb_dlinkDelete(&source_p
->lnode
, &me
.serv
->users
);
1600 if(IsOper(source_p
))
1601 rb_dlinkFindDestroy(source_p
, &local_oper_list
);
1603 sendto_realops_snomask(SNO_CCONN
, L_ALL
,
1604 "Client exiting: %s (%s@%s) [%s] [%s]",
1606 source_p
->username
, source_p
->host
, comment
,
1607 show_ip(NULL
, source_p
) ? source_p
->sockhost
: "255.255.255.255");
1609 sendto_realops_snomask(SNO_CCONNEXT
, L_ALL
,
1610 "CLIEXIT %s %s %s %s 0 %s",
1611 source_p
->name
, source_p
->username
, source_p
->host
,
1612 show_ip(NULL
, source_p
) ? source_p
->sockhost
: "255.255.255.255",
1615 on_for
= rb_current_time() - source_p
->localClient
->firsttime
;
1617 ilog(L_USER
, "%s (%3lu:%02lu:%02lu): %s!%s@%s %d/%d",
1618 myctime(rb_current_time()), on_for
/ 3600,
1619 (on_for
% 3600) / 60, on_for
% 60,
1620 source_p
->name
, source_p
->username
, source_p
->host
,
1621 source_p
->localClient
->sendK
, source_p
->localClient
->receiveK
);
1623 sendto_one(source_p
, "ERROR :Closing Link: %s (%s)", source_p
->host
, comment
);
1624 close_connection(source_p
);
1626 if((source_p
->flags
& FLAGS_KILLED
) == 0)
1628 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
1629 ":%s QUIT :%s", use_id(source_p
), comment
);
1630 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
,
1631 ":%s QUIT :%s", source_p
->name
, comment
);
1635 rb_dlinkAddAlloc(source_p
, &dead_list
);
1636 return(CLIENT_EXITED
);
1641 ** exit_client - This is old "m_bye". Name changed, because this is not a
1642 ** protocol function, but a general server utility function.
1644 ** This function exits a client of *any* type (user, server, etc)
1645 ** from this server. Also, this generates all necessary prototol
1646 ** messages that this exit may cause.
1648 ** 1) If the client is a local client, then this implicitly
1649 ** exits all other clients depending on this connection (e.g.
1650 ** remote clients having 'from'-field that points to this.
1652 ** 2) If the client is a remote client, then only this is exited.
1654 ** For convenience, this function returns a suitable value for
1655 ** m_function return value:
1657 ** CLIENT_EXITED if (client_p == source_p)
1658 ** 0 if (client_p != source_p)
1661 exit_client(struct Client
*client_p
, /* The local client originating the
1662 * exit or NULL, if this exit is
1663 * generated by this server for
1665 * This will not get any of the
1666 * generated messages. */
1667 struct Client
*source_p
, /* Client exiting */
1668 struct Client
*from
, /* Client firing off this Exit,
1670 const char *comment
/* Reason for the exit */
1673 hook_data_client_exit hdata
;
1674 if(IsClosing(source_p
))
1677 /* note, this HAS to be here, when we exit a client we attempt to
1678 * send them data, if this generates a write error we must *not* add
1679 * them to the abort list --fl
1681 SetClosing(source_p
);
1683 hdata
.local_link
= client_p
;
1684 hdata
.target
= source_p
;
1686 hdata
.comment
= comment
;
1687 call_hook(h_client_exit
, &hdata
);
1689 if(MyConnect(source_p
))
1691 /* Local clients of various types */
1692 if(IsPerson(source_p
))
1693 return exit_local_client(client_p
, source_p
, from
, comment
);
1694 else if(IsServer(source_p
))
1695 return exit_local_server(client_p
, source_p
, from
, comment
);
1696 /* IsUnknown || IsConnecting || IsHandShake */
1697 else if(!IsReject(source_p
))
1698 return exit_unknown_client(client_p
, source_p
, from
, comment
);
1703 if(IsPerson(source_p
))
1704 return exit_remote_client(client_p
, source_p
, from
, comment
);
1705 else if(IsServer(source_p
))
1706 return exit_remote_server(client_p
, source_p
, from
, comment
);
1713 * Count up local client memory
1716 /* XXX one common Client list now */
1718 count_local_client_memory(size_t * count
, size_t * local_client_memory_used
)
1721 rb_bh_usage(lclient_heap
, count
, NULL
, &lusage
, NULL
);
1722 *local_client_memory_used
= lusage
+ (*count
* (sizeof(void *) + sizeof(struct Client
)));
1726 * Count up remote client memory
1729 count_remote_client_memory(size_t * count
, size_t * remote_client_memory_used
)
1731 size_t lcount
, rcount
;
1732 rb_bh_usage(lclient_heap
, &lcount
, NULL
, NULL
, NULL
);
1733 rb_bh_usage(client_heap
, &rcount
, NULL
, NULL
, NULL
);
1734 *count
= rcount
- lcount
;
1735 *remote_client_memory_used
= *count
* (sizeof(void *) + sizeof(struct Client
));
1740 * accept processing, this adds a form of "caller ID" to ircd
1742 * If a client puts themselves into "caller ID only" mode,
1743 * only clients that match a client pointer they have put on
1744 * the accept list will be allowed to message them.
1746 * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ]
1748 * [target.allow_list] -> [ source1 ] -> [source2 ]
1750 * i.e. a target will have a link list of source pointers it will allow
1751 * each source client then has a back pointer pointing back
1752 * to the client that has it on its accept list.
1753 * This allows for exit_one_client to remove these now bogus entries
1754 * from any client having an accept on them.
1759 * inputs - pointer to exiting client
1761 * side effects - Walk through given clients allow_list and on_allow_list
1762 * remove all references to this client
1765 del_all_accepts(struct Client
*client_p
)
1768 rb_dlink_node
*next_ptr
;
1769 struct Client
*target_p
;
1771 if(MyClient(client_p
) && client_p
->localClient
->allow_list
.head
)
1773 /* clear this clients accept list, and remove them from
1774 * everyones on_accept_list
1776 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->localClient
->allow_list
.head
)
1778 target_p
= ptr
->data
;
1779 rb_dlinkFindDestroy(client_p
, &target_p
->on_allow_list
);
1780 rb_dlinkDestroy(ptr
, &client_p
->localClient
->allow_list
);
1784 /* remove this client from everyones accept list */
1785 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->on_allow_list
.head
)
1787 target_p
= ptr
->data
;
1788 rb_dlinkFindDestroy(client_p
, &target_p
->localClient
->allow_list
);
1789 rb_dlinkDestroy(ptr
, &client_p
->on_allow_list
);
1794 * show_ip() - asks if the true IP shoudl be shown when source is
1795 * askin for info about target
1797 * Inputs - source_p who is asking
1798 * - target_p who do we want the info on
1799 * Output - returns 1 if clear IP can be showed, otherwise 0
1800 * Side Effects - none
1804 show_ip(struct Client
*source_p
, struct Client
*target_p
)
1806 if(IsAnyServer(target_p
))
1808 #ifndef HIDE_SERVERS_IPS
1809 if(source_p
== NULL
|| IsOper(source_p
))
1814 else if(IsIPSpoof(target_p
))
1816 /* source == NULL indicates message is being sent
1819 if(!ConfigFileEntry
.hide_spoof_ips
&&
1820 (source_p
== NULL
|| MyOper(source_p
)))
1824 else if(IsDynSpoof(target_p
) && (source_p
!= NULL
&& !IsOper(source_p
)))
1831 show_ip_conf(struct ConfItem
*aconf
, struct Client
*source_p
)
1833 if(IsConfDoSpoofIp(aconf
))
1835 if(!ConfigFileEntry
.hide_spoof_ips
&& MyOper(source_p
))
1850 * side effects - Creates a block heap for struct Users
1853 static rb_bh
*user_heap
;
1857 user_heap
= rb_bh_create(sizeof(struct User
), USER_HEAP_SIZE
, "user_heap");
1865 * inputs - pointer to client struct
1866 * output - pointer to struct User
1867 * side effects - add's an User information block to a client
1868 * if it was not previously allocated.
1871 make_user(struct Client
*client_p
)
1875 user
= client_p
->user
;
1878 user
= (struct User
*) rb_bh_alloc(user_heap
);
1880 client_p
->user
= user
;
1888 * inputs - pointer to client struct
1889 * output - pointer to server_t
1890 * side effects - add's an Server information block to a client
1891 * if it was not previously allocated.
1894 make_server(struct Client
*client_p
)
1896 server_t
*serv
= client_p
->serv
;
1900 serv
= (server_t
*) rb_malloc(sizeof(server_t
));
1901 client_p
->serv
= serv
;
1903 return client_p
->serv
;
1909 * inputs - pointer to user struct
1910 * - pointer to client struct
1912 * side effects - Decrease user reference count by one and release block,
1913 * if count reaches 0
1916 free_user(struct User
*user
, struct Client
*client_p
)
1918 free_away(client_p
);
1920 if(--user
->refcnt
<= 0)
1923 rb_free((char *) user
->away
);
1927 if(user
->refcnt
< 0 || user
->invited
.head
|| user
->channel
.head
)
1929 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1930 "* %#lx user (%s!%s@%s) %#lx %#lx %#lx %lu %d *",
1931 (unsigned long) client_p
,
1932 client_p
? client_p
->
1936 (unsigned long) user
,
1937 (unsigned long) user
->invited
.head
,
1938 (unsigned long) user
->channel
.head
,
1939 rb_dlink_list_length(&user
->channel
),
1941 s_assert(!user
->refcnt
);
1942 s_assert(!user
->invited
.head
);
1943 s_assert(!user
->channel
.head
);
1946 rb_bh_free(user_heap
, user
);
1951 allocate_away(struct Client
*client_p
)
1953 if(client_p
->user
->away
== NULL
)
1954 client_p
->user
->away
= rb_bh_alloc(away_heap
);
1959 free_away(struct Client
*client_p
)
1961 if(client_p
->user
!= NULL
&& client_p
->user
->away
!= NULL
) {
1962 rb_bh_free(away_heap
, client_p
->user
->away
);
1963 client_p
->user
->away
= NULL
;
1972 for(i
= 0; i
< 3; i
++)
1973 current_uid
[i
] = me
.id
[i
];
1975 for(i
= 3; i
< 9; i
++)
1976 current_uid
[i
] = 'A';
1978 current_uid
[9] = '\0';
1987 for(i
= 8; i
> 3; i
--)
1989 if(current_uid
[i
] == 'Z')
1991 current_uid
[i
] = '0';
1994 else if(current_uid
[i
] != '9')
2000 current_uid
[i
] = 'A';
2003 /* if this next if() triggers, we're fucked. */
2004 if(current_uid
[3] == 'Z')
2006 current_uid
[i
] = 'A';
2017 * Close the physical connection. This function must make
2018 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
2021 close_connection(struct Client
*client_p
)
2023 s_assert(client_p
!= NULL
);
2024 if(client_p
== NULL
)
2027 s_assert(MyConnect(client_p
));
2028 if(!MyConnect(client_p
))
2031 if(IsServer(client_p
))
2033 struct server_conf
*server_p
;
2035 ServerStats
.is_sv
++;
2036 ServerStats
.is_sbs
+= client_p
->localClient
->sendB
;
2037 ServerStats
.is_sbr
+= client_p
->localClient
->receiveB
;
2038 ServerStats
.is_sks
+= client_p
->localClient
->sendK
;
2039 ServerStats
.is_skr
+= client_p
->localClient
->receiveK
;
2040 ServerStats
.is_sti
+= rb_current_time() - client_p
->localClient
->firsttime
;
2041 if(ServerStats
.is_sbs
> 2047)
2043 ServerStats
.is_sks
+= (ServerStats
.is_sbs
>> 10);
2044 ServerStats
.is_sbs
&= 0x3ff;
2046 if(ServerStats
.is_sbr
> 2047)
2048 ServerStats
.is_skr
+= (ServerStats
.is_sbr
>> 10);
2049 ServerStats
.is_sbr
&= 0x3ff;
2053 * If the connection has been up for a long amount of time, schedule
2054 * a 'quick' reconnect, else reset the next-connect cycle.
2056 if((server_p
= find_server_conf(client_p
->name
)) != NULL
)
2059 * Reschedule a faster reconnect, if this was a automatically
2060 * connected configuration entry. (Note that if we have had
2061 * a rehash in between, the status has been changed to
2062 * CONF_ILLEGAL). But only do this if it was a "good" link.
2064 server_p
->hold
= time(NULL
);
2066 (server_p
->hold
- client_p
->localClient
->lasttime
>
2067 HANGONGOODLINK
) ? HANGONRETRYDELAY
: ConFreq(server_p
->class);
2071 else if(IsClient(client_p
))
2073 ServerStats
.is_cl
++;
2074 ServerStats
.is_cbs
+= client_p
->localClient
->sendB
;
2075 ServerStats
.is_cbr
+= client_p
->localClient
->receiveB
;
2076 ServerStats
.is_cks
+= client_p
->localClient
->sendK
;
2077 ServerStats
.is_ckr
+= client_p
->localClient
->receiveK
;
2078 ServerStats
.is_cti
+= rb_current_time() - client_p
->localClient
->firsttime
;
2079 if(ServerStats
.is_cbs
> 2047)
2081 ServerStats
.is_cks
+= (ServerStats
.is_cbs
>> 10);
2082 ServerStats
.is_cbs
&= 0x3ff;
2084 if(ServerStats
.is_cbr
> 2047)
2086 ServerStats
.is_ckr
+= (ServerStats
.is_cbr
>> 10);
2087 ServerStats
.is_cbr
&= 0x3ff;
2091 ServerStats
.is_ni
++;
2093 if(client_p
->localClient
->F
)
2095 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
2096 if(!IsIOError(client_p
))
2097 send_queued(client_p
);
2099 rb_close(client_p
->localClient
->F
);
2100 client_p
->localClient
->F
= NULL
;
2103 if(client_p
->localClient
->ctrlF
)
2105 rb_close(client_p
->localClient
->ctrlF
);
2106 client_p
->localClient
->ctrlF
= NULL
;
2109 rb_linebuf_donebuf(&client_p
->localClient
->buf_sendq
);
2110 rb_linebuf_donebuf(&client_p
->localClient
->buf_recvq
);
2111 detach_conf(client_p
);
2113 /* XXX shouldnt really be done here. */
2114 detach_server_conf(client_p
);
2116 client_p
->from
= NULL
; /* ...this should catch them! >:) --msa */
2117 ClearMyConnect(client_p
);
2118 SetIOError(client_p
);
2124 error_exit_client(struct Client
*client_p
, int error
)
2127 * ...hmm, with non-blocking sockets we might get
2128 * here from quite valid reasons, although.. why
2129 * would select report "data available" when there
2130 * wasn't... so, this must be an error anyway... --msa
2131 * actually, EOF occurs when read() returns 0 and
2132 * in due course, select() returns that fd as ready
2133 * for reading even though it ends up being an EOF. -avalon
2136 int current_error
= rb_get_sockerr(client_p
->localClient
->F
);
2138 SetIOError(client_p
);
2140 if(IsServer(client_p
) || IsHandshake(client_p
))
2144 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) && !IsServer(client_p
) ? L_NETWIDE
: L_ALL
,
2145 "Server %s closed the connection",
2146 get_server_name(client_p
, SHOW_IP
));
2148 ilog(L_SERVER
, "Server %s closed the connection",
2149 log_client_name(client_p
, SHOW_IP
));
2153 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) && !IsServer(client_p
) ? L_NETWIDE
: L_ALL
,
2154 "Lost connection to %s: %s",
2155 client_p
->name
, strerror(current_error
));
2156 ilog(L_SERVER
, "Lost connection to %s: %s",
2157 log_client_name(client_p
, SHOW_IP
), strerror(current_error
));
2162 strlcpy(errmsg
, "Remote host closed the connection", sizeof(errmsg
));
2164 rb_snprintf(errmsg
, sizeof(errmsg
), "Read error: %s", strerror(current_error
));
2166 exit_client(client_p
, client_p
, &me
, errmsg
);