net.c 48 KB

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