conn.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2007 Alexander Barton (alex@barton.de)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. *
  11. * Connection management
  12. */
  13. #define CONN_MODULE
  14. #include "portab.h"
  15. #include "io.h"
  16. static char UNUSED id[] = "$Id: conn.c,v 1.221 2008/02/26 22:04:17 fw Exp $";
  17. #include "imp.h"
  18. #include <assert.h>
  19. #ifdef PROTOTYPES
  20. # include <stdarg.h>
  21. #else
  22. # include <varargs.h>
  23. #endif
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <sys/socket.h>
  30. #include <sys/time.h>
  31. #include <sys/types.h>
  32. #include <time.h>
  33. #include <netinet/in.h>
  34. #ifdef HAVE_NETINET_IP_H
  35. # include <netinet/ip.h>
  36. #endif
  37. #ifdef HAVE_STDINT_H
  38. # include <stdint.h> /* e.g. for Mac OS X */
  39. #endif
  40. #ifdef TCPWRAP
  41. # include <tcpd.h> /* for TCP Wrappers */
  42. #endif
  43. #include "array.h"
  44. #include "defines.h"
  45. #include "resolve.h"
  46. #include "exp.h"
  47. #include "conn.h"
  48. #include "imp.h"
  49. #include "ngircd.h"
  50. #include "client.h"
  51. #include "conf.h"
  52. #include "conn-zip.h"
  53. #include "conn-func.h"
  54. #include "log.h"
  55. #include "parse.h"
  56. #include "tool.h"
  57. #ifdef ZEROCONF
  58. # include "rendezvous.h"
  59. #endif
  60. #include "exp.h"
  61. #define SERVER_WAIT (NONE - 1)
  62. static bool Handle_Write PARAMS(( CONN_ID Idx ));
  63. static bool Conn_Write PARAMS(( CONN_ID Idx, char *Data, size_t Len ));
  64. static int New_Connection PARAMS(( int Sock ));
  65. static CONN_ID Socket2Index PARAMS(( int Sock ));
  66. static void Read_Request PARAMS(( CONN_ID Idx ));
  67. static bool Handle_Buffer PARAMS(( CONN_ID Idx ));
  68. static void Check_Connections PARAMS(( void ));
  69. static void Check_Servers PARAMS(( void ));
  70. static void Init_Conn_Struct PARAMS(( CONN_ID Idx ));
  71. static bool Init_Socket PARAMS(( int Sock ));
  72. static void New_Server PARAMS(( int Server, ng_ipaddr_t *dest ));
  73. static void Simple_Message PARAMS(( int Sock, const char *Msg ));
  74. static int NewListener PARAMS(( const char *listen_addr, UINT16 Port ));
  75. static array My_Listeners;
  76. static array My_ConnArray;
  77. #ifdef TCPWRAP
  78. int allow_severity = LOG_INFO;
  79. int deny_severity = LOG_ERR;
  80. #endif
  81. static void server_login PARAMS((CONN_ID idx));
  82. static void cb_Read_Resolver_Result PARAMS(( int sock, UNUSED short what));
  83. static void cb_Connect_to_Server PARAMS(( int sock, UNUSED short what));
  84. static void cb_clientserver PARAMS((int sock, short what));
  85. static void
  86. cb_listen(int sock, short irrelevant)
  87. {
  88. (void) irrelevant;
  89. New_Connection( sock );
  90. }
  91. static void
  92. cb_connserver(int sock, UNUSED short what)
  93. {
  94. int res, err;
  95. socklen_t sock_len;
  96. CONN_ID idx = Socket2Index( sock );
  97. if (idx <= NONE) {
  98. LogDebug("cb_connserver wants to write on unknown socket?!");
  99. io_close(sock);
  100. return;
  101. }
  102. assert( what & IO_WANTWRITE);
  103. /* connect() finished, get result. */
  104. sock_len = sizeof( err );
  105. res = getsockopt( My_Connections[idx].sock, SOL_SOCKET, SO_ERROR, &err, &sock_len );
  106. assert( sock_len == sizeof( err ));
  107. /* Error while connecting? */
  108. if ((res != 0) || (err != 0)) {
  109. if (res != 0)
  110. Log(LOG_CRIT, "getsockopt (connection %d): %s!",
  111. idx, strerror(errno));
  112. else
  113. Log(LOG_CRIT,
  114. "Can't connect socket to \"%s:%d\" (connection %d): %s!",
  115. My_Connections[idx].host,
  116. Conf_Server[Conf_GetServer(idx)].port,
  117. idx, strerror(err));
  118. res = Conf_GetServer(idx);
  119. assert(res >= 0);
  120. Conn_Close(idx, "Can't connect!", NULL, false);
  121. if (res < 0)
  122. return;
  123. if (ng_ipaddr_af(&Conf_Server[res].dst_addr[0])) {
  124. /* more addresses to try... */
  125. New_Server(res, &Conf_Server[res].dst_addr[0]);
  126. /* connection to dst_addr[0] in progress, remove this address... */
  127. Conf_Server[res].dst_addr[0] = Conf_Server[res].dst_addr[1];
  128. memset(&Conf_Server[res].dst_addr[1], 0, sizeof(&Conf_Server[res].dst_addr[1]));
  129. }
  130. return;
  131. }
  132. res = Conf_GetServer(idx);
  133. assert(res >= 0);
  134. if (res >= 0) /* connect succeeded, remove all additional addresses */
  135. memset(&Conf_Server[res].dst_addr, 0, sizeof(&Conf_Server[res].dst_addr));
  136. Conn_OPTION_DEL( &My_Connections[idx], CONN_ISCONNECTING );
  137. server_login(idx);
  138. }
  139. static void
  140. server_login(CONN_ID idx)
  141. {
  142. Log( LOG_INFO, "Connection %d with \"%s:%d\" established. Now logging in ...", idx,
  143. My_Connections[idx].host, Conf_Server[Conf_GetServer( idx )].port );
  144. io_event_setcb( My_Connections[idx].sock, cb_clientserver);
  145. io_event_add( My_Connections[idx].sock, IO_WANTREAD|IO_WANTWRITE);
  146. /* Send PASS and SERVER command to peer */
  147. Conn_WriteStr( idx, "PASS %s %s", Conf_Server[Conf_GetServer( idx )].pwd_out, NGIRCd_ProtoID );
  148. Conn_WriteStr( idx, "SERVER %s :%s", Conf_ServerName, Conf_ServerInfo );
  149. }
  150. static void
  151. cb_clientserver(int sock, short what)
  152. {
  153. CONN_ID idx = Socket2Index( sock );
  154. if (idx <= NONE) {
  155. #ifdef DEBUG
  156. Log(LOG_WARNING, "WTF: cb_clientserver wants to write on unknown socket?!");
  157. #endif
  158. io_close(sock);
  159. return;
  160. }
  161. if (what & IO_WANTREAD)
  162. Read_Request( idx );
  163. if (what & IO_WANTWRITE)
  164. Handle_Write( idx );
  165. }
  166. GLOBAL void
  167. Conn_Init( void )
  168. {
  169. /* Modul initialisieren: statische Strukturen "ausnullen". */
  170. CONN_ID i;
  171. /* Speicher fuer Verbindungs-Pool anfordern */
  172. Pool_Size = CONNECTION_POOL;
  173. if( Conf_MaxConnections > 0 )
  174. {
  175. /* konfiguriertes Limit beachten */
  176. if( Pool_Size > Conf_MaxConnections ) Pool_Size = Conf_MaxConnections;
  177. }
  178. if (!array_alloc(&My_ConnArray, sizeof(CONNECTION), (size_t)Pool_Size)) {
  179. Log( LOG_EMERG, "Can't allocate memory! [Conn_Init]" );
  180. exit( 1 );
  181. }
  182. /* FIXME: My_Connetions/Pool_Size is needed by other parts of the
  183. * code; remove them! */
  184. My_Connections = (CONNECTION*) array_start(&My_ConnArray);
  185. LogDebug("Allocated connection pool for %d items (%ld bytes).",
  186. array_length(&My_ConnArray, sizeof( CONNECTION )), array_bytes(&My_ConnArray));
  187. assert( array_length(&My_ConnArray, sizeof( CONNECTION )) >= (size_t) Pool_Size);
  188. array_free( &My_Listeners );
  189. /* Connection-Struktur initialisieren */
  190. for( i = 0; i < Pool_Size; i++ ) Init_Conn_Struct( i );
  191. /* Global write counter */
  192. WCounter = 0;
  193. } /* Conn_Init */
  194. GLOBAL void
  195. Conn_Exit( void )
  196. {
  197. /* Modul abmelden: alle noch offenen Connections
  198. * schliessen und freigeben. */
  199. CONN_ID idx;
  200. LogDebug("Shutting down all connections ..." );
  201. Conn_ExitListeners();
  202. /* Sockets schliessen */
  203. for( idx = 0; idx < Pool_Size; idx++ ) {
  204. if( My_Connections[idx].sock > NONE ) {
  205. Conn_Close( idx, NULL, NGIRCd_SignalRestart ?
  206. "Server going down (restarting)":"Server going down", true );
  207. }
  208. }
  209. array_free(&My_ConnArray);
  210. My_Connections = NULL;
  211. Pool_Size = 0;
  212. io_library_shutdown();
  213. } /* Conn_Exit */
  214. static unsigned int
  215. ports_initlisteners(array *a, const char *listen_addr, void (*func)(int,short))
  216. {
  217. unsigned int created = 0;
  218. size_t len;
  219. int fd;
  220. UINT16 *port;
  221. len = array_length(a, sizeof (UINT16));
  222. port = array_start(a);
  223. while (len--) {
  224. fd = NewListener(listen_addr, *port);
  225. if (fd < 0) {
  226. port++;
  227. continue;
  228. }
  229. if (!io_event_create( fd, IO_WANTREAD, func )) {
  230. Log( LOG_ERR, "io_event_create(): Could not add listening fd %d (port %u): %s!",
  231. fd, (unsigned int) *port, strerror(errno));
  232. close(fd);
  233. port++;
  234. continue;
  235. }
  236. created++;
  237. port++;
  238. }
  239. return created;
  240. }
  241. GLOBAL unsigned int
  242. Conn_InitListeners( void )
  243. {
  244. /* Initialize ports on which the server should accept connections */
  245. unsigned int created = 0;
  246. char *copy, *listen_addr;
  247. if (!io_library_init(CONNECTION_POOL)) {
  248. Log(LOG_EMERG, "Cannot initialize IO routines: %s", strerror(errno));
  249. return -1;
  250. }
  251. assert(Conf_ListenAddress);
  252. /* can't use Conf_ListenAddress directly, see below */
  253. copy = strdup(Conf_ListenAddress);
  254. if (!copy) {
  255. Log(LOG_CRIT, "Cannot copy %s: %s", Conf_ListenAddress, strerror(errno));
  256. return 0;
  257. }
  258. listen_addr = strtok(copy, ",");
  259. while (listen_addr) {
  260. ngt_TrimStr(listen_addr);
  261. if (*listen_addr)
  262. created += ports_initlisteners(&Conf_ListenPorts, listen_addr, cb_listen);
  263. listen_addr = strtok(NULL, ",");
  264. }
  265. /*
  266. * can't free() Conf_ListenAddress here. On /REHASH, if the config file
  267. * cannot be re-loaded, we'd end up with a NULL Conf_ListenAddress.
  268. * Instead, free() takes place in conf.c, before the config file
  269. * is being parsed.
  270. */
  271. free(copy);
  272. return created;
  273. } /* Conn_InitListeners */
  274. GLOBAL void
  275. Conn_ExitListeners( void )
  276. {
  277. /* Close down all listening sockets */
  278. int *fd;
  279. size_t arraylen;
  280. #ifdef ZEROCONF
  281. Rendezvous_UnregisterListeners( );
  282. #endif
  283. arraylen = array_length(&My_Listeners, sizeof (int));
  284. Log( LOG_INFO, "Shutting down all listening sockets (%d total)...", arraylen );
  285. fd = array_start(&My_Listeners);
  286. while(arraylen--) {
  287. assert(fd != NULL);
  288. assert(*fd >= 0);
  289. io_close(*fd);
  290. LogDebug("Listening socket %d closed.", *fd );
  291. fd++;
  292. }
  293. array_free(&My_Listeners);
  294. } /* Conn_ExitListeners */
  295. static bool
  296. InitSinaddrListenAddr(ng_ipaddr_t *addr, const char *listen_addrstr, UINT16 Port)
  297. {
  298. bool ret;
  299. ret = ng_ipaddr_init(addr, listen_addrstr, Port);
  300. if (!ret) {
  301. assert(listen_addrstr);
  302. Log(LOG_CRIT, "Can't bind to [%s]:%u: can't convert ip address \"%s\"",
  303. listen_addrstr, Port, listen_addrstr);
  304. }
  305. return ret;
  306. }
  307. static void
  308. set_v6_only(int af, int sock)
  309. {
  310. #if defined(IPV6_V6ONLY) && defined(WANT_IPV6)
  311. int on = 1;
  312. if (af != AF_INET6)
  313. return;
  314. if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)))
  315. Log(LOG_ERR, "Could not set IPV6_V6ONLY: %s", strerror(errno));
  316. #else
  317. (void)af;
  318. (void)sock;
  319. #endif
  320. }
  321. /* return new listening port file descriptor or -1 on failure */
  322. static int
  323. NewListener(const char *listen_addr, UINT16 Port)
  324. {
  325. /* Create new listening socket on specified port */
  326. ng_ipaddr_t addr;
  327. int sock, af;
  328. #ifdef ZEROCONF
  329. char name[CLIENT_ID_LEN], *info;
  330. #endif
  331. if (!InitSinaddrListenAddr(&addr, listen_addr, Port))
  332. return -1;
  333. af = ng_ipaddr_af(&addr);
  334. sock = socket(af, SOCK_STREAM, 0);
  335. if( sock < 0 ) {
  336. Log(LOG_CRIT, "Can't create socket (af %d) : %s!", af, strerror(errno));
  337. return -1;
  338. }
  339. set_v6_only(af, sock);
  340. if (!Init_Socket(sock))
  341. return -1;
  342. if (bind(sock, (struct sockaddr *)&addr, ng_ipaddr_salen(&addr)) != 0) {
  343. Log(LOG_CRIT, "Can't bind socket to address %s:%d - %s",
  344. ng_ipaddr_tostr(&addr), Port, strerror(errno));
  345. close(sock);
  346. return -1;
  347. }
  348. if( listen( sock, 10 ) != 0 ) {
  349. Log( LOG_CRIT, "Can't listen on socket: %s!", strerror( errno ));
  350. close( sock );
  351. return -1;
  352. }
  353. /* keep fd in list so we can close it when ngircd restarts/shuts down */
  354. if (!array_catb( &My_Listeners,(char*) &sock, sizeof(int) )) {
  355. Log( LOG_CRIT, "Can't add socket to My_Listeners array: %s!", strerror( errno ));
  356. close( sock );
  357. return -1;
  358. }
  359. Log(LOG_INFO, "Now listening on [%s]:%d (socket %d).", ng_ipaddr_tostr(&addr), Port, sock);
  360. #ifdef ZEROCONF
  361. /* Get best server description text */
  362. if( ! Conf_ServerInfo[0] ) info = Conf_ServerName;
  363. else
  364. {
  365. /* Use server info string */
  366. info = NULL;
  367. if( Conf_ServerInfo[0] == '[' )
  368. {
  369. /* Cut off leading hostname part in "[]" */
  370. info = strchr( Conf_ServerInfo, ']' );
  371. if( info )
  372. {
  373. info++;
  374. while( *info == ' ' ) info++;
  375. }
  376. }
  377. if( ! info ) info = Conf_ServerInfo;
  378. }
  379. /* Add port number to description if non-standard */
  380. if (Port != 6667)
  381. snprintf(name, sizeof name, "%s (port %u)", info,
  382. (unsigned int)Port);
  383. else
  384. strlcpy(name, info, sizeof name);
  385. /* Register service */
  386. Rendezvous_Register( name, MDNS_TYPE, Port );
  387. #endif
  388. return sock;
  389. } /* NewListener */
  390. GLOBAL void
  391. Conn_Handler( void )
  392. {
  393. /* "Main Loop.": Loop until a signal (for shutdown or restart) arrives.
  394. * Call io_dispatch() to check for read/writeable sockets every second
  395. * Wait for status change on pending connections (e.g: when the hostname has been resolved)
  396. * check for penalty/timeouts
  397. * handle input buffers
  398. */
  399. int i;
  400. unsigned int wdatalen;
  401. struct timeval tv;
  402. time_t t;
  403. bool timeout;
  404. while(( ! NGIRCd_SignalQuit ) && ( ! NGIRCd_SignalRestart )) {
  405. timeout = true;
  406. #ifdef ZEROCONF
  407. Rendezvous_Handler( );
  408. #endif
  409. /* Should the configuration be reloaded? */
  410. if (NGIRCd_SignalRehash) {
  411. NGIRCd_Rehash( );
  412. }
  413. /* Check configured servers and established links */
  414. Check_Servers( );
  415. Check_Connections( );
  416. t = time( NULL );
  417. /* noch volle Lese-Buffer suchen */
  418. for( i = 0; i < Pool_Size; i++ ) {
  419. if(( My_Connections[i].sock > NONE ) && ( array_bytes(&My_Connections[i].rbuf) > 0 ) &&
  420. ( My_Connections[i].delaytime < t ))
  421. {
  422. /* Kann aus dem Buffer noch ein Befehl extrahiert werden? */
  423. if (Handle_Buffer( i )) timeout = false;
  424. }
  425. }
  426. /* noch volle Schreib-Puffer suchen */
  427. for( i = 0; i < Pool_Size; i++ ) {
  428. if ( My_Connections[i].sock <= NONE )
  429. continue;
  430. wdatalen = (unsigned int)array_bytes(&My_Connections[i].wbuf);
  431. #ifdef ZLIB
  432. if (( wdatalen > 0 ) || ( array_bytes(&My_Connections[i].zip.wbuf)> 0 ))
  433. #else
  434. if ( wdatalen > 0 )
  435. #endif
  436. {
  437. /* Socket der Verbindung in Set aufnehmen */
  438. io_event_add( My_Connections[i].sock, IO_WANTWRITE );
  439. }
  440. }
  441. /* von welchen Sockets koennte gelesen werden? */
  442. for (i = 0; i < Pool_Size; i++ ) {
  443. if ( My_Connections[i].sock <= NONE )
  444. continue;
  445. if (Resolve_INPROGRESS(&My_Connections[i].res_stat)) {
  446. /* wait for completion of Resolver Sub-Process */
  447. io_event_del( My_Connections[i].sock, IO_WANTREAD );
  448. continue;
  449. }
  450. if ( Conn_OPTION_ISSET( &My_Connections[i], CONN_ISCONNECTING ))
  451. continue; /* wait for completion of connect() */
  452. if( My_Connections[i].delaytime > t ) {
  453. /* Fuer die Verbindung ist eine "Penalty-Zeit" gesetzt */
  454. io_event_del( My_Connections[i].sock, IO_WANTREAD );
  455. continue;
  456. }
  457. io_event_add( My_Connections[i].sock, IO_WANTREAD );
  458. }
  459. /* (re-)set timeout - tv_sec/usec are undefined after io_dispatch() returns */
  460. tv.tv_usec = 0;
  461. tv.tv_sec = timeout ? 1 : 0;
  462. /* wait for activity */
  463. i = io_dispatch( &tv );
  464. if (i == -1 && errno != EINTR ) {
  465. Log(LOG_EMERG, "Conn_Handler(): io_dispatch(): %s!", strerror(errno));
  466. Log(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
  467. exit( 1 );
  468. }
  469. }
  470. if( NGIRCd_SignalQuit ) Log( LOG_NOTICE|LOG_snotice, "Server going down NOW!" );
  471. else if( NGIRCd_SignalRestart ) Log( LOG_NOTICE|LOG_snotice, "Server restarting NOW!" );
  472. } /* Conn_Handler */
  473. /**
  474. * Write a text string into the socket of a connection.
  475. * This function automatically appends CR+LF to the string and validates that
  476. * the result is a valid IRC message (oversized messages are shortened, for
  477. * example). Then it calls the Conn_Write() function to do the actual sending.
  478. * @param Idx Index fo the connection.
  479. * @param Format Format string, see printf().
  480. * @return true on success, false otherwise.
  481. */
  482. #ifdef PROTOTYPES
  483. GLOBAL bool
  484. Conn_WriteStr( CONN_ID Idx, char *Format, ... )
  485. #else
  486. GLOBAL bool
  487. Conn_WriteStr( Idx, Format, va_alist )
  488. CONN_ID Idx;
  489. char *Format;
  490. va_dcl
  491. #endif
  492. {
  493. char buffer[COMMAND_LEN];
  494. size_t len;
  495. bool ok;
  496. va_list ap;
  497. assert( Idx > NONE );
  498. assert( Format != NULL );
  499. #ifdef PROTOTYPES
  500. va_start( ap, Format );
  501. #else
  502. va_start( ap );
  503. #endif
  504. if (vsnprintf( buffer, COMMAND_LEN - 2, Format, ap ) >= COMMAND_LEN - 2 ) {
  505. /*
  506. * The string that should be written to the socket is longer
  507. * than the allowed size of COMMAND_LEN bytes (including both
  508. * the CR and LF characters). This can be caused by the
  509. * IRC_WriteXXX() functions when the prefix of this server had
  510. * to be added to an already "quite long" command line which
  511. * has been received from a regular IRC client, for example.
  512. *
  513. * We are not allowed to send such "oversized" messages to
  514. * other servers and clients, see RFC 2812 2.3 and 2813 3.3
  515. * ("these messages SHALL NOT exceed 512 characters in length,
  516. * counting all characters including the trailing CR-LF").
  517. *
  518. * So we have a big problem here: we should send more bytes
  519. * to the network than we are allowed to and we don't know
  520. * the originator (any more). The "old" behaviour of blaming
  521. * the receiver ("next hop") is a bad idea (it could be just
  522. * an other server only routing the message!), so the only
  523. * option left is to shorten the string and to hope that the
  524. * result is still somewhat useful ...
  525. * -alex-
  526. */
  527. strcpy (buffer + sizeof(buffer) - strlen(CUT_TXTSUFFIX) - 2 - 1,
  528. CUT_TXTSUFFIX);
  529. }
  530. #ifdef SNIFFER
  531. if (NGIRCd_Sniffer)
  532. Log(LOG_DEBUG, " -> connection %d: '%s'.", Idx, buffer);
  533. #endif
  534. len = strlcat( buffer, "\r\n", sizeof( buffer ));
  535. ok = Conn_Write(Idx, buffer, len);
  536. My_Connections[Idx].msg_out++;
  537. va_end( ap );
  538. return ok;
  539. } /* Conn_WriteStr */
  540. /**
  541. * Append Data to the outbound write buffer of a connection.
  542. * @param Idx Index of the connection.
  543. * @param Data pointer to the data.
  544. * @param Len length of Data.
  545. * @return true on success, false otherwise.
  546. */
  547. static bool
  548. Conn_Write( CONN_ID Idx, char *Data, size_t Len )
  549. {
  550. CLIENT *c;
  551. size_t writebuf_limit = WRITEBUFFER_LEN;
  552. assert( Idx > NONE );
  553. assert( Data != NULL );
  554. assert( Len > 0 );
  555. c = Conn_GetClient(Idx);
  556. assert( c != NULL);
  557. /* Servers do get special write buffer limits, so they can generate
  558. * all the messages that are required while peering. */
  559. if (Client_Type(c) == CLIENT_SERVER)
  560. writebuf_limit = WRITEBUFFER_SLINK_LEN;
  561. /* Is the socket still open? A previous call to Conn_Write()
  562. * may have closed the connection due to a fatal error.
  563. * In this case it is sufficient to return an error, as well. */
  564. if( My_Connections[Idx].sock <= NONE ) {
  565. LogDebug("Skipped write on closed socket (connection %d).", Idx);
  566. return false;
  567. }
  568. #ifdef ZLIB
  569. if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
  570. /* Compressed link:
  571. * Zip_Buffer() does all the dirty work for us: it flushes
  572. * the (pre-)compression buffers if required and handles
  573. * all error conditions. */
  574. if (!Zip_Buffer(Idx, Data, Len))
  575. return false;
  576. }
  577. else
  578. #endif
  579. {
  580. /* Uncompressed link:
  581. * Check if outbound buffer has enough space for the data. */
  582. if (array_bytes(&My_Connections[Idx].wbuf) + Len >=
  583. writebuf_limit) {
  584. /* Buffer is full, flush it. Handle_Write deals with
  585. * low-level errors, if any. */
  586. if (!Handle_Write(Idx))
  587. return false;
  588. }
  589. /* When the write buffer is still too big after flushing it,
  590. * the connection will be killed. */
  591. if (array_bytes(&My_Connections[Idx].wbuf) + Len >=
  592. writebuf_limit) {
  593. Log(LOG_NOTICE,
  594. "Write buffer overflow (connection %d, size %lu byte)!",
  595. Idx,
  596. (unsigned long)array_bytes(&My_Connections[Idx].wbuf));
  597. Conn_Close(Idx, "Write buffer overflow!", NULL, false);
  598. return false;
  599. }
  600. /* Copy data to write buffer */
  601. if (!array_catb(&My_Connections[Idx].wbuf, Data, Len))
  602. return false;
  603. My_Connections[Idx].bytes_out += Len;
  604. }
  605. /* Adjust global write counter */
  606. WCounter += Len;
  607. return true;
  608. } /* Conn_Write */
  609. GLOBAL void
  610. Conn_Close( CONN_ID Idx, char *LogMsg, char *FwdMsg, bool InformClient )
  611. {
  612. /* Close connection. Open pipes of asyncronous resolver
  613. * sub-processes are closed down. */
  614. CLIENT *c;
  615. char *txt;
  616. double in_k, out_k;
  617. UINT16 port;
  618. #ifdef ZLIB
  619. double in_z_k, out_z_k;
  620. int in_p, out_p;
  621. #endif
  622. assert( Idx > NONE );
  623. /* Is this link already shutting down? */
  624. if( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ISCLOSING )) {
  625. /* Conn_Close() has been called recursively for this link;
  626. * probabe reason: Handle_Write() failed -- see below. */
  627. LogDebug("Recursive request to close connection: %d", Idx );
  628. return;
  629. }
  630. assert( My_Connections[Idx].sock > NONE );
  631. /* Mark link as "closing" */
  632. Conn_OPTION_ADD( &My_Connections[Idx], CONN_ISCLOSING );
  633. if (LogMsg)
  634. txt = LogMsg;
  635. else
  636. txt = FwdMsg;
  637. if (! txt)
  638. txt = "Reason unknown";
  639. port = ng_ipaddr_getport(&My_Connections[Idx].addr);
  640. Log(LOG_INFO, "Shutting down connection %d (%s) with %s:%d ...", Idx,
  641. LogMsg ? LogMsg : FwdMsg, My_Connections[Idx].host, port);
  642. /* Search client, if any */
  643. c = Conn_GetClient( Idx );
  644. /* Should the client be informed? */
  645. if (InformClient) {
  646. #ifndef STRICT_RFC
  647. /* Send statistics to client if registered as user: */
  648. if ((c != NULL) && (Client_Type(c) == CLIENT_USER)) {
  649. Conn_WriteStr( Idx,
  650. ":%s NOTICE %s :%sConnection statistics: client %.1f kb, server %.1f kb.",
  651. Client_ID(Client_ThisServer()), Client_ID(c),
  652. NOTICE_TXTPREFIX,
  653. (double)My_Connections[Idx].bytes_in / 1024,
  654. (double)My_Connections[Idx].bytes_out / 1024);
  655. }
  656. #endif
  657. /* Send ERROR to client (see RFC!) */
  658. if (FwdMsg)
  659. Conn_WriteStr(Idx, "ERROR :%s", FwdMsg);
  660. else
  661. Conn_WriteStr(Idx, "ERROR :Closing connection.");
  662. }
  663. /* Try to write out the write buffer. Note: Handle_Write() eventually
  664. * removes the CLIENT structure associated with this connection if an
  665. * error occurs! So we have to re-check if there is still an valid
  666. * CLIENT structure after calling Handle_Write() ...*/
  667. (void)Handle_Write( Idx );
  668. /* Search client, if any (re-check!) */
  669. c = Conn_GetClient( Idx );
  670. /* Shut down socket */
  671. if (! io_close(My_Connections[Idx].sock)) {
  672. /* Oops, we can't close the socket!? This is ... ugly! */
  673. Log(LOG_CRIT,
  674. "Error closing connection %d (socket %d) with %s:%d - %s! (ignored)",
  675. Idx, My_Connections[Idx].sock, My_Connections[Idx].host,
  676. port, strerror(errno));
  677. }
  678. /* Mark socket as invalid: */
  679. My_Connections[Idx].sock = NONE;
  680. /* If there is still a client, unregister it now */
  681. if (c)
  682. Client_Destroy(c, LogMsg, FwdMsg, true);
  683. /* Calculate statistics and log information */
  684. in_k = (double)My_Connections[Idx].bytes_in / 1024;
  685. out_k = (double)My_Connections[Idx].bytes_out / 1024;
  686. #ifdef ZLIB
  687. if (Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP)) {
  688. in_z_k = (double)My_Connections[Idx].zip.bytes_in / 1024;
  689. out_z_k = (double)My_Connections[Idx].zip.bytes_out / 1024;
  690. /* Make sure that no division by zero can occur during
  691. * the calculation of in_p and out_p: in_z_k and out_z_k
  692. * are non-zero, that's guaranteed by the protocol until
  693. * compression can be enabled. */
  694. if (! in_z_k)
  695. in_z_k = in_k;
  696. if (! out_z_k)
  697. out_z_k = out_k;
  698. in_p = (int)(( in_k * 100 ) / in_z_k );
  699. out_p = (int)(( out_k * 100 ) / out_z_k );
  700. Log(LOG_INFO,
  701. "Connection %d with %s:%d closed (in: %.1fk/%.1fk/%d%%, out: %.1fk/%.1fk/%d%%).",
  702. Idx, My_Connections[Idx].host, port,
  703. in_k, in_z_k, in_p, out_k, out_z_k, out_p);
  704. }
  705. else
  706. #endif
  707. {
  708. Log(LOG_INFO,
  709. "Connection %d with %s:%d closed (in: %.1fk, out: %.1fk).",
  710. Idx, My_Connections[Idx].host, port,
  711. in_k, out_k);
  712. }
  713. /* cancel running resolver */
  714. if (Resolve_INPROGRESS(&My_Connections[Idx].res_stat))
  715. Resolve_Shutdown(&My_Connections[Idx].res_stat);
  716. /* Servers: Modify time of next connect attempt? */
  717. Conf_UnsetServer( Idx );
  718. #ifdef ZLIB
  719. /* Clean up zlib, if link was compressed */
  720. if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP )) {
  721. inflateEnd( &My_Connections[Idx].zip.in );
  722. deflateEnd( &My_Connections[Idx].zip.out );
  723. array_free(&My_Connections[Idx].zip.rbuf);
  724. array_free(&My_Connections[Idx].zip.wbuf);
  725. }
  726. #endif
  727. array_free(&My_Connections[Idx].rbuf);
  728. array_free(&My_Connections[Idx].wbuf);
  729. /* Clean up connection structure (=free it) */
  730. Init_Conn_Struct( Idx );
  731. LogDebug("Shutdown of connection %d completed.", Idx );
  732. } /* Conn_Close */
  733. GLOBAL void
  734. Conn_SyncServerStruct( void )
  735. {
  736. /* Synchronize server structures (connection IDs):
  737. * connections <-> configuration */
  738. CLIENT *client;
  739. CONN_ID i;
  740. int c;
  741. for( i = 0; i < Pool_Size; i++ ) {
  742. /* Established connection? */
  743. if (My_Connections[i].sock < 0)
  744. continue;
  745. /* Server connection? */
  746. client = Conn_GetClient( i );
  747. if(( ! client ) || ( Client_Type( client ) != CLIENT_SERVER )) continue;
  748. for( c = 0; c < MAX_SERVERS; c++ )
  749. {
  750. /* Configured server? */
  751. if( ! Conf_Server[c].host[0] ) continue;
  752. /* Duplicate? */
  753. if( strcmp( Conf_Server[c].name, Client_ID( client )) == 0 )
  754. Conf_Server[c].conn_id = i;
  755. }
  756. }
  757. } /* SyncServerStruct */
  758. /**
  759. * Send out data of write buffer; connect new sockets.
  760. */
  761. static bool
  762. Handle_Write( CONN_ID Idx )
  763. {
  764. ssize_t len;
  765. size_t wdatalen;
  766. assert( Idx > NONE );
  767. if ( My_Connections[Idx].sock < 0 ) {
  768. LogDebug("Handle_Write() on closed socket, connection %d", Idx);
  769. return false;
  770. }
  771. assert( My_Connections[Idx].sock > NONE );
  772. wdatalen = array_bytes(&My_Connections[Idx].wbuf );
  773. #ifdef ZLIB
  774. if (wdatalen == 0) {
  775. /* Write buffer is empty, so we try to flush the compression
  776. * buffer and get some data to work with from there :-) */
  777. if (!Zip_Flush(Idx))
  778. return false;
  779. /* Now the write buffer most probably has changed: */
  780. wdatalen = array_bytes(&My_Connections[Idx].wbuf);
  781. }
  782. #endif
  783. if (wdatalen == 0) {
  784. /* Still no data, fine. */
  785. io_event_del(My_Connections[Idx].sock, IO_WANTWRITE );
  786. return true;
  787. }
  788. LogDebug
  789. ("Handle_Write() called for connection %d, %ld bytes pending ...",
  790. Idx, wdatalen);
  791. len = write(My_Connections[Idx].sock,
  792. array_start(&My_Connections[Idx].wbuf), wdatalen );
  793. if( len < 0 ) {
  794. if (errno == EAGAIN || errno == EINTR)
  795. return true;
  796. Log(LOG_ERR, "Write error on connection %d (socket %d): %s!",
  797. Idx, My_Connections[Idx].sock, strerror(errno));
  798. Conn_Close(Idx, "Write error!", NULL, false);
  799. return false;
  800. }
  801. /* move any data not yet written to beginning */
  802. array_moveleft(&My_Connections[Idx].wbuf, 1, (size_t)len);
  803. return true;
  804. } /* Handle_Write */
  805. static int
  806. Count_Connections(ng_ipaddr_t *a)
  807. {
  808. int i, cnt;
  809. cnt = 0;
  810. for (i = 0; i < Pool_Size; i++) {
  811. if (My_Connections[i].sock <= NONE)
  812. continue;
  813. if (ng_ipaddr_ipequal(&My_Connections[i].addr, a))
  814. cnt++;
  815. }
  816. return cnt;
  817. } /* Count_Connections */
  818. static int
  819. New_Connection( int Sock )
  820. {
  821. /* Neue Client-Verbindung von Listen-Socket annehmen und
  822. * CLIENT-Struktur anlegen. */
  823. #ifdef TCPWRAP
  824. struct request_info req;
  825. #endif
  826. ng_ipaddr_t new_addr;
  827. char ip_str[NG_INET_ADDRSTRLEN];
  828. int new_sock, new_sock_len, new_Pool_Size;
  829. CLIENT *c;
  830. long cnt;
  831. assert( Sock > NONE );
  832. /* Connection auf Listen-Socket annehmen */
  833. new_sock_len = (int)sizeof(new_addr);
  834. new_sock = accept(Sock, (struct sockaddr *)&new_addr,
  835. (socklen_t *)&new_sock_len);
  836. if (new_sock < 0) {
  837. Log(LOG_CRIT, "Can't accept connection: %s!", strerror(errno));
  838. return -1;
  839. }
  840. if (!ng_ipaddr_tostr_r(&new_addr, ip_str)) {
  841. Log(LOG_CRIT, "fd %d: Can't convert IP address!", new_sock);
  842. Simple_Message(new_sock, "ERROR :Internal Server Error");
  843. close(new_sock);
  844. }
  845. #ifdef TCPWRAP
  846. /* Validate socket using TCP Wrappers */
  847. request_init( &req, RQ_DAEMON, PACKAGE_NAME, RQ_FILE, new_sock, RQ_CLIENT_SIN, &new_addr, NULL );
  848. fromhost(&req);
  849. if (!hosts_access(&req)) {
  850. Log (deny_severity, "Refused connection from %s (by TCP Wrappers)!", ip_str);
  851. Simple_Message( new_sock, "ERROR :Connection refused" );
  852. close( new_sock );
  853. return -1;
  854. }
  855. #endif
  856. /* Socket initialisieren */
  857. if (!Init_Socket( new_sock ))
  858. return -1;
  859. /* Check IP-based connection limit */
  860. cnt = Count_Connections(&new_addr);
  861. if ((Conf_MaxConnectionsIP > 0) && (cnt >= Conf_MaxConnectionsIP)) {
  862. /* Access denied, too many connections from this IP address! */
  863. Log( LOG_ERR, "Refused connection from %s: too may connections (%ld) from this IP address!", ip_str, cnt);
  864. Simple_Message( new_sock, "ERROR :Connection refused, too many connections from your IP address!" );
  865. close( new_sock );
  866. return -1;
  867. }
  868. if( new_sock >= Pool_Size ) {
  869. new_Pool_Size = new_sock + 1;
  870. /* No free Connection Structures, check if we may accept further connections */
  871. if ((( Conf_MaxConnections > 0) && Pool_Size >= Conf_MaxConnections) ||
  872. (new_Pool_Size < Pool_Size))
  873. {
  874. Log( LOG_ALERT, "Can't accept connection: limit (%d) reached!", Pool_Size );
  875. Simple_Message( new_sock, "ERROR :Connection limit reached" );
  876. close( new_sock );
  877. return -1;
  878. }
  879. if (!array_alloc(&My_ConnArray, sizeof(CONNECTION),
  880. (size_t)new_sock)) {
  881. Log( LOG_EMERG, "Can't allocate memory! [New_Connection]" );
  882. Simple_Message( new_sock, "ERROR: Internal error" );
  883. close( new_sock );
  884. return -1;
  885. }
  886. LogDebug("Bumped connection pool to %ld items (internal: %ld items, %ld bytes)",
  887. new_sock, array_length(&My_ConnArray, sizeof(CONNECTION)), array_bytes(&My_ConnArray));
  888. /* Adjust pointer to new block */
  889. My_Connections = array_start(&My_ConnArray);
  890. while (Pool_Size < new_Pool_Size)
  891. Init_Conn_Struct(Pool_Size++);
  892. }
  893. /* register callback */
  894. if (!io_event_create( new_sock, IO_WANTREAD, cb_clientserver)) {
  895. Log(LOG_ALERT, "Can't accept connection: io_event_create failed!");
  896. Simple_Message(new_sock, "ERROR :Internal error");
  897. close(new_sock);
  898. return -1;
  899. }
  900. c = Client_NewLocal(new_sock, ip_str, CLIENT_UNKNOWN, false );
  901. if( ! c ) {
  902. Log(LOG_ALERT, "Can't accept connection: can't create client structure!");
  903. Simple_Message(new_sock, "ERROR :Internal error");
  904. io_close(new_sock);
  905. return -1;
  906. }
  907. Init_Conn_Struct( new_sock );
  908. My_Connections[new_sock].sock = new_sock;
  909. My_Connections[new_sock].addr = new_addr;
  910. My_Connections[new_sock].client = c;
  911. Log( LOG_INFO, "Accepted connection %d from %s:%d on socket %d.", new_sock,
  912. ip_str, ng_ipaddr_getport(&new_addr), Sock);
  913. /* Hostnamen ermitteln */
  914. strlcpy(My_Connections[new_sock].host, ip_str, sizeof(My_Connections[new_sock].host));
  915. Client_SetHostname(c, My_Connections[new_sock].host);
  916. if (!Conf_NoDNS)
  917. Resolve_Addr(&My_Connections[new_sock].res_stat, &new_addr,
  918. My_Connections[new_sock].sock, cb_Read_Resolver_Result);
  919. Conn_SetPenalty(new_sock, 4);
  920. return new_sock;
  921. } /* New_Connection */
  922. static CONN_ID
  923. Socket2Index( int Sock )
  924. {
  925. /* zum Socket passende Connection suchen */
  926. assert( Sock >= 0 );
  927. if( Sock >= Pool_Size || My_Connections[Sock].sock != Sock ) {
  928. /* die Connection wurde vermutlich (wegen eines
  929. * Fehlers) bereits wieder abgebaut ... */
  930. LogDebug("Socket2Index: can't get connection for socket %d!", Sock);
  931. return NONE;
  932. }
  933. return Sock;
  934. } /* Socket2Index */
  935. /**
  936. * Read data from the network to the read buffer. If an error occures,
  937. * the socket of this connection will be shut down.
  938. */
  939. static void
  940. Read_Request( CONN_ID Idx )
  941. {
  942. ssize_t len;
  943. char readbuf[READBUFFER_LEN];
  944. CLIENT *c;
  945. assert( Idx > NONE );
  946. assert( My_Connections[Idx].sock > NONE );
  947. #ifdef ZLIB
  948. if ((array_bytes(&My_Connections[Idx].rbuf) >= READBUFFER_LEN) ||
  949. (array_bytes(&My_Connections[Idx].zip.rbuf) >= READBUFFER_LEN))
  950. #else
  951. if (array_bytes(&My_Connections[Idx].rbuf) >= READBUFFER_LEN)
  952. #endif
  953. {
  954. /* Read buffer is full */
  955. Log(LOG_ERR,
  956. "Receive buffer overflow (connection %d): %d bytes!",
  957. Idx, array_bytes(&My_Connections[Idx].rbuf));
  958. Conn_Close( Idx, "Receive buffer overflow!", NULL, false );
  959. return;
  960. }
  961. len = read(My_Connections[Idx].sock, readbuf, sizeof(readbuf));
  962. if (len == 0) {
  963. Log(LOG_INFO, "%s:%u (%s) is closing the connection ...",
  964. My_Connections[Idx].host,
  965. (unsigned int) ng_ipaddr_getport(&My_Connections[Idx].addr),
  966. ng_ipaddr_tostr(&My_Connections[Idx].addr));
  967. Conn_Close(Idx,
  968. "Socket closed!", "Client closed connection",
  969. false);
  970. return;
  971. }
  972. if (len < 0) {
  973. if( errno == EAGAIN ) return;
  974. Log(LOG_ERR, "Read error on connection %d (socket %d): %s!",
  975. Idx, My_Connections[Idx].sock, strerror(errno));
  976. Conn_Close(Idx, "Read error!", "Client closed connection",
  977. false);
  978. return;
  979. }
  980. #ifdef ZLIB
  981. if (Conn_OPTION_ISSET(&My_Connections[Idx], CONN_ZIP)) {
  982. if (!array_catb(&My_Connections[Idx].zip.rbuf, readbuf,
  983. (size_t) len)) {
  984. Log(LOG_ERR,
  985. "Could not append recieved data to zip input buffer (connn %d): %d bytes!",
  986. Idx, len);
  987. Conn_Close(Idx, "Receive buffer overflow!", NULL,
  988. false);
  989. return;
  990. }
  991. } else
  992. #endif
  993. {
  994. if (!array_catb( &My_Connections[Idx].rbuf, readbuf, len)) {
  995. Log( LOG_ERR, "Could not append recieved data to input buffer (connn %d): %d bytes!", Idx, len );
  996. Conn_Close( Idx, "Receive buffer overflow!", NULL, false );
  997. }
  998. }
  999. /* Update connection statistics */
  1000. My_Connections[Idx].bytes_in += len;
  1001. /* Update timestamp of last data received if this connection is
  1002. * registered as a user, server or service connection. Don't update
  1003. * otherwise, so users have at least Conf_PongTimeout seconds time to
  1004. * register with the IRC server -- see Check_Connections().
  1005. * Set "lastping", too, so we can handle time shifts backwards ... */
  1006. c = Conn_GetClient(Idx);
  1007. if (c && (Client_Type(c) == CLIENT_USER
  1008. || Client_Type(c) == CLIENT_SERVER
  1009. || Client_Type(c) == CLIENT_SERVICE)) {
  1010. My_Connections[Idx].lastdata = time(NULL);
  1011. My_Connections[Idx].lastping = My_Connections[Idx].lastdata;
  1012. }
  1013. /* Look at the data in the (read-) buffer of this connection */
  1014. Handle_Buffer(Idx);
  1015. } /* Read_Request */
  1016. static bool
  1017. Handle_Buffer( CONN_ID Idx )
  1018. {
  1019. /* Handle Data in Connections Read-Buffer.
  1020. * Return true if a reuqest was handled, false otherwise (also returned on errors). */
  1021. #ifndef STRICT_RFC
  1022. char *ptr1, *ptr2, *first_eol;
  1023. #endif
  1024. char *ptr;
  1025. size_t len, delta;
  1026. bool result;
  1027. time_t starttime;
  1028. #ifdef ZLIB
  1029. bool old_z;
  1030. #endif
  1031. starttime = time(NULL);
  1032. result = false;
  1033. for (;;) {
  1034. /* Check penalty */
  1035. if( My_Connections[Idx].delaytime > starttime) return result;
  1036. #ifdef ZLIB
  1037. /* unpack compressed data */
  1038. if ( Conn_OPTION_ISSET( &My_Connections[Idx], CONN_ZIP ))
  1039. if( ! Unzip_Buffer( Idx )) return false;
  1040. #endif
  1041. if (0 == array_bytes(&My_Connections[Idx].rbuf))
  1042. break;
  1043. if (!array_cat0_temporary(&My_Connections[Idx].rbuf)) /* make sure buf is NULL terminated */
  1044. return false;
  1045. /* A Complete Request end with CR+LF, see RFC 2812. */
  1046. delta = 2;
  1047. ptr = strstr( array_start(&My_Connections[Idx].rbuf), "\r\n" );
  1048. #ifndef STRICT_RFC
  1049. /* Check for non-RFC-compliant request (only CR or LF)?
  1050. * Unfortunately, there are quite a few clients out there
  1051. * that do this -- e. g. mIRC, BitchX, and Trillian :-( */
  1052. ptr1 = strchr(array_start(&My_Connections[Idx].rbuf), '\r');
  1053. ptr2 = strchr(array_start(&My_Connections[Idx].rbuf), '\n');
  1054. if (ptr) {
  1055. /* Check if there is a single CR or LF _before_ the
  1056. * corerct CR+LF line terminator: */
  1057. first_eol = ptr1 < ptr2 ? ptr1 : ptr2;
  1058. if (first_eol < ptr) {
  1059. /* Single CR or LF before CR+LF found */
  1060. ptr = first_eol;
  1061. delta = 1;
  1062. }
  1063. } else if (ptr1 || ptr2) {
  1064. /* No CR+LF terminated command found, but single
  1065. * CR or LF found ... */
  1066. if (ptr1 && ptr2)
  1067. ptr = ptr1 < ptr2 ? ptr1 : ptr2;
  1068. else
  1069. ptr = ptr1 ? ptr1 : ptr2;
  1070. delta = 1;
  1071. }
  1072. #endif
  1073. if( ! ptr )
  1074. break;
  1075. /* End of request found */
  1076. *ptr = '\0';
  1077. len = ( ptr - (char*) array_start(&My_Connections[Idx].rbuf)) + delta;
  1078. if( len > ( COMMAND_LEN - 1 )) {
  1079. /* Request must not exceed 512 chars (incl. CR+LF!), see
  1080. * RFC 2812. Disconnect Client if this happens. */
  1081. Log( LOG_ERR, "Request too long (connection %d): %d bytes (max. %d expected)!",
  1082. Idx, array_bytes(&My_Connections[Idx].rbuf), COMMAND_LEN - 1 );
  1083. Conn_Close( Idx, NULL, "Request too long", true );
  1084. return false;
  1085. }
  1086. if (len <= 2) { /* request was empty (only '\r\n') */
  1087. array_moveleft(&My_Connections[Idx].rbuf, 1, delta); /* delta is either 1 or 2 */
  1088. break;
  1089. }
  1090. #ifdef ZLIB
  1091. /* remember if stream is already compressed */
  1092. old_z = My_Connections[Idx].options & CONN_ZIP;
  1093. #endif
  1094. My_Connections[Idx].msg_in++;
  1095. if (!Parse_Request(Idx, (char*)array_start(&My_Connections[Idx].rbuf) ))
  1096. return false;
  1097. result = true;
  1098. array_moveleft(&My_Connections[Idx].rbuf, 1, len);
  1099. LogDebug("Connection %d: %d bytes left in read buffer.",
  1100. Idx, array_bytes(&My_Connections[Idx].rbuf));
  1101. #ifdef ZLIB
  1102. if(( ! old_z ) && ( My_Connections[Idx].options & CONN_ZIP ) &&
  1103. ( array_bytes(&My_Connections[Idx].rbuf) > 0 ))
  1104. {
  1105. /* The last Command activated Socket-Compression.
  1106. * Data that was read after that needs to be copied to Unzip-buf
  1107. * for decompression */
  1108. if (!array_copy( &My_Connections[Idx].zip.rbuf, &My_Connections[Idx].rbuf ))
  1109. return false;
  1110. array_trunc(&My_Connections[Idx].rbuf);
  1111. LogDebug("Moved already received data (%u bytes) to uncompression buffer.",
  1112. array_bytes(&My_Connections[Idx].zip.rbuf));
  1113. }
  1114. #endif /* ZLIB */
  1115. }
  1116. return result;
  1117. } /* Handle_Buffer */
  1118. static void
  1119. Check_Connections(void)
  1120. {
  1121. /* check if connections are alive. if not, play PING-PONG first.
  1122. * if this doesn't help either, disconnect client. */
  1123. CLIENT *c;
  1124. CONN_ID i;
  1125. for (i = 0; i < Pool_Size; i++) {
  1126. if (My_Connections[i].sock < 0)
  1127. continue;
  1128. c = Conn_GetClient(i);
  1129. if (c && ((Client_Type(c) == CLIENT_USER)
  1130. || (Client_Type(c) == CLIENT_SERVER)
  1131. || (Client_Type(c) == CLIENT_SERVICE))) {
  1132. /* connected User, Server or Service */
  1133. if (My_Connections[i].lastping >
  1134. My_Connections[i].lastdata) {
  1135. /* We already sent a ping */
  1136. if (My_Connections[i].lastping <
  1137. time(NULL) - Conf_PongTimeout) {
  1138. /* Timeout */
  1139. LogDebug
  1140. ("Connection %d: Ping timeout: %d seconds.",
  1141. i, Conf_PongTimeout);
  1142. Conn_Close(i, NULL, "Ping timeout",
  1143. true);
  1144. }
  1145. } else if (My_Connections[i].lastdata <
  1146. time(NULL) - Conf_PingTimeout) {
  1147. /* We need to send a PING ... */
  1148. LogDebug("Connection %d: sending PING ...", i);
  1149. My_Connections[i].lastping = time(NULL);
  1150. Conn_WriteStr(i, "PING :%s",
  1151. Client_ID(Client_ThisServer()));
  1152. }
  1153. } else {
  1154. /* The connection is not fully established yet, so
  1155. * we don't do the PING-PONG game here but instead
  1156. * disconnect the client after "a short time" if it's
  1157. * still not registered. */
  1158. if (My_Connections[i].lastdata <
  1159. time(NULL) - Conf_PongTimeout) {
  1160. LogDebug
  1161. ("Unregistered connection %d timed out ...",
  1162. i);
  1163. Conn_Close(i, NULL, "Timeout", false);
  1164. }
  1165. }
  1166. }
  1167. } /* Check_Connections */
  1168. static void
  1169. Check_Servers( void )
  1170. {
  1171. /* Check if we can establish further server links */
  1172. int i, n;
  1173. time_t time_now;
  1174. /* Check all configured servers */
  1175. for( i = 0; i < MAX_SERVERS; i++ ) {
  1176. /* Valid outgoing server which isn't already connected or disabled? */
  1177. if(( ! Conf_Server[i].host[0] ) || ( ! Conf_Server[i].port > 0 ) ||
  1178. ( Conf_Server[i].conn_id > NONE ) || ( Conf_Server[i].flags & CONF_SFLAG_DISABLED ))
  1179. continue;
  1180. /* Is there already a connection in this group? */
  1181. if( Conf_Server[i].group > NONE ) {
  1182. for (n = 0; n < MAX_SERVERS; n++) {
  1183. if (n == i) continue;
  1184. if ((Conf_Server[n].conn_id != NONE) &&
  1185. (Conf_Server[n].group == Conf_Server[i].group))
  1186. break;
  1187. }
  1188. if (n < MAX_SERVERS) continue;
  1189. }
  1190. /* Check last connect attempt? */
  1191. time_now = time(NULL);
  1192. if( Conf_Server[i].lasttry > (time_now - Conf_ConnectRetry))
  1193. continue;
  1194. /* Okay, try to connect now */
  1195. Conf_Server[i].lasttry = time_now;
  1196. Conf_Server[i].conn_id = SERVER_WAIT;
  1197. assert(Resolve_Getfd(&Conf_Server[i].res_stat) < 0);
  1198. Resolve_Name(&Conf_Server[i].res_stat, Conf_Server[i].host, cb_Connect_to_Server);
  1199. }
  1200. } /* Check_Servers */
  1201. static void
  1202. New_Server( int Server , ng_ipaddr_t *dest)
  1203. {
  1204. /* Establish new server link */
  1205. char ip_str[NG_INET_ADDRSTRLEN];
  1206. int af_dest, res, new_sock;
  1207. CLIENT *c;
  1208. assert( Server > NONE );
  1209. if (!ng_ipaddr_tostr_r(dest, ip_str)) {
  1210. Log(LOG_WARNING, "New_Server: Could not convert IP to string");
  1211. return;
  1212. }
  1213. Log( LOG_INFO, "Establishing connection to \"%s\", %s, port %d ... ",
  1214. Conf_Server[Server].host, ip_str, Conf_Server[Server].port );
  1215. af_dest = ng_ipaddr_af(dest);
  1216. new_sock = socket(af_dest, SOCK_STREAM, 0);
  1217. if (new_sock < 0) {
  1218. Log( LOG_CRIT, "Can't create socket (af %d) : %s!", af_dest, strerror( errno ));
  1219. return;
  1220. }
  1221. if (!Init_Socket(new_sock))
  1222. return;
  1223. /* is a bind address configured? */
  1224. res = ng_ipaddr_af(&Conf_Server[Server].bind_addr);
  1225. /* if yes, bind now. If it fails, warn and let connect() pick a source address */
  1226. if (res && bind(new_sock, (struct sockaddr *) &Conf_Server[Server].bind_addr,
  1227. ng_ipaddr_salen(&Conf_Server[Server].bind_addr)))
  1228. {
  1229. ng_ipaddr_tostr_r(&Conf_Server[Server].bind_addr, ip_str);
  1230. Log(LOG_WARNING, "Can't bind socket to %s: %s!", ip_str, strerror(errno));
  1231. }
  1232. ng_ipaddr_setport(dest, Conf_Server[Server].port);
  1233. res = connect(new_sock, (struct sockaddr *) dest, ng_ipaddr_salen(dest));
  1234. if(( res != 0 ) && ( errno != EINPROGRESS )) {
  1235. Log( LOG_CRIT, "Can't connect socket: %s!", strerror( errno ));
  1236. close( new_sock );
  1237. return;
  1238. }
  1239. if (!array_alloc(&My_ConnArray, sizeof(CONNECTION), (size_t)new_sock)) {
  1240. Log(LOG_ALERT,
  1241. "Cannot allocate memory for server connection (socket %d)",
  1242. new_sock);
  1243. close( new_sock );
  1244. return;
  1245. }
  1246. My_Connections = array_start(&My_ConnArray);
  1247. assert(My_Connections[new_sock].sock <= 0);
  1248. Init_Conn_Struct(new_sock);
  1249. ng_ipaddr_tostr_r(dest, ip_str);
  1250. c = Client_NewLocal(new_sock, ip_str, CLIENT_UNKNOWNSERVER, false);
  1251. if (!c) {
  1252. Log( LOG_ALERT, "Can't establish connection: can't create client structure!" );
  1253. close( new_sock );
  1254. return;
  1255. }
  1256. Client_SetIntroducer( c, c );
  1257. Client_SetToken( c, TOKEN_OUTBOUND );
  1258. /* Register connection */
  1259. Conf_Server[Server].conn_id = new_sock;
  1260. My_Connections[new_sock].sock = new_sock;
  1261. My_Connections[new_sock].addr = *dest;
  1262. My_Connections[new_sock].client = c;
  1263. strlcpy( My_Connections[new_sock].host, Conf_Server[Server].host,
  1264. sizeof(My_Connections[new_sock].host ));
  1265. /* Register new socket */
  1266. if (!io_event_create( new_sock, IO_WANTWRITE, cb_connserver)) {
  1267. Log( LOG_ALERT, "io_event_create(): could not add fd %d", strerror(errno));
  1268. Conn_Close( new_sock, "io_event_create() failed", NULL, false );
  1269. Init_Conn_Struct( new_sock );
  1270. Conf_Server[Server].conn_id = NONE;
  1271. }
  1272. LogDebug("Registered new connection %d on socket %d.",
  1273. new_sock, My_Connections[new_sock].sock );
  1274. Conn_OPTION_ADD( &My_Connections[new_sock], CONN_ISCONNECTING );
  1275. } /* New_Server */
  1276. /**
  1277. * Initialize connection structure.
  1278. */
  1279. static void
  1280. Init_Conn_Struct(CONN_ID Idx)
  1281. {
  1282. time_t now = time(NULL);
  1283. memset(&My_Connections[Idx], 0, sizeof(CONNECTION));
  1284. My_Connections[Idx].sock = -1;
  1285. My_Connections[Idx].signon = now;
  1286. My_Connections[Idx].lastdata = now;
  1287. My_Connections[Idx].lastprivmsg = now;
  1288. Resolve_Init(&My_Connections[Idx].res_stat);
  1289. } /* Init_Conn_Struct */
  1290. static bool
  1291. Init_Socket( int Sock )
  1292. {
  1293. /* Initialize socket (set options) */
  1294. int value;
  1295. if (!io_setnonblock(Sock)) {
  1296. Log( LOG_CRIT, "Can't enable non-blocking mode for socket: %s!", strerror( errno ));
  1297. close( Sock );
  1298. return false;
  1299. }
  1300. /* Don't block this port after socket shutdown */
  1301. value = 1;
  1302. if( setsockopt( Sock, SOL_SOCKET, SO_REUSEADDR, &value, (socklen_t)sizeof( value )) != 0 )
  1303. {
  1304. Log( LOG_ERR, "Can't set socket option SO_REUSEADDR: %s!", strerror( errno ));
  1305. /* ignore this error */
  1306. }
  1307. /* Set type of service (TOS) */
  1308. #if defined(IP_TOS) && defined(IPTOS_LOWDELAY)
  1309. value = IPTOS_LOWDELAY;
  1310. LogDebug("Setting option IP_TOS on socket %d to IPTOS_LOWDELAY (%d).", Sock, value );
  1311. if( setsockopt( Sock, SOL_IP, IP_TOS, &value, (socklen_t)sizeof( value )) != 0 )
  1312. {
  1313. Log( LOG_ERR, "Can't set socket option IP_TOS: %s!", strerror( errno ));
  1314. /* ignore this error */
  1315. }
  1316. #endif
  1317. return true;
  1318. } /* Init_Socket */
  1319. static void
  1320. cb_Connect_to_Server(int fd, UNUSED short events)
  1321. {
  1322. /* Read result of resolver sub-process from pipe and start connection */
  1323. int i;
  1324. size_t len;
  1325. ng_ipaddr_t dest_addrs[4]; /* we can handle at most 3; but we read up to
  1326. four so we can log the 'more than we can handle'
  1327. condition */
  1328. LogDebug("Resolver: Got forward lookup callback on fd %d, events %d", fd, events);
  1329. for (i=0; i < MAX_SERVERS; i++) {
  1330. if (Resolve_Getfd(&Conf_Server[i].res_stat) == fd )
  1331. break;
  1332. }
  1333. if( i >= MAX_SERVERS) {
  1334. /* Ops, no matching server found?! */
  1335. io_close( fd );
  1336. LogDebug("Resolver: Got Forward Lookup callback for unknown server!?");
  1337. return;
  1338. }
  1339. /* Read result from pipe */
  1340. len = Resolve_Read(&Conf_Server[i].res_stat, dest_addrs, sizeof(dest_addrs));
  1341. if (len == 0)
  1342. return;
  1343. assert((len % sizeof(ng_ipaddr_t)) == 0);
  1344. LogDebug("Got result from resolver: %u structs (%u bytes).", len/sizeof(ng_ipaddr_t), len);
  1345. memset(&Conf_Server[i].dst_addr, 0, sizeof(&Conf_Server[i].dst_addr));
  1346. if (len > sizeof(ng_ipaddr_t)) {
  1347. /* more than one address for this hostname, remember them
  1348. * in case first address is unreachable/not available */
  1349. len -= sizeof(ng_ipaddr_t);
  1350. if (len > sizeof(&Conf_Server[i].dst_addr)) {
  1351. len = sizeof(&Conf_Server[i].dst_addr);
  1352. Log(LOG_NOTICE, "Notice: Resolver returned more IP Addresses for host than we can handle,"
  1353. " additional addresses dropped");
  1354. }
  1355. memcpy(&Conf_Server[i].dst_addr, &dest_addrs[1], len);
  1356. }
  1357. /* connect() */
  1358. New_Server(i, dest_addrs);
  1359. } /* cb_Read_Forward_Lookup */
  1360. static void
  1361. cb_Read_Resolver_Result( int r_fd, UNUSED short events )
  1362. {
  1363. /* Read result of resolver sub-process from pipe and update the
  1364. * apropriate connection/client structure(s): hostname and/or
  1365. * IDENT user name.*/
  1366. CLIENT *c;
  1367. int i;
  1368. size_t len;
  1369. char *identptr;
  1370. #ifdef IDENTAUTH
  1371. char readbuf[HOST_LEN + 2 + CLIENT_USER_LEN];
  1372. #else
  1373. char readbuf[HOST_LEN + 1];
  1374. #endif
  1375. LogDebug("Resolver: Got callback on fd %d, events %d", r_fd, events );
  1376. /* Search associated connection ... */
  1377. for( i = 0; i < Pool_Size; i++ ) {
  1378. if(( My_Connections[i].sock != NONE )
  1379. && ( Resolve_Getfd(&My_Connections[i].res_stat) == r_fd ))
  1380. break;
  1381. }
  1382. if( i >= Pool_Size ) {
  1383. /* Ops, none found? Probably the connection has already
  1384. * been closed!? We'll ignore that ... */
  1385. io_close( r_fd );
  1386. LogDebug("Resolver: Got callback for unknown connection!?");
  1387. return;
  1388. }
  1389. /* Read result from pipe */
  1390. len = Resolve_Read(&My_Connections[i].res_stat, readbuf, sizeof readbuf -1);
  1391. if (len == 0)
  1392. return;
  1393. readbuf[len] = '\0';
  1394. identptr = strchr(readbuf, '\n');
  1395. assert(identptr != NULL);
  1396. if (!identptr) {
  1397. Log( LOG_CRIT, "Resolver: Got malformed result!");
  1398. return;
  1399. }
  1400. *identptr = '\0';
  1401. LogDebug("Got result from resolver: \"%s\" (%u bytes read).", readbuf, len);
  1402. /* Okay, we got a complete result: this is a host name for outgoing
  1403. * connections and a host name and IDENT user name (if enabled) for
  1404. * incoming connections.*/
  1405. assert ( My_Connections[i].sock >= 0 );
  1406. /* Incoming connection. Search client ... */
  1407. c = Conn_GetClient( i );
  1408. assert( c != NULL );
  1409. /* Only update client information of unregistered clients */
  1410. if( Client_Type( c ) == CLIENT_UNKNOWN ) {
  1411. strlcpy(My_Connections[i].host, readbuf, sizeof( My_Connections[i].host));
  1412. Client_SetHostname( c, readbuf);
  1413. #ifdef IDENTAUTH
  1414. ++identptr;
  1415. if (*identptr) {
  1416. Log(LOG_INFO, "IDENT lookup for connection %d: \"%s\".", i, identptr);
  1417. Client_SetUser(c, identptr, true);
  1418. } else {
  1419. Log(LOG_INFO, "IDENT lookup for connection %d: no result.", i);
  1420. }
  1421. #endif
  1422. }
  1423. #ifdef DEBUG
  1424. else Log( LOG_DEBUG, "Resolver: discarding result for already registered connection %d.", i );
  1425. #endif
  1426. /* Reset penalty time */
  1427. Conn_ResetPenalty( i );
  1428. } /* cb_Read_Resolver_Result */
  1429. static void
  1430. Simple_Message( int Sock, const char *Msg )
  1431. {
  1432. char buf[COMMAND_LEN];
  1433. size_t len;
  1434. /* Write "simple" message to socket, without using compression
  1435. * or even the connection write buffers. Used e.g. for error
  1436. * messages by New_Connection(). */
  1437. assert( Sock > NONE );
  1438. assert( Msg != NULL );
  1439. strlcpy( buf, Msg, sizeof buf - 2);
  1440. len = strlcat( buf, "\r\n", sizeof buf);
  1441. (void)write(Sock, buf, len);
  1442. } /* Simple_Error */
  1443. GLOBAL CLIENT *
  1444. Conn_GetClient( CONN_ID Idx )
  1445. {
  1446. /* return Client-Structure that belongs to the local Connection Idx.
  1447. * If none is found, return NULL.
  1448. */
  1449. CONNECTION *c;
  1450. assert( Idx >= 0 );
  1451. c = array_get(&My_ConnArray, sizeof (CONNECTION), (size_t)Idx);
  1452. assert(c != NULL);
  1453. return c ? c->client : NULL;
  1454. }
  1455. /* -eof- */