net.c 48 KB

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