net.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  1. /*
  2. * net.c -- handles:
  3. * all raw network i/o
  4. *
  5. */
  6. #include <fcntl.h>
  7. #include "common.h"
  8. #include "net.h"
  9. #include "misc.h"
  10. #include "main.h"
  11. #include "debug.h"
  12. #include "dccutil.h"
  13. #include "crypt.h"
  14. #include "egg_timer.h"
  15. #include "traffic.h"
  16. #include <limits.h>
  17. #include <string.h>
  18. #include <netdb.h>
  19. #include <signal.h>
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <setjmp.h>
  23. #if HAVE_SYS_SELECT_H
  24. # include <sys/select.h>
  25. #endif /* HAVE_SYS_SELECT_H */
  26. #include <netinet/in.h>
  27. #include <arpa/inet.h>
  28. #include <errno.h>
  29. #include <sys/stat.h>
  30. #if HAVE_UNISTD_H
  31. # include <unistd.h>
  32. #endif /* HAVE_UNITSTD_H */
  33. extern egg_traffic_t traffic;
  34. #ifdef HAVE_SSL
  35. SSL_CTX *ssl_c_ctx = NULL, *ssl_s_ctx = NULL;
  36. char *tls_rand_file = NULL;
  37. #endif /* HAVE_SSL */
  38. union sockaddr_union cached_myip4_so;
  39. #ifdef USE_IPV6
  40. union sockaddr_union cached_myip6_so;
  41. #endif /* USE_IPV6 */
  42. int identd_hack = 0; /* identd_open() won't work on most servers, dont even bother warning. */
  43. char firewall[121] = ""; /* Socks server for firewall */
  44. port_t firewallport = 1080; /* Default port of Sock4/5 firewalls */
  45. char botuser[21] = ""; /* Username of the user running the bot */
  46. int resolve_timeout = 10; /* hostname/address lookup timeout */
  47. sock_list *socklist = NULL; /* Enough to be safe */
  48. int MAXSOCKS = 0;
  49. jmp_buf alarmret; /* Env buffer for alarm() returns */
  50. /* Types of proxy */
  51. #define PROXY_SOCKS 1
  52. #define PROXY_SUN 2
  53. /* I need an UNSIGNED long for dcc type stuff
  54. */
  55. unsigned long my_atoul(char *s)
  56. {
  57. unsigned long ret = 0;
  58. while ((*s >= '0') && (*s <= '9')) {
  59. ret *= 10;
  60. ret += ((*s) - '0');
  61. s++;
  62. }
  63. return ret;
  64. }
  65. int hostprotocol(char *host)
  66. {
  67. #ifdef USE_IPV6
  68. struct hostent *he = NULL;
  69. # ifndef HAVE_GETHOSTBYNAME2
  70. int error_num;
  71. # endif /* !HAVE_GETHOSTBYNAME2 */
  72. if (!host || (host && !host[0]))
  73. return 0;
  74. if (!setjmp(alarmret)) {
  75. alarm(resolve_timeout);
  76. # ifdef HAVE_GETHOSTBYNAME2
  77. he = gethostbyname2(host, AF_INET6);
  78. # else
  79. he = getipnodebyname(host, AF_INET6, AI_DEFAULT, &error_num);
  80. # endif /* HAVE_GETHOSTBYNAME2 */
  81. alarm(0);
  82. } else
  83. he = NULL;
  84. if (!he)
  85. return AF_INET;
  86. return AF_INET6;
  87. #else
  88. return 0;
  89. #endif /* USE_IPV6 */
  90. }
  91. /* get the protocol used on a socket */
  92. int sockprotocol(int sock)
  93. {
  94. struct sockaddr sa;
  95. int i = sizeof(sa);
  96. if (getsockname(sock, &sa, &i))
  97. return -1;
  98. else
  99. return sa.sa_family;
  100. }
  101. /* AF_INET-independent resolving routine */
  102. int get_ip(char *hostname, union sockaddr_union *so)
  103. {
  104. #ifdef USE_IPV6
  105. struct addrinfo hints, *ai = NULL, *res = NULL;
  106. int error = 0;
  107. #else
  108. struct hostent *hp = NULL;
  109. #endif /* USE_IPV6 */
  110. egg_memset(so, 0, sizeof(union sockaddr_union));
  111. debug1("get_ip(%s)", hostname);
  112. if (!hostname || (hostname && !hostname[0]))
  113. return 1;
  114. #ifdef USE_IPV6
  115. egg_memset(&hints, 0, sizeof(struct addrinfo));
  116. hints.ai_socktype = SOCK_STREAM;
  117. if ((error = getaddrinfo(hostname, NULL, &hints, &res)))
  118. return error;
  119. error = 1;
  120. for (ai = res; ai != NULL; ai = ai->ai_next) {
  121. if ((ai->ai_family == AF_INET6) || (ai->ai_family == AF_INET)) {
  122. memcpy(so, ai->ai_addr, ai->ai_addrlen);
  123. error = 0;
  124. break;
  125. }
  126. }
  127. freeaddrinfo(res);
  128. return error;
  129. #else
  130. if (!(hp = gethostbyname(hostname)))
  131. return -1;
  132. memcpy(&so->sin.sin_addr, hp->h_addr, 4);
  133. so->sin.sin_family = AF_INET;
  134. return 0;
  135. #endif /* USE_IPV6 */
  136. }
  137. #ifdef HAVE_SSL
  138. int seed_PRNG(void)
  139. {
  140. char stackdata[1024] = "";
  141. static char rand_file[300] = "";
  142. FILE *fh = NULL;
  143. #if OPENSSL_VERSION_NUMBER >= 0x00905100
  144. if (RAND_status()) return 0;
  145. #endif /* OPENSSL_VERSION_NUMBER */
  146. if ((fh = fopen("/dev/urandom", "r"))) {
  147. fclose(fh);
  148. return 0;
  149. }
  150. if (RAND_file_name(rand_file, sizeof(rand_file)))
  151. tls_rand_file = rand_file;
  152. else
  153. return 1;
  154. if (!RAND_load_file(rand_file, 1024)) {
  155. unsigned int c;
  156. c = now;
  157. RAND_seed(&c, sizeof(c));
  158. c = getpid();
  159. RAND_seed(&c, sizeof(c));
  160. RAND_seed(stackdata, sizeof(stackdata));
  161. }
  162. #if OPENSSL_VERSION_NUMBER >= 0x00905100
  163. if (!RAND_status()) return 2;
  164. #endif /* OPENSSL_VERSION_NUMBER >= 0x00905100 */
  165. return 0;
  166. }
  167. #endif /* HAVE_SSL */
  168. /* Initialize the socklist
  169. */
  170. void init_net()
  171. {
  172. int i;
  173. for (i = 0; i < MAXSOCKS; i++) {
  174. egg_bzero(&socklist[i], sizeof(socklist[i]));
  175. #ifdef HAVE_SSL
  176. socklist[i].ssl=NULL;
  177. #endif /* HAVE_SSL */
  178. socklist[i].flags = SOCK_UNUSED;
  179. }
  180. #ifdef HAVE_SSL
  181. SSL_load_error_strings();
  182. OpenSSL_add_ssl_algorithms();
  183. ssl_c_ctx = SSL_CTX_new(SSLv23_client_method());
  184. ssl_s_ctx = SSL_CTX_new(SSLv23_server_method());
  185. if (!ssl_c_ctx || !ssl_s_ctx)
  186. fatal("SSL Inititlization failed", 0);
  187. if (seed_PRNG())
  188. fatal("SSL PRNG seeding failed!", 0);
  189. #endif /* HAVE_SSL */
  190. }
  191. #ifdef HAVE_SSL
  192. int ssl_cleanup() {
  193. if (ssl_c_ctx) {
  194. SSL_CTX_free(ssl_c_ctx);
  195. ssl_c_ctx = NULL;
  196. }
  197. if (ssl_s_ctx) {
  198. SSL_CTX_free(ssl_s_ctx);
  199. ssl_s_ctx = NULL;
  200. }
  201. if (tls_rand_file) RAND_write_file(tls_rand_file);
  202. return 0;
  203. }
  204. #endif /* HAVE_SSL */
  205. /* Get my ipv? ip
  206. */
  207. char *myipstr(int af_type)
  208. {
  209. #ifdef USE_IPV6
  210. if (af_type == 6) {
  211. static char s[UHOSTLEN + 1] = "";
  212. egg_inet_ntop(AF_INET6, &cached_myip6_so.sin6.sin6_addr, s, 119);
  213. s[120] = 0;
  214. return s;
  215. } else
  216. #endif /* USE_IPV6 */
  217. if (af_type == 4) {
  218. static char s[UHOSTLEN + 1] = "";
  219. egg_inet_ntop(AF_INET, &cached_myip4_so.sin.sin_addr, s, 119);
  220. s[120] = 0;
  221. return s;
  222. }
  223. return "";
  224. }
  225. /* Get my ip number
  226. */
  227. IP getmyip() {
  228. return (IP) cached_myip4_so.sin.sin_addr.s_addr;
  229. }
  230. /* see if it's necessary to set inaddr_any... because if we can't resolve, we die anyway */
  231. void cache_my_ip()
  232. {
  233. char s[121] = "";
  234. int error;
  235. #ifdef USE_IPV6
  236. int any = 0;
  237. #endif /* USE_IPV6 */
  238. debug0("cache_my_ip()");
  239. egg_memset(&cached_myip4_so, 0, sizeof(union sockaddr_union));
  240. #ifdef USE_IPV6
  241. egg_memset(&cached_myip6_so, 0, sizeof(union sockaddr_union));
  242. if (conf.bot->ip6) {
  243. sdprintf("ip6: %s", conf.bot->ip6);
  244. if (get_ip(conf.bot->ip6, &cached_myip6_so))
  245. any = 1;
  246. } else if (conf.bot->host6) {
  247. sdprintf("host6: %s", conf.bot->host6);
  248. if (get_ip(conf.bot->host6, &cached_myip6_so))
  249. any = 1;
  250. } else
  251. any = 1;
  252. if (any) {
  253. sdprintf("IPV6 addr_any is set.");
  254. cached_myip6_so.sin6.sin6_family = AF_INET6;
  255. cached_myip6_so.sin6.sin6_addr = in6addr_any;
  256. }
  257. #endif /* USE_IPV6 */
  258. error = 0;
  259. if (conf.bot->ip) {
  260. if (get_ip(conf.bot->ip, &cached_myip4_so))
  261. error = 1;
  262. } else if (conf.bot->host) {
  263. if (get_ip(conf.bot->host, &cached_myip4_so))
  264. error = 2;
  265. } else {
  266. gethostname(s, sizeof(s));
  267. if (get_ip(s, &cached_myip4_so)) {
  268. /* error = 3; */
  269. sdprintf("IPV4 addr_any is set.");
  270. cached_myip4_so.sin.sin_family = AF_INET;
  271. cached_myip4_so.sin.sin_addr.s_addr = INADDR_ANY;
  272. }
  273. }
  274. if (error) {
  275. putlog(LOG_DEBUG, "*", "Hostname self-lookup error: %d", error);
  276. fatal("Hostname self-lookup failed.", 0);
  277. }
  278. }
  279. void neterror(char *s)
  280. {
  281. switch (errno) {
  282. case EADDRINUSE:
  283. strcpy(s, "Address already in use");
  284. break;
  285. case EADDRNOTAVAIL:
  286. strcpy(s, "Address invalid on remote machine");
  287. break;
  288. case EAFNOSUPPORT:
  289. strcpy(s, "Address family not supported");
  290. break;
  291. case EALREADY:
  292. strcpy(s, "Socket already in use");
  293. break;
  294. case EBADF:
  295. strcpy(s, "Socket descriptor is bad");
  296. break;
  297. case ECONNREFUSED:
  298. strcpy(s, "Connection refused");
  299. break;
  300. case EFAULT:
  301. strcpy(s, "Namespace segment violation");
  302. break;
  303. case EINPROGRESS:
  304. strcpy(s, "Operation in progress");
  305. break;
  306. case EINTR:
  307. strcpy(s, "Timeout");
  308. break;
  309. case EINVAL:
  310. strcpy(s, "Invalid namespace");
  311. break;
  312. case EISCONN:
  313. strcpy(s, "Socket already connected");
  314. break;
  315. case ENETUNREACH:
  316. strcpy(s, "Network unreachable");
  317. break;
  318. case ENOTSOCK:
  319. strcpy(s, "File descriptor, not a socket");
  320. break;
  321. case ETIMEDOUT:
  322. strcpy(s, "Connection timed out");
  323. break;
  324. case ENOTCONN:
  325. strcpy(s, "Socket is not connected");
  326. break;
  327. case EHOSTUNREACH:
  328. strcpy(s, "Host is unreachable");
  329. break;
  330. case EPIPE:
  331. strcpy(s, "Broken pipe");
  332. break;
  333. #ifdef ECONNRESET
  334. case ECONNRESET:
  335. strcpy(s, "Connection reset by peer");
  336. break;
  337. #endif /* ECONNRESET */
  338. #ifdef EACCES
  339. case EACCES:
  340. strcpy(s, "Permission denied");
  341. break;
  342. #endif /* EACCESS */
  343. #ifdef EMFILE
  344. case EMFILE:
  345. strcpy(s, "Too many open files");
  346. break;
  347. #endif /* EMFILE */
  348. case 0:
  349. strcpy(s, "Error 0");
  350. break;
  351. default:
  352. sprintf(s, "Unforseen error %d", errno);
  353. break;
  354. }
  355. }
  356. /* Sets/Unsets options for a specific socket.
  357. *
  358. * Returns: 0 - on success
  359. * -1 - socket not found
  360. * -2 - illegal operation
  361. */
  362. int sockoptions(int sock, int operation, int sock_options)
  363. {
  364. int i;
  365. for (i = 0; i < MAXSOCKS; i++)
  366. if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
  367. if (operation == EGG_OPTION_SET)
  368. socklist[i].flags |= sock_options;
  369. else if (operation == EGG_OPTION_UNSET)
  370. socklist[i].flags &= ~sock_options;
  371. else
  372. return -2;
  373. return 0;
  374. }
  375. return -1;
  376. }
  377. /* Return a free entry in the socket entry
  378. */
  379. int allocsock(int sock, int options)
  380. {
  381. int i;
  382. for (i = 0; i < MAXSOCKS; i++) {
  383. if (socklist[i].flags & SOCK_UNUSED) {
  384. /* yay! there is table space */
  385. socklist[i].inbuf = socklist[i].outbuf = NULL;
  386. socklist[i].inbuflen = socklist[i].outbuflen = 0;
  387. #ifdef HAVE_SSL
  388. socklist[i].ssl = NULL;
  389. #endif /* HAVE_SSL */
  390. socklist[i].flags = options;
  391. socklist[i].sock = sock;
  392. socklist[i].encstatus = 0;
  393. socklist[i].gz = 0;
  394. egg_bzero(&socklist[i].okey, sizeof(socklist[i].okey));
  395. egg_bzero(&socklist[i].ikey, sizeof(socklist[i].ikey));
  396. socklist[i].okey[0] = 0;
  397. socklist[i].ikey[0] = 0;
  398. return i;
  399. }
  400. }
  401. fatal("Socket table is full!", 0);
  402. return -1; /* Never reached */
  403. }
  404. /* Request a normal socket for i/o
  405. */
  406. void setsock(int sock, int options)
  407. {
  408. int i = allocsock(sock, options), parm;
  409. if (((sock != STDOUT) || backgrd) && !(socklist[i].flags & SOCK_NONSOCK)) {
  410. parm = 1;
  411. setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *) &parm, sizeof(int));
  412. parm = 0;
  413. setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &parm, sizeof(int));
  414. }
  415. if (options & SOCK_LISTEN) {
  416. /* Tris says this lets us grab the same port again next time */
  417. parm = 1;
  418. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &parm, sizeof(int));
  419. }
  420. /* Yay async i/o ! */
  421. fcntl(sock, F_SETFL, O_NONBLOCK);
  422. }
  423. #ifdef USE_IPV6
  424. int real_getsock(int options, int af_def, char *fname, int line)
  425. {
  426. #else
  427. int real_getsock(int options, char *fname, int line)
  428. {
  429. int af_def = AF_INET;
  430. #endif /* USE_IPV6 */
  431. int sock;
  432. sock = socket(af_def, SOCK_STREAM, 0);
  433. if (sock >= 0)
  434. setsock(sock, options);
  435. else if (!identd_hack)
  436. putlog(LOG_WARNING, "*", "Warning: Can't create new socket! (%s:%d)", fname, line);
  437. else if (identd_hack)
  438. identd_hack = 0;
  439. return sock;
  440. }
  441. void dropssl(register int sock)
  442. {
  443. #ifdef HAVE_SSL
  444. int i;
  445. if (sock < 0)
  446. return;
  447. for (i = 0; (i < MAXSOCKS); i++)
  448. if (socklist[i].sock == sock) break;
  449. if (socklist[i].ssl) {
  450. SSL_set_quiet_shutdown(socklist[i].ssl, 1);
  451. SSL_shutdown(socklist[i].ssl);
  452. usleep(1000 * 500);
  453. SSL_free(socklist[i].ssl);
  454. usleep(1000 * 500);
  455. socklist[i].ssl = NULL;
  456. }
  457. #endif /* HAVE_SSL */
  458. }
  459. /* Done with a socket
  460. */
  461. void real_killsock(register int sock, const char *file, int line)
  462. {
  463. register int i;
  464. if (sock < 0) {
  465. putlog(LOG_ERRORS, "*", "Attempt to kill socket -1 %s:%d", file, line);
  466. return;
  467. }
  468. for (i = 0; i < MAXSOCKS; i++) {
  469. if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
  470. dropssl(sock);
  471. close(socklist[i].sock);
  472. if (socklist[i].inbuf != NULL) {
  473. free(socklist[i].inbuf);
  474. socklist[i].inbuf = NULL;
  475. }
  476. if (socklist[i].outbuf != NULL) {
  477. free(socklist[i].outbuf);
  478. socklist[i].outbuf = NULL;
  479. socklist[i].outbuflen = 0;
  480. }
  481. if (socklist[i].host)
  482. free(socklist[i].host);
  483. egg_bzero(&socklist[i], sizeof(socklist[i]));
  484. socklist[i].flags = SOCK_UNUSED;
  485. return;
  486. }
  487. }
  488. putlog(LOG_MISC, "*", "Attempt to kill un-allocated socket %d %s:%d !!", sock, file, line);
  489. }
  490. /* Send connection request to proxy
  491. */
  492. static int proxy_connect(int sock, char *host, int port, int proxy)
  493. {
  494. #ifdef USE_IPV6
  495. unsigned char x[32] = "";
  496. int af_ty;
  497. #else
  498. unsigned char x[10] = "";
  499. #endif /* USE_IPV6 */
  500. struct hostent *hp = NULL;
  501. char s[256] = "";
  502. int i;
  503. #ifdef USE_IPV6
  504. af_ty = sockprotocol(sock);
  505. #endif /* USE_IPV6 */
  506. /* socks proxy */
  507. if (proxy == PROXY_SOCKS) {
  508. /* numeric IP? */
  509. #ifdef USE_IPV6
  510. if ((host[strlen(host) - 1] >= '0' && host[strlen(host) - 1] <= '9') && af_ty != AF_INET6) {
  511. #else
  512. if (host[strlen(host) - 1] >= '0' && host[strlen(host) - 1] <= '9') {
  513. #endif /* USE_IPV6 */
  514. IP ip = ((IP) inet_addr(host));
  515. egg_memcpy(x, &ip, 4);
  516. } else {
  517. /* no, must be host.domain */
  518. if (!setjmp(alarmret)) {
  519. #ifdef USE_IPV6
  520. alarm(resolve_timeout);
  521. if (af_ty == AF_INET6)
  522. hp = gethostbyname(host);
  523. else
  524. #endif /* USE_IPV6 */
  525. hp = gethostbyname(host);
  526. #ifdef USE_IPV6
  527. alarm(0);
  528. #endif /* USE_IPV6 */
  529. } else
  530. hp = NULL;
  531. if (hp == NULL) {
  532. killsock(sock);
  533. return -2;
  534. }
  535. egg_memcpy(x, hp->h_addr, hp->h_length);
  536. }
  537. for (i = 0; i < MAXSOCKS; i++)
  538. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
  539. socklist[i].flags |= SOCK_PROXYWAIT; /* drummer */
  540. #ifdef USE_IPV6
  541. if (af_ty == AF_INET6)
  542. egg_snprintf(s, sizeof s,
  543. "\004\001%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%s",
  544. (port >> 8) % 256, (port % 256), x[0], x[1], x[2], x[3],
  545. x[4], x[5], x[6], x[7], x[9], x[9], x[10], x[11], x[12],
  546. x[13], x[14], x[15], botuser);
  547. else
  548. #endif /* USE_IPV6 */
  549. egg_snprintf(s, sizeof s, "\004\001%c%c%c%c%c%c%s", (port >> 8) % 256,
  550. (port % 256), x[0], x[1], x[2], x[3], botuser);
  551. tputs(sock, s, strlen(botuser) + 9); /* drummer */
  552. } else if (proxy == PROXY_SUN) {
  553. egg_snprintf(s, sizeof s, "%s %d\n", host, port);
  554. tputs(sock, s, strlen(s)); /* drummer */
  555. }
  556. return sock;
  557. }
  558. /* Starts a connection attempt to a socket
  559. *
  560. * If given a normal hostname, this will be resolved to the corresponding
  561. * IP address first. PLEASE try to use the non-blocking dns functions
  562. * instead and then call this function with the IP address to avoid blocking.
  563. *
  564. * returns <0 if connection refused:
  565. * -1 neterror() type error
  566. * -2 can't resolve hostname
  567. */
  568. int open_telnet_raw(int sock, char *server, port_t sport)
  569. {
  570. static port_t port = 0;
  571. static int error = 0;
  572. union sockaddr_union so;
  573. char host[121] = "";
  574. int i, rc;
  575. volatile int proxy;
  576. /* firewall? use socks */
  577. if (firewall[0]) {
  578. if (firewall[0] == '!') {
  579. proxy = PROXY_SUN;
  580. strcpy(host, &firewall[1]);
  581. } else {
  582. proxy = PROXY_SOCKS;
  583. strcpy(host, firewall);
  584. }
  585. port = firewallport;
  586. } else {
  587. proxy = 0;
  588. strncpyz(host, server, sizeof host);
  589. port = sport;
  590. }
  591. error = 0;
  592. if (!setjmp(alarmret)) {
  593. alarm(resolve_timeout);
  594. if (!get_ip(host, &so)) {
  595. alarm(0);
  596. /* ok, we resolved it, bind an appropriate ip */
  597. #ifdef USE_IPV6
  598. if (so.sa.sa_family == AF_INET6) {
  599. if (bind(sock, &cached_myip6_so.sa, SIZEOF_SOCKADDR(cached_myip6_so)) < 0) {
  600. if (sock >= 0)
  601. killsock(sock);
  602. return -1;
  603. }
  604. } else {
  605. #endif /* USE_IPV6 */
  606. if (bind(sock, &cached_myip4_so.sa, SIZEOF_SOCKADDR(cached_myip4_so)) < 0) {
  607. if (sock >= 0)
  608. killsock(sock);
  609. return -3;
  610. }
  611. #ifdef USE_IPV6
  612. }
  613. if (so.sa.sa_family == AF_INET6)
  614. so.sin6.sin6_port = htons(port);
  615. else
  616. #endif /* USE_IPV6 */
  617. so.sin.sin_port = htons(port);
  618. } else {
  619. alarm(0);
  620. error = 1;
  621. }
  622. }
  623. /* FIXME: (error) is unitialized */
  624. /* I guess we broke something */
  625. if (error) {
  626. killsock(sock);
  627. return -2;
  628. }
  629. for (i = 0; i < MAXSOCKS; i++) {
  630. if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock)) {
  631. socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT;
  632. socklist[i].host = strdup(server);
  633. socklist[i].port = port;
  634. }
  635. }
  636. rc = connect(sock, &so.sa, SIZEOF_SOCKADDR(so));
  637. if (rc < 0) { if (errno == EINPROGRESS) {
  638. /* Firewall? announce connect attempt to proxy */
  639. if (firewall[0])
  640. return proxy_connect(sock, server, sport, proxy);
  641. return sock; /* async success! */
  642. } else
  643. return -1;
  644. }
  645. /* Synchronous? :/ */
  646. if (firewall[0])
  647. return proxy_connect(sock, server, sport, proxy);
  648. return sock;
  649. }
  650. /* Ordinary non-binary connection attempt */
  651. int open_telnet(char *server, port_t port)
  652. {
  653. int ret = -1, sock = -1;
  654. #ifdef USE_IPV6
  655. sock = getsock(0, hostprotocol(server));
  656. #else
  657. sock = getsock(0);
  658. #endif /* USE_IPV6 */
  659. if (sock >= 0)
  660. ret = open_telnet_raw(sock, server, port);
  661. return ret;
  662. }
  663. /* Returns a socket number for a listening socket that will accept any
  664. * connection on a certain address -- port # is returned in port
  665. *
  666. * 'addr' is ignored if af_def is AF_INET6 -poptix (02/03/03)
  667. */
  668. #ifdef USE_IPV6
  669. int open_address_listen(IP addr, int af_def, port_t *port)
  670. #else
  671. int open_address_listen(IP addr, port_t *port)
  672. #endif /* USE_IPV6 */
  673. {
  674. int sock = 0;
  675. unsigned int addrlen;
  676. struct sockaddr_in name;
  677. if (firewall[0]) {
  678. /* FIXME: can't do listen port thru firewall yet */
  679. putlog(LOG_MISC, "*", "!! Cant open a listen port (you are using a "
  680. "firewall)");
  681. return -1;
  682. }
  683. #ifdef USE_IPV6
  684. if (af_def == AF_INET6) {
  685. struct sockaddr_in6 name6;
  686. sock = getsock(SOCK_LISTEN, af_def);
  687. if (sock < 1)
  688. return -1;
  689. debug2("Opening listen socket on port %d with AF_INET6, sock: %d", *port, sock);
  690. egg_bzero((char *) &name6, sizeof(name6));
  691. name6.sin6_family = af_def;
  692. name6.sin6_port = htons(*port); /* 0 = just assign us a port */
  693. /* memcpy(&name6.sin6_addr, &in6addr_any, 16); */ /* this is the only way to get ipv6+ipv4 in 1 socket */
  694. memcpy(&name6.sin6_addr, &cached_myip6_so.sin6.sin6_addr, 16);
  695. if (bind(sock, (struct sockaddr *) &name6, sizeof(name6)) < 0) {
  696. killsock(sock);
  697. return -1;
  698. }
  699. addrlen = sizeof(name6);
  700. if (getsockname(sock, (struct sockaddr *) &name6, &addrlen) < 0) {
  701. killsock(sock);
  702. return -1;
  703. }
  704. *port = ntohs(name6.sin6_port);
  705. if (listen(sock, 1) < 0) {
  706. killsock(sock);
  707. return -1;
  708. }
  709. } else {
  710. sock = getsock(SOCK_LISTEN, AF_INET);
  711. #else
  712. sock = getsock(SOCK_LISTEN);
  713. #endif /* USE_IPV6 */
  714. if (sock < 1)
  715. return -1;
  716. debug2("Opening listen socket on port %d with AF_INET, sock: %d", *port, sock);
  717. egg_bzero((char *) &name, sizeof(struct sockaddr_in));
  718. name.sin_family = AF_INET;
  719. name.sin_port = htons(*port); /* 0 = just assign us a port */
  720. name.sin_addr.s_addr = addr;
  721. if (bind(sock, (struct sockaddr *) &name, sizeof(name)) < 0) {
  722. killsock(sock);
  723. return -1;
  724. }
  725. /* what port are we on? */
  726. addrlen = sizeof(name);
  727. if (getsockname(sock, (struct sockaddr *) &name, &addrlen) < 0) {
  728. killsock(sock);
  729. return -1;
  730. }
  731. *port = ntohs(name.sin_port);
  732. if (listen(sock, 1) < 0) {
  733. killsock(sock);
  734. return -1;
  735. }
  736. #ifdef USE_IPV6
  737. }
  738. #endif /* USE_IPV6 */
  739. return sock;
  740. }
  741. /* Returns a socket number for a listening socket that will accept any
  742. * connection -- port # is returned in port
  743. */
  744. inline int open_listen(port_t *port)
  745. {
  746. #ifdef USE_IPV6
  747. return open_address_listen(conf.bot->ip ? getmyip() : INADDR_ANY, AF_INET, port);
  748. #else
  749. return open_address_listen(conf.bot->ip ? getmyip() : INADDR_ANY, port);
  750. #endif /* USE_IPV6 */
  751. }
  752. /* Same as above, except this one can be called with an AF_ type
  753. * the above is being left in for compatibility, and should NOT LONGER BE USED IN THE CORE CODE.
  754. */
  755. inline int open_listen_by_af(port_t *port, int af_def)
  756. {
  757. #ifdef USE_IPV6
  758. return open_address_listen(conf.bot->ip ? getmyip() : INADDR_ANY, af_def, port);
  759. #else
  760. return 0;
  761. #endif /* USE_IPV6 */
  762. }
  763. int ssl_link(register int sock, int state)
  764. {
  765. #ifdef HAVE_SSL
  766. int err = 0, i = 0, errs = 0;
  767. debug2("ssl_link(%d, %d)", sock, state);
  768. for (i = 0; (i < MAXSOCKS); i++) {
  769. if (socklist[i].sock == sock) break;
  770. }
  771. if (socklist[i].ssl) {
  772. putlog(LOG_ERROR, "*", "Switching to SSL (%d,%d) - already active", state, sock);
  773. return 0;
  774. }
  775. if (state == CONNECT_SSL) {
  776. socklist[i].ssl = SSL_new(ssl_c_ctx);
  777. } else if (state == ACCEPT_SSL) {
  778. socklist[i].ssl = SSL_new(ssl_s_ctx);
  779. }
  780. if (!socklist[i].ssl) {
  781. putlog(LOG_ERROR, "*", "Switching to SSL (%d) - SSL_new(%d) failed", sock, state);
  782. return 0;
  783. }
  784. if (!SSL_set_fd(socklist[i].ssl, socklist[i].sock)) {
  785. putlog(LOG_ERROR, "*", "SSL_set_fd(%d) (%d) failed", state, socklist[i].sock);
  786. return 0;
  787. }
  788. if (state == CONNECT_SSL) {
  789. SSL_set_connect_state(socklist[i].ssl);
  790. } else if (state == ACCEPT_SSL) {
  791. SSL_set_accept_state(socklist[i].ssl);
  792. } else {
  793. putlog(LOG_DEBUG, "*", "ssl_link(%d, 0?) NO STATE?", sock);
  794. return 0;
  795. }
  796. if (state == CONNECT_SSL) {
  797. err = SSL_connect(socklist[i].ssl);
  798. } else if (state == ACCEPT_SSL) {
  799. err = SSL_accept(socklist[i].ssl);
  800. }
  801. if (!setjmp(alarmret)) {
  802. alarm(5); /* this is plenty of time */
  803. while ((err < 1) && (errno == EAGAIN)) {
  804. if (state == CONNECT_SSL) {
  805. err = SSL_connect(socklist[i].ssl);
  806. } else if (state == ACCEPT_SSL) {
  807. err = SSL_accept(socklist[i].ssl);
  808. }
  809. /* if ((errs!=SSL_ERROR_WANT_READ)&&(errs!=SSL_ERROR_WANT_WRITE)&& (errs!=SSL_ERROR_WANT_X509_LOOKUP)) */
  810. /* break; anything not one of these is a sufficient condition to break out... */
  811. }
  812. alarm(0);
  813. }
  814. errs = SSL_get_error(socklist[i].ssl, err);
  815. putlog(LOG_DEBUG, "*", "SSL_link(%d, %d) = %d, errs: %d (%d), %s", sock, state, err, errs, errno, (char *)ERR_error_string(ERR_get_error(), NULL));
  816. if (errno) putlog(LOG_DEBUG, "*", "errno %d: %s", errno, strerror(errno));
  817. if (err == 1) {
  818. putlog(LOG_ERROR, "*", "SSL_link(%d, %d) was successfull", sock, state);
  819. return 1;
  820. } else {
  821. putlog(LOG_ERROR, "*", "SSL_link(%d, %d) failed", sock, state);
  822. dropssl(socklist[i].sock);
  823. }
  824. #endif /* HAVE_SSL */
  825. return 0;
  826. }
  827. /* Given a network-style IP address, returns the hostname. The hostname
  828. * will be in the "##.##.##.##" format if there was an error.
  829. *
  830. * NOTE: This function is depreciated. Try using the async dns approach
  831. * instead.
  832. */
  833. char *hostnamefromip(IP ip)
  834. {
  835. struct hostent *hp = NULL;
  836. IP addr = ip;
  837. unsigned char *p = NULL;
  838. static char s[UHOSTLEN] = "";
  839. if (!setjmp(alarmret)) {
  840. alarm(resolve_timeout);
  841. hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
  842. alarm(0);
  843. } else {
  844. hp = NULL;
  845. }
  846. if (hp == NULL) {
  847. p = (unsigned char *) &addr;
  848. sprintf(s, "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
  849. return s;
  850. }
  851. strncpyz(s, hp->h_name, sizeof s);
  852. return s;
  853. }
  854. /* Returns the given network byte order IP address in the
  855. * dotted format - "##.##.##.##"
  856. */
  857. char *iptostr(IP ip)
  858. {
  859. static char ipbuf[32];
  860. struct in_addr a;
  861. a.s_addr = ip;
  862. return (char *) egg_inet_ntop(AF_INET, &a, ipbuf, sizeof(ipbuf));
  863. }
  864. /* Short routine to answer a connect received on a socket made previously
  865. * by open_listen ... returns hostname of the caller & the new socket
  866. * does NOT dispose of old "public" socket!
  867. */
  868. int answer(int sock, char *caller, IP *ip, port_t *port, int binary)
  869. {
  870. int new_sock;
  871. unsigned int addrlen;
  872. struct sockaddr_in from;
  873. #ifdef USE_IPV6
  874. int af_ty = sockprotocol(sock);
  875. struct sockaddr_in6 from6;
  876. egg_bzero(&from6, sizeof(struct sockaddr_in6));
  877. if (af_ty == AF_INET6) {
  878. addrlen = sizeof(from6);
  879. new_sock = accept(sock, (struct sockaddr *) &from6, &addrlen);
  880. } else {
  881. #endif /* USE_IPV6 */
  882. addrlen = sizeof(struct sockaddr);
  883. new_sock = accept(sock, (struct sockaddr *) &from, &addrlen);
  884. #ifdef USE_IPV6
  885. }
  886. #endif /* USE_IPV6 */
  887. if (new_sock < 0)
  888. return -1;
  889. if (ip != NULL) {
  890. #ifdef USE_IPV6
  891. /* Detect IPv4 in IPv6 mapped address .... */
  892. if (af_ty == AF_INET6 && (!IN6_IS_ADDR_V4MAPPED(&from6.sin6_addr))) {
  893. egg_inet_ntop(AF_INET6, &from6.sin6_addr, caller, 119);
  894. caller[120] = 0;
  895. *ip = 0L;
  896. } else if (IN6_IS_ADDR_V4MAPPED(&from6.sin6_addr)) { /* ...and convert it to plain (AF_INET) IPv4 address (openssh) */
  897. struct sockaddr_in *from4 = (struct sockaddr_in *)&from6;
  898. struct in_addr addr;
  899. memcpy(&addr, ((char *)&from6.sin6_addr) + 12, sizeof(addr));
  900. egg_memset(&from, 0, sizeof(from));
  901. from4->sin_family = AF_INET;
  902. addrlen = sizeof(*from4);
  903. memcpy(&from4->sin_addr, &addr, sizeof(addr));
  904. *ip = from4->sin_addr.s_addr;
  905. strncpyz(caller, iptostr(*ip), 121);
  906. *ip = ntohl(*ip);
  907. } else {
  908. #endif /* USE_IPV6 */
  909. *ip = from.sin_addr.s_addr;
  910. /* This is now done asynchronously. We now only provide the IP address.
  911. *
  912. * strncpy(caller, hostnamefromip(*ip), 120);
  913. */
  914. strncpyz(caller, iptostr(*ip), 121);
  915. *ip = ntohl(*ip);
  916. #ifdef USE_IPV6
  917. }
  918. #endif /* USE_IPV6 */
  919. }
  920. if (port != NULL) {
  921. #ifdef USE_IPV6
  922. if (af_ty == AF_INET6)
  923. *port = ntohs(from6.sin6_port);
  924. else
  925. #endif /* USE_IPV6 */
  926. *port = ntohs(from.sin_port);
  927. }
  928. /* Set up all the normal socket crap */
  929. setsock(new_sock, (binary ? SOCK_BINARY : 0));
  930. return new_sock;
  931. }
  932. /* Like open_telnet, but uses server & port specifications of dcc
  933. */
  934. int open_telnet_dcc(int sock, char *server, char *port)
  935. {
  936. int p;
  937. unsigned long addr;
  938. char sv[500] = "";
  939. unsigned char c[4] = "";
  940. #ifdef DEBUG_IPV6
  941. debug1("open_telnet_dcc %s", server);
  942. #endif /* DEBUG_IPV6 */
  943. if (port != NULL)
  944. p = atoi(port);
  945. else
  946. p = 2000;
  947. #ifdef USE_IPV6
  948. if (sockprotocol(sock) == AF_INET6) {
  949. # ifdef DEBUG_IPV6
  950. debug0("open_telnet_dcc, af_inet6!");
  951. # endif /* DEBUG_IPV6 */
  952. strncpyz(sv, server, sizeof sv);
  953. debug2("%s should be %s",sv,server);
  954. } else {
  955. #endif /* USE_IPV6 */
  956. if (server != NULL)
  957. addr = my_atoul(server);
  958. else
  959. addr = 0L;
  960. if (addr < (1 << 24))
  961. return -3; /* fake address */
  962. c[0] = (addr >> 24) & 0xff;
  963. c[1] = (addr >> 16) & 0xff;
  964. c[2] = (addr >> 8) & 0xff;
  965. c[3] = addr & 0xff;
  966. sprintf(sv, "%u.%u.%u.%u", c[0], c[1], c[2], c[3]);
  967. #ifdef USE_IPV6
  968. }
  969. /* strcpy(sv,hostnamefromip(addr)); */
  970. # ifdef DEBUG_IPV6
  971. debug3("open_telnet_raw %s %d %d", sv, sock,p);
  972. # endif /* DEBUG_IPV6 */
  973. #endif /* USE_IPV6 */
  974. p = open_telnet_raw(sock, sv, p);
  975. return p;
  976. }
  977. /* Attempts to read from all the sockets in socklist
  978. * fills s with up to 511 bytes if available, and returns the array index
  979. *
  980. * on EOF: returns -1, with socket in len
  981. * on socket error: returns -2
  982. * if nothing is ready: returns -3
  983. */
  984. static int sockread(char *s, int *len)
  985. {
  986. fd_set fd;
  987. int fds = 0, i, x, fdtmp;
  988. struct timeval t;
  989. int grab = SGRAB + 1;
  990. egg_timeval_t howlong;
  991. if (timer_get_shortest(&howlong)) {
  992. /* No timer, default to 1 second. */
  993. t.tv_sec = 1;
  994. t.tv_usec = 0;
  995. }
  996. else {
  997. t.tv_sec = howlong.sec;
  998. t.tv_usec = howlong.usec;
  999. }
  1000. FD_ZERO(&fd);
  1001. for (i = 0; i < MAXSOCKS; i++) {
  1002. if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_VIRTUAL))) {
  1003. if ((socklist[i].sock == STDOUT) && !backgrd)
  1004. fdtmp = STDIN;
  1005. else
  1006. fdtmp = socklist[i].sock;
  1007. if (fdtmp > fds)
  1008. fds = fdtmp;
  1009. FD_SET(fdtmp, &fd);
  1010. }
  1011. }
  1012. fds++;
  1013. x = select(fds, &fd, NULL, NULL, &t);
  1014. if (x > 0) {
  1015. /* Something happened */
  1016. for (i = 0; i < MAXSOCKS; i++) {
  1017. if ((!(socklist[i].flags & SOCK_UNUSED)) && ((FD_ISSET(socklist[i].sock, &fd)) ||
  1018. #ifdef HAVE_SSL
  1019. ((socklist[i].ssl) && (SSL_pending(socklist[i].ssl))) ||
  1020. #endif /* HAVE_SSL */
  1021. ((socklist[i].sock == STDOUT) && (!backgrd) && (FD_ISSET(STDIN, &fd))))) {
  1022. if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
  1023. /* Listening socket -- don't read, just return activity */
  1024. /* Same for connection attempt */
  1025. /* (for strong connections, require a read to succeed first) */
  1026. if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
  1027. /* Hang around to get the return code from proxy */
  1028. grab = 10;
  1029. } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
  1030. debug1("net: connect! sock %d", socklist[i].sock);
  1031. s[0] = 0;
  1032. *len = 0;
  1033. #ifdef HAVE_SSL
  1034. /* debug0("CALLING SSL_LINK() FROM SOCKREAD");
  1035. if (!ssl_link(socklist[i].sock))
  1036. debug0("SSL_LINK FAILED");
  1037. debug0("BACK FROM SSL_LINK()"); */
  1038. #endif /* HAVE_SSL */
  1039. return i;
  1040. }
  1041. } else if (socklist[i].flags & SOCK_PASS) {
  1042. s[0] = 0;
  1043. *len = 0;
  1044. return i;
  1045. }
  1046. errno = 0;
  1047. if ((socklist[i].sock == STDOUT) && !backgrd)
  1048. x = read(STDIN, s, grab);
  1049. else {
  1050. #ifdef HAVE_SSL
  1051. if (socklist[i].ssl) {
  1052. x = SSL_read(socklist[i].ssl, s, grab);
  1053. if (x < 0) {
  1054. int err = SSL_get_error(socklist[i].ssl, x);
  1055. x = -1;
  1056. switch (err) {
  1057. case SSL_ERROR_WANT_READ:
  1058. errno = EAGAIN;
  1059. break;
  1060. case SSL_ERROR_WANT_WRITE:
  1061. errno = EAGAIN;
  1062. break;
  1063. case SSL_ERROR_WANT_X509_LOOKUP:
  1064. errno = EAGAIN;
  1065. break;
  1066. }
  1067. }
  1068. } else
  1069. #endif /* HAVE_SSL */
  1070. x = read(socklist[i].sock, s, grab);
  1071. }
  1072. if (x <= 0) { /* eof */
  1073. if (errno != EAGAIN) { /* EAGAIN happens when the operation would block
  1074. on a non-blocking socket, if the socket is going
  1075. to die, it will die later, otherwise it will connect */
  1076. *len = socklist[i].sock;
  1077. socklist[i].flags &= ~SOCK_CONNECT;
  1078. debug1("net: eof!(read) socket %d", socklist[i].sock);
  1079. return -1;
  1080. } else {
  1081. debug3("sockread EAGAIN: %d %d (%s)", socklist[i].sock, errno, strerror(errno));
  1082. continue; /* EAGAIN */
  1083. }
  1084. }
  1085. s[x] = 0;
  1086. *len = x;
  1087. if (socklist[i].flags & SOCK_PROXYWAIT) {
  1088. debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
  1089. socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
  1090. switch (s[1]) {
  1091. case 90: /* Success */
  1092. s[0] = 0;
  1093. *len = 0;
  1094. return i;
  1095. case 91: /* Failed */
  1096. errno = ECONNREFUSED;
  1097. break;
  1098. case 92: /* No identd */
  1099. case 93: /* Identd said wrong username */
  1100. /* A better error message would be "socks misconfigured"
  1101. * or "identd not working" but this is simplest.
  1102. */
  1103. errno = ENETUNREACH;
  1104. break;
  1105. }
  1106. *len = socklist[i].sock;
  1107. return -1;
  1108. }
  1109. return i;
  1110. }
  1111. }
  1112. } else if (x == -1)
  1113. return -2; /* socket error */
  1114. else {
  1115. s[0] = 0;
  1116. *len = 0;
  1117. }
  1118. return -3;
  1119. }
  1120. inline static int
  1121. prand(int *seed, int range)
  1122. {
  1123. long long i1;
  1124. i1 = *seed;
  1125. i1 = (i1 * 0x08088405 + 1) & 0xFFFFFFFF;
  1126. *seed = i1;
  1127. i1 = (i1 * range) >> 32;
  1128. return i1;
  1129. }
  1130. char *botlink_decrypt(int snum, char *src)
  1131. {
  1132. char *line = NULL;
  1133. line = decrypt_string(socklist[snum].ikey, src);
  1134. strcpy(src, line);
  1135. free(line);
  1136. if (socklist[snum].iseed) {
  1137. *(dword *) & socklist[snum].ikey[0 * 4] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  1138. *(dword *) & socklist[snum].ikey[1 * 4] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  1139. *(dword *) & socklist[snum].ikey[2 * 4] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  1140. *(dword *) & socklist[snum].ikey[3 * 4] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  1141. if (!socklist[snum].iseed)
  1142. socklist[snum].iseed++;
  1143. }
  1144. return src;
  1145. }
  1146. char *botlink_encrypt(int snum, char *src, size_t *len)
  1147. {
  1148. char *srcbuf = NULL, *buf = NULL, *line = NULL, *eol = NULL, *eline = NULL;
  1149. int bufpos = 0;
  1150. srcbuf = calloc(1, *len + 9 + 1);
  1151. strcpy(srcbuf, src);
  1152. line = srcbuf;
  1153. if (!line) {
  1154. free(srcbuf);
  1155. return NULL;
  1156. }
  1157. eol = strchr(line, '\n');
  1158. while (eol) {
  1159. *eol++ = 0;
  1160. eline = encrypt_string(socklist[snum].okey, line);
  1161. if (socklist[snum].oseed) {
  1162. *(dword *) & socklist[snum].okey[0 * 4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1163. *(dword *) & socklist[snum].okey[1 * 4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1164. *(dword *) & socklist[snum].okey[2 * 4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1165. *(dword *) & socklist[snum].okey[3 * 4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1166. if (!socklist[snum].oseed)
  1167. socklist[snum].oseed++;
  1168. }
  1169. buf = realloc(buf, bufpos + strlen(eline) + 1 + 9);
  1170. strcpy((char *) &buf[bufpos], eline);
  1171. free(eline);
  1172. strcat(buf, "\n");
  1173. bufpos = strlen(buf);
  1174. line = eol;
  1175. eol = strchr(line, '\n');
  1176. }
  1177. if (line[0]) {
  1178. eline = encrypt_string(socklist[snum].okey, line);
  1179. if (socklist[snum].oseed) {
  1180. *(dword *) & socklist[snum].okey[0 * 4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1181. *(dword *) & socklist[snum].okey[1 * 4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1182. *(dword *) & socklist[snum].okey[2 * 4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1183. *(dword *) & socklist[snum].okey[3 * 4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1184. if (!socklist[snum].oseed)
  1185. socklist[snum].oseed++;
  1186. }
  1187. buf = realloc(buf, bufpos + strlen(eline) + 1 + 9);
  1188. strcpy((char *) &buf[bufpos], eline);
  1189. free(eline);
  1190. strcat(buf, "\n");
  1191. }
  1192. free(srcbuf);
  1193. *len = strlen(buf);
  1194. return buf;
  1195. }
  1196. /* sockgets: buffer and read from sockets
  1197. *
  1198. * Attempts to read from all registered sockets for up to one second. if
  1199. * after one second, no complete data has been received from any of the
  1200. * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
  1201. * if there is returnable data received from a socket, the data will be
  1202. * in 's' (null-terminated if non-binary), the length will be returned
  1203. * in len, and the socket number will be returned.
  1204. * normal sockets have their input buffered, and each call to sockgets
  1205. * will return one line terminated with a '\n'. binary sockets are not
  1206. * buffered and return whatever coems in as soon as it arrives.
  1207. * listening sockets will return an empty string when a connection comes in.
  1208. * connecting sockets will return an empty string on a successful connect,
  1209. * or EOF on a failed connect.
  1210. * if an EOF is detected from any of the sockets, that socket number will be
  1211. * put in len, and -1 will be returned.
  1212. * the maximum length of the string returned is 512 (including null)
  1213. *
  1214. * Returns -4 if we handled something that shouldn't be handled by the
  1215. * dcc functions. Simply ignore it.
  1216. */
  1217. int sockgets(char *s, int *len)
  1218. {
  1219. char xx[SGRAB + 4] = "", *p = NULL, *px = NULL;
  1220. int ret, i, data = 0;
  1221. for (i = 0; i < MAXSOCKS; i++) {
  1222. /* Check for stored-up data waiting to be processed */
  1223. if (!(socklist[i].flags & SOCK_UNUSED) && !(socklist[i].flags & SOCK_BUFFER) && (socklist[i].inbuf != NULL)) {
  1224. if (!(socklist[i].flags & SOCK_BINARY)) {
  1225. /* look for \r too cos windows can't follow RFCs */
  1226. p = strchr(socklist[i].inbuf, '\n');
  1227. if (p == NULL)
  1228. p = strchr(socklist[i].inbuf, '\r');
  1229. if (p != NULL) {
  1230. *p = 0;
  1231. if (strlen(socklist[i].inbuf) > SGRAB)
  1232. socklist[i].inbuf[SGRAB] = 0;
  1233. strcpy(s, socklist[i].inbuf);
  1234. px = calloc(1, strlen(p + 1) + 1);
  1235. strcpy(px, p + 1);
  1236. free(socklist[i].inbuf);
  1237. if (px[0])
  1238. socklist[i].inbuf = px;
  1239. else {
  1240. free(px);
  1241. socklist[i].inbuf = NULL;
  1242. }
  1243. /* Strip CR if this was CR/LF combo */
  1244. if (s[strlen(s) - 1] == '\r')
  1245. s[strlen(s) - 1] = 0;
  1246. if (socklist[i].encstatus && s[0])
  1247. botlink_decrypt(i, s);
  1248. *len = strlen(s);
  1249. return socklist[i].sock;
  1250. }
  1251. } else {
  1252. /* i dont think any of this is *ever* called */
  1253. /* Handling buffered binary data (must have been SOCK_BUFFER before). */
  1254. if (socklist[i].inbuflen <= SGRAB) {
  1255. *len = socklist[i].inbuflen;
  1256. egg_memcpy(s, socklist[i].inbuf, socklist[i].inbuflen);
  1257. free(socklist[i].inbuf);
  1258. socklist[i].inbuf = NULL;
  1259. socklist[i].inbuflen = 0;
  1260. } else {
  1261. /* Split up into chunks of SGRAB bytes. */
  1262. *len = SGRAB;
  1263. egg_memcpy(s, socklist[i].inbuf, *len);
  1264. egg_memcpy(socklist[i].inbuf, socklist[i].inbuf + *len, *len);
  1265. socklist[i].inbuflen -= *len;
  1266. socklist[i].inbuf = realloc(socklist[i].inbuf, socklist[i].inbuflen);
  1267. }
  1268. return socklist[i].sock;
  1269. }
  1270. }
  1271. /* Also check any sockets that might have EOF'd during write */
  1272. if (!(socklist[i].flags & SOCK_UNUSED)
  1273. && (socklist[i].flags & SOCK_EOFD)) {
  1274. s[0] = 0;
  1275. *len = socklist[i].sock;
  1276. return -1;
  1277. }
  1278. }
  1279. /* No pent-up data of any worth -- down to business */
  1280. *len = 0;
  1281. ret = sockread(xx, len);
  1282. if (ret < 0) {
  1283. s[0] = 0;
  1284. return ret;
  1285. }
  1286. /* Binary, listening and passed on sockets don't get buffered. */
  1287. if (socklist[ret].flags & SOCK_CONNECT) {
  1288. if (socklist[ret].flags & SOCK_STRONGCONN) {
  1289. socklist[ret].flags &= ~SOCK_STRONGCONN;
  1290. /* Buffer any data that came in, for future read. */
  1291. socklist[ret].inbuflen = *len;
  1292. socklist[ret].inbuf = calloc(1, *len + 1);
  1293. /* It might be binary data. You never know. */
  1294. egg_memcpy(socklist[ret].inbuf, xx, *len);
  1295. socklist[ret].inbuf[*len] = 0;
  1296. }
  1297. socklist[ret].flags &= ~SOCK_CONNECT;
  1298. s[0] = 0;
  1299. return socklist[ret].sock;
  1300. }
  1301. if (socklist[ret].flags & SOCK_BINARY) {
  1302. egg_memcpy(s, xx, *len);
  1303. return socklist[ret].sock;
  1304. }
  1305. if ((socklist[ret].flags & SOCK_LISTEN) || (socklist[ret].flags & SOCK_PASS))
  1306. return socklist[ret].sock;
  1307. if (socklist[ret].flags & SOCK_BUFFER) {
  1308. socklist[ret].inbuf = (char *) realloc(socklist[ret].inbuf,
  1309. socklist[ret].inbuflen + *len + 1);
  1310. egg_memcpy(socklist[ret].inbuf + socklist[ret].inbuflen, xx, *len);
  1311. socklist[ret].inbuflen += *len;
  1312. /* We don't know whether it's binary data. Make sure normal strings
  1313. will be handled properly later on too. */
  1314. socklist[ret].inbuf[socklist[ret].inbuflen] = 0;
  1315. return -4; /* Ignore this one. */
  1316. }
  1317. /* Might be necessary to prepend stored-up data! */
  1318. if (socklist[ret].inbuf != NULL) {
  1319. p = socklist[ret].inbuf;
  1320. socklist[ret].inbuf = calloc(1, strlen(p) + strlen(xx) + 1);
  1321. strcpy(socklist[ret].inbuf, p);
  1322. strcat(socklist[ret].inbuf, xx);
  1323. free(p);
  1324. if (strlen(socklist[ret].inbuf) < (SGRAB + 2)) {
  1325. strcpy(xx, socklist[ret].inbuf);
  1326. free(socklist[ret].inbuf);
  1327. socklist[ret].inbuf = NULL;
  1328. socklist[ret].inbuflen = 0;
  1329. } else {
  1330. p = socklist[ret].inbuf;
  1331. socklist[ret].inbuflen = strlen(p) - SGRAB;
  1332. socklist[ret].inbuf = (char *) calloc(1, socklist[ret].inbuflen + 1);
  1333. strcpy(socklist[ret].inbuf, p + SGRAB);
  1334. *(p + SGRAB) = 0;
  1335. strcpy(xx, p);
  1336. free(p);
  1337. /* (leave the rest to be post-pended later) */
  1338. }
  1339. }
  1340. /* Look for EOL marker; if it's there, i have something to show */
  1341. p = strchr(xx, '\n');
  1342. if (p == NULL)
  1343. p = strchr(xx, '\r');
  1344. if (p != NULL) {
  1345. *p = 0;
  1346. strcpy(s, xx);
  1347. /* strcpy(xx, p + 1); */
  1348. sprintf(xx, "%s", p + 1);
  1349. /* if (s[0] && strlen(s) && (s[strlen(s) - 1] == '\r')) */
  1350. if (s[strlen(s) - 1] == '\r')
  1351. s[strlen(s) - 1] = 0;
  1352. data = 1; /* DCC_CHAT may now need to process a blank line */
  1353. /* NO! */
  1354. /* if (!s[0]) strcpy(s," "); */
  1355. } else {
  1356. s[0] = 0;
  1357. if (strlen(xx) >= SGRAB) {
  1358. /* String is too long, so just insert fake \n */
  1359. strcpy(s, xx);
  1360. xx[0] = 0;
  1361. data = 1;
  1362. }
  1363. }
  1364. if (socklist[ret].encstatus && s[0])
  1365. botlink_decrypt(ret, s);
  1366. *len = strlen(s);
  1367. /* Anything left that needs to be saved? */
  1368. if (!xx[0]) {
  1369. if (data)
  1370. return socklist[ret].sock;
  1371. else
  1372. return -3;
  1373. }
  1374. /* Prepend old data back */
  1375. if (socklist[ret].inbuf != NULL) {
  1376. p = socklist[ret].inbuf;
  1377. socklist[ret].inbuflen = strlen(p) + strlen(xx);
  1378. socklist[ret].inbuf = calloc(1, socklist[ret].inbuflen + 1);
  1379. strcpy(socklist[ret].inbuf, xx);
  1380. strcat(socklist[ret].inbuf, p);
  1381. free(p);
  1382. } else {
  1383. socklist[ret].inbuflen = strlen(xx);
  1384. socklist[ret].inbuf = calloc(1, socklist[ret].inbuflen + 1);
  1385. strcpy(socklist[ret].inbuf, xx);
  1386. }
  1387. if (data) {
  1388. return socklist[ret].sock;
  1389. } else {
  1390. return -3;
  1391. }
  1392. }
  1393. /* Dump something to a socket
  1394. *
  1395. * NOTE: Do NOT put Contexts in here if you want DEBUG to be meaningful!!
  1396. */
  1397. void tputs(register int z, char *s, size_t len)
  1398. {
  1399. register int i, x, idx;
  1400. char *p = NULL;
  1401. static int inhere = 0;
  1402. if (z < 0) /* um... HELLO?! sanity check please! */
  1403. return;
  1404. if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
  1405. write(z, s, len);
  1406. return;
  1407. }
  1408. for (i = 0; i < MAXSOCKS; i++) {
  1409. if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
  1410. for (idx = 0; idx < dcc_total; idx++) {
  1411. if ((dcc[idx].sock == z) && dcc[idx].type && dcc[idx].type->name) {
  1412. if (!strncmp(dcc[idx].type->name, "BOT", 3))
  1413. traffic.out_today.bn += len;
  1414. else if (!strcmp(dcc[idx].type->name, "SERVER"))
  1415. traffic.out_today.irc += len;
  1416. else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
  1417. traffic.out_today.dcc += len;
  1418. else if (!strncmp(dcc[idx].type->name, "FILES", 5))
  1419. traffic.out_today.filesys += len;
  1420. else if (!strcmp(dcc[idx].type->name, "SEND"))
  1421. traffic.out_today.trans += len;
  1422. else if (!strncmp(dcc[idx].type->name, "GET", 3))
  1423. traffic.out_today.trans += len;
  1424. else
  1425. traffic.out_today.unknown += len;
  1426. break;
  1427. }
  1428. }
  1429. if (socklist[i].encstatus && (len > 0))
  1430. s = botlink_encrypt(i, s, &len); /* will modify len */
  1431. if (socklist[i].outbuf != NULL) {
  1432. /* Already queueing: just add it */
  1433. p = (char *) realloc(socklist[i].outbuf, socklist[i].outbuflen + len);
  1434. egg_memcpy(p + socklist[i].outbuflen, s, len);
  1435. socklist[i].outbuf = p;
  1436. socklist[i].outbuflen += len;
  1437. if (socklist[i].encstatus && s)
  1438. free(s);
  1439. return;
  1440. }
  1441. /* Try. */
  1442. #ifdef HAVE_SSL
  1443. if (socklist[i].ssl) {
  1444. x = SSL_write(socklist[i].ssl, s, len);
  1445. if (x < 0) {
  1446. int err = SSL_get_error(socklist[i].ssl, x);
  1447. x = -1;
  1448. switch (err) {
  1449. case SSL_ERROR_WANT_READ:
  1450. errno = EAGAIN;
  1451. break;
  1452. case SSL_ERROR_WANT_WRITE:
  1453. errno = EAGAIN;
  1454. break;
  1455. case SSL_ERROR_WANT_X509_LOOKUP:
  1456. errno = EAGAIN;
  1457. break;
  1458. }
  1459. }
  1460. } else
  1461. #endif /* HAVE_SSL */
  1462. #ifdef HAVE_ZLIB_H
  1463. /*
  1464. if (socklist[i].gz) {
  1465. FILE *fp;
  1466. fp = gzdopen(z, "wb0");
  1467. x = gzwrite(fp, s, len);
  1468. } else
  1469. */
  1470. #endif /* HAVE_ZLIB_H */
  1471. x = write(z, s, len);
  1472. if (x == -1)
  1473. x = 0;
  1474. if ((size_t) x < len) {
  1475. /* Socket is full, queue it */
  1476. socklist[i].outbuf = calloc(1, len - x);
  1477. egg_memcpy(socklist[i].outbuf, &s[x], len - x);
  1478. socklist[i].outbuflen = len - x;
  1479. }
  1480. if (socklist[i].encstatus && s)
  1481. free(s);
  1482. return;
  1483. }
  1484. }
  1485. /* Make sure we don't cause a crash by looping here */
  1486. if (!inhere) {
  1487. inhere = 1;
  1488. putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
  1489. s[strlen(s) - 1] = 0;
  1490. putlog(LOG_MISC, "*", "!-> '%s'", s);
  1491. inhere = 0;
  1492. }
  1493. /* if (socklist[i].encstatus > 0)
  1494. free(s);
  1495. */
  1496. }
  1497. int findanyidx(register int z)
  1498. {
  1499. register int j;
  1500. if (z != -1)
  1501. for (j = 0; j < dcc_total; j++)
  1502. if (dcc[j].sock == z)
  1503. return j;
  1504. return -1;
  1505. }
  1506. /* tputs might queue data for sockets, let's dump as much of it as
  1507. * possible.
  1508. */
  1509. void dequeue_sockets()
  1510. {
  1511. int i, x;
  1512. int z = 0, fds = 0;
  1513. fd_set wfds;
  1514. struct timeval tv;
  1515. /* ^-- start poptix test code, this should avoid writes to sockets not ready to be written to. */
  1516. FD_ZERO(&wfds);
  1517. tv.tv_sec = 0;
  1518. tv.tv_usec = 0; /* we only want to see if it's ready for writing, no need to actually wait.. */
  1519. for (i = 0; i < MAXSOCKS; i++) {
  1520. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].outbuf != NULL) {
  1521. FD_SET(socklist[i].sock, &wfds);
  1522. if (socklist[i].sock > fds)
  1523. fds = socklist[i].sock;
  1524. z = 1;
  1525. }
  1526. }
  1527. if (!z)
  1528. return; /* nothing to write */
  1529. fds++;
  1530. select(fds, NULL, &wfds, NULL, &tv);
  1531. /* end poptix */
  1532. for (i = 0; i < MAXSOCKS; i++) {
  1533. if (!(socklist[i].flags & SOCK_UNUSED) &&
  1534. (socklist[i].outbuf != NULL) && (FD_ISSET(socklist[i].sock, &wfds))) {
  1535. /* Trick tputs into doing the work */
  1536. errno = 0;
  1537. #ifdef HAVE_SSL
  1538. if (socklist[i].ssl) {
  1539. x = write(socklist[i].sock, socklist[i].outbuf, socklist[i].outbuflen);
  1540. if (x < 0) {
  1541. int err = SSL_get_error(socklist[i].ssl, x);
  1542. x = -1;
  1543. switch (err) {
  1544. case SSL_ERROR_WANT_READ:
  1545. errno = EAGAIN;
  1546. break;
  1547. case SSL_ERROR_WANT_WRITE:
  1548. errno = EAGAIN;
  1549. break;
  1550. case SSL_ERROR_WANT_X509_LOOKUP:
  1551. errno = EAGAIN;
  1552. break;
  1553. }
  1554. }
  1555. } else
  1556. #endif /* HAVE_SSL */
  1557. x = write(socklist[i].sock, socklist[i].outbuf, socklist[i].outbuflen);
  1558. if ((x < 0) && (errno != EAGAIN)
  1559. #ifdef EBADSLT
  1560. && (errno != EBADSLT)
  1561. #endif /* EBADSLT */
  1562. #ifdef ENOTCONN
  1563. && (errno != ENOTCONN)
  1564. #endif /* EBADSLT */
  1565. ) {
  1566. /* This detects an EOF during writing */
  1567. debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock,
  1568. strerror(errno), errno);
  1569. socklist[i].flags |= SOCK_EOFD;
  1570. } else if ((size_t) x == socklist[i].outbuflen) {
  1571. /* If the whole buffer was sent, nuke it */
  1572. free(socklist[i].outbuf);
  1573. socklist[i].outbuf = NULL;
  1574. socklist[i].outbuflen = 0;
  1575. } else if (x > 0) {
  1576. char *p = socklist[i].outbuf;
  1577. /* This removes any sent bytes from the beginning of the buffer */
  1578. socklist[i].outbuf = (char *) calloc(1, socklist[i].outbuflen - x);
  1579. egg_memcpy(socklist[i].outbuf, p + x, socklist[i].outbuflen - x);
  1580. socklist[i].outbuflen -= x;
  1581. free(p);
  1582. } else {
  1583. debug3("dequeue_sockets(): errno = %d (%s) on %d", errno,
  1584. strerror(errno), socklist[i].sock);
  1585. }
  1586. /* All queued data was sent. Call handler if one exists and the
  1587. * dcc entry wants it.
  1588. */
  1589. if (!socklist[i].outbuf) {
  1590. int idx = findanyidx(socklist[i].sock);
  1591. if (idx > 0 && dcc[idx].type && dcc[idx].type->outdone)
  1592. dcc[idx].type->outdone(idx);
  1593. }
  1594. }
  1595. }
  1596. }
  1597. /*
  1598. * Debugging stuff
  1599. */
  1600. void tell_netdebug(int idx)
  1601. {
  1602. int i;
  1603. char s[80] = "";
  1604. dprintf(idx, "Open sockets:");
  1605. for (i = 0; i < MAXSOCKS; i++) {
  1606. if (!(socklist[i].flags & SOCK_UNUSED)) {
  1607. sprintf(s, " %d", socklist[i].sock);
  1608. if (socklist[i].flags & SOCK_BINARY)
  1609. strcat(s, " (binary)");
  1610. if (socklist[i].flags & SOCK_LISTEN)
  1611. strcat(s, " (listen)");
  1612. if (socklist[i].flags & SOCK_PASS)
  1613. strcat(s, " (passed on)");
  1614. if (socklist[i].flags & SOCK_CONNECT)
  1615. strcat(s, " (connecting)");
  1616. if (socklist[i].flags & SOCK_STRONGCONN)
  1617. strcat(s, " (strong)");
  1618. if (socklist[i].flags & SOCK_NONSOCK)
  1619. strcat(s, " (file)");
  1620. if (socklist[i].inbuf != NULL)
  1621. sprintf(&s[strlen(s)], " (inbuf: %04X)", strlen(socklist[i].inbuf));
  1622. if (socklist[i].outbuf != NULL)
  1623. sprintf(&s[strlen(s)], " (outbuf: %06lX)", (unsigned long) socklist[i].outbuflen);
  1624. if (socklist[i].host)
  1625. sprintf(&s[strlen(s)], " (%s:%d)", socklist[i].host, socklist[i].port);
  1626. strcat(s, ",");
  1627. dprintf(idx, "%s", s);
  1628. }
  1629. }
  1630. dprintf(idx, " done.\n");
  1631. }
  1632. /* Checks wether the referenced socket has data queued.
  1633. *
  1634. * Returns true if the incoming/outgoing (depending on 'type') queues
  1635. * contain data, otherwise false.
  1636. */
  1637. int sock_has_data(int type, int sock)
  1638. {
  1639. int ret = 0, i;
  1640. for (i = 0; i < MAXSOCKS; i++)
  1641. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
  1642. break;
  1643. if (i < MAXSOCKS) {
  1644. switch (type) {
  1645. case SOCK_DATA_OUTGOING:
  1646. ret = (socklist[i].outbuf != NULL);
  1647. break;
  1648. case SOCK_DATA_INCOMING:
  1649. ret = (socklist[i].inbuf != NULL);
  1650. break;
  1651. }
  1652. } else
  1653. debug1("sock_has_data: could not find socket #%d, returning false.", sock);
  1654. return ret;
  1655. }
  1656. /* flush_inbuf():
  1657. * checks if there's data in the incoming buffer of an connection
  1658. * and flushs the buffer if possible
  1659. *
  1660. * returns: -1 if the dcc entry wasn't found
  1661. * -2 if dcc[idx].type->activity doesn't exist and the data couldn't
  1662. * be handled
  1663. * 0 if buffer was empty
  1664. * otherwise length of flushed buffer
  1665. */
  1666. int flush_inbuf(int idx)
  1667. {
  1668. int i, len;
  1669. char *inbuf = NULL;
  1670. Assert((idx >= 0) && (idx < dcc_total));
  1671. for (i = 0; i < MAXSOCKS; i++) {
  1672. if ((dcc[idx].sock == socklist[i].sock)
  1673. && !(socklist[i].flags & SOCK_UNUSED)) {
  1674. len = socklist[i].inbuflen;
  1675. if ((len > 0) && socklist[i].inbuf) {
  1676. if (dcc[idx].type && dcc[idx].type->activity) {
  1677. inbuf = socklist[i].inbuf;
  1678. socklist[i].inbuf = NULL;
  1679. dcc[idx].type->activity(idx, inbuf, len);
  1680. free(inbuf);
  1681. return len;
  1682. } else
  1683. return -2;
  1684. } else
  1685. return 0;
  1686. }
  1687. }
  1688. return -1;
  1689. }