net.c 47 KB

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