net.c 48 KB

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