net.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  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 "crypt.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 firewall[121] = ""; /* Socks server for firewall */
  47. port_t firewallport = 1080; /* Default port of Sock4/5 firewalls */
  48. char botuser[21] = ""; /* Username of the user running the bot */
  49. int resolve_timeout = 10; /* hostname/address lookup timeout */
  50. int socks_total = 0; /* total number of sockets */
  51. sock_list *socklist = NULL; /* Enough to be safe */
  52. int MAXSOCKS = 0;
  53. jmp_buf alarmret; /* Env buffer for alarm() returns */
  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(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. for (int i = 0; i < MAXSOCKS; i++) {
  183. egg_bzero(&socklist[i], sizeof(socklist[i]));
  184. #ifdef HAVE_SSL
  185. socklist[i].ssl=NULL;
  186. #endif /* HAVE_SSL */
  187. socklist[i].flags = SOCK_UNUSED;
  188. }
  189. #ifdef HAVE_SSL
  190. SSL_load_error_strings();
  191. OpenSSL_add_ssl_algorithms();
  192. ssl_c_ctx = SSL_CTX_new(SSLv23_client_method());
  193. ssl_s_ctx = SSL_CTX_new(SSLv23_server_method());
  194. if (!ssl_c_ctx || !ssl_s_ctx)
  195. fatal("SSL Inititlization failed", 0);
  196. if (seed_PRNG())
  197. fatal("SSL PRNG seeding failed!", 0);
  198. #endif /* HAVE_SSL */
  199. }
  200. #ifdef HAVE_SSL
  201. int ssl_cleanup() {
  202. if (ssl_c_ctx) {
  203. SSL_CTX_free(ssl_c_ctx);
  204. ssl_c_ctx = NULL;
  205. }
  206. if (ssl_s_ctx) {
  207. SSL_CTX_free(ssl_s_ctx);
  208. ssl_s_ctx = NULL;
  209. }
  210. if (tls_rand_file) RAND_write_file(tls_rand_file);
  211. return 0;
  212. }
  213. #endif /* HAVE_SSL */
  214. /* Get my ipv? ip
  215. */
  216. char *myipstr(int af_type)
  217. {
  218. #ifdef USE_IPV6
  219. if (af_type == 6) {
  220. static char s[UHOSTLEN + 1] = "";
  221. egg_inet_ntop(AF_INET6, &cached_myip6_so.sin6.sin6_addr, s, 119);
  222. s[120] = 0;
  223. return s;
  224. } else
  225. #endif /* USE_IPV6 */
  226. if (af_type == 4) {
  227. static char s[UHOSTLEN + 1] = "";
  228. egg_inet_ntop(AF_INET, &cached_myip4_so.sin.sin_addr, s, 119);
  229. s[120] = 0;
  230. return s;
  231. }
  232. return "";
  233. }
  234. /* Get my ip number
  235. */
  236. in_addr_t getmyip() {
  237. return natip[0] ? inet_addr(natip) : cached_myip4_so.sin.sin_addr.s_addr;
  238. }
  239. /* see if it's necessary to set inaddr_any... because if we can't resolve, we die anyway */
  240. void cache_my_ip()
  241. {
  242. int error = 0;
  243. debug0("cache_my_ip()");
  244. egg_memset(&cached_myip4_so, 0, sizeof(union sockaddr_union));
  245. #ifdef USE_IPV6
  246. bool any = 0;
  247. egg_memset(&cached_myip6_so, 0, sizeof(union sockaddr_union));
  248. if (conf.bot->net.ip6) {
  249. if (get_ip(conf.bot->net.ip6, &cached_myip6_so))
  250. any = 1;
  251. } else if (conf.bot->net.host6) {
  252. if (get_ip(conf.bot->net.host6, &cached_myip6_so))
  253. any = 1;
  254. } else
  255. any = 1;
  256. if (any) {
  257. cached_myip6_so.sin6.sin6_family = AF_INET6;
  258. cached_myip6_so.sin6.sin6_addr = in6addr_any;
  259. }
  260. #endif /* USE_IPV6 */
  261. if (conf.bot->net.ip) {
  262. if (get_ip(conf.bot->net.ip, &cached_myip4_so))
  263. error = 1;
  264. } else if (conf.bot->net.host) {
  265. if (get_ip(conf.bot->net.host, &cached_myip4_so))
  266. error = 2;
  267. } else {
  268. /*
  269. char s[121] = "";
  270. gethostname(s, sizeof(s));
  271. if (get_ip(s, &cached_myip4_so)) {
  272. */
  273. /* error = 3; */
  274. cached_myip4_so.sin.sin_family = AF_INET;
  275. cached_myip4_so.sin.sin_addr.s_addr = INADDR_ANY;
  276. // }
  277. }
  278. if (error) {
  279. putlog(LOG_DEBUG, "*", "Hostname self-lookup error: %d", error);
  280. fatal("Hostname self-lookup failed.", 0);
  281. }
  282. }
  283. /* Sets/Unsets options for a specific socket.
  284. *
  285. * Returns: 0 - on success
  286. * -1 - socket not found
  287. * -2 - illegal operation
  288. */
  289. int sockoptions(int sock, int operation, int sock_options)
  290. {
  291. for (int i = 0; i < MAXSOCKS; i++) {
  292. if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
  293. if (operation == EGG_OPTION_SET)
  294. socklist[i].flags |= sock_options;
  295. else if (operation == EGG_OPTION_UNSET)
  296. socklist[i].flags &= ~sock_options;
  297. else
  298. return -2;
  299. return 0;
  300. }
  301. }
  302. return -1;
  303. }
  304. /* Return a free entry in the socket entry
  305. */
  306. int allocsock(int sock, int options)
  307. {
  308. for (int i = 0; i < MAXSOCKS; i++) {
  309. if (socklist[i].flags & SOCK_UNUSED) {
  310. /* yay! there is table space */
  311. socklist[i].inbuf = socklist[i].outbuf = NULL;
  312. socklist[i].inbuflen = socklist[i].outbuflen = 0;
  313. #ifdef HAVE_SSL
  314. socklist[i].ssl = NULL;
  315. #endif /* HAVE_SSL */
  316. socklist[i].flags = options;
  317. socklist[i].sock = sock;
  318. socklist[i].encstatus = 0;
  319. socklist[i].gz = 0;
  320. egg_bzero(&socklist[i].okey, sizeof(socklist[i].okey));
  321. egg_bzero(&socklist[i].ikey, sizeof(socklist[i].ikey));
  322. socklist[i].okey[0] = 0;
  323. socklist[i].ikey[0] = 0;
  324. socks_total++;
  325. sdprintf("allocsock(%d) = %d", i, sock);
  326. return i;
  327. }
  328. }
  329. fatal("Socket table is full!", 0);
  330. return -1; /* Never reached */
  331. }
  332. /* Request a normal socket for i/o
  333. */
  334. void setsock(int sock, int options)
  335. {
  336. int i = allocsock(sock, options);
  337. bool parm;
  338. if (((sock != STDOUT) || backgrd) && !(socklist[i].flags & SOCK_NONSOCK)) {
  339. parm = 1;
  340. setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *) &parm, sizeof(int));
  341. parm = 0;
  342. setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &parm, sizeof(int));
  343. }
  344. if (options & SOCK_LISTEN) {
  345. /* Tris says this lets us grab the same port again next time */
  346. parm = 1;
  347. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &parm, sizeof(int));
  348. }
  349. /* Yay async i/o ! */
  350. fcntl(sock, F_SETFL, O_NONBLOCK);
  351. }
  352. #ifdef USE_IPV6
  353. int real_getsock(int options, int af_def, char *fname, int line)
  354. {
  355. #else
  356. int real_getsock(int options, char *fname, int line)
  357. {
  358. int af_def = AF_INET;
  359. #endif /* USE_IPV6 */
  360. int sock = socket(af_def, SOCK_STREAM, 0);
  361. if (sock >= 0)
  362. setsock(sock, options);
  363. else if (!identd_hack)
  364. putlog(LOG_WARNING, "*", "Warning: Can't create new socket! (%s:%d)", fname, line);
  365. else if (identd_hack)
  366. identd_hack = 0;
  367. return sock;
  368. }
  369. #ifdef HAVE_SSL
  370. void dropssl(register int sock)
  371. {
  372. if (sock < 0)
  373. return;
  374. int i;
  375. for (i = 0; (i < MAXSOCKS); i++)
  376. if (socklist[i].sock == sock) break;
  377. if (socklist[i].ssl) {
  378. SSL_set_quiet_shutdown(socklist[i].ssl, 1);
  379. SSL_shutdown(socklist[i].ssl);
  380. usleep(1000 * 500);
  381. SSL_free(socklist[i].ssl);
  382. usleep(1000 * 500);
  383. socklist[i].ssl = NULL;
  384. }
  385. }
  386. #endif /* HAVE_SSL */
  387. /* Done with a socket
  388. */
  389. void real_killsock(register int sock, const char *file, int line)
  390. {
  391. if (sock < 0) {
  392. putlog(LOG_ERRORS, "*", "Attempt to kill socket -1 %s:%d", file, line);
  393. return;
  394. }
  395. for (register int i = 0; i < MAXSOCKS; i++) {
  396. if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
  397. #ifdef HAVE_SSL
  398. dropssl(sock);
  399. #endif /* HAVE_SSL */
  400. close(socklist[i].sock);
  401. if (socklist[i].inbuf != NULL) {
  402. free(socklist[i].inbuf);
  403. socklist[i].inbuf = NULL;
  404. }
  405. if (socklist[i].outbuf != NULL) {
  406. free(socklist[i].outbuf);
  407. socklist[i].outbuf = NULL;
  408. socklist[i].outbuflen = 0;
  409. }
  410. if (socklist[i].host)
  411. free(socklist[i].host);
  412. egg_bzero(&socklist[i], sizeof(socklist[i]));
  413. socklist[i].flags = SOCK_UNUSED;
  414. socks_total--;
  415. sdprintf("killsock(%d, %s, %d) (socklist: %d)", sock, file, line, i);
  416. return;
  417. }
  418. }
  419. putlog(LOG_MISC, "*", "Attempt to kill un-allocated socket %d %s:%d !!", sock, file, line);
  420. }
  421. /* Send connection request to proxy
  422. */
  423. static int proxy_connect(int sock, char *host, int port, int proxy)
  424. {
  425. #ifdef USE_IPV6
  426. unsigned char x[32] = "";
  427. int af_ty = sockprotocol(sock);
  428. #else
  429. unsigned char x[10] = "";
  430. #endif /* USE_IPV6 */
  431. char s[256] = "";
  432. /* socks proxy */
  433. if (proxy == PROXY_SOCKS) {
  434. /* numeric IP? */
  435. #ifdef USE_IPV6
  436. if ((host[strlen(host) - 1] >= '0' && host[strlen(host) - 1] <= '9') && af_ty != AF_INET6) {
  437. #else
  438. if (host[strlen(host) - 1] >= '0' && host[strlen(host) - 1] <= '9') {
  439. #endif /* USE_IPV6 */
  440. in_addr_t ip = ((in_addr_t) inet_addr(host));
  441. egg_memcpy(x, &ip, 4);
  442. } else {
  443. struct hostent *hp = NULL;
  444. /* no, must be host.domain */
  445. if (!setjmp(alarmret)) {
  446. #ifdef USE_IPV6
  447. alarm(resolve_timeout);
  448. if (af_ty == AF_INET6)
  449. hp = gethostbyname(host);
  450. else
  451. #endif /* USE_IPV6 */
  452. hp = gethostbyname(host);
  453. #ifdef USE_IPV6
  454. alarm(0);
  455. #endif /* USE_IPV6 */
  456. } else
  457. hp = NULL;
  458. if (hp == NULL) {
  459. killsock(sock);
  460. return -2;
  461. }
  462. egg_memcpy(x, hp->h_addr, hp->h_length);
  463. }
  464. for (int i = 0; i < MAXSOCKS; i++)
  465. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
  466. socklist[i].flags |= SOCK_PROXYWAIT; /* drummer */
  467. #ifdef USE_IPV6
  468. if (af_ty == AF_INET6)
  469. egg_snprintf(s, sizeof s,
  470. "\004\001%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%s",
  471. (port >> 8) % 256, (port % 256), x[0], x[1], x[2], x[3],
  472. x[4], x[5], x[6], x[7], x[9], x[9], x[10], x[11], x[12],
  473. x[13], x[14], x[15], botuser);
  474. else
  475. #endif /* USE_IPV6 */
  476. egg_snprintf(s, sizeof s, "\004\001%c%c%c%c%c%c%s", (port >> 8) % 256,
  477. (port % 256), x[0], x[1], x[2], x[3], botuser);
  478. tputs(sock, s, strlen(botuser) + 9); /* drummer */
  479. } else if (proxy == PROXY_SUN) {
  480. egg_snprintf(s, sizeof s, "%s %d\n", host, port);
  481. tputs(sock, s, strlen(s)); /* drummer */
  482. } else if (proxy == PROXY_HTTP) {
  483. egg_snprintf(s, sizeof s, "CONNECT %s:%d\n\n", host, port);
  484. tputs(sock, s, strlen(s));
  485. }
  486. return sock;
  487. }
  488. /* FIXME: REPLACE WITH SOCK_NAME() */
  489. void initialize_sockaddr(int af_type, const char *host, port_t port, union sockaddr_union *so)
  490. {
  491. egg_bzero(so, sizeof(*so));
  492. so->sa.sa_family = af_type;
  493. if (af_type == AF_INET) {
  494. so->sin.sin_family = AF_INET;
  495. if (host) {
  496. inet_pton(AF_INET, host, &so->sin.sin_addr);
  497. so->sin.sin_port = htons(port);
  498. } else {
  499. so->sin.sin_addr.s_addr = getmyip();
  500. }
  501. #ifdef USE_IPV6
  502. } else {
  503. so->sin6.sin6_family = AF_INET6;
  504. if (host) {
  505. inet_pton(AF_INET6, host, &so->sin6.sin6_addr);
  506. so->sin6.sin6_port = htons(port);
  507. } else {
  508. memcpy(&so->sin6.sin6_addr, &cached_myip6_so.sin6.sin6_addr, 16);
  509. }
  510. #endif /* USE_IPV6 */
  511. }
  512. }
  513. /* Starts a connection attempt to a socket
  514. *
  515. * If given a normal hostname, this will be resolved to the corresponding
  516. * IP address first. PLEASE try to use the non-blocking dns functions
  517. * instead and then call this function with the IP address to avoid blocking.
  518. *
  519. * returns <0 if connection refused:
  520. * -1 strerror()/errno type error
  521. * -2 can't resolve hostname
  522. */
  523. int open_telnet_raw(int sock, char *server, port_t sport)
  524. {
  525. static port_t port = 0;
  526. union sockaddr_union so;
  527. char host[121] = "";
  528. volatile int proxy;
  529. int is_resolved = 0;
  530. /* firewall? use socks */
  531. if (firewall[0]) {
  532. if (firewall[0] == '!') {
  533. proxy = PROXY_SUN;
  534. strcpy(host, &firewall[1]);
  535. } else if (firewall[0] == '@') {
  536. proxy = PROXY_HTTP;
  537. strcpy(host, &firewall[1]);
  538. } else {
  539. proxy = PROXY_SOCKS;
  540. strcpy(host, firewall);
  541. }
  542. port = firewallport;
  543. } else {
  544. proxy = 0;
  545. strncpyz(host, server, sizeof host);
  546. port = sport;
  547. }
  548. /* figure out which ip to bind to locally (v4 or v6) based on what the host ip is .. */
  549. if ((is_resolved = is_dotted_ip(server))) { /* already resolved */
  550. initialize_sockaddr(is_resolved, NULL, 0, &so);
  551. if (bind(sock, &so.sa, SIZEOF_SOCKADDR(so)) < 0) {
  552. killsock(sock);
  553. return -1;
  554. }
  555. initialize_sockaddr(is_resolved, host, port, &so);
  556. } else { /* if not resolved, resolve it with blocking calls.. (shouldn't happen ever) */
  557. sdprintf("WARNING: open_telnet_raw() is about to block in get_ip()!");
  558. if (!setjmp(alarmret)) {
  559. alarm(resolve_timeout);
  560. if (!get_ip(host, &so)) {
  561. alarm(0);
  562. /* ok, we resolved it, bind an appropriate ip */
  563. #ifdef USE_IPV6
  564. if (so.sa.sa_family == AF_INET6) {
  565. if (bind(sock, &cached_myip6_so.sa, SIZEOF_SOCKADDR(cached_myip6_so)) < 0) {
  566. killsock(sock);
  567. return -1;
  568. }
  569. } else {
  570. #endif /* USE_IPV6 */
  571. if (bind(sock, &cached_myip4_so.sa, SIZEOF_SOCKADDR(cached_myip4_so)) < 0) {
  572. killsock(sock);
  573. return -3;
  574. }
  575. #ifdef USE_IPV6
  576. }
  577. if (so.sa.sa_family == AF_INET6)
  578. so.sin6.sin6_port = htons(port);
  579. else
  580. #endif /* USE_IPV6 */
  581. so.sin.sin_port = htons(port);
  582. } else {
  583. alarm(0);
  584. killsock(sock);
  585. return -2;
  586. }
  587. }
  588. }
  589. for (int i = 0; i < MAXSOCKS; i++) {
  590. if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock)) {
  591. socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT;
  592. socklist[i].host = strdup(server);
  593. socklist[i].port = port;
  594. }
  595. }
  596. int rc = connect(sock, &so.sa, SIZEOF_SOCKADDR(so));
  597. if (rc < 0) {
  598. if (errno == EINPROGRESS) {
  599. /* Firewall? announce connect attempt to proxy */
  600. if (firewall[0])
  601. return proxy_connect(sock, server, sport, proxy);
  602. return sock; /* async success! */
  603. } else {
  604. sdprintf("connect(%s) failed: %s", server, strerror(errno));
  605. killsock(sock);
  606. return -1;
  607. }
  608. }
  609. /* Synchronous? :/ */
  610. if (firewall[0])
  611. return proxy_connect(sock, server, sport, proxy);
  612. return sock;
  613. }
  614. /* Ordinary non-binary connection attempt */
  615. int open_telnet(char *server, port_t port)
  616. {
  617. int sock = -1;
  618. #ifdef USE_IPV6
  619. sock = getsock(0, hostprotocol(server));
  620. #else
  621. sock = getsock(0);
  622. #endif /* USE_IPV6 */
  623. if (sock >= 0)
  624. return open_telnet_raw(sock, server, port);
  625. return -1;
  626. }
  627. /* Returns a socket number for a listening socket that will accept any
  628. * connection on a certain address -- port # is returned in port
  629. *
  630. * 'addr' is ignored if af_def is AF_INET6 -poptix (02/03/03)
  631. */
  632. #ifdef USE_IPV6
  633. int open_address_listen(in_addr_t addr, int af_def, port_t *port)
  634. #else
  635. intt open_address_listen(in_addr_t addr, port_t *port)
  636. #endif /* USE_IPV6 */
  637. {
  638. if (firewall[0]) {
  639. /* FIXME: can't do listen port thru firewall yet */
  640. putlog(LOG_MISC, "*", "!! Cant open a listen port (you are using a firewall)");
  641. return -1;
  642. }
  643. int sock = 0;
  644. socklen_t addrlen;
  645. struct sockaddr_in name;
  646. #ifdef USE_IPV6
  647. if (af_def == AF_INET6) {
  648. struct sockaddr_in6 name6;
  649. sock = getsock(SOCK_LISTEN, af_def);
  650. if (sock < 0)
  651. return -1;
  652. debug2("Opening listen socket on port %d with AF_INET6, sock: %d", *port, sock);
  653. egg_bzero((char *) &name6, sizeof(name6));
  654. name6.sin6_family = af_def;
  655. name6.sin6_port = htons(*port); /* 0 = just assign us a port */
  656. /* memcpy(&name6.sin6_addr, &in6addr_any, 16); */ /* this is the only way to get ipv6+ipv4 in 1 socket */
  657. memcpy(&name6.sin6_addr, &cached_myip6_so.sin6.sin6_addr, 16);
  658. if (bind(sock, (struct sockaddr *) &name6, sizeof(name6)) < 0) {
  659. sdprintf("Failed to open sock '%d' for listen on port '%d'", sock, *port);
  660. killsock(sock);
  661. return -1;
  662. }
  663. addrlen = sizeof(name6);
  664. if (getsockname(sock, (struct sockaddr *) &name6, &addrlen) < 0) {
  665. sdprintf("Failed to open sock '%d' for listen on port '%d'", sock, *port);
  666. killsock(sock);
  667. return -1;
  668. }
  669. *port = ntohs(name6.sin6_port);
  670. if (listen(sock, 1) < 0) {
  671. sdprintf("Failed to open sock '%d' for listen on port '%d'", sock, *port);
  672. killsock(sock);
  673. return -1;
  674. }
  675. } else {
  676. sock = getsock(SOCK_LISTEN, AF_INET);
  677. #else
  678. sock = getsock(SOCK_LISTEN);
  679. #endif /* USE_IPV6 */
  680. if (sock < 0)
  681. return -1;
  682. debug2("Opening listen socket on port %d with AF_INET, sock: %d", *port, sock);
  683. egg_bzero((char *) &name, sizeof(struct sockaddr_in));
  684. name.sin_family = AF_INET;
  685. name.sin_port = htons(*port); /* 0 = just assign us a port */
  686. name.sin_addr.s_addr = addr;
  687. if (bind(sock, (struct sockaddr *) &name, sizeof(name)) < 0) {
  688. sdprintf("Failed to open sock '%d' for listen on port '%d'", sock, *port);
  689. killsock(sock);
  690. return -1;
  691. }
  692. /* what port are we on? */
  693. addrlen = sizeof(name);
  694. if (getsockname(sock, (struct sockaddr *) &name, &addrlen) < 0) {
  695. sdprintf("Failed to open sock '%d' for listen on port '%d'", sock, *port);
  696. killsock(sock);
  697. return -1;
  698. }
  699. *port = ntohs(name.sin_port);
  700. if (listen(sock, 1) < 0) {
  701. sdprintf("Failed to open sock '%d' for listen on port '%d'", sock, *port);
  702. killsock(sock);
  703. return -1;
  704. }
  705. #ifdef USE_IPV6
  706. }
  707. #endif /* USE_IPV6 */
  708. debug2("Opened listen socket on port %d with AF_INET, sock: %d", *port, sock);
  709. return sock;
  710. }
  711. /* Returns a socket number for a listening socket that will accept any
  712. * connection -- port # is returned in port
  713. */
  714. int open_listen(port_t *port)
  715. {
  716. #ifdef USE_IPV6
  717. return open_address_listen(getmyip(), AF_INET, port);
  718. #else
  719. return open_address_listen(getmyip(), port);
  720. #endif /* USE_IPV6 */
  721. }
  722. /* Same as above, except this one can be called with an AF_ type
  723. * the above is being left in for compatibility, and should NOT LONGER BE USED IN THE CORE CODE.
  724. */
  725. int open_listen_by_af(port_t *port, int af_def)
  726. {
  727. #ifdef USE_IPV6
  728. return open_address_listen(getmyip(), af_def, port);
  729. #else
  730. return -1;
  731. #endif /* USE_IPV6 */
  732. }
  733. #ifdef HAVE_SSL
  734. int ssl_link(register int sock, int state)
  735. {
  736. int err = 0, i = 0, errs = 0;
  737. debug2("ssl_link(%d, %d)", sock, state);
  738. for (i = 0; (i < MAXSOCKS); i++) {
  739. if (socklist[i].sock == sock) break;
  740. }
  741. if (socklist[i].ssl) {
  742. putlog(LOG_ERROR, "*", "Switching to SSL (%d,%d) - already active", state, sock);
  743. return 0;
  744. }
  745. if (state == CONNECT_SSL) {
  746. socklist[i].ssl = SSL_new(ssl_c_ctx);
  747. } else if (state == ACCEPT_SSL) {
  748. socklist[i].ssl = SSL_new(ssl_s_ctx);
  749. }
  750. if (!socklist[i].ssl) {
  751. putlog(LOG_ERROR, "*", "Switching to SSL (%d) - SSL_new(%d) failed", sock, state);
  752. return 0;
  753. }
  754. if (!SSL_set_fd(socklist[i].ssl, socklist[i].sock)) {
  755. putlog(LOG_ERROR, "*", "SSL_set_fd(%d) (%d) failed", state, socklist[i].sock);
  756. return 0;
  757. }
  758. if (state == CONNECT_SSL) {
  759. SSL_set_connect_state(socklist[i].ssl);
  760. } else if (state == ACCEPT_SSL) {
  761. SSL_set_accept_state(socklist[i].ssl);
  762. } else {
  763. putlog(LOG_DEBUG, "*", "ssl_link(%d, 0?) NO STATE?", sock);
  764. return 0;
  765. }
  766. if (state == CONNECT_SSL) {
  767. err = SSL_connect(socklist[i].ssl);
  768. } else if (state == ACCEPT_SSL) {
  769. err = SSL_accept(socklist[i].ssl);
  770. }
  771. if (!setjmp(alarmret)) {
  772. alarm(5); /* this is plenty of time */
  773. while ((err < 1) && (errno == EAGAIN)) {
  774. if (state == CONNECT_SSL) {
  775. err = SSL_connect(socklist[i].ssl);
  776. } else if (state == ACCEPT_SSL) {
  777. err = SSL_accept(socklist[i].ssl);
  778. }
  779. /* if ((errs!=SSL_ERROR_WANT_READ)&&(errs!=SSL_ERROR_WANT_WRITE)&& (errs!=SSL_ERROR_WANT_X509_LOOKUP)) */
  780. /* break; anything not one of these is a sufficient condition to break out... */
  781. }
  782. alarm(0);
  783. }
  784. errs = SSL_get_error(socklist[i].ssl, err);
  785. 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));
  786. if (errno) putlog(LOG_DEBUG, "*", "errno %d: %s", errno, strerror(errno));
  787. if (err == 1) {
  788. putlog(LOG_ERROR, "*", "SSL_link(%d, %d) was successfull", sock, state);
  789. return 1;
  790. } else {
  791. putlog(LOG_ERROR, "*", "SSL_link(%d, %d) failed", sock, state);
  792. dropssl(socklist[i].sock);
  793. }
  794. return 0;
  795. }
  796. #endif /* HAVE_SSL */
  797. /* Returns the given network byte order IP address in the
  798. * dotted format - "##.##.##.##"
  799. */
  800. char *iptostr(in_addr_t ip)
  801. {
  802. static char ipbuf[32];
  803. struct in_addr a;
  804. a.s_addr = ip;
  805. return (char *) egg_inet_ntop(AF_INET, &a, ipbuf, sizeof(ipbuf));
  806. }
  807. /* Short routine to answer a connect received on a socket made previously
  808. * by open_listen ... returns hostname of the caller & the new socket
  809. * does NOT dispose of old "public" socket!
  810. */
  811. int answer(int sock, char *caller, in_addr_t *ip, port_t *port, int binary)
  812. {
  813. int new_sock;
  814. socklen_t addrlen;
  815. struct sockaddr_in from;
  816. #ifdef USE_IPV6
  817. int af_ty = sockprotocol(sock);
  818. struct sockaddr_in6 from6;
  819. egg_bzero(&from6, sizeof(struct sockaddr_in6));
  820. if (af_ty == AF_INET6) {
  821. addrlen = sizeof(from6);
  822. new_sock = accept(sock, (struct sockaddr *) &from6, &addrlen);
  823. } else {
  824. #endif /* USE_IPV6 */
  825. addrlen = sizeof(struct sockaddr);
  826. new_sock = accept(sock, (struct sockaddr *) &from, &addrlen);
  827. #ifdef USE_IPV6
  828. }
  829. #endif /* USE_IPV6 */
  830. if (new_sock < 0)
  831. return -1;
  832. if (ip != NULL) {
  833. #ifdef USE_IPV6
  834. /* Detect IPv4 in IPv6 mapped address .... */
  835. if (af_ty == AF_INET6 && (!IN6_IS_ADDR_V4MAPPED(&from6.sin6_addr))) {
  836. egg_inet_ntop(AF_INET6, &from6.sin6_addr, caller, 119);
  837. caller[120] = 0;
  838. *ip = 0L;
  839. } else if (IN6_IS_ADDR_V4MAPPED(&from6.sin6_addr)) { /* ...and convert it to plain (AF_INET) IPv4 address (openssh) */
  840. struct sockaddr_in *from4 = (struct sockaddr_in *)&from6;
  841. struct in_addr addr;
  842. memcpy(&addr, ((char *)&from6.sin6_addr) + 12, sizeof(addr));
  843. egg_memset(&from, 0, sizeof(from));
  844. from4->sin_family = AF_INET;
  845. addrlen = sizeof(*from4);
  846. memcpy(&from4->sin_addr, &addr, sizeof(addr));
  847. *ip = from4->sin_addr.s_addr;
  848. strncpyz(caller, iptostr(*ip), 121);
  849. *ip = ntohl(*ip);
  850. } else {
  851. #endif /* USE_IPV6 */
  852. *ip = from.sin_addr.s_addr;
  853. strncpyz(caller, iptostr(*ip), 121);
  854. *ip = ntohl(*ip);
  855. #ifdef USE_IPV6
  856. }
  857. #endif /* USE_IPV6 */
  858. }
  859. if (port != NULL) {
  860. #ifdef USE_IPV6
  861. if (af_ty == AF_INET6)
  862. *port = ntohs(from6.sin6_port);
  863. else
  864. #endif /* USE_IPV6 */
  865. *port = ntohs(from.sin_port);
  866. }
  867. /* Set up all the normal socket crap */
  868. setsock(new_sock, (binary ? SOCK_BINARY : 0));
  869. sdprintf("Answered socket %d: %s", new_sock, caller);
  870. return new_sock;
  871. }
  872. /* Like open_telnet, but uses server & port specifications of dcc
  873. */
  874. int open_telnet_dcc(int sock, char *server, char *port)
  875. {
  876. port_t p;
  877. unsigned long addr;
  878. char sv[500] = "";
  879. unsigned char c[4] = "";
  880. #ifdef DEBUG_IPV6
  881. debug1("open_telnet_dcc %s", server);
  882. #endif /* DEBUG_IPV6 */
  883. if (port != NULL)
  884. p = atoi(port);
  885. else
  886. p = 2000;
  887. #ifdef USE_IPV6
  888. if (sockprotocol(sock) == AF_INET6) {
  889. # ifdef DEBUG_IPV6
  890. debug0("open_telnet_dcc, af_inet6!");
  891. # endif /* DEBUG_IPV6 */
  892. strncpyz(sv, server, sizeof sv);
  893. debug2("%s should be %s",sv,server);
  894. } else {
  895. #endif /* USE_IPV6 */
  896. if (server != NULL)
  897. addr = my_atoul(server);
  898. else
  899. addr = 0L;
  900. if (addr < (1 << 24))
  901. return -3; /* fake address */
  902. c[0] = (addr >> 24) & 0xff;
  903. c[1] = (addr >> 16) & 0xff;
  904. c[2] = (addr >> 8) & 0xff;
  905. c[3] = addr & 0xff;
  906. sprintf(sv, "%u.%u.%u.%u", c[0], c[1], c[2], c[3]);
  907. #ifdef USE_IPV6
  908. }
  909. /* strcpy(sv,hostnamefromip(addr)); */
  910. # ifdef DEBUG_IPV6
  911. debug3("open_telnet_raw %s %d %d", sv, sock, p);
  912. # endif /* DEBUG_IPV6 */
  913. #endif /* USE_IPV6 */
  914. return open_telnet_raw(sock, sv, p);
  915. }
  916. /* Attempts to read from all the sockets in socklist
  917. * fills s with up to 511 bytes if available, and returns the array index
  918. *
  919. * on EOF: returns -1, with socket in len
  920. * on socket error: returns -2
  921. * if nothing is ready: returns -3
  922. */
  923. static int sockread(char *s, int *len)
  924. {
  925. fd_set fd;
  926. int fds = 0, i, fdtmp, x;
  927. struct timeval t;
  928. int grab = SGRAB + 1;
  929. egg_timeval_t howlong;
  930. if (timer_get_shortest(&howlong)) {
  931. /* No timer, default to 1 second. */
  932. t.tv_sec = 1;
  933. t.tv_usec = 0;
  934. }
  935. else {
  936. t.tv_sec = howlong.sec;
  937. t.tv_usec = howlong.usec;
  938. }
  939. FD_ZERO(&fd);
  940. for (i = 0; i < MAXSOCKS; i++) {
  941. if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_VIRTUAL))) {
  942. if ((socklist[i].sock == STDOUT) && !backgrd)
  943. fdtmp = STDIN;
  944. else
  945. fdtmp = socklist[i].sock;
  946. if (fdtmp > fds)
  947. fds = fdtmp;
  948. FD_SET(fdtmp, &fd);
  949. }
  950. }
  951. fds++;
  952. x = select(fds, &fd, NULL, NULL, &t);
  953. if (x > 0) {
  954. /* Something happened */
  955. for (i = 0; i < MAXSOCKS; i++) {
  956. if ((!(socklist[i].flags & SOCK_UNUSED)) && ((FD_ISSET(socklist[i].sock, &fd)) ||
  957. #ifdef HAVE_SSL
  958. ((socklist[i].ssl) && (SSL_pending(socklist[i].ssl))) ||
  959. #endif /* HAVE_SSL */
  960. ((socklist[i].sock == STDOUT) && (!backgrd) && (FD_ISSET(STDIN, &fd))))) {
  961. if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
  962. /* Listening socket -- don't read, just return activity */
  963. /* Same for connection attempt */
  964. /* (for strong connections, require a read to succeed first) */
  965. if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
  966. /* Hang around to get the return code from proxy */
  967. grab = 10;
  968. } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
  969. debug1("net: connect! sock %d", socklist[i].sock);
  970. s[0] = 0;
  971. *len = 0;
  972. #ifdef HAVE_SSL
  973. /* debug0("CALLING SSL_LINK() FROM SOCKREAD");
  974. if (!ssl_link(socklist[i].sock))
  975. debug0("SSL_LINK FAILED");
  976. debug0("BACK FROM SSL_LINK()"); */
  977. #endif /* HAVE_SSL */
  978. return i;
  979. }
  980. } else if (socklist[i].flags & SOCK_PASS) {
  981. s[0] = 0;
  982. *len = 0;
  983. return i;
  984. }
  985. errno = 0;
  986. if ((socklist[i].sock == STDOUT) && !backgrd)
  987. x = read(STDIN, s, grab);
  988. else {
  989. #ifdef HAVE_SSL
  990. if (socklist[i].ssl) {
  991. x = SSL_read(socklist[i].ssl, s, grab);
  992. if (x < 0) {
  993. int err = SSL_get_error(socklist[i].ssl, x);
  994. x = -1;
  995. switch (err) {
  996. case SSL_ERROR_WANT_READ:
  997. errno = EAGAIN;
  998. break;
  999. case SSL_ERROR_WANT_WRITE:
  1000. errno = EAGAIN;
  1001. break;
  1002. case SSL_ERROR_WANT_X509_LOOKUP:
  1003. errno = EAGAIN;
  1004. break;
  1005. }
  1006. }
  1007. } else
  1008. #endif /* HAVE_SSL */
  1009. x = read(socklist[i].sock, s, grab);
  1010. }
  1011. if (x <= 0) { /* eof */
  1012. if (errno != EAGAIN) { /* EAGAIN happens when the operation would block
  1013. on a non-blocking socket, if the socket is going
  1014. to die, it will die later, otherwise it will connect */
  1015. *len = socklist[i].sock;
  1016. socklist[i].flags &= ~SOCK_CONNECT;
  1017. debug1("net: eof!(read) socket %d", socklist[i].sock);
  1018. return -1;
  1019. } else {
  1020. debug3("sockread EAGAIN: %d %d (%s)", socklist[i].sock, errno, strerror(errno));
  1021. continue; /* EAGAIN */
  1022. }
  1023. }
  1024. s[x] = 0;
  1025. *len = x;
  1026. if (socklist[i].flags & SOCK_PROXYWAIT) {
  1027. debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
  1028. socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
  1029. switch (s[1]) {
  1030. case 90: /* Success */
  1031. s[0] = 0;
  1032. *len = 0;
  1033. return i;
  1034. case 91: /* Failed */
  1035. errno = ECONNREFUSED;
  1036. break;
  1037. case 92: /* No identd */
  1038. case 93: /* Identd said wrong username */
  1039. /* A better error message would be "socks misconfigured"
  1040. * or "identd not working" but this is simplest.
  1041. */
  1042. errno = ENETUNREACH;
  1043. break;
  1044. }
  1045. *len = socklist[i].sock;
  1046. return -1;
  1047. }
  1048. return i;
  1049. }
  1050. }
  1051. } else if (x == -1)
  1052. return -2; /* socket error */
  1053. else {
  1054. s[0] = 0;
  1055. *len = 0;
  1056. }
  1057. return -3;
  1058. }
  1059. inline static int
  1060. prand(int *seed, int range)
  1061. {
  1062. long long i1 = *seed;
  1063. i1 = (i1 * 0x08088405 + 1) & 0xFFFFFFFF;
  1064. *seed = i1;
  1065. return ((i1 * range) >> 32);
  1066. }
  1067. char *botlink_decrypt(int snum, char *src)
  1068. {
  1069. char *line = decrypt_string(socklist[snum].ikey, src);
  1070. strcpy(src, line);
  1071. free(line);
  1072. if (socklist[snum].iseed) {
  1073. *(dword *) & socklist[snum].ikey[0] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  1074. *(dword *) & socklist[snum].ikey[4] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  1075. *(dword *) & socklist[snum].ikey[8] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  1076. *(dword *) & socklist[snum].ikey[12] = prand(&socklist[snum].iseed, 0xFFFFFFFF);
  1077. if (!socklist[snum].iseed)
  1078. socklist[snum].iseed++;
  1079. }
  1080. return src;
  1081. }
  1082. char *botlink_encrypt(int snum, char *src, size_t *len)
  1083. {
  1084. char *srcbuf = NULL, *buf = NULL, *line = NULL, *eol = NULL, *eline = NULL;
  1085. size_t bufpos = 0;
  1086. srcbuf = (char *) my_calloc(1, *len + 9 + 1);
  1087. strcpy(srcbuf, src);
  1088. line = srcbuf;
  1089. eol = strchr(line, '\n');
  1090. while (eol) {
  1091. *eol++ = 0;
  1092. eline = encrypt_string(socklist[snum].okey, line);
  1093. if (socklist[snum].oseed) {
  1094. *(dword *) & socklist[snum].okey[0] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1095. *(dword *) & socklist[snum].okey[4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1096. *(dword *) & socklist[snum].okey[8] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1097. *(dword *) & socklist[snum].okey[12] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1098. if (!socklist[snum].oseed)
  1099. socklist[snum].oseed++;
  1100. }
  1101. buf = (char *) my_realloc(buf, bufpos + strlen(eline) + 1 + 9);
  1102. strcpy((char *) &buf[bufpos], eline);
  1103. free(eline);
  1104. strcat(buf, "\n");
  1105. bufpos = strlen(buf);
  1106. line = eol;
  1107. eol = strchr(line, '\n');
  1108. }
  1109. if (line[0]) {
  1110. eline = encrypt_string(socklist[snum].okey, line);
  1111. if (socklist[snum].oseed) {
  1112. *(dword *) & socklist[snum].okey[0] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1113. *(dword *) & socklist[snum].okey[4] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1114. *(dword *) & socklist[snum].okey[8] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1115. *(dword *) & socklist[snum].okey[12] = prand(&socklist[snum].oseed, 0xFFFFFFFF);
  1116. if (!socklist[snum].oseed)
  1117. socklist[snum].oseed++;
  1118. }
  1119. buf = (char *) my_realloc(buf, bufpos + strlen(eline) + 1 + 9);
  1120. strcpy((char *) &buf[bufpos], eline);
  1121. free(eline);
  1122. strcat(buf, "\n");
  1123. }
  1124. free(srcbuf);
  1125. *len = strlen(buf);
  1126. return buf;
  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 (socklist[i].encstatus && s[0])
  1179. botlink_decrypt(i, s);
  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 (socklist[ret].encstatus && s[0])
  1298. botlink_decrypt(ret, s);
  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 (socklist[i].encstatus && (len > 0))
  1362. s = botlink_encrypt(i, s, &len); /* will modify 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. 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. }