net.c 47 KB

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