net.c 48 KB

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