1
0

net.c 48 KB

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