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
38 #include "s_newconf.h"
51 #include "blacklist.h"
54 #include "irc_dictionary.h"
58 #define DEBUG_EXITED_CLIENTS
60 static void check_pings_list(rb_dlink_list
* list
);
61 static void check_unknowns_list(rb_dlink_list
* list
);
62 static void free_exited_clients(void *unused
);
63 static void exit_aborted_clients(void *unused
);
65 static int exit_remote_client(struct Client
*, struct Client
*, struct Client
*,const char *);
66 static int exit_remote_server(struct Client
*, struct Client
*, struct Client
*,const char *);
67 static int exit_local_client(struct Client
*, struct Client
*, struct Client
*,const char *);
68 static int exit_unknown_client(struct Client
*, struct Client
*, struct Client
*,const char *);
69 static int exit_local_server(struct Client
*, struct Client
*, struct Client
*,const char *);
70 static int qs_server(struct Client
*, struct Client
*, struct Client
*, const char *comment
);
72 static EVH check_pings
;
74 static rb_bh
*client_heap
= NULL
;
75 static rb_bh
*lclient_heap
= NULL
;
76 static rb_bh
*pclient_heap
= NULL
;
77 static rb_bh
*user_heap
= NULL
;
78 static rb_bh
*away_heap
= NULL
;
79 static char current_uid
[IDLEN
];
80 static int32_t current_connid
= 0;
82 struct Dictionary
*nd_dict
= NULL
;
90 rb_dlink_list dead_list
;
91 #ifdef DEBUG_EXITED_CLIENTS
92 static rb_dlink_list dead_remote_list
;
98 struct Client
*client
;
99 char notice
[REASONLEN
];
102 static rb_dlink_list abort_list
;
110 * side effects - initialize client free memory
116 * start off the check ping event .. -- adrian
117 * Every 30 seconds is plenty -- db
119 client_heap
= rb_bh_create(sizeof(struct Client
), CLIENT_HEAP_SIZE
, "client_heap");
120 lclient_heap
= rb_bh_create(sizeof(struct LocalUser
), LCLIENT_HEAP_SIZE
, "lclient_heap");
121 pclient_heap
= rb_bh_create(sizeof(struct PreClient
), PCLIENT_HEAP_SIZE
, "pclient_heap");
122 user_heap
= rb_bh_create(sizeof(struct User
), USER_HEAP_SIZE
, "user_heap");
123 away_heap
= rb_bh_create(AWAYLEN
, AWAY_HEAP_SIZE
, "away_heap");
125 rb_event_addish("check_pings", check_pings
, NULL
, 30);
126 rb_event_addish("free_exited_clients", &free_exited_clients
, NULL
, 4);
127 rb_event_addish("exit_aborted_clients", exit_aborted_clients
, NULL
, 1);
128 rb_event_add("flood_recalc", flood_recalc
, NULL
, 1);
130 nd_dict
= irc_dictionary_create("nickdelay", irccmp
);
135 * make_client - create a new Client struct and set it to initial state.
137 * from == NULL, create local client (a client connected
140 * from, create remote client (behind a socket
141 * associated with the client defined by
142 * 'from'). ('from' is a local client!!).
145 make_client(struct Client
*from
)
147 struct Client
*client_p
= NULL
;
148 struct LocalUser
*localClient
;
150 client_p
= rb_bh_alloc(client_heap
);
154 client_p
->from
= client_p
; /* 'from' of local client is self! */
156 localClient
= rb_bh_alloc(lclient_heap
);
157 SetMyConnect(client_p
);
158 client_p
->localClient
= localClient
;
160 client_p
->localClient
->lasttime
= client_p
->localClient
->firsttime
= rb_current_time();
162 client_p
->localClient
->F
= NULL
;
164 if(current_connid
+1 == 0)
167 client_p
->localClient
->connid
= ++current_connid
;
169 if(current_connid
+1 == 0)
172 client_p
->localClient
->zconnid
= ++current_connid
;
173 add_to_cli_connid_hash(client_p
);
175 client_p
->preClient
= rb_bh_alloc(pclient_heap
);
177 /* as good a place as any... */
178 rb_dlinkAdd(client_p
, &client_p
->localClient
->tnode
, &unknown_list
);
181 { /* from is not NULL */
182 client_p
->localClient
= NULL
;
183 client_p
->preClient
= NULL
;
184 client_p
->from
= from
; /* 'from' of local client is self! */
187 SetUnknown(client_p
);
188 strcpy(client_p
->username
, "unknown");
194 free_pre_client(struct Client
*client_p
)
196 struct Blacklist
*blptr
;
198 s_assert(NULL
!= client_p
);
200 if(client_p
->preClient
== NULL
)
203 blptr
= client_p
->preClient
->dnsbl_listed
;
205 unref_blacklist(blptr
);
206 s_assert(rb_dlink_list_length(&client_p
->preClient
->dnsbl_queries
) == 0);
208 rb_bh_free(pclient_heap
, client_p
->preClient
);
209 client_p
->preClient
= NULL
;
213 free_local_client(struct Client
*client_p
)
215 s_assert(NULL
!= client_p
);
216 s_assert(&me
!= client_p
);
218 if(client_p
->localClient
== NULL
)
222 * clean up extra sockets from P-lines which have been discarded.
224 if(client_p
->localClient
->listener
)
226 s_assert(0 < client_p
->localClient
->listener
->ref_count
);
227 if(0 == --client_p
->localClient
->listener
->ref_count
228 && !client_p
->localClient
->listener
->active
)
229 free_listener(client_p
->localClient
->listener
);
230 client_p
->localClient
->listener
= 0;
233 del_from_cli_connid_hash(client_p
);
234 if(client_p
->localClient
->F
!= NULL
)
236 rb_close(client_p
->localClient
->F
);
239 if(client_p
->localClient
->passwd
)
241 memset(client_p
->localClient
->passwd
, 0,
242 strlen(client_p
->localClient
->passwd
));
243 rb_free(client_p
->localClient
->passwd
);
246 rb_free(client_p
->localClient
->auth_user
);
247 rb_free(client_p
->localClient
->challenge
);
248 rb_free(client_p
->localClient
->fullcaps
);
249 rb_free(client_p
->localClient
->opername
);
250 rb_free(client_p
->localClient
->mangledhost
);
251 if (client_p
->localClient
->privset
)
252 privilegeset_unref(client_p
->localClient
->privset
);
255 ssld_decrement_clicount(client_p
->localClient
->ssl_ctl
);
257 if(IsCapable(client_p
, CAP_ZIP
))
258 ssld_decrement_clicount(client_p
->localClient
->z_ctl
);
260 rb_bh_free(lclient_heap
, client_p
->localClient
);
261 client_p
->localClient
= NULL
;
265 free_client(struct Client
*client_p
)
267 s_assert(NULL
!= client_p
);
268 s_assert(&me
!= client_p
);
269 free_local_client(client_p
);
270 free_pre_client(client_p
);
271 rb_free(client_p
->certfp
);
272 rb_bh_free(client_heap
, client_p
);
276 * check_pings - go through the local client list and check activity
277 * kill off stuff that should die
279 * inputs - NOT USED (from event)
280 * output - next time_t when check_pings() should be called again
284 * A PING can be sent to clients as necessary.
286 * Client/Server ping outs are handled.
290 * Addon from adrian. We used to call this after nextping seconds,
291 * however I've changed it to run once a second. This is only for
292 * PING timeouts, not K/etc-line checks (thanks dianora!). Having it
293 * run once a second makes life a lot easier - when a new client connects
294 * and they need a ping in 4 seconds, if nextping was set to 20 seconds
295 * we end up waiting 20 seconds. This is stupid. :-)
296 * I will optimise (hah!) check_pings() once I've finished working on
297 * tidying up other network IO evilnesses.
302 check_pings(void *notused
)
304 check_pings_list(&lclient_list
);
305 check_pings_list(&serv_list
);
306 check_unknowns_list(&unknown_list
);
312 * inputs - pointer to list to check
317 check_pings_list(rb_dlink_list
* list
)
319 char scratch
[32]; /* way too generous but... */
320 struct Client
*client_p
; /* current local client_p being examined */
321 int ping
= 0; /* ping time value from client */
322 rb_dlink_node
*ptr
, *next_ptr
;
324 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
326 client_p
= ptr
->data
;
328 if(!MyConnect(client_p
) || IsDead(client_p
))
331 ping
= get_client_ping(client_p
);
333 if(ping
< (rb_current_time() - client_p
->localClient
->lasttime
))
336 * If the client/server hasnt talked to us in 2*ping seconds
337 * and it has a ping time, then close its connection.
339 if(((rb_current_time() - client_p
->localClient
->lasttime
) >= (2 * ping
)
340 && (client_p
->flags
& FLAGS_PINGSENT
)))
342 if(IsServer(client_p
))
344 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
345 "No response from %s, closing link",
348 "No response from %s, closing link",
349 log_client_name(client_p
, HIDE_IP
));
351 (void) snprintf(scratch
, sizeof(scratch
),
352 "Ping timeout: %d seconds",
353 (int) (rb_current_time() - client_p
->localClient
->lasttime
));
355 exit_client(client_p
, client_p
, &me
, scratch
);
358 else if((client_p
->flags
& FLAGS_PINGSENT
) == 0)
361 * if we havent PINGed the connection and we havent
362 * heard from it in a while, PING it to make sure
365 client_p
->flags
|= FLAGS_PINGSENT
;
366 /* not nice but does the job */
367 client_p
->localClient
->lasttime
= rb_current_time() - ping
;
368 sendto_one(client_p
, "PING :%s", me
.name
);
377 * check_unknowns_list
379 * inputs - pointer to list of unknown clients
381 * side effects - unknown clients get marked for termination after n seconds
384 check_unknowns_list(rb_dlink_list
* list
)
386 rb_dlink_node
*ptr
, *next_ptr
;
387 struct Client
*client_p
;
390 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
392 client_p
= ptr
->data
;
394 if(IsDead(client_p
) || IsClosing(client_p
))
397 /* still has DNSbls to validate against */
398 if(client_p
->preClient
!= NULL
&&
399 rb_dlink_list_length(&client_p
->preClient
->dnsbl_queries
) > 0)
403 * Check UNKNOWN connections - if they have been in this state
404 * for > 30s, close them.
407 timeout
= IsAnyServer(client_p
) ? ConfigFileEntry
.connect_timeout
: 30;
408 if((rb_current_time() - client_p
->localClient
->firsttime
) > timeout
)
410 if(IsAnyServer(client_p
))
412 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) ? L_NETWIDE
: L_ALL
,
413 "No response from %s, closing link",
416 "No response from %s, closing link",
417 log_client_name(client_p
, HIDE_IP
));
419 exit_client(client_p
, client_p
, &me
, "Connection timed out");
425 notify_banned_client(struct Client
*client_p
, struct ConfItem
*aconf
, int ban
)
427 static const char conn_closed
[] = "Connection closed";
428 static const char d_lined
[] = "D-lined";
429 static const char k_lined
[] = "K-lined";
430 const char *reason
= NULL
;
431 const char *exit_reason
= conn_closed
;
433 if(ConfigFileEntry
.kline_with_reason
)
435 reason
= get_user_ban_reason(aconf
);
436 exit_reason
= reason
;
440 reason
= aconf
->status
== D_LINED
? d_lined
: k_lined
;
443 if(ban
== D_LINED
&& !IsPerson(client_p
))
444 sendto_one(client_p
, "NOTICE DLINE :*** You have been D-lined");
446 sendto_one(client_p
, form_str(ERR_YOUREBANNEDCREEP
),
447 me
.name
, client_p
->name
, reason
);
449 exit_client(client_p
, client_p
, &me
,
450 EmptyString(ConfigFileEntry
.kline_reason
) ? exit_reason
:
451 ConfigFileEntry
.kline_reason
);
458 * side effects - Check all connections for a pending k/dline against the
459 * client, exit the client if found.
462 check_banned_lines(void)
469 /* check_klines_event()
473 * side effects - check_klines() is called, kline_queued unset
476 check_klines_event(void *unused
)
486 * side effects - all clients will be checked for klines
491 struct Client
*client_p
;
492 struct ConfItem
*aconf
;
494 rb_dlink_node
*next_ptr
;
496 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
498 client_p
= ptr
->data
;
500 if(IsMe(client_p
) || !IsPerson(client_p
))
503 if((aconf
= find_kline(client_p
)) != NULL
)
505 if(IsExemptKline(client_p
))
507 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
508 "KLINE over-ruled for %s, client is kline_exempt [%s@%s]",
509 get_client_name(client_p
, HIDE_IP
),
510 aconf
->user
, aconf
->host
);
514 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
515 "KLINE active for %s",
516 get_client_name(client_p
, HIDE_IP
));
518 notify_banned_client(client_p
, aconf
, K_LINED
);
528 * side effects - all clients will be checked for dlines
533 struct Client
*client_p
;
534 struct ConfItem
*aconf
;
536 rb_dlink_node
*next_ptr
;
538 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
540 client_p
= ptr
->data
;
545 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
,client_p
->localClient
->ip
.ss_family
)) != NULL
)
547 if(aconf
->status
& CONF_EXEMPTDLINE
)
550 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
551 "DLINE active for %s",
552 get_client_name(client_p
, HIDE_IP
));
554 notify_banned_client(client_p
, aconf
, D_LINED
);
559 /* dlines need to be checked against unknowns too */
560 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, unknown_list
.head
)
562 client_p
= ptr
->data
;
564 if((aconf
= find_dline((struct sockaddr
*)&client_p
->localClient
->ip
,client_p
->localClient
->ip
.ss_family
)) != NULL
)
566 if(aconf
->status
& CONF_EXEMPTDLINE
)
569 notify_banned_client(client_p
, aconf
, D_LINED
);
578 * side effects - all clients will be checked for xlines
583 struct Client
*client_p
;
584 struct ConfItem
*aconf
;
586 rb_dlink_node
*next_ptr
;
588 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
590 client_p
= ptr
->data
;
592 if(IsMe(client_p
) || !IsPerson(client_p
))
595 if((aconf
= find_xline(client_p
->info
, 1)) != NULL
)
597 if(IsExemptKline(client_p
))
599 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
600 "XLINE over-ruled for %s, client is kline_exempt [%s]",
601 get_client_name(client_p
, HIDE_IP
),
606 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "XLINE active for %s",
607 get_client_name(client_p
, HIDE_IP
));
609 (void) exit_client(client_p
, client_p
, &me
, "Bad user info");
617 * inputs - resv, reason, time
619 * side effects - all local clients matching resv will be FNC'd
622 resv_nick_fnc(const char *mask
, const char *reason
, int temp_time
)
624 struct Client
*client_p
, *target_p
;
626 rb_dlink_node
*next_ptr
;
628 char note
[NICKLEN
+10];
630 if (!ConfigFileEntry
.resv_fnc
)
633 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, lclient_list
.head
)
635 client_p
= ptr
->data
;
637 if(IsMe(client_p
) || !IsPerson(client_p
) || IsExemptResv(client_p
))
640 /* Skip users that already have UID nicks. */
641 if(IsDigit(client_p
->name
[0]))
644 if(match_esc(mask
, client_p
->name
))
649 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
650 "RESV forced nick change for %s!%s@%s to %s; nick matched [%s] (%s)",
651 client_p
->name
, client_p
->username
, client_p
->host
, nick
, mask
, reason
);
653 sendto_realops_snomask(SNO_NCHANGE
, L_ALL
,
654 "Nick change: From %s to %s [%s@%s]",
655 client_p
->name
, nick
, client_p
->username
, client_p
->host
);
660 sendto_one_notice(client_p
,
661 ":*** Nick %s is temporarily unavailable on this server.",
666 sendto_one_notice(client_p
,
667 ":*** Nick %s is no longer available on this server.",
671 /* Do all of the nick-changing gymnastics. */
672 client_p
->tsinfo
= rb_current_time();
673 whowas_add_history(client_p
, 1);
675 monitor_signoff(client_p
);
677 invalidate_bancache_user(client_p
);
679 sendto_common_channels_local(client_p
, NOCAPS
, NOCAPS
, ":%s!%s@%s NICK :%s",
680 client_p
->name
, client_p
->username
, client_p
->host
, nick
);
681 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s NICK %s :%ld",
682 use_id(client_p
), nick
, (long) client_p
->tsinfo
);
684 del_from_client_hash(client_p
->name
, client_p
);
685 rb_strlcpy(client_p
->name
, nick
, sizeof(client_p
->name
));
686 add_to_client_hash(nick
, client_p
);
688 monitor_signon(client_p
);
690 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->on_allow_list
.head
)
692 target_p
= ptr
->data
;
693 rb_dlinkFindDestroy(client_p
, &target_p
->localClient
->allow_list
);
694 rb_dlinkDestroy(ptr
, &client_p
->on_allow_list
);
697 snprintf(note
, sizeof(note
), "Nick: %s", nick
);
698 rb_note(client_p
->localClient
->F
, note
);
704 * update_client_exit_stats
706 * input - pointer to client
711 update_client_exit_stats(struct Client
*client_p
)
713 if(IsServer(client_p
))
715 sendto_realops_snomask(SNO_EXTERNAL
, L_ALL
,
716 "Server %s split from %s",
717 client_p
->name
, client_p
->servptr
->name
);
718 if(HasSentEob(client_p
))
721 else if(IsClient(client_p
))
726 if(IsInvisible(client_p
))
730 if(splitchecking
&& !splitmode
)
731 check_splitmode(NULL
);
735 * release_client_state
737 * input - pointer to client to release
742 release_client_state(struct Client
*client_p
)
744 if(client_p
->user
!= NULL
)
746 free_user(client_p
->user
, client_p
); /* try this here */
750 if(client_p
->serv
->user
!= NULL
)
751 free_user(client_p
->serv
->user
, client_p
);
752 if(client_p
->serv
->fullcaps
)
753 rb_free(client_p
->serv
->fullcaps
);
754 rb_free(client_p
->serv
);
759 * remove_client_from_list
760 * inputs - point to client to remove
762 * side effects - taken the code from ExitOneClient() for this
763 * and placed it here. - avalon
766 remove_client_from_list(struct Client
*client_p
)
768 s_assert(NULL
!= client_p
);
773 /* A client made with make_client()
774 * is on the unknown_list until removed.
775 * If it =does= happen to exit before its removed from that list
776 * and its =not= on the global_client_list, it will core here.
777 * short circuit that case now -db
779 if(client_p
->node
.prev
== NULL
&& client_p
->node
.next
== NULL
)
782 rb_dlinkDelete(&client_p
->node
, &global_client_list
);
784 update_client_exit_stats(client_p
);
790 * input - nickname to check, flag for nick from local client
791 * output - 0 if erroneous, else 1
795 clean_nick(const char *nick
, int loc_client
)
799 /* nicks cant start with a digit or -, and must have a length */
800 if(*nick
== '-' || *nick
== '\0')
803 if(loc_client
&& IsDigit(*nick
))
809 if(!IsNickChar(*nick
))
814 if(len
>= NICKLEN
&& (unsigned int)len
>= ConfigFileEntry
.nicklen
)
821 * find_person - find person by (nick)name.
822 * inputs - pointer to name
823 * output - return client pointer
827 find_person(const char *name
)
829 struct Client
*c2ptr
;
831 c2ptr
= find_client(name
);
833 if(c2ptr
&& IsPerson(c2ptr
))
839 find_named_person(const char *name
)
841 struct Client
*c2ptr
;
843 c2ptr
= find_named_client(name
);
845 if(c2ptr
&& IsPerson(c2ptr
))
852 * find_chasing - find the client structure for a nick name (user)
853 * using history mechanism if necessary. If the client is not found,
854 * an error message (NO SUCH NICK) is generated. If the client was found
855 * through the history, chasing will be 1 and otherwise 0.
858 find_chasing(struct Client
*source_p
, const char *user
, int *chasing
)
862 if(MyClient(source_p
))
863 who
= find_named_person(user
);
865 who
= find_person(user
);
870 if(who
|| IsDigit(*user
))
873 if(!(who
= whowas_get_history(user
, (long) KILLCHASETIMELIMIT
)))
875 sendto_one_numeric(source_p
, ERR_NOSUCHNICK
,
876 form_str(ERR_NOSUCHNICK
), user
);
885 * get_client_name - Return the name of the client
886 * for various tracking and
887 * admin purposes. The main purpose of this function is to
888 * return the "socket host" name of the client, if that
889 * differs from the advertised name (other than case).
890 * But, this can be used to any client structure.
893 * Watch out the allocation of "nbuf", if either source_p->name
894 * or source_p->sockhost gets changed into pointers instead of
895 * directly allocated within the structure...
898 * Function return either a pointer to the structure (source_p) or
899 * to internal buffer (nbuf). *NEVER* use the returned pointer
900 * to modify what it points!!!
904 get_client_name(struct Client
*client
, int showip
)
906 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
908 s_assert(NULL
!= client
);
912 if(MyConnect(client
))
914 if(!irccmp(client
->name
, client
->host
))
917 if(ConfigFileEntry
.hide_spoof_ips
&&
918 showip
== SHOW_IP
&& IsIPSpoof(client
))
920 if(IsAnyServer(client
))
923 /* And finally, let's get the host information, ip or name */
927 snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
928 client
->name
, client
->username
,
932 snprintf(nbuf
, sizeof(nbuf
), "%s[%s@255.255.255.255]",
933 client
->name
, client
->username
);
936 snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]",
937 client
->name
, client
->username
, client
->host
);
942 /* As pointed out by Adel Mezibra
943 * Neph|l|m@EFnet. Was missing a return here.
950 * This version is the same as get_client_name, but doesnt contain the
951 * code that will hide IPs always. This should be used for logfiles.
954 log_client_name(struct Client
*target_p
, int showip
)
956 static char nbuf
[HOSTLEN
* 2 + USERLEN
+ 5];
961 if(MyConnect(target_p
))
963 if(irccmp(target_p
->name
, target_p
->host
) == 0)
964 return target_p
->name
;
969 snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]", target_p
->name
,
970 target_p
->username
, target_p
->sockhost
);
974 snprintf(nbuf
, sizeof(nbuf
), "%s[%s@%s]", target_p
->name
,
975 target_p
->username
, target_p
->host
);
981 return target_p
->name
;
984 /* is_remote_connect - Returns whether a server was /connect'ed by a remote
985 * oper (send notices netwide) */
987 is_remote_connect(struct Client
*client_p
)
991 if (client_p
->serv
== NULL
)
993 oper
= find_named_person(client_p
->serv
->by
);
994 return oper
!= NULL
&& IsOper(oper
) && !MyConnect(oper
);
998 free_exited_clients(void *unused
)
1000 rb_dlink_node
*ptr
, *next
;
1001 struct Client
*target_p
;
1003 RB_DLINK_FOREACH_SAFE(ptr
, next
, dead_list
.head
)
1005 target_p
= ptr
->data
;
1007 #ifdef DEBUG_EXITED_CLIENTS
1009 struct abort_client
*abt
;
1010 rb_dlink_node
*aptr
;
1013 RB_DLINK_FOREACH(aptr
, abort_list
.head
)
1016 if(abt
->client
== target_p
)
1019 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1020 "On abort_list: %s stat: %u flags: %u/%u handler: %c",
1021 target_p
->name
, (unsigned int) target_p
->status
,
1022 target_p
->flags
, target_p
->flags2
, target_p
->handler
);
1023 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1024 "Please report this to the charybdis developers!");
1031 rb_dlinkDestroy(ptr
, &dead_list
);
1037 if(ptr
->data
== NULL
)
1039 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1040 "Warning: null client on dead_list!");
1041 rb_dlinkDestroy(ptr
, &dead_list
);
1044 release_client_state(target_p
);
1045 free_client(target_p
);
1046 rb_dlinkDestroy(ptr
, &dead_list
);
1049 #ifdef DEBUG_EXITED_CLIENTS
1050 RB_DLINK_FOREACH_SAFE(ptr
, next
, dead_remote_list
.head
)
1052 target_p
= ptr
->data
;
1054 if(ptr
->data
== NULL
)
1056 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1057 "Warning: null client on dead_list!");
1058 rb_dlinkDestroy(ptr
, &dead_list
);
1061 release_client_state(target_p
);
1062 free_client(target_p
);
1063 rb_dlinkDestroy(ptr
, &dead_remote_list
);
1070 ** Remove all clients that depend on source_p; assumes all (S)QUITs have
1071 ** already been sent. we make sure to exit a server's dependent clients
1072 ** and servers before the server itself; exit_one_client takes care of
1073 ** actually removing things off llists. tweaked from +CSr31 -orabidoo
1076 * added sanity test code.... source_p->serv might be NULL...
1079 recurse_remove_clients(struct Client
*source_p
, const char *comment
)
1081 struct Client
*target_p
;
1082 rb_dlink_node
*ptr
, *ptr_next
;
1087 if(source_p
->serv
== NULL
) /* oooops. uh this is actually a major bug */
1090 /* this is very ugly, but it saves cpu :P */
1091 if(ConfigFileEntry
.nick_delay
> 0)
1093 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1095 target_p
= ptr
->data
;
1096 target_p
->flags
|= FLAGS_KILLED
;
1097 add_nd_entry(target_p
->name
);
1099 if(!IsDead(target_p
) && !IsClosing(target_p
))
1100 exit_remote_client(NULL
, target_p
, &me
, comment
);
1105 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->users
.head
)
1107 target_p
= ptr
->data
;
1108 target_p
->flags
|= FLAGS_KILLED
;
1110 if(!IsDead(target_p
) && !IsClosing(target_p
))
1111 exit_remote_client(NULL
, target_p
, &me
, comment
);
1115 RB_DLINK_FOREACH_SAFE(ptr
, ptr_next
, source_p
->serv
->servers
.head
)
1117 target_p
= ptr
->data
;
1118 recurse_remove_clients(target_p
, comment
);
1119 qs_server(NULL
, target_p
, &me
, comment
);
1124 ** Remove *everything* that depends on source_p, from all lists, and sending
1125 ** all necessary SQUITs. source_p itself is still on the lists,
1126 ** and its SQUITs have been sent except for the upstream one -orabidoo
1129 remove_dependents(struct Client
*client_p
,
1130 struct Client
*source_p
,
1131 struct Client
*from
, const char *comment
, const char *comment1
)
1134 rb_dlink_node
*ptr
, *next
;
1136 RB_DLINK_FOREACH_SAFE(ptr
, next
, serv_list
.head
)
1140 if(IsMe(to
) || to
== source_p
->from
|| to
== client_p
)
1143 sendto_one(to
, "SQUIT %s :%s", get_id(source_p
, to
), comment
);
1146 recurse_remove_clients(source_p
, comment1
);
1150 exit_aborted_clients(void *unused
)
1152 struct abort_client
*abt
;
1153 rb_dlink_node
*ptr
, *next
;
1154 RB_DLINK_FOREACH_SAFE(ptr
, next
, abort_list
.head
)
1158 #ifdef DEBUG_EXITED_CLIENTS
1160 if(rb_dlinkFind(abt
->client
, &dead_list
))
1163 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1164 "On dead_list: %s stat: %u flags: %u/%u handler: %c",
1165 abt
->client
->name
, (unsigned int) abt
->client
->status
,
1166 abt
->client
->flags
, abt
->client
->flags2
, abt
->client
->handler
);
1167 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1168 "Please report this to the charybdis developers!");
1174 s_assert(*((unsigned long*)abt
->client
) != 0xdeadbeef); /* This is lame but its a debug thing */
1175 rb_dlinkDelete(ptr
, &abort_list
);
1177 if(IsAnyServer(abt
->client
))
1178 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1179 "Closing link to %s: %s",
1180 abt
->client
->name
, abt
->notice
);
1182 /* its no longer on abort list - we *must* remove
1183 * FLAGS_CLOSING otherwise exit_client() will not run --fl
1185 abt
->client
->flags
&= ~FLAGS_CLOSING
;
1186 exit_client(abt
->client
, abt
->client
, &me
, abt
->notice
);
1193 * dead_link - Adds client to a list of clients that need an exit_client()
1196 dead_link(struct Client
*client_p
, int sendqex
)
1198 struct abort_client
*abt
;
1200 s_assert(!IsMe(client_p
));
1201 if(IsDead(client_p
) || IsClosing(client_p
) || IsMe(client_p
))
1204 abt
= (struct abort_client
*) rb_malloc(sizeof(struct abort_client
));
1207 rb_strlcpy(abt
->notice
, "Max SendQ exceeded", sizeof(abt
->notice
));
1209 snprintf(abt
->notice
, sizeof(abt
->notice
), "Write error: %s", strerror(errno
));
1211 abt
->client
= client_p
;
1212 SetIOError(client_p
);
1214 SetClosing(client_p
);
1215 rb_dlinkAdd(abt
, &abt
->node
, &abort_list
);
1219 /* This does the remove of the user from channels..local or remote */
1221 exit_generic_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1222 const char *comment
)
1224 rb_dlink_node
*ptr
, *next_ptr
;
1226 if(IsOper(source_p
))
1227 rb_dlinkFindDestroy(source_p
, &oper_list
);
1229 sendto_common_channels_local(source_p
, NOCAPS
, NOCAPS
, ":%s!%s@%s QUIT :%s",
1231 source_p
->username
, source_p
->host
, comment
);
1233 remove_user_from_channels(source_p
);
1235 /* Should not be in any channels now */
1236 s_assert(source_p
->user
->channel
.head
== NULL
);
1238 /* Clean up invitefield */
1239 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, source_p
->user
->invited
.head
)
1241 del_invite(ptr
->data
, source_p
);
1244 /* Clean up allow lists */
1245 del_all_accepts(source_p
);
1247 whowas_add_history(source_p
, 0);
1248 whowas_off_history(source_p
);
1250 monitor_signoff(source_p
);
1252 if(has_id(source_p
))
1253 del_from_id_hash(source_p
->id
, source_p
);
1255 del_from_hostname_hash(source_p
->orighost
, source_p
);
1256 del_from_client_hash(source_p
->name
, source_p
);
1257 remove_client_from_list(source_p
);
1261 * Assumes IsPerson(source_p) && !MyConnect(source_p)
1265 exit_remote_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1266 const char *comment
)
1268 exit_generic_client(client_p
, source_p
, from
, comment
);
1270 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1272 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->users
);
1275 if((source_p
->flags
& FLAGS_KILLED
) == 0)
1277 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
1278 ":%s QUIT :%s", use_id(source_p
), comment
);
1282 #ifdef DEBUG_EXITED_CLIENTS
1283 rb_dlinkAddAlloc(source_p
, &dead_remote_list
);
1285 rb_dlinkAddAlloc(source_p
, &dead_list
);
1287 return(CLIENT_EXITED
);
1291 * This assumes IsUnknown(source_p) == TRUE and MyConnect(source_p) == TRUE
1295 exit_unknown_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1296 const char *comment
)
1298 delete_auth_queries(source_p
);
1299 abort_blacklist_queries(source_p
);
1300 rb_dlinkDelete(&source_p
->localClient
->tnode
, &unknown_list
);
1302 if(!IsIOError(source_p
))
1303 sendto_one(source_p
, "ERROR :Closing Link: %s (%s)",
1304 source_p
->user
!= NULL
? source_p
->host
: "127.0.0.1",
1307 close_connection(source_p
);
1309 if(has_id(source_p
))
1310 del_from_id_hash(source_p
->id
, source_p
);
1312 del_from_hostname_hash(source_p
->host
, source_p
);
1313 del_from_client_hash(source_p
->name
, source_p
);
1314 remove_client_from_list(source_p
);
1316 rb_dlinkAddAlloc(source_p
, &dead_list
);
1318 /* Note that we don't need to add unknowns to the dead_list */
1319 return(CLIENT_EXITED
);
1323 exit_remote_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1324 const char *comment
)
1326 static char comment1
[(HOSTLEN
*2)+2];
1327 static char newcomment
[BUFSIZE
];
1328 struct Client
*target_p
;
1330 if(ConfigServerHide
.flatten_links
)
1331 strcpy(comment1
, "*.net *.split");
1334 strcpy(comment1
, source_p
->servptr
->name
);
1335 strcat(comment1
, " ");
1336 strcat(comment1
, source_p
->name
);
1339 snprintf(newcomment
, sizeof(newcomment
), "by %s: %s",
1340 from
->name
, comment
);
1342 if(source_p
->serv
!= NULL
)
1343 remove_dependents(client_p
, source_p
, from
, IsPerson(from
) ? newcomment
: comment
, comment1
);
1345 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1346 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1350 rb_dlinkFindDestroy(source_p
, &global_serv_list
);
1351 target_p
= source_p
->from
;
1353 if(target_p
!= NULL
&& IsServer(target_p
) && target_p
!= client_p
&&
1354 !IsMe(target_p
) && (source_p
->flags
& FLAGS_KILLED
) == 0)
1356 sendto_one(target_p
, ":%s SQUIT %s :%s",
1357 get_id(from
, target_p
), get_id(source_p
, target_p
),
1361 if(has_id(source_p
))
1362 del_from_id_hash(source_p
->id
, source_p
);
1364 del_from_client_hash(source_p
->name
, source_p
);
1365 remove_client_from_list(source_p
);
1366 scache_split(source_p
->serv
->nameinfo
);
1369 #ifdef DEBUG_EXITED_CLIENTS
1370 rb_dlinkAddAlloc(source_p
, &dead_remote_list
);
1372 rb_dlinkAddAlloc(source_p
, &dead_list
);
1378 qs_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1379 const char *comment
)
1381 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1382 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1386 rb_dlinkFindDestroy(source_p
, &global_serv_list
);
1388 if(has_id(source_p
))
1389 del_from_id_hash(source_p
->id
, source_p
);
1391 del_from_client_hash(source_p
->name
, source_p
);
1392 remove_client_from_list(source_p
);
1393 scache_split(source_p
->serv
->nameinfo
);
1396 rb_dlinkAddAlloc(source_p
, &dead_list
);
1401 exit_local_server(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1402 const char *comment
)
1404 static char comment1
[(HOSTLEN
*2)+2];
1405 static char newcomment
[BUFSIZE
];
1406 unsigned int sendk
, recvk
;
1408 rb_dlinkDelete(&source_p
->localClient
->tnode
, &serv_list
);
1409 rb_dlinkFindDestroy(source_p
, &global_serv_list
);
1411 sendk
= source_p
->localClient
->sendK
;
1412 recvk
= source_p
->localClient
->receiveK
;
1414 /* Always show source here, so the server notices show
1415 * which side initiated the split -- jilles
1417 snprintf(newcomment
, sizeof(newcomment
), "by %s: %s",
1418 from
== source_p
? me
.name
: from
->name
, comment
);
1419 if (!IsIOError(source_p
))
1420 sendto_one(source_p
, "SQUIT %s :%s", use_id(source_p
),
1422 if(client_p
!= NULL
&& source_p
!= client_p
&& !IsIOError(source_p
))
1424 sendto_one(source_p
, "ERROR :Closing Link: 127.0.0.1 %s (%s)",
1425 source_p
->name
, comment
);
1428 if(source_p
->servptr
&& source_p
->servptr
->serv
)
1429 rb_dlinkDelete(&source_p
->lnode
, &source_p
->servptr
->serv
->servers
);
1434 close_connection(source_p
);
1436 if(ConfigServerHide
.flatten_links
)
1437 strcpy(comment1
, "*.net *.split");
1440 strcpy(comment1
, source_p
->servptr
->name
);
1441 strcat(comment1
, " ");
1442 strcat(comment1
, source_p
->name
);
1445 if(source_p
->serv
!= NULL
)
1446 remove_dependents(client_p
, source_p
, from
, IsPerson(from
) ? newcomment
: comment
, comment1
);
1448 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "%s was connected"
1449 " for %ld seconds. %d/%d sendK/recvK.",
1450 source_p
->name
, (long) rb_current_time() - source_p
->localClient
->firsttime
, sendk
, recvk
);
1452 ilog(L_SERVER
, "%s was connected for %ld seconds. %d/%d sendK/recvK.",
1453 source_p
->name
, (long) rb_current_time() - source_p
->localClient
->firsttime
, sendk
, recvk
);
1455 if(has_id(source_p
))
1456 del_from_id_hash(source_p
->id
, source_p
);
1458 del_from_client_hash(source_p
->name
, source_p
);
1459 remove_client_from_list(source_p
);
1460 scache_split(source_p
->serv
->nameinfo
);
1463 rb_dlinkAddAlloc(source_p
, &dead_list
);
1469 * This assumes IsPerson(source_p) == TRUE && MyConnect(source_p) == TRUE
1473 exit_local_client(struct Client
*client_p
, struct Client
*source_p
, struct Client
*from
,
1474 const char *comment
)
1476 unsigned long on_for
;
1479 exit_generic_client(client_p
, source_p
, from
, comment
);
1480 clear_monitor(source_p
);
1482 s_assert(IsPerson(source_p
));
1483 rb_dlinkDelete(&source_p
->localClient
->tnode
, &lclient_list
);
1484 rb_dlinkDelete(&source_p
->lnode
, &me
.serv
->users
);
1486 if(IsOper(source_p
))
1487 rb_dlinkFindDestroy(source_p
, &local_oper_list
);
1489 sendto_realops_snomask(SNO_CCONN
, L_ALL
,
1490 "Client exiting: %s (%s@%s) [%s] [%s]",
1492 source_p
->username
, source_p
->host
, comment
,
1493 show_ip(NULL
, source_p
) ? source_p
->sockhost
: "255.255.255.255");
1495 sendto_realops_snomask(SNO_CCONNEXT
, L_ALL
,
1496 "CLIEXIT %s %s %s %s 0 %s",
1497 source_p
->name
, source_p
->username
, source_p
->host
,
1498 show_ip(NULL
, source_p
) ? source_p
->sockhost
: "255.255.255.255",
1501 on_for
= rb_current_time() - source_p
->localClient
->firsttime
;
1503 ilog(L_USER
, "%s (%3lu:%02lu:%02lu): %s!%s@%s %s %d/%d",
1504 rb_ctime(rb_current_time(), tbuf
, sizeof(tbuf
)), on_for
/ 3600,
1505 (on_for
% 3600) / 60, on_for
% 60,
1506 source_p
->name
, source_p
->username
, source_p
->host
,
1508 source_p
->localClient
->sendK
, source_p
->localClient
->receiveK
);
1510 sendto_one(source_p
, "ERROR :Closing Link: %s (%s)", source_p
->host
, comment
);
1511 close_connection(source_p
);
1513 if((source_p
->flags
& FLAGS_KILLED
) == 0)
1515 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
,
1516 ":%s QUIT :%s", use_id(source_p
), comment
);
1520 rb_dlinkAddAlloc(source_p
, &dead_list
);
1521 return(CLIENT_EXITED
);
1526 ** exit_client - This is old "m_bye". Name changed, because this is not a
1527 ** protocol function, but a general server utility function.
1529 ** This function exits a client of *any* type (user, server, etc)
1530 ** from this server. Also, this generates all necessary prototol
1531 ** messages that this exit may cause.
1533 ** 1) If the client is a local client, then this implicitly
1534 ** exits all other clients depending on this connection (e.g.
1535 ** remote clients having 'from'-field that points to this.
1537 ** 2) If the client is a remote client, then only this is exited.
1539 ** For convenience, this function returns a suitable value for
1540 ** m_function return value:
1542 ** CLIENT_EXITED if (client_p == source_p)
1543 ** 0 if (client_p != source_p)
1546 exit_client(struct Client
*client_p
, /* The local client originating the
1547 * exit or NULL, if this exit is
1548 * generated by this server for
1550 * This will not get any of the
1551 * generated messages. */
1552 struct Client
*source_p
, /* Client exiting */
1553 struct Client
*from
, /* Client firing off this Exit,
1555 const char *comment
/* Reason for the exit */
1558 hook_data_client_exit hdata
;
1559 if(IsClosing(source_p
))
1562 /* note, this HAS to be here, when we exit a client we attempt to
1563 * send them data, if this generates a write error we must *not* add
1564 * them to the abort list --fl
1566 SetClosing(source_p
);
1568 hdata
.local_link
= client_p
;
1569 hdata
.target
= source_p
;
1571 hdata
.comment
= comment
;
1572 call_hook(h_client_exit
, &hdata
);
1574 if(MyConnect(source_p
))
1576 /* Local clients of various types */
1577 if(IsPerson(source_p
))
1578 return exit_local_client(client_p
, source_p
, from
, comment
);
1579 else if(IsServer(source_p
))
1580 return exit_local_server(client_p
, source_p
, from
, comment
);
1581 /* IsUnknown || IsConnecting || IsHandShake */
1582 else if(!IsReject(source_p
))
1583 return exit_unknown_client(client_p
, source_p
, from
, comment
);
1588 if(IsPerson(source_p
))
1589 return exit_remote_client(client_p
, source_p
, from
, comment
);
1590 else if(IsServer(source_p
))
1591 return exit_remote_server(client_p
, source_p
, from
, comment
);
1598 * Count up local client memory
1601 /* XXX one common Client list now */
1603 count_local_client_memory(size_t * count
, size_t * local_client_memory_used
)
1606 rb_bh_usage(lclient_heap
, count
, NULL
, &lusage
, NULL
);
1607 *local_client_memory_used
= lusage
+ (*count
* (sizeof(void *) + sizeof(struct Client
)));
1611 * Count up remote client memory
1614 count_remote_client_memory(size_t * count
, size_t * remote_client_memory_used
)
1616 size_t lcount
, rcount
;
1617 rb_bh_usage(lclient_heap
, &lcount
, NULL
, NULL
, NULL
);
1618 rb_bh_usage(client_heap
, &rcount
, NULL
, NULL
, NULL
);
1619 *count
= rcount
- lcount
;
1620 *remote_client_memory_used
= *count
* (sizeof(void *) + sizeof(struct Client
));
1625 * accept processing, this adds a form of "caller ID" to ircd
1627 * If a client puts themselves into "caller ID only" mode,
1628 * only clients that match a client pointer they have put on
1629 * the accept list will be allowed to message them.
1631 * [ source.on_allow_list ] -> [ target1 ] -> [ target2 ]
1633 * [target.allow_list] -> [ source1 ] -> [source2 ]
1635 * i.e. a target will have a link list of source pointers it will allow
1636 * each source client then has a back pointer pointing back
1637 * to the client that has it on its accept list.
1638 * This allows for exit_one_client to remove these now bogus entries
1639 * from any client having an accept on them.
1644 * inputs - pointer to exiting client
1646 * side effects - Walk through given clients allow_list and on_allow_list
1647 * remove all references to this client
1650 del_all_accepts(struct Client
*client_p
)
1653 rb_dlink_node
*next_ptr
;
1654 struct Client
*target_p
;
1656 if(MyClient(client_p
) && client_p
->localClient
->allow_list
.head
)
1658 /* clear this clients accept list, and remove them from
1659 * everyones on_accept_list
1661 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->localClient
->allow_list
.head
)
1663 target_p
= ptr
->data
;
1664 rb_dlinkFindDestroy(client_p
, &target_p
->on_allow_list
);
1665 rb_dlinkDestroy(ptr
, &client_p
->localClient
->allow_list
);
1669 /* remove this client from everyones accept list */
1670 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, client_p
->on_allow_list
.head
)
1672 target_p
= ptr
->data
;
1673 rb_dlinkFindDestroy(client_p
, &target_p
->localClient
->allow_list
);
1674 rb_dlinkDestroy(ptr
, &client_p
->on_allow_list
);
1679 * show_ip() - asks if the true IP should be shown when source is
1680 * asking for info about target
1682 * Inputs - source_p who is asking
1683 * - target_p who do we want the info on
1684 * Output - returns 1 if clear IP can be shown, otherwise 0
1685 * Side Effects - none
1689 show_ip(struct Client
*source_p
, struct Client
*target_p
)
1691 if(IsAnyServer(target_p
))
1695 else if(IsIPSpoof(target_p
))
1697 /* source == NULL indicates message is being sent
1700 if(!ConfigFileEntry
.hide_spoof_ips
&&
1701 (source_p
== NULL
|| MyOper(source_p
)))
1705 else if(IsDynSpoof(target_p
) && (source_p
!= NULL
&& !IsOper(source_p
)))
1712 show_ip_conf(struct ConfItem
*aconf
, struct Client
*source_p
)
1714 if(IsConfDoSpoofIp(aconf
))
1716 if(!ConfigFileEntry
.hide_spoof_ips
&& MyOper(source_p
))
1726 show_ip_whowas(struct Whowas
*whowas
, struct Client
*source_p
)
1728 if(whowas
->flags
& WHOWAS_IP_SPOOFING
)
1729 if(ConfigFileEntry
.hide_spoof_ips
|| !MyOper(source_p
))
1731 if(whowas
->flags
& WHOWAS_DYNSPOOF
)
1732 if(!IsOper(source_p
))
1740 * inputs - pointer to client struct
1741 * output - pointer to struct User
1742 * side effects - add's an User information block to a client
1743 * if it was not previously allocated.
1746 make_user(struct Client
*client_p
)
1750 user
= client_p
->user
;
1753 user
= (struct User
*) rb_bh_alloc(user_heap
);
1755 client_p
->user
= user
;
1763 * inputs - pointer to client struct
1764 * output - pointer to server_t
1765 * side effects - add's an Server information block to a client
1766 * if it was not previously allocated.
1769 make_server(struct Client
*client_p
)
1771 struct Server
*serv
= client_p
->serv
;
1775 serv
= (struct Server
*) rb_malloc(sizeof(struct Server
));
1776 client_p
->serv
= serv
;
1778 return client_p
->serv
;
1784 * inputs - pointer to user struct
1785 * - pointer to client struct
1787 * side effects - Decrease user reference count by one and release block,
1788 * if count reaches 0
1791 free_user(struct User
*user
, struct Client
*client_p
)
1793 free_away(client_p
);
1795 if(--user
->refcnt
<= 0)
1798 rb_free((char *) user
->away
);
1802 if(user
->refcnt
< 0 || user
->invited
.head
|| user
->channel
.head
)
1804 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
1805 "* %#lx user (%s!%s@%s) %#lx %#lx %#lx %lu %d *",
1806 (unsigned long) client_p
,
1807 client_p
? client_p
->
1811 (unsigned long) user
,
1812 (unsigned long) user
->invited
.head
,
1813 (unsigned long) user
->channel
.head
,
1814 rb_dlink_list_length(&user
->channel
),
1816 s_assert(!user
->refcnt
);
1817 s_assert(!user
->invited
.head
);
1818 s_assert(!user
->channel
.head
);
1821 rb_bh_free(user_heap
, user
);
1826 allocate_away(struct Client
*client_p
)
1828 if(client_p
->user
->away
== NULL
)
1829 client_p
->user
->away
= rb_bh_alloc(away_heap
);
1834 free_away(struct Client
*client_p
)
1836 if(client_p
->user
!= NULL
&& client_p
->user
->away
!= NULL
) {
1837 rb_bh_free(away_heap
, client_p
->user
->away
);
1838 client_p
->user
->away
= NULL
;
1847 for(i
= 0; i
< 3; i
++)
1848 current_uid
[i
] = me
.id
[i
];
1850 for(i
= 3; i
< 9; i
++)
1851 current_uid
[i
] = 'A';
1853 current_uid
[9] = '\0';
1860 static int flipped
= 0;
1864 for(i
= 8; i
> 3; i
--)
1866 if(current_uid
[i
] == 'Z')
1868 current_uid
[i
] = '0';
1871 else if(current_uid
[i
] != '9')
1877 current_uid
[i
] = 'A';
1880 /* if this next if() triggers, we're fucked. */
1881 if(current_uid
[3] == 'Z')
1883 current_uid
[i
] = 'A';
1889 /* if this happens..well, i'm not sure what to say, but lets handle it correctly */
1890 if(rb_unlikely(flipped
))
1892 /* this slows down uid generation a bit... */
1893 if(find_id(current_uid
) != NULL
)
1901 * Close the physical connection. This function must make
1902 * MyConnect(client_p) == FALSE, and set client_p->from == NULL.
1905 close_connection(struct Client
*client_p
)
1907 s_assert(client_p
!= NULL
);
1908 if(client_p
== NULL
)
1911 s_assert(MyConnect(client_p
));
1912 if(!MyConnect(client_p
))
1915 if(IsServer(client_p
))
1917 struct server_conf
*server_p
;
1919 ServerStats
.is_sv
++;
1920 ServerStats
.is_sbs
+= client_p
->localClient
->sendB
;
1921 ServerStats
.is_sbr
+= client_p
->localClient
->receiveB
;
1922 ServerStats
.is_sti
+= (unsigned long long)(rb_current_time() - client_p
->localClient
->firsttime
);
1925 * If the connection has been up for a long amount of time, schedule
1926 * a 'quick' reconnect, else reset the next-connect cycle.
1928 if((server_p
= find_server_conf(client_p
->name
)) != NULL
)
1931 * Reschedule a faster reconnect, if this was a automatically
1932 * connected configuration entry. (Note that if we have had
1933 * a rehash in between, the status has been changed to
1934 * CONF_ILLEGAL). But only do this if it was a "good" link.
1936 server_p
->hold
= time(NULL
);
1938 (server_p
->hold
- client_p
->localClient
->lasttime
>
1939 HANGONGOODLINK
) ? HANGONRETRYDELAY
: ConFreq(server_p
->class);
1943 else if(IsClient(client_p
))
1945 ServerStats
.is_cl
++;
1946 ServerStats
.is_cbs
+= client_p
->localClient
->sendB
;
1947 ServerStats
.is_cbr
+= client_p
->localClient
->receiveB
;
1948 ServerStats
.is_cti
+= (unsigned long long)(rb_current_time() - client_p
->localClient
->firsttime
);
1951 ServerStats
.is_ni
++;
1953 del_from_cli_connid_hash(client_p
);
1955 if(client_p
->localClient
->F
!= NULL
)
1957 /* attempt to flush any pending dbufs. Evil, but .. -- adrian */
1958 if(!IsIOError(client_p
))
1959 send_queued(client_p
);
1961 rb_close(client_p
->localClient
->F
);
1962 client_p
->localClient
->F
= NULL
;
1965 rb_linebuf_donebuf(&client_p
->localClient
->buf_sendq
);
1966 rb_linebuf_donebuf(&client_p
->localClient
->buf_recvq
);
1967 detach_conf(client_p
);
1969 /* XXX shouldnt really be done here. */
1970 detach_server_conf(client_p
);
1972 client_p
->from
= NULL
; /* ...this should catch them! >:) --msa */
1973 ClearMyConnect(client_p
);
1974 SetIOError(client_p
);
1980 error_exit_client(struct Client
*client_p
, int error
)
1983 * ...hmm, with non-blocking sockets we might get
1984 * here from quite valid reasons, although.. why
1985 * would select report "data available" when there
1986 * wasn't... so, this must be an error anyway... --msa
1987 * actually, EOF occurs when read() returns 0 and
1988 * in due course, select() returns that fd as ready
1989 * for reading even though it ends up being an EOF. -avalon
1992 int current_error
= rb_get_sockerr(client_p
->localClient
->F
);
1994 SetIOError(client_p
);
1996 if(IsServer(client_p
) || IsHandshake(client_p
))
2000 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) && !IsServer(client_p
) ? L_NETWIDE
: L_ALL
,
2001 "Server %s closed the connection",
2004 ilog(L_SERVER
, "Server %s closed the connection",
2005 log_client_name(client_p
, SHOW_IP
));
2009 sendto_realops_snomask(SNO_GENERAL
, is_remote_connect(client_p
) && !IsServer(client_p
) ? L_NETWIDE
: L_ALL
,
2010 "Lost connection to %s: %s",
2011 client_p
->name
, strerror(current_error
));
2012 ilog(L_SERVER
, "Lost connection to %s: %s",
2013 log_client_name(client_p
, SHOW_IP
), strerror(current_error
));
2018 rb_strlcpy(errmsg
, "Remote host closed the connection", sizeof(errmsg
));
2020 snprintf(errmsg
, sizeof(errmsg
), "Read error: %s", strerror(current_error
));
2022 exit_client(client_p
, client_p
, &me
, errmsg
);