net.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * net.c -- handles:
  22. * all raw network i/o
  23. *
  24. */
  25. #include <fcntl.h>
  26. #include "common.h"
  27. #include "net.h"
  28. #include "socket.h"
  29. #include "misc.h"
  30. #include "main.h"
  31. #include "debug.h"
  32. #include "dccutil.h"
  33. #include "enclink.h"
  34. #include "egg_timer.h"
  35. #include "traffic.h"
  36. #include "adns.h"
  37. #include <bdlib/src/String.h>
  38. #include <bdlib/src/Stream.h>
  39. #include <limits.h>
  40. #include <string.h>
  41. #include <netdb.h>
  42. #include <signal.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #include <setjmp.h>
  46. #if HAVE_SYS_SELECT_H
  47. # include <sys/select.h>
  48. #endif /* HAVE_SYS_SELECT_H */
  49. #include <netinet/in.h>
  50. #include <arpa/inet.h>
  51. #include <errno.h>
  52. #include <sys/stat.h>
  53. #if HAVE_UNISTD_H
  54. # include <unistd.h>
  55. #endif /* HAVE_UNITSTD_H */
  56. extern egg_traffic_t traffic;
  57. union sockaddr_union cached_myip4_so;
  58. #ifdef USE_IPV6
  59. union sockaddr_union cached_myip6_so;
  60. #endif /* USE_IPV6 */
  61. bool cached_ip = 0; /* Set to 1 after cache_my_ip is called */
  62. bool identd_hack = 0; /* identd_open() won't work on most servers, dont even bother warning. */
  63. char botuser[21] = ""; /* Username of the user running the bot */
  64. int socks_total = 0; /* total number of sockets */
  65. sock_list *socklist = NULL; /* Enough to be safe */
  66. int MAXSOCKS = 0;
  67. jmp_buf alarmret; /* Env buffer for alarm() returns */
  68. /* This *MUST* be an ip */
  69. char firewall[121] = ""; /* Socks server for firewall */
  70. port_t firewallport = 1080; /* Default port of Sock4/5 firewalls */
  71. /* Types of proxy */
  72. #define PROXY_SOCKS 1
  73. #define PROXY_SUN 2
  74. #define PROXY_HTTP 3
  75. /* I need an UNSIGNED long for dcc type stuff
  76. */
  77. unsigned long my_atoul(const char *s)
  78. {
  79. unsigned long ret = 0;
  80. while ((*s >= '0') && (*s <= '9')) {
  81. ret *= 10;
  82. ret += ((*s) - '0');
  83. ++s;
  84. }
  85. return ret;
  86. }
  87. /* get the protocol used on a socket */
  88. int sockprotocol(int sock)
  89. {
  90. struct sockaddr sa;
  91. socklen_t socklen = sizeof(sa);
  92. bzero(&sa, socklen);
  93. if (getsockname(sock, &sa, &socklen))
  94. return -1;
  95. else
  96. return sa.sa_family;
  97. }
  98. /* AF_INET-independent resolving routine */
  99. static int get_ip(char *hostname, union sockaddr_union *so)
  100. {
  101. if (!hostname || (hostname && !hostname[0]))
  102. return 1;
  103. memset(so, 0, sizeof(union sockaddr_union));
  104. debug1("get_ip(%s)", hostname);
  105. #ifdef USE_IPV6
  106. struct addrinfo hints, *ai = NULL, *res = NULL;
  107. int error = 0;
  108. memset(&hints, 0, sizeof(struct addrinfo));
  109. hints.ai_socktype = SOCK_STREAM;
  110. if ((error = getaddrinfo(hostname, NULL, &hints, &res))) {
  111. if (res)
  112. freeaddrinfo(res);
  113. return error;
  114. }
  115. error = 1;
  116. for (ai = res; ai != NULL; ai = ai->ai_next) {
  117. if ((ai->ai_family == AF_INET6) || (ai->ai_family == AF_INET)) {
  118. memcpy(so, ai->ai_addr, ai->ai_addrlen);
  119. error = 0;
  120. break;
  121. }
  122. }
  123. if (res)
  124. freeaddrinfo(res);
  125. return error;
  126. #else
  127. struct hostent *hp = NULL;
  128. if (!(hp = gethostbyname(hostname)))
  129. return -1;
  130. memcpy(&so->sin.sin_addr, hp->h_addr, 4);
  131. so->sin.sin_family = AF_INET;
  132. return 0;
  133. #endif /* USE_IPV6 */
  134. }
  135. /* Initialize the socklist
  136. */
  137. void init_net()
  138. {
  139. MAXSOCKS = max_dcc + 10;
  140. if (socklist)
  141. socklist = (sock_list *) my_realloc((void *) socklist, sizeof(sock_list) * MAXSOCKS);
  142. else
  143. socklist = (sock_list *) my_calloc(1, sizeof(sock_list) * MAXSOCKS);
  144. for (int i = 0; i < MAXSOCKS; i++) {
  145. bzero(&socklist[i], sizeof(socklist[i]));
  146. socklist[i].flags = SOCK_UNUSED;
  147. socklist[i].sock = -1;
  148. }
  149. }
  150. /* Get my ipv? ip
  151. */
  152. char *myipstr(int af_type)
  153. {
  154. if (cached_ip) {
  155. #ifdef USE_IPV6
  156. if (af_type == AF_INET6) {
  157. static char s[UHOSTLEN + 1] = "";
  158. inet_ntop(AF_INET6, &cached_myip6_so.sin6.sin6_addr, s, 119);
  159. s[120] = 0;
  160. return s;
  161. } else
  162. #endif /* USE_IPV6 */
  163. if (af_type == AF_INET) {
  164. static char s[UHOSTLEN + 1] = "";
  165. inet_ntop(AF_INET, &cached_myip4_so.sin.sin_addr, s, 119);
  166. s[120] = 0;
  167. return s;
  168. }
  169. }
  170. return "";
  171. }
  172. /* Get my ip number
  173. */
  174. in_addr_t getmyip() {
  175. return cached_myip4_so.sin.sin_addr.s_addr;
  176. }
  177. /* see if it's necessary to set inaddr_any... because if we can't resolve, we die anyway */
  178. void cache_my_ip()
  179. {
  180. #ifdef no
  181. cached_myip6_so.sin6.sin6_family = AF_INET6;
  182. cached_myip6_so.sin6.sin6_addr = in6addr_any;
  183. cached_myip4_so.sin.sin_family = AF_INET;
  184. cached_myip4_so.sin.sin_addr.s_addr = INADDR_ANY;
  185. #endif
  186. int error = 0;
  187. debug0("cache_my_ip()");
  188. memset(&cached_myip4_so, 0, sizeof(union sockaddr_union));
  189. #ifdef USE_IPV6
  190. bool any = 0;
  191. memset(&cached_myip6_so, 0, sizeof(union sockaddr_union));
  192. if (conf.bot->net.ip6) {
  193. if (get_ip(conf.bot->net.ip6, &cached_myip6_so))
  194. any = 1;
  195. } else if (conf.bot->net.host6) {
  196. if (get_ip(conf.bot->net.host6, &cached_myip6_so))
  197. any = 1;
  198. } else
  199. any = 1;
  200. if (any) {
  201. cached_myip6_so.sin6.sin6_family = AF_INET6;
  202. cached_myip6_so.sin6.sin6_addr = in6addr_any;
  203. }
  204. #endif /* USE_IPV6 */
  205. if (conf.bot->net.ip) {
  206. if (get_ip(conf.bot->net.ip, &cached_myip4_so))
  207. error = 1;
  208. } else if (conf.bot->net.host) {
  209. if (get_ip(conf.bot->net.host, &cached_myip4_so))
  210. error = 2;
  211. } else {
  212. /*
  213. char s[121] = "";
  214. gethostname(s, sizeof(s));
  215. if (get_ip(s, &cached_myip4_so)) {
  216. */
  217. /* error = 3; */
  218. cached_myip4_so.sin.sin_family = AF_INET;
  219. cached_myip4_so.sin.sin_addr.s_addr = INADDR_ANY;
  220. // }
  221. }
  222. if (error) {
  223. putlog(LOG_DEBUG, "*", "Hostname self-lookup error: %d", error);
  224. fatal("Hostname self-lookup failed.", 0);
  225. }
  226. cached_ip = 1;
  227. }
  228. /* Sets/Unsets options for a specific socket.
  229. *
  230. * Returns: 0 - on success
  231. * -1 - socket not found
  232. * -2 - illegal operation
  233. */
  234. int sockoptions(int sock, int operation, int sock_options)
  235. {
  236. for (int i = 0; i < MAXSOCKS; i++) {
  237. if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
  238. if (operation == EGG_OPTION_SET)
  239. socklist[i].flags |= sock_options;
  240. else if (operation == EGG_OPTION_UNSET)
  241. socklist[i].flags &= ~sock_options;
  242. else
  243. return -2;
  244. return 0;
  245. }
  246. }
  247. return -1;
  248. }
  249. int
  250. sock_read(bd::Stream& stream)
  251. {
  252. int fd = -1;
  253. bd::String buf, type;
  254. while (stream.tell() < stream.length()) {
  255. buf = stream.getline().chomp();
  256. if (buf == STR("+sock"))
  257. return fd;
  258. type = newsplit(buf);
  259. if (type == STR("sock")) {
  260. int sock = atoi(newsplit(buf).c_str()), options = atoi(newsplit(buf).c_str());
  261. fd = allocsock(sock, options);
  262. }
  263. if (fd >= 0) {
  264. #ifdef USE_IPV6
  265. if (type == STR("af"))
  266. socklist[fd].af = atoi(buf.c_str());
  267. #endif
  268. if (type == STR("host"))
  269. socklist[fd].host = strdup(buf.c_str());
  270. if (type == STR("port"))
  271. socklist[fd].port = atoi(buf.c_str());
  272. }
  273. }
  274. return -1;
  275. }
  276. void
  277. sock_write(bd::Stream &stream, int fd)
  278. {
  279. if (socklist[fd].sock > 0) {
  280. bd::String buf;
  281. stream << buf.printf(STR("-sock\n"));
  282. stream << buf.printf(STR("sock %d %d\n"), socklist[fd].sock, socklist[fd].flags);
  283. #ifdef USE_IPV6
  284. stream << buf.printf(STR("af %u\n"), socklist[fd].af);
  285. #endif
  286. if (socklist[fd].host)
  287. stream << buf.printf(STR("host %s\n"), socklist[fd].host);
  288. if (socklist[fd].port)
  289. stream << buf.printf(STR("port %d\n"), socklist[fd].port);
  290. stream << buf.printf(STR("+sock\n"));
  291. }
  292. }
  293. /* Return a free entry in the socket entry
  294. */
  295. int allocsock(int sock, int options)
  296. {
  297. for (int i = 0; i < MAXSOCKS; i++) {
  298. if (socklist[i].flags & SOCK_UNUSED) {
  299. /* yay! there is table space */
  300. socklist[i].inbuf = NULL;
  301. socklist[i].outbuf = NULL;
  302. socklist[i].flags = options;
  303. socklist[i].sock = sock;
  304. socklist[i].encstatus = 0;
  305. socklist[i].enclink = -1;
  306. socklist[i].gz = 0;
  307. bzero(&(socklist[i].okey), ENC_KEY_LEN + 1);
  308. bzero(&(socklist[i].ikey), ENC_KEY_LEN + 1);
  309. socks_total++;
  310. sdprintf("allocsock(%d) = %d", i, sock);
  311. return i;
  312. }
  313. }
  314. fatal("Socket table is full!", 0);
  315. return -1; /* Never reached */
  316. }
  317. /* Request a normal socket for i/o
  318. */
  319. void setsock(int sock, int options)
  320. {
  321. int i = allocsock(sock, options);
  322. bool parm;
  323. if (((sock != STDOUT) || backgrd) && !(socklist[i].flags & SOCK_NONSOCK)) {
  324. parm = 1;
  325. setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *) &parm, sizeof(int));
  326. parm = 0;
  327. setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &parm, sizeof(int));
  328. }
  329. if (options & SOCK_LISTEN) {
  330. /* Tris says this lets us grab the same port again next time */
  331. parm = 1;
  332. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &parm, sizeof(int));
  333. }
  334. /* Yay async i/o ! */
  335. fcntl(sock, F_SETFL, O_NONBLOCK);
  336. }
  337. #ifdef USE_IPV6
  338. int real_getsock(int options, int af_def, const char *fname, int line)
  339. {
  340. #else
  341. int real_getsock(int options, const char *fname, int line)
  342. {
  343. int af_def = AF_INET;
  344. #endif /* USE_IPV6 */
  345. int sock = -1;
  346. if (!af_def)
  347. af_def = AF_INET;
  348. sock = socket(af_def, SOCK_STREAM, 0);
  349. if (sock >= 0)
  350. setsock(sock, options);
  351. else if (!identd_hack)
  352. putlog(LOG_WARNING, "*", "Warning: Can't create new socket! (%s:%d): %s", fname, line, strerror(errno));
  353. else if (identd_hack)
  354. identd_hack = 0;
  355. return sock;
  356. }
  357. /* Done with a socket
  358. */
  359. void real_killsock(register int sock, const char *file, int line)
  360. {
  361. if (sock < 0) {
  362. putlog(LOG_ERRORS, "*", "Attempt to kill socket -1 %s:%d", file, line);
  363. return;
  364. }
  365. for (register int i = 0; i < MAXSOCKS; i++) {
  366. if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
  367. close(socklist[i].sock);
  368. if (socklist[i].inbuf != NULL) {
  369. delete socklist[i].inbuf;
  370. socklist[i].inbuf = NULL;
  371. }
  372. if (socklist[i].outbuf != NULL) {
  373. delete socklist[i].outbuf;
  374. socklist[i].outbuf = NULL;
  375. }
  376. if (socklist[i].host)
  377. free(socklist[i].host);
  378. bzero(&socklist[i], sizeof(socklist[i]));
  379. socklist[i].flags = SOCK_UNUSED;
  380. socks_total--;
  381. sdprintf("killsock(%d, %s, %d) (socklist: %d)", sock, file, line, i);
  382. return;
  383. }
  384. }
  385. putlog(LOG_MISC, "*", "Attempt to kill un-allocated socket %d %s:%d !!", sock, file, line);
  386. }
  387. /* Send connection request to proxy
  388. */
  389. static int proxy_connect(int sock, const char *ip, port_t port, int proxy_type)
  390. {
  391. sdprintf("proxy_connect(%d, %s, %d, %d)", sock, ip, port, proxy_type);
  392. #ifdef USE_IPV6
  393. unsigned char x[32] = "";
  394. int af_ty = sockprotocol(sock);
  395. #else
  396. unsigned char x[10] = "";
  397. #endif /* USE_IPV6 */
  398. char s[256] = "";
  399. /* socks proxy */
  400. if (proxy_type == PROXY_SOCKS) {
  401. /* numeric IP? */
  402. if (is_dotted_ip(ip)) {
  403. in_addr_t ipaddr = ((in_addr_t) inet_addr(ip));
  404. memcpy(x, &ipaddr, 4);
  405. } else { /* if not resolved, resolve it with blocking calls.. (shouldn't happen ever) */
  406. return -1;
  407. }
  408. for (int i = 0; i < MAXSOCKS; i++)
  409. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
  410. socklist[i].flags |= SOCK_PROXYWAIT;
  411. #ifdef USE_IPV6
  412. if (af_ty == AF_INET6)
  413. simple_snprintf(s, sizeof s,
  414. "\004\001%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%s",
  415. (port >> 8) % 256, (port % 256), x[0], x[1], x[2], x[3],
  416. x[4], x[5], x[6], x[7], x[9], x[9], x[10], x[11], x[12],
  417. x[13], x[14], x[15], botuser);
  418. else
  419. #endif /* USE_IPV6 */
  420. simple_snprintf(s, sizeof s, "\004\001%c%c%c%c%c%c%s", (port >> 8) % 256,
  421. (port % 256), x[0], x[1], x[2], x[3], botuser);
  422. tputs(sock, s, strlen(botuser) + 9);
  423. } else if (proxy_type == PROXY_SUN) {
  424. simple_snprintf(s, sizeof s, "%s %d\n", ip, port);
  425. tputs(sock, s, strlen(s));
  426. } else if (proxy_type == PROXY_HTTP) {
  427. simple_snprintf(s, sizeof s, "CONNECT %s:%d\n\n", ip, port);
  428. tputs(sock, s, strlen(s));
  429. }
  430. return sock;
  431. }
  432. /* FIXME: REPLACE WITH SOCK_NAME() */
  433. void initialize_sockaddr(int af_type, const char *host, port_t port, union sockaddr_union *so)
  434. {
  435. bzero(so, sizeof(*so));
  436. so->sa.sa_family = af_type;
  437. if (af_type == AF_INET) {
  438. so->sin.sin_family = AF_INET;
  439. if (host) {
  440. inet_pton(AF_INET, host, &so->sin.sin_addr);
  441. so->sin.sin_port = htons(port);
  442. } else {
  443. so->sin.sin_addr.s_addr = getmyip();
  444. }
  445. #ifdef USE_IPV6
  446. } else {
  447. so->sin6.sin6_family = AF_INET6;
  448. if (host) {
  449. inet_pton(AF_INET6, host, &so->sin6.sin6_addr);
  450. so->sin6.sin6_port = htons(port);
  451. } else {
  452. memcpy(&so->sin6.sin6_addr, &cached_myip6_so.sin6.sin6_addr, 16);
  453. }
  454. #endif /* USE_IPV6 */
  455. }
  456. }
  457. /* Starts a connection attempt to a socket
  458. *
  459. * If given a normal hostname, this will be resolved to the corresponding
  460. * IP address first. PLEASE try to use the non-blocking dns functions
  461. * instead and then call this function with the IP address to avoid blocking.
  462. *
  463. * returns <0 if connection refused:
  464. * -1 strerror()/errno type error
  465. * -2 can't resolve hostname
  466. */
  467. int open_telnet_raw(int sock, const char *ipIn, port_t sport, bool proxy_on, int identd)
  468. {
  469. static port_t port = 0;
  470. union sockaddr_union so;
  471. char ip[121] = "";
  472. int is_resolved = 0;
  473. volatile int proxy_type = 0, proxy = proxy_on;
  474. /* firewall? use socks */
  475. if (proxy) {
  476. switch (firewall[0]) {
  477. case '!':
  478. proxy_type = PROXY_SUN;
  479. strlcpy(ip, &firewall[1], sizeof(ip));
  480. break;
  481. case '@':
  482. proxy_type = PROXY_HTTP;
  483. strlcpy(ip, &firewall[1], sizeof(ip));
  484. break;
  485. default:
  486. proxy_type = PROXY_SOCKS;
  487. strlcpy(ip, firewall, sizeof(ip));
  488. break;
  489. }
  490. port = firewallport;
  491. } else {
  492. proxy_type = 0;
  493. strlcpy(ip, ipIn, sizeof(ip));
  494. port = sport;
  495. }
  496. socklen_t socklen;
  497. if (sport) {
  498. /* figure out which ip to bind to locally (v4 or v6) based on what the host ip is .. */
  499. if ((is_resolved = is_dotted_ip(ip))) { /* already resolved */
  500. /* bind to our cached ip for v4/v6 depending on what the ip is */
  501. initialize_sockaddr(is_resolved, NULL, 0, &so);
  502. if (bind(sock, &so.sa, SIZEOF_SOCKADDR(so)) < 0) {
  503. putlog(LOG_DEBUG, "*", "Failed to bind to socket %d: %s", sock, strerror(errno));
  504. killsock(sock);
  505. return -1;
  506. }
  507. /* initialize so for connect using the host/port */
  508. initialize_sockaddr(is_resolved, ip, port, &so);
  509. } else { /* if not resolved, resolve it with blocking calls.. (shouldn't happen ever) */
  510. sdprintf("WARNING: open_telnet_raw(%s,%d) was passed an unresolved hostname.", ip, port);
  511. killsock(sock);
  512. return -1;
  513. }
  514. socklen = SIZEOF_SOCKADDR(so);
  515. } else { // Unix domain socket
  516. so.sock_un.sun_family = AF_UNIX;
  517. strcpy(so.sock_un.sun_path, ip);
  518. socklen = SUN_LEN(&so.sock_un);
  519. }
  520. for (int i = 0; i < MAXSOCKS; i++) {
  521. if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock)) {
  522. socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT;
  523. socklist[i].host = strdup(ipIn);
  524. socklist[i].port = port;
  525. break;
  526. }
  527. }
  528. if (identd && sport) //Only open identd if not a unix domain socket
  529. identd_open(myipstr(is_resolved), ipIn, identd);
  530. int rc = -1;
  531. /* make the connect attempt */
  532. rc = connect(sock, (struct sockaddr *)&so.sa, socklen);
  533. if (rc < 0) {
  534. if (errno == EINPROGRESS) {
  535. debug3("net: connect(%d, %s, %d)", sock, ipIn, sport);
  536. /* Firewall? announce connect attempt to proxy */
  537. if (proxy)
  538. return proxy_connect(sock, ipIn, sport, proxy_type);
  539. return sock; /* async success! */
  540. } else {
  541. sdprintf("connect(%d, %s, %d) failed: %s", sock, ip, sport, strerror(errno));
  542. killsock(sock);
  543. return -1;
  544. }
  545. }
  546. /* Synchronous? :/ */
  547. debug3("net: (BLOCKING) connect(%d, %s, %d)", sock, ipIn, sport);
  548. if (proxy)
  549. return proxy_connect(sock, ipIn, sport, proxy_type);
  550. return sock;
  551. }
  552. /* Ordinary non-binary connection attempt */
  553. int open_telnet(const char *ip, port_t port, bool proxy, int identd)
  554. {
  555. int sock = -1;
  556. #ifdef USE_IPV6
  557. sock = getsock(0, is_dotted_ip(ip));
  558. #else
  559. sock = getsock(0);
  560. #endif /* USE_IPV6 */
  561. if (sock >= 0)
  562. return open_telnet_raw(sock, ip, port, proxy, identd);
  563. return -1;
  564. }
  565. /* Returns a socket number for a listening socket that will accept any
  566. * connection on a certain address -- port # is returned in port
  567. *
  568. * 'addr' is ignored if af_def is AF_INET6 -poptix (02/03/03)
  569. */
  570. #ifdef USE_IPV6
  571. int open_address_listen(const char* ip, int af_def, port_t *port)
  572. #else
  573. int open_address_listen(const char* ip, port_t *port)
  574. #endif /* USE_IPV6 */
  575. {
  576. // if (firewall[0]) {
  577. // /* FIXME: can't do listen port thru firewall yet */
  578. // putlog(LOG_MISC, "*", "!! Cant open a listen port (you are using a firewall)");
  579. // return -1;
  580. // }
  581. int sock = 0;
  582. socklen_t addrlen;
  583. union sockaddr_union name;
  584. #ifdef USE_IPV6
  585. if (af_def == AF_INET6) {
  586. struct sockaddr_in6 name6;
  587. sock = getsock(SOCK_LISTEN, af_def);
  588. if (sock < 0)
  589. return -1;
  590. debug2("Opening listen socket on port %d with AF_INET6, sock: %d", *port, sock);
  591. bzero((char *) &name6, sizeof(name6));
  592. name6.sin6_family = af_def;
  593. name6.sin6_port = htons(*port); /* 0 = just assign us a port */
  594. /* memcpy(&name6.sin6_addr, &in6addr_any, 16); */ /* this is the only way to get ipv6+ipv4 in 1 socket */
  595. memcpy(&name6.sin6_addr, &cached_myip6_so.sin6.sin6_addr, 16);
  596. if (bind(sock, (struct sockaddr *) &name6, sizeof(name6)) < 0) {
  597. if (!(identd_hack && *port == 113))
  598. putlog(LOG_DEBUG, "*", "Failed to bind to socket %d for listen on port %d: %s", sock, *port, strerror(errno));
  599. killsock(sock);
  600. return -1;
  601. }
  602. addrlen = sizeof(name6);
  603. if (getsockname(sock, (struct sockaddr *) &name6, &addrlen) < 0) {
  604. if (!(identd_hack && *port == 113))
  605. putlog(LOG_DEBUG, "*", "Failed to getsockname on socket %d for listen on port %d: %s", sock, *port, strerror(errno));
  606. killsock(sock);
  607. return -1;
  608. }
  609. *port = ntohs(name6.sin6_port);
  610. if (listen(sock, 1) < 0) {
  611. if (!(identd_hack && *port == 113))
  612. putlog(LOG_DEBUG, "*", "Failed to listen on socket %d for listen on port %d: %s", sock, *port, strerror(errno));
  613. killsock(sock);
  614. return -1;
  615. }
  616. } else {
  617. sock = getsock(SOCK_LISTEN, af_def);
  618. #else
  619. sock = getsock(SOCK_LISTEN);
  620. #endif /* USE_IPV6 */
  621. if (sock < 0)
  622. return -1;
  623. if (af_def == AF_UNIX)
  624. debug2("Opening listen socket on %s, sock: %d", ip, sock);
  625. else
  626. debug3("Opening listen socket on %s:%d with AF_INET, sock: %d", ip, *port, sock);
  627. bzero((char *) &name, sizeof(struct sockaddr *));
  628. if (af_def == AF_UNIX) {
  629. name.sock_un.sun_family = AF_UNIX;
  630. strcpy(name.sock_un.sun_path, ip);
  631. unlink(name.sock_un.sun_path);
  632. addrlen = SUN_LEN(&name.sock_un);
  633. } else {
  634. name.sin.sin_family = af_def;
  635. name.sin.sin_port = htons(*port); /* 0 = just assign us a port */
  636. name.sin.sin_addr.s_addr = inet_addr(ip);
  637. addrlen = sizeof(struct sockaddr_in);
  638. }
  639. if (bind(sock, (struct sockaddr *) &name, addrlen) < 0) {
  640. if (!(identd_hack && *port == 113)) {
  641. if (af_def == AF_UNIX)
  642. putlog(LOG_DEBUG, "*", "Failed to bind to socket %d for listen on %s: %s", sock, ip, strerror(errno));
  643. else
  644. putlog(LOG_DEBUG, "*", "Failed to bind to socket %d for listen on %s:%d: %s", sock, ip, *port, strerror(errno));
  645. }
  646. killsock(sock);
  647. return -1;
  648. }
  649. if (af_def != AF_UNIX) {
  650. /* what port are we on? */
  651. if (getsockname(sock, (struct sockaddr *) &name, &addrlen) < 0) {
  652. if (!(identd_hack && *port == 113))
  653. putlog(LOG_DEBUG, "*", "Failed to getsockname on socket %d for listen on port %d: %s", sock, *port, strerror(errno));
  654. killsock(sock);
  655. return -1;
  656. }
  657. *port = ntohs(name.sin.sin_port);
  658. }
  659. if (listen(sock, 1) < 0) {
  660. if (!(identd_hack && *port == 113) && af_def != AF_UNIX)
  661. putlog(LOG_DEBUG, "*", "Failed to listen on socket %d for on port %d: %s", sock, *port, strerror(errno));
  662. killsock(sock);
  663. return -1;
  664. }
  665. #ifdef USE_IPV6
  666. }
  667. #endif /* USE_IPV6 */
  668. if (af_def == AF_UNIX)
  669. debug2("Opened listen socket on %s, sock: %d", ip, sock);
  670. else
  671. debug3("Opened listen socket on %s:%d with AF_INET, sock: %d", ip, *port, sock);
  672. return sock;
  673. }
  674. /* Returns a socket number for a listening socket that will accept any
  675. * connection -- port # is returned in port
  676. */
  677. int open_listen(port_t *port)
  678. {
  679. #ifdef USE_IPV6
  680. return open_address_listen(iptostr(getmyip()), AF_INET, port);
  681. #else
  682. return open_address_listen(iptostr(getmyip()), port);
  683. #endif /* USE_IPV6 */
  684. }
  685. /* Same as above, except this one can be called with an AF_ type
  686. * the above is being left in for compatibility, and should NOT LONGER BE USED IN THE CORE CODE.
  687. */
  688. int open_listen_by_af(port_t *port, int af_def)
  689. {
  690. #ifdef USE_IPV6
  691. return open_address_listen(iptostr(getmyip()), af_def, port);
  692. #else
  693. return -1;
  694. #endif /* USE_IPV6 */
  695. }
  696. int open_listen_addr_by_af(const char *ip, port_t *port, int af_def)
  697. {
  698. if (!ip)
  699. ip = iptostr(getmyip());
  700. #ifdef USE_IPV6
  701. return open_address_listen(ip, af_def, port);
  702. #else
  703. return -1;
  704. #endif /* USE_IPV6 */
  705. }
  706. /* Returns the given network byte order IP address in the
  707. * dotted format - "##.##.##.##"
  708. */
  709. char *iptostr(in_addr_t ip)
  710. {
  711. static char ipbuf[32];
  712. struct in_addr a;
  713. a.s_addr = ip;
  714. return (char *) inet_ntop(AF_INET, &a, ipbuf, sizeof(ipbuf));
  715. }
  716. /* Short routine to answer a connect received on a socket made previously
  717. * by open_listen ... returns hostname of the caller & the new socket
  718. * does NOT dispose of old "public" socket!
  719. */
  720. int answer(int sock, char *caller, in_addr_t *ip, port_t *port, int binary)
  721. {
  722. int new_sock;
  723. socklen_t addrlen;
  724. struct sockaddr_in from;
  725. #ifdef USE_IPV6
  726. int af_ty = sockprotocol(sock);
  727. struct sockaddr_in6 from6;
  728. bzero(&from6, sizeof(struct sockaddr_in6));
  729. if (af_ty == AF_INET6) {
  730. addrlen = sizeof(from6);
  731. new_sock = accept(sock, (struct sockaddr *) &from6, &addrlen);
  732. } else {
  733. #endif /* USE_IPV6 */
  734. addrlen = sizeof(struct sockaddr);
  735. new_sock = accept(sock, (struct sockaddr *) &from, &addrlen);
  736. #ifdef USE_IPV6
  737. }
  738. #endif /* USE_IPV6 */
  739. if (new_sock < 0)
  740. return -1;
  741. if (ip != NULL) {
  742. #ifdef USE_IPV6
  743. /* Detect IPv4 in IPv6 mapped address .... */
  744. if (af_ty == AF_INET6 && (!IN6_IS_ADDR_V4MAPPED(&from6.sin6_addr))) {
  745. inet_ntop(AF_INET6, &from6.sin6_addr, caller, 119);
  746. caller[120] = 0;
  747. *ip = 0L;
  748. } else if (IN6_IS_ADDR_V4MAPPED(&from6.sin6_addr)) { /* ...and convert it to plain (AF_INET) IPv4 address (openssh) */
  749. struct sockaddr_in *from4 = (struct sockaddr_in *)&from6;
  750. struct in_addr addr;
  751. memcpy(&addr, ((char *)&from6.sin6_addr) + 12, sizeof(addr));
  752. memset(&from, 0, sizeof(from));
  753. from4->sin_family = AF_INET;
  754. addrlen = sizeof(*from4);
  755. memcpy(&from4->sin_addr, &addr, sizeof(addr));
  756. *ip = from4->sin_addr.s_addr;
  757. strlcpy(caller, iptostr(*ip), 121);
  758. *ip = ntohl(*ip);
  759. } else {
  760. #endif /* USE_IPV6 */
  761. if (af_ty == AF_UNIX) {
  762. struct sockaddr_un sock_un;
  763. socklen_t socklen = sizeof(sock_un);
  764. bzero(&sock_un, socklen);
  765. getsockname(sock, (struct sockaddr*) &sock_un, &socklen);
  766. strcpy(caller, sock_un.sun_path);
  767. *port = 0;
  768. } else {
  769. *ip = from.sin_addr.s_addr;
  770. strlcpy(caller, iptostr(*ip), 121);
  771. *ip = ntohl(*ip);
  772. }
  773. #ifdef USE_IPV6
  774. }
  775. #endif /* USE_IPV6 */
  776. }
  777. if (port != NULL) {
  778. #ifdef USE_IPV6
  779. if (af_ty == AF_INET6)
  780. *port = ntohs(from6.sin6_port);
  781. else if (af_ty == AF_INET)
  782. #endif /* USE_IPV6 */
  783. *port = ntohs(from.sin_port);
  784. }
  785. /* Set up all the normal socket crap */
  786. setsock(new_sock, (binary ? SOCK_BINARY : 0));
  787. sdprintf("Answered socket %d: %s", new_sock, caller);
  788. return new_sock;
  789. }
  790. /* Like open_telnet, but uses ip & port specifications of dcc
  791. Take a longip and make into dotted form
  792. */
  793. int open_telnet_dcc(int sock, char *ip, char *port)
  794. {
  795. port_t p;
  796. unsigned long addr;
  797. char sv[100] = "";
  798. unsigned char c[4] = "";
  799. #ifdef DEBUG_IPV6
  800. debug1("open_telnet_dcc %s", ip);
  801. #endif /* DEBUG_IPV6 */
  802. if (port != NULL)
  803. p = atoi(port);
  804. else
  805. p = 2000;
  806. #ifdef USE_IPV6
  807. if (sockprotocol(sock) == AF_INET6) {
  808. # ifdef DEBUG_IPV6
  809. debug0("open_telnet_dcc, af_inet6!");
  810. # endif /* DEBUG_IPV6 */
  811. strlcpy(sv, ip, sizeof(sv));
  812. } else {
  813. #endif /* USE_IPV6 */
  814. if (ip != NULL)
  815. addr = my_atoul(ip);
  816. else
  817. addr = 0L;
  818. if (addr < (1 << 24))
  819. return -3; /* fake address */
  820. c[0] = (addr >> 24) & 0xff;
  821. c[1] = (addr >> 16) & 0xff;
  822. c[2] = (addr >> 8) & 0xff;
  823. c[3] = addr & 0xff;
  824. simple_snprintf(sv, sizeof(sv), "%u.%u.%u.%u", c[0], c[1], c[2], c[3]);
  825. #ifdef USE_IPV6
  826. }
  827. /* strcpy(sv,hostnamefromip(addr)); */
  828. # ifdef DEBUG_IPV6
  829. debug3("open_telnet_raw %s %d %d", sv, sock, p);
  830. # endif /* DEBUG_IPV6 */
  831. #endif /* USE_IPV6 */
  832. return open_telnet_raw(sock, sv, p, 0);
  833. }
  834. /* Attempts to read from all the sockets in socklist
  835. * fills s with up to 511 bytes if available, and returns the array index
  836. *
  837. * on EOF: returns -1, with socket in len
  838. * on socket error: returns -2
  839. * if nothing is ready: returns -3
  840. */
  841. static int sockread(char *s, int *len)
  842. {
  843. fd_set fd;
  844. int fds = 0, i, fdtmp, x;
  845. struct timeval t;
  846. int grab = SGRAB + 1;
  847. egg_timeval_t howlong;
  848. if (timer_get_shortest(&howlong)) {
  849. /* No timer, default to 1 second. */
  850. t.tv_sec = 1;
  851. t.tv_usec = 0;
  852. }
  853. else {
  854. t.tv_sec = howlong.sec;
  855. t.tv_usec = howlong.usec;
  856. }
  857. FD_ZERO(&fd);
  858. for (i = 0; i < MAXSOCKS; i++) {
  859. if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_VIRTUAL))) {
  860. if ((socklist[i].sock == STDOUT) && !backgrd)
  861. fdtmp = STDIN;
  862. else
  863. fdtmp = socklist[i].sock;
  864. if (fdtmp > fds)
  865. fds = fdtmp;
  866. FD_SET(fdtmp, &fd);
  867. }
  868. }
  869. fds++;
  870. x = select(fds, &fd, NULL, NULL, &t);
  871. if (x > 0) {
  872. /* Something happened */
  873. for (i = 0; i < MAXSOCKS; i++) {
  874. if ((!(socklist[i].flags & SOCK_UNUSED)) && ((FD_ISSET(socklist[i].sock, &fd)) ||
  875. ((socklist[i].sock == STDOUT) && (!backgrd) && (FD_ISSET(STDIN, &fd))))) {
  876. if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
  877. /* Listening socket -- don't read, just return activity */
  878. /* Same for connection attempt */
  879. /* (for strong connections, require a read to succeed first) */
  880. if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
  881. /* Hang around to get the return code from proxy */
  882. grab = 10;
  883. } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
  884. debug1("net: connect! sock %d", socklist[i].sock);
  885. s[0] = 0;
  886. *len = 0;
  887. return i;
  888. }
  889. } else if (socklist[i].flags & SOCK_PASS) {
  890. s[0] = 0;
  891. *len = 0;
  892. return i;
  893. }
  894. errno = 0;
  895. if ((socklist[i].sock == STDOUT) && !backgrd)
  896. x = read(STDIN, s, grab);
  897. else
  898. x = read(socklist[i].sock, s, grab);
  899. if (x <= 0) { /* eof */
  900. if (errno != EAGAIN) { /* EAGAIN happens when the operation would block
  901. on a non-blocking socket, if the socket is going
  902. to die, it will die later, otherwise it will connect */
  903. *len = socklist[i].sock;
  904. socklist[i].flags &= ~SOCK_CONNECT;
  905. debug1("net: eof!(read) socket %d", socklist[i].sock);
  906. return -1;
  907. } else {
  908. debug3("sockread EAGAIN: %d %d (%s)", socklist[i].sock, errno, strerror(errno));
  909. continue; /* EAGAIN */
  910. }
  911. }
  912. s[x] = 0;
  913. *len = x;
  914. if (socklist[i].flags & SOCK_PROXYWAIT) {
  915. debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
  916. socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
  917. switch (s[1]) {
  918. case 90: /* Success */
  919. s[0] = 0;
  920. *len = 0;
  921. return i;
  922. case 91: /* Failed */
  923. errno = ECONNREFUSED;
  924. break;
  925. case 92: /* No identd */
  926. case 93: /* Identd said wrong username */
  927. /* A better error message would be "socks misconfigured"
  928. * or "identd not working" but this is simplest.
  929. */
  930. errno = ENETUNREACH;
  931. break;
  932. }
  933. *len = socklist[i].sock;
  934. return -1;
  935. }
  936. return i;
  937. }
  938. }
  939. } else if (x == -1)
  940. return -2; /* socket error */
  941. else {
  942. s[0] = 0;
  943. *len = 0;
  944. }
  945. return -3;
  946. }
  947. /* sockgets: buffer and read from sockets
  948. *
  949. * Attempts to read from all registered sockets for up to one second. if
  950. * after one second, no complete data has been received from any of the
  951. * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
  952. * if there is returnable data received from a socket, the data will be
  953. * in 's' (null-terminated if non-binary), the length will be returned
  954. * in len, and the socket number will be returned.
  955. * normal sockets have their input buffered, and each call to sockgets
  956. * will return one line terminated with a '\n'. binary sockets are not
  957. * buffered and return whatever coems in as soon as it arrives.
  958. * listening sockets will return an empty string when a connection comes in.
  959. * connecting sockets will return an empty string on a successful connect,
  960. * or EOF on a failed connect.
  961. * if an EOF is detected from any of the sockets, that socket number will be
  962. * put in len, and -1 will be returned.
  963. * the maximum length of the string returned is 512 (including null)
  964. *
  965. * Returns -4 if we handled something that shouldn't be handled by the
  966. * dcc functions. Simply ignore it.
  967. */
  968. int sockgets(char *s, int *len)
  969. {
  970. char xx[SGRAB + 4] = "", *p = NULL;
  971. int ret;
  972. size_t newline_index = size_t(-1);
  973. bool was_crlf = 0;
  974. for (int i = 0; i < MAXSOCKS; i++) {
  975. /* Check for stored-up data waiting to be processed */
  976. if (!(socklist[i].flags & SOCK_UNUSED) && !(socklist[i].flags & SOCK_BUFFER) && (socklist[i].inbuf != NULL)) {
  977. if (!(socklist[i].flags & SOCK_BINARY)) {
  978. /* Find EOL */
  979. newline_index = socklist[i].inbuf->find('\n');
  980. if (newline_index != size_t(-1) && newline_index != 0 && socklist[i].inbuf->charAt(newline_index - 1) == '\r') {
  981. --newline_index;
  982. was_crlf = 1;
  983. }
  984. if (newline_index != size_t(-1)) {
  985. // Split off a line
  986. bd::String line(socklist[i].inbuf->substring(0, newline_index));
  987. strlcpy(s, line.c_str(), SGRAB + 1);
  988. *(socklist[i].inbuf) += int(newline_index) + 1;
  989. if (was_crlf)
  990. *(socklist[i].inbuf) += 1;
  991. if (s[0] && socklist[i].encstatus)
  992. link_read(i, s, (size_t *) len);
  993. *len = strlen(s);
  994. return socklist[i].sock;
  995. }
  996. } else {
  997. /* i dont think any of this is *ever* called */
  998. /* Handling buffered binary data (must have been SOCK_BUFFER before). */
  999. if (socklist[i].inbuf->length() <= SGRAB) {
  1000. *len = socklist[i].inbuf->length();
  1001. memcpy(s, socklist[i].inbuf->data(), socklist[i].inbuf->length());
  1002. delete socklist[i].inbuf;
  1003. socklist[i].inbuf = NULL;
  1004. } else {
  1005. /* Split up into chunks of SGRAB bytes. */
  1006. *len = SGRAB;
  1007. memcpy(s, socklist[i].inbuf->data(), *len);
  1008. *(socklist[i].inbuf) += *len;
  1009. }
  1010. return socklist[i].sock;
  1011. }
  1012. }
  1013. /* Also check any sockets that might have EOF'd during write */
  1014. if (!(socklist[i].flags & SOCK_UNUSED)
  1015. && (socklist[i].flags & SOCK_EOFD)) {
  1016. s[0] = 0;
  1017. *len = socklist[i].sock;
  1018. return -1;
  1019. }
  1020. }
  1021. /* No pent-up data of any worth -- down to business */
  1022. *len = 0;
  1023. ret = sockread(xx, len);
  1024. if (ret < 0) {
  1025. s[0] = 0;
  1026. return ret;
  1027. }
  1028. /* Binary, listening and passed on sockets don't get buffered. */
  1029. if (socklist[ret].flags & SOCK_CONNECT) {
  1030. if (socklist[ret].flags & SOCK_STRONGCONN) {
  1031. socklist[ret].flags &= ~SOCK_STRONGCONN;
  1032. /* Buffer any data that came in, for future read. */
  1033. socklist[ret].inbuf = new bd::String(xx, *len);
  1034. }
  1035. socklist[ret].flags &= ~SOCK_CONNECT;
  1036. s[0] = 0;
  1037. return socklist[ret].sock;
  1038. }
  1039. if (socklist[ret].flags & SOCK_BINARY) {
  1040. memcpy(s, xx, *len);
  1041. return socklist[ret].sock;
  1042. }
  1043. if ((socklist[ret].flags & SOCK_LISTEN) || (socklist[ret].flags & SOCK_PASS))
  1044. return socklist[ret].sock;
  1045. if (socklist[ret].flags & SOCK_BUFFER) {
  1046. *(socklist[ret].inbuf) += bd::String(xx, *len);
  1047. return -4; /* Ignore this one. */
  1048. }
  1049. /* Might be necessary to prepend stored-up data! */
  1050. if (socklist[ret].inbuf != NULL) {
  1051. *(socklist[ret].inbuf) += bd::String(xx);
  1052. if (socklist[ret].inbuf->length() < (SGRAB + 2)) {
  1053. strlcpy(xx, socklist[ret].inbuf->c_str(), sizeof(xx));
  1054. delete socklist[ret].inbuf;
  1055. socklist[ret].inbuf = NULL;
  1056. } else {
  1057. // Take out an SGRAB sized chunk and advance the buffer
  1058. strlcpy(xx, socklist[ret].inbuf->c_str(), SGRAB + 1);
  1059. *(socklist[ret].inbuf) += SGRAB;
  1060. /* (leave the rest to be post-pended later) */
  1061. }
  1062. }
  1063. bool data = 0;
  1064. /* Look for EOL marker; if it's there, i have something to show */
  1065. p = strchr(xx, '\n');
  1066. if (p == NULL)
  1067. p = strchr(xx, '\r');
  1068. if (p != NULL) {
  1069. *p = 0;
  1070. strlcpy(s, xx, SGRAB + 10); //buf@main.c
  1071. memmove(xx, p + 1, strlen(p + 1) + 1);
  1072. /* if (s[0] && strlen(s) && (s[strlen(s) - 1] == '\r')) */
  1073. if (strlen(s) && s[strlen(s) - 1] == '\r')
  1074. s[strlen(s) - 1] = 0;
  1075. data = 1; /* DCC_CHAT may now need to process a blank line */
  1076. /* NO! */
  1077. /* if (!s[0]) strcpy(s," "); */
  1078. } else {
  1079. s[0] = 0;
  1080. if (strlen(xx) >= SGRAB) {
  1081. /* String is too long, so just insert fake \n */
  1082. strlcpy(s, xx, SGRAB + 10); //buf@main.c
  1083. xx[0] = 0;
  1084. data = 1;
  1085. }
  1086. }
  1087. if (s[0] && socklist[ret].encstatus)
  1088. link_read(ret, s, (size_t *) len);
  1089. *len = strlen(s);
  1090. /* Anything left that needs to be saved? */
  1091. if (!xx[0]) {
  1092. if (data)
  1093. return socklist[ret].sock;
  1094. else
  1095. return -3;
  1096. }
  1097. /* Prepend old data back */
  1098. if (socklist[ret].inbuf != NULL) {
  1099. socklist[ret].inbuf->insert(0, xx);
  1100. } else {
  1101. socklist[ret].inbuf = new bd::String(xx);
  1102. }
  1103. if (data) {
  1104. return socklist[ret].sock;
  1105. } else {
  1106. return -3;
  1107. }
  1108. }
  1109. /* Dump something to a socket
  1110. *
  1111. * NOTE: Do NOT put Contexts in here if you want DEBUG to be meaningful!!
  1112. */
  1113. void tputs(register int z, const char *s, size_t len)
  1114. {
  1115. if (z < 0) /* um... HELLO?! sanity check please! */
  1116. return;
  1117. if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
  1118. if (write(z, s, len) == -1) {
  1119. ;
  1120. }
  1121. return;
  1122. }
  1123. register int x, idx;
  1124. for (register int i = 0; i < MAXSOCKS; i++) {
  1125. if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
  1126. for (idx = 0; idx < dcc_total; idx++) {
  1127. if (dcc[idx].type && (dcc[idx].sock == z) && dcc[idx].type->name) {
  1128. if (!strncmp(dcc[idx].type->name, "BOT", 3))
  1129. traffic.out_today.bn += len;
  1130. else if (!strcmp(dcc[idx].type->name, "SERVER"))
  1131. traffic.out_today.irc += len;
  1132. else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
  1133. traffic.out_today.dcc += len;
  1134. else if (!strncmp(dcc[idx].type->name, "FILES", 5))
  1135. traffic.out_today.filesys += len;
  1136. else if (!strcmp(dcc[idx].type->name, "SEND"))
  1137. traffic.out_today.trans += len;
  1138. else if (!strncmp(dcc[idx].type->name, "GET", 3))
  1139. traffic.out_today.trans += len;
  1140. else
  1141. traffic.out_today.unknown += len;
  1142. break;
  1143. }
  1144. }
  1145. if (len && socklist[i].encstatus)
  1146. s = link_write(i, s, &len);
  1147. if (socklist[i].outbuf != NULL) {
  1148. /* Already queueing: just add it */
  1149. *(socklist[i].outbuf) += bd::String(s, len);
  1150. return;
  1151. }
  1152. /* Try. */
  1153. #ifdef HAVE_ZLIB_H
  1154. /*
  1155. if (socklist[i].gz) {
  1156. FILE *fp;
  1157. fp = gzdopen(z, "wb0");
  1158. x = gzwrite(fp, s, len);
  1159. } else
  1160. */
  1161. #endif /* HAVE_ZLIB_H */
  1162. x = write(z, s, len);
  1163. if (x == -1)
  1164. x = 0;
  1165. if ((size_t) x < len) {
  1166. /* Socket is full, queue it */
  1167. socklist[i].outbuf = new bd::String(&s[x], len - x);
  1168. }
  1169. // if (socklist[i].encstatus && s)
  1170. // free(s);
  1171. return;
  1172. }
  1173. }
  1174. /* Make sure we don't cause a crash by looping here */
  1175. static int inhere = 0;
  1176. if (!inhere) {
  1177. inhere = 1;
  1178. putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
  1179. if (strlen(s)) {
  1180. char *tmp = strdup(s); /* To null-terminate */
  1181. putlog(LOG_MISC, "*", "!-> '%s'", tmp);
  1182. free(tmp);
  1183. }
  1184. inhere = 0;
  1185. }
  1186. /* if (socklist[i].encstatus > 0)
  1187. free(s);
  1188. */
  1189. }
  1190. int findanysnum(register int sock)
  1191. {
  1192. register int i = 0;
  1193. if (sock != -1)
  1194. for (i = 0; i < MAXSOCKS; i++)
  1195. if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED))
  1196. return i;
  1197. return -1;
  1198. }
  1199. static int findanyidx(register int sock)
  1200. {
  1201. register int j;
  1202. if (sock != -1)
  1203. for (j = 0; j < dcc_total; j++)
  1204. if (dcc[j].type && dcc[j].sock == sock)
  1205. return j;
  1206. return -1;
  1207. }
  1208. /* tputs might queue data for sockets, let's dump as much of it as
  1209. * possible.
  1210. */
  1211. void dequeue_sockets()
  1212. {
  1213. int i, x, z = 0, fds = 0;
  1214. fd_set wfds;
  1215. struct timeval tv;
  1216. /* ^-- start poptix test code, this should avoid writes to sockets not ready to be written to. */
  1217. FD_ZERO(&wfds);
  1218. tv.tv_sec = 0;
  1219. tv.tv_usec = 0; /* we only want to see if it's ready for writing, no need to actually wait.. */
  1220. for (i = 0; i < MAXSOCKS; i++) {
  1221. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].outbuf != NULL) {
  1222. FD_SET(socklist[i].sock, &wfds);
  1223. if (socklist[i].sock > fds)
  1224. fds = socklist[i].sock;
  1225. z = 1;
  1226. }
  1227. }
  1228. if (!z)
  1229. return; /* nothing to write */
  1230. fds++;
  1231. select(fds, NULL, &wfds, NULL, &tv);
  1232. /* end poptix */
  1233. for (i = 0; i < MAXSOCKS; i++) {
  1234. if (!(socklist[i].flags & SOCK_UNUSED) &&
  1235. (socklist[i].outbuf != NULL) && (FD_ISSET(socklist[i].sock, &wfds))) {
  1236. /* Trick tputs into doing the work */
  1237. errno = 0;
  1238. x = write(socklist[i].sock, socklist[i].outbuf->data(), socklist[i].outbuf->length());
  1239. if ((x < 0) && (errno != EAGAIN)
  1240. #ifdef EBADSLT
  1241. && (errno != EBADSLT)
  1242. #endif /* EBADSLT */
  1243. #ifdef ENOTCONN
  1244. && (errno != ENOTCONN)
  1245. #endif /* EBADSLT */
  1246. ) {
  1247. /* This detects an EOF during writing */
  1248. debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock, strerror(errno), errno);
  1249. socklist[i].flags |= SOCK_EOFD;
  1250. } else if ((size_t) x == socklist[i].outbuf->length()) {
  1251. /* If the whole buffer was sent, nuke it */
  1252. delete socklist[i].outbuf;
  1253. socklist[i].outbuf = NULL;
  1254. } else if (x > 0) {
  1255. *(socklist[i].outbuf) += x;
  1256. } else {
  1257. debug3("dequeue_sockets(): errno = %d (%s) on %d", errno, strerror(errno), socklist[i].sock);
  1258. }
  1259. /* All queued data was sent. Call handler if one exists and the
  1260. * dcc entry wants it.
  1261. */
  1262. if (!socklist[i].outbuf) {
  1263. int idx = findanyidx(socklist[i].sock);
  1264. if (idx >= 0 && dcc[idx].type && dcc[idx].type->outdone)
  1265. dcc[idx].type->outdone(idx);
  1266. }
  1267. }
  1268. }
  1269. }
  1270. /*
  1271. * Debugging stuff
  1272. */
  1273. void tell_netdebug(int idx)
  1274. {
  1275. char s[80] = "";
  1276. dprintf(idx, "Open sockets:");
  1277. for (int i = 0; i < MAXSOCKS; i++) {
  1278. if (!(socklist[i].flags & SOCK_UNUSED)) {
  1279. simple_snprintf(s, sizeof(s), " %d", socklist[i].sock);
  1280. if (socklist[i].flags & SOCK_BINARY)
  1281. strlcat(s, " (binary)", sizeof(s));
  1282. if (socklist[i].flags & SOCK_LISTEN)
  1283. strlcat(s, " (listen)", sizeof(s));
  1284. if (socklist[i].flags & SOCK_PASS)
  1285. strlcat(s, " (passed on)", sizeof(s));
  1286. if (socklist[i].flags & SOCK_CONNECT)
  1287. strlcat(s, " (connecting)", sizeof(s));
  1288. if (socklist[i].flags & SOCK_STRONGCONN)
  1289. strlcat(s, " (strong)", sizeof(s));
  1290. if (socklist[i].flags & SOCK_NONSOCK)
  1291. strlcat(s, " (file)", sizeof(s));
  1292. if (socklist[i].inbuf != NULL)
  1293. simple_sprintf(&s[strlen(s)], " (inbuf: %zu)", socklist[i].inbuf ? socklist[i].inbuf->length() : 0);
  1294. if (socklist[i].outbuf != NULL)
  1295. simple_sprintf(&s[strlen(s)], " (outbuf: %zu)", socklist[i].outbuf ? socklist[i].outbuf->length() : 0);
  1296. if (socklist[i].host)
  1297. simple_sprintf(&s[strlen(s)], " (%s:%d)", socklist[i].host, socklist[i].port);
  1298. strlcat(s, ",", sizeof(s));
  1299. dprintf(idx, "%s", s);
  1300. }
  1301. }
  1302. dprintf(idx, " done.\n");
  1303. }
  1304. /* Checks wether the referenced socket has data queued.
  1305. *
  1306. * Returns true if the incoming/outgoing (depending on 'type') queues
  1307. * contain data, otherwise false.
  1308. */
  1309. bool sock_has_data(int type, int sock)
  1310. {
  1311. bool ret = 0;
  1312. int i;
  1313. for (i = 0; i < MAXSOCKS; i++)
  1314. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
  1315. break;
  1316. if (i < MAXSOCKS) {
  1317. switch (type) {
  1318. case SOCK_DATA_OUTGOING:
  1319. ret = (socklist[i].outbuf != NULL);
  1320. break;
  1321. case SOCK_DATA_INCOMING:
  1322. ret = (socklist[i].inbuf != NULL);
  1323. break;
  1324. }
  1325. } else
  1326. debug1("sock_has_data: could not find socket #%d, returning false.", sock);
  1327. return ret;
  1328. }