1
0

net.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  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 = socklist[i].outbuf = NULL;
  301. socklist[i].inbuflen = socklist[i].outbuflen = 0;
  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. free(socklist[i].inbuf);
  370. socklist[i].inbuf = NULL;
  371. }
  372. if (socklist[i].outbuf != NULL) {
  373. free(socklist[i].outbuf);
  374. socklist[i].outbuf = NULL;
  375. socklist[i].outbuflen = 0;
  376. }
  377. if (socklist[i].host)
  378. free(socklist[i].host);
  379. bzero(&socklist[i], sizeof(socklist[i]));
  380. socklist[i].flags = SOCK_UNUSED;
  381. socks_total--;
  382. sdprintf("killsock(%d, %s, %d) (socklist: %d)", sock, file, line, i);
  383. return;
  384. }
  385. }
  386. putlog(LOG_MISC, "*", "Attempt to kill un-allocated socket %d %s:%d !!", sock, file, line);
  387. }
  388. /* Send connection request to proxy
  389. */
  390. static int proxy_connect(int sock, const char *ip, port_t port, int proxy_type)
  391. {
  392. sdprintf("proxy_connect(%d, %s, %d, %d)", sock, ip, port, proxy_type);
  393. #ifdef USE_IPV6
  394. unsigned char x[32] = "";
  395. int af_ty = sockprotocol(sock);
  396. #else
  397. unsigned char x[10] = "";
  398. #endif /* USE_IPV6 */
  399. char s[256] = "";
  400. /* socks proxy */
  401. if (proxy_type == PROXY_SOCKS) {
  402. /* numeric IP? */
  403. if (is_dotted_ip(ip)) {
  404. in_addr_t ipaddr = ((in_addr_t) inet_addr(ip));
  405. memcpy(x, &ipaddr, 4);
  406. } else { /* if not resolved, resolve it with blocking calls.. (shouldn't happen ever) */
  407. return -1;
  408. }
  409. for (int i = 0; i < MAXSOCKS; i++)
  410. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
  411. socklist[i].flags |= SOCK_PROXYWAIT;
  412. #ifdef USE_IPV6
  413. if (af_ty == AF_INET6)
  414. simple_snprintf(s, sizeof s,
  415. "\004\001%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%s",
  416. (port >> 8) % 256, (port % 256), x[0], x[1], x[2], x[3],
  417. x[4], x[5], x[6], x[7], x[9], x[9], x[10], x[11], x[12],
  418. x[13], x[14], x[15], botuser);
  419. else
  420. #endif /* USE_IPV6 */
  421. simple_snprintf(s, sizeof s, "\004\001%c%c%c%c%c%c%s", (port >> 8) % 256,
  422. (port % 256), x[0], x[1], x[2], x[3], botuser);
  423. tputs(sock, s, strlen(botuser) + 9);
  424. } else if (proxy_type == PROXY_SUN) {
  425. simple_snprintf(s, sizeof s, "%s %d\n", ip, port);
  426. tputs(sock, s, strlen(s));
  427. } else if (proxy_type == PROXY_HTTP) {
  428. simple_snprintf(s, sizeof s, "CONNECT %s:%d\n\n", ip, port);
  429. tputs(sock, s, strlen(s));
  430. }
  431. return sock;
  432. }
  433. /* FIXME: REPLACE WITH SOCK_NAME() */
  434. void initialize_sockaddr(int af_type, const char *host, port_t port, union sockaddr_union *so)
  435. {
  436. bzero(so, sizeof(*so));
  437. so->sa.sa_family = af_type;
  438. if (af_type == AF_INET) {
  439. so->sin.sin_family = AF_INET;
  440. if (host) {
  441. inet_pton(AF_INET, host, &so->sin.sin_addr);
  442. so->sin.sin_port = htons(port);
  443. } else {
  444. so->sin.sin_addr.s_addr = getmyip();
  445. }
  446. #ifdef USE_IPV6
  447. } else {
  448. so->sin6.sin6_family = AF_INET6;
  449. if (host) {
  450. inet_pton(AF_INET6, host, &so->sin6.sin6_addr);
  451. so->sin6.sin6_port = htons(port);
  452. } else {
  453. memcpy(&so->sin6.sin6_addr, &cached_myip6_so.sin6.sin6_addr, 16);
  454. }
  455. #endif /* USE_IPV6 */
  456. }
  457. }
  458. /* Starts a connection attempt to a socket
  459. *
  460. * If given a normal hostname, this will be resolved to the corresponding
  461. * IP address first. PLEASE try to use the non-blocking dns functions
  462. * instead and then call this function with the IP address to avoid blocking.
  463. *
  464. * returns <0 if connection refused:
  465. * -1 strerror()/errno type error
  466. * -2 can't resolve hostname
  467. */
  468. int open_telnet_raw(int sock, const char *ipIn, port_t sport, bool proxy_on, int identd)
  469. {
  470. static port_t port = 0;
  471. union sockaddr_union so;
  472. char ip[121] = "";
  473. int is_resolved = 0;
  474. volatile int proxy_type = 0, proxy = proxy_on;
  475. /* firewall? use socks */
  476. if (proxy) {
  477. switch (firewall[0]) {
  478. case '!':
  479. proxy_type = PROXY_SUN;
  480. strlcpy(ip, &firewall[1], sizeof(ip));
  481. break;
  482. case '@':
  483. proxy_type = PROXY_HTTP;
  484. strlcpy(ip, &firewall[1], sizeof(ip));
  485. break;
  486. default:
  487. proxy_type = PROXY_SOCKS;
  488. strlcpy(ip, firewall, sizeof(ip));
  489. break;
  490. }
  491. port = firewallport;
  492. } else {
  493. proxy_type = 0;
  494. strlcpy(ip, ipIn, sizeof(ip));
  495. port = sport;
  496. }
  497. socklen_t socklen;
  498. if (sport) {
  499. /* figure out which ip to bind to locally (v4 or v6) based on what the host ip is .. */
  500. if ((is_resolved = is_dotted_ip(ip))) { /* already resolved */
  501. /* bind to our cached ip for v4/v6 depending on what the ip is */
  502. initialize_sockaddr(is_resolved, NULL, 0, &so);
  503. if (bind(sock, &so.sa, SIZEOF_SOCKADDR(so)) < 0) {
  504. putlog(LOG_DEBUG, "*", "Failed to bind to socket %d: %s", sock, strerror(errno));
  505. killsock(sock);
  506. return -1;
  507. }
  508. /* initialize so for connect using the host/port */
  509. initialize_sockaddr(is_resolved, ip, port, &so);
  510. } else { /* if not resolved, resolve it with blocking calls.. (shouldn't happen ever) */
  511. sdprintf("WARNING: open_telnet_raw(%s,%d) was passed an unresolved hostname.", ip, port);
  512. killsock(sock);
  513. return -1;
  514. }
  515. socklen = SIZEOF_SOCKADDR(so);
  516. } else { // Unix domain socket
  517. so.sock_un.sun_family = AF_UNIX;
  518. strcpy(so.sock_un.sun_path, ip);
  519. socklen = SUN_LEN(&so.sock_un);
  520. }
  521. for (int i = 0; i < MAXSOCKS; i++) {
  522. if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock)) {
  523. socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT;
  524. socklist[i].host = strdup(ipIn);
  525. socklist[i].port = port;
  526. break;
  527. }
  528. }
  529. if (identd && sport) //Only open identd if not a unix domain socket
  530. identd_open(myipstr(is_resolved), ipIn, identd);
  531. int rc = -1;
  532. /* make the connect attempt */
  533. rc = connect(sock, (struct sockaddr *)&so.sa, socklen);
  534. if (rc < 0) {
  535. if (errno == EINPROGRESS) {
  536. debug3("net: connect(%d, %s, %d)", sock, ipIn, sport);
  537. /* Firewall? announce connect attempt to proxy */
  538. if (proxy)
  539. return proxy_connect(sock, ipIn, sport, proxy_type);
  540. return sock; /* async success! */
  541. } else {
  542. sdprintf("connect(%d, %s, %d) failed: %s", sock, ip, sport, strerror(errno));
  543. killsock(sock);
  544. return -1;
  545. }
  546. }
  547. /* Synchronous? :/ */
  548. debug3("net: (BLOCKING) connect(%d, %s, %d)", sock, ipIn, sport);
  549. if (proxy)
  550. return proxy_connect(sock, ipIn, sport, proxy_type);
  551. return sock;
  552. }
  553. /* Ordinary non-binary connection attempt */
  554. int open_telnet(const char *ip, port_t port, bool proxy, int identd)
  555. {
  556. int sock = -1;
  557. #ifdef USE_IPV6
  558. sock = getsock(0, is_dotted_ip(ip));
  559. #else
  560. sock = getsock(0);
  561. #endif /* USE_IPV6 */
  562. if (sock >= 0)
  563. return open_telnet_raw(sock, ip, port, proxy, identd);
  564. return -1;
  565. }
  566. /* Returns a socket number for a listening socket that will accept any
  567. * connection on a certain address -- port # is returned in port
  568. *
  569. * 'addr' is ignored if af_def is AF_INET6 -poptix (02/03/03)
  570. */
  571. #ifdef USE_IPV6
  572. int open_address_listen(const char* ip, int af_def, port_t *port)
  573. #else
  574. int open_address_listen(const char* ip, port_t *port)
  575. #endif /* USE_IPV6 */
  576. {
  577. // if (firewall[0]) {
  578. // /* FIXME: can't do listen port thru firewall yet */
  579. // putlog(LOG_MISC, "*", "!! Cant open a listen port (you are using a firewall)");
  580. // return -1;
  581. // }
  582. int sock = 0;
  583. socklen_t addrlen;
  584. union sockaddr_union name;
  585. #ifdef USE_IPV6
  586. if (af_def == AF_INET6) {
  587. struct sockaddr_in6 name6;
  588. sock = getsock(SOCK_LISTEN, af_def);
  589. if (sock < 0)
  590. return -1;
  591. debug2("Opening listen socket on port %d with AF_INET6, sock: %d", *port, sock);
  592. bzero((char *) &name6, sizeof(name6));
  593. name6.sin6_family = af_def;
  594. name6.sin6_port = htons(*port); /* 0 = just assign us a port */
  595. /* memcpy(&name6.sin6_addr, &in6addr_any, 16); */ /* this is the only way to get ipv6+ipv4 in 1 socket */
  596. memcpy(&name6.sin6_addr, &cached_myip6_so.sin6.sin6_addr, 16);
  597. if (bind(sock, (struct sockaddr *) &name6, sizeof(name6)) < 0) {
  598. if (!(identd_hack && *port == 113))
  599. putlog(LOG_DEBUG, "*", "Failed to bind to socket %d for listen on port %d: %s", sock, *port, strerror(errno));
  600. killsock(sock);
  601. return -1;
  602. }
  603. addrlen = sizeof(name6);
  604. if (getsockname(sock, (struct sockaddr *) &name6, &addrlen) < 0) {
  605. if (!(identd_hack && *port == 113))
  606. putlog(LOG_DEBUG, "*", "Failed to getsockname on socket %d for listen on port %d: %s", sock, *port, strerror(errno));
  607. killsock(sock);
  608. return -1;
  609. }
  610. *port = ntohs(name6.sin6_port);
  611. if (listen(sock, 1) < 0) {
  612. if (!(identd_hack && *port == 113))
  613. putlog(LOG_DEBUG, "*", "Failed to listen on socket %d for listen on port %d: %s", sock, *port, strerror(errno));
  614. killsock(sock);
  615. return -1;
  616. }
  617. } else {
  618. sock = getsock(SOCK_LISTEN, af_def);
  619. #else
  620. sock = getsock(SOCK_LISTEN);
  621. #endif /* USE_IPV6 */
  622. if (sock < 0)
  623. return -1;
  624. if (af_def == AF_UNIX)
  625. debug2("Opening listen socket on %s, sock: %d", ip, sock);
  626. else
  627. debug3("Opening listen socket on %s:%d with AF_INET, sock: %d", ip, *port, sock);
  628. bzero((char *) &name, sizeof(struct sockaddr *));
  629. if (af_def == AF_UNIX) {
  630. name.sock_un.sun_family = AF_UNIX;
  631. strcpy(name.sock_un.sun_path, ip);
  632. unlink(name.sock_un.sun_path);
  633. addrlen = SUN_LEN(&name.sock_un);
  634. } else {
  635. name.sin.sin_family = af_def;
  636. name.sin.sin_port = htons(*port); /* 0 = just assign us a port */
  637. name.sin.sin_addr.s_addr = inet_addr(ip);
  638. addrlen = sizeof(struct sockaddr_in);
  639. }
  640. if (bind(sock, (struct sockaddr *) &name, addrlen) < 0) {
  641. if (!(identd_hack && *port == 113)) {
  642. if (af_def == AF_UNIX)
  643. putlog(LOG_DEBUG, "*", "Failed to bind to socket %d for listen on %s: %s", sock, ip, strerror(errno));
  644. else
  645. putlog(LOG_DEBUG, "*", "Failed to bind to socket %d for listen on %s:%d: %s", sock, ip, *port, strerror(errno));
  646. }
  647. killsock(sock);
  648. return -1;
  649. }
  650. if (af_def != AF_UNIX) {
  651. /* what port are we on? */
  652. if (getsockname(sock, (struct sockaddr *) &name, &addrlen) < 0) {
  653. if (!(identd_hack && *port == 113))
  654. putlog(LOG_DEBUG, "*", "Failed to getsockname on socket %d for listen on port %d: %s", sock, *port, strerror(errno));
  655. killsock(sock);
  656. return -1;
  657. }
  658. *port = ntohs(name.sin.sin_port);
  659. }
  660. if (listen(sock, 1) < 0) {
  661. if (!(identd_hack && *port == 113) && af_def != AF_UNIX)
  662. putlog(LOG_DEBUG, "*", "Failed to listen on socket %d for on port %d: %s", sock, *port, strerror(errno));
  663. killsock(sock);
  664. return -1;
  665. }
  666. #ifdef USE_IPV6
  667. }
  668. #endif /* USE_IPV6 */
  669. if (af_def == AF_UNIX)
  670. debug2("Opened listen socket on %s, sock: %d", ip, sock);
  671. else
  672. debug3("Opened listen socket on %s:%d with AF_INET, sock: %d", ip, *port, sock);
  673. return sock;
  674. }
  675. /* Returns a socket number for a listening socket that will accept any
  676. * connection -- port # is returned in port
  677. */
  678. int open_listen(port_t *port)
  679. {
  680. #ifdef USE_IPV6
  681. return open_address_listen(iptostr(getmyip()), AF_INET, port);
  682. #else
  683. return open_address_listen(iptostr(getmyip()), port);
  684. #endif /* USE_IPV6 */
  685. }
  686. /* Same as above, except this one can be called with an AF_ type
  687. * the above is being left in for compatibility, and should NOT LONGER BE USED IN THE CORE CODE.
  688. */
  689. int open_listen_by_af(port_t *port, int af_def)
  690. {
  691. #ifdef USE_IPV6
  692. return open_address_listen(iptostr(getmyip()), af_def, port);
  693. #else
  694. return -1;
  695. #endif /* USE_IPV6 */
  696. }
  697. int open_listen_addr_by_af(const char *ip, port_t *port, int af_def)
  698. {
  699. if (!ip)
  700. ip = iptostr(getmyip());
  701. #ifdef USE_IPV6
  702. return open_address_listen(ip, af_def, port);
  703. #else
  704. return -1;
  705. #endif /* USE_IPV6 */
  706. }
  707. /* Returns the given network byte order IP address in the
  708. * dotted format - "##.##.##.##"
  709. */
  710. char *iptostr(in_addr_t ip)
  711. {
  712. static char ipbuf[32];
  713. struct in_addr a;
  714. a.s_addr = ip;
  715. return (char *) inet_ntop(AF_INET, &a, ipbuf, sizeof(ipbuf));
  716. }
  717. /* Short routine to answer a connect received on a socket made previously
  718. * by open_listen ... returns hostname of the caller & the new socket
  719. * does NOT dispose of old "public" socket!
  720. */
  721. int answer(int sock, char *caller, in_addr_t *ip, port_t *port, int binary)
  722. {
  723. int new_sock;
  724. socklen_t addrlen;
  725. struct sockaddr_in from;
  726. #ifdef USE_IPV6
  727. int af_ty = sockprotocol(sock);
  728. struct sockaddr_in6 from6;
  729. bzero(&from6, sizeof(struct sockaddr_in6));
  730. if (af_ty == AF_INET6) {
  731. addrlen = sizeof(from6);
  732. new_sock = accept(sock, (struct sockaddr *) &from6, &addrlen);
  733. } else {
  734. #endif /* USE_IPV6 */
  735. addrlen = sizeof(struct sockaddr);
  736. new_sock = accept(sock, (struct sockaddr *) &from, &addrlen);
  737. #ifdef USE_IPV6
  738. }
  739. #endif /* USE_IPV6 */
  740. if (new_sock < 0)
  741. return -1;
  742. if (ip != NULL) {
  743. #ifdef USE_IPV6
  744. /* Detect IPv4 in IPv6 mapped address .... */
  745. if (af_ty == AF_INET6 && (!IN6_IS_ADDR_V4MAPPED(&from6.sin6_addr))) {
  746. inet_ntop(AF_INET6, &from6.sin6_addr, caller, 119);
  747. caller[120] = 0;
  748. *ip = 0L;
  749. } else if (IN6_IS_ADDR_V4MAPPED(&from6.sin6_addr)) { /* ...and convert it to plain (AF_INET) IPv4 address (openssh) */
  750. struct sockaddr_in *from4 = (struct sockaddr_in *)&from6;
  751. struct in_addr addr;
  752. memcpy(&addr, ((char *)&from6.sin6_addr) + 12, sizeof(addr));
  753. memset(&from, 0, sizeof(from));
  754. from4->sin_family = AF_INET;
  755. addrlen = sizeof(*from4);
  756. memcpy(&from4->sin_addr, &addr, sizeof(addr));
  757. *ip = from4->sin_addr.s_addr;
  758. strlcpy(caller, iptostr(*ip), 121);
  759. *ip = ntohl(*ip);
  760. } else {
  761. #endif /* USE_IPV6 */
  762. if (af_ty == AF_UNIX) {
  763. struct sockaddr_un sock_un;
  764. socklen_t socklen = sizeof(sock_un);
  765. bzero(&sock_un, socklen);
  766. getsockname(sock, (struct sockaddr*) &sock_un, &socklen);
  767. strcpy(caller, sock_un.sun_path);
  768. *port = 0;
  769. } else {
  770. *ip = from.sin_addr.s_addr;
  771. strlcpy(caller, iptostr(*ip), 121);
  772. *ip = ntohl(*ip);
  773. }
  774. #ifdef USE_IPV6
  775. }
  776. #endif /* USE_IPV6 */
  777. }
  778. if (port != NULL) {
  779. #ifdef USE_IPV6
  780. if (af_ty == AF_INET6)
  781. *port = ntohs(from6.sin6_port);
  782. else if (af_ty == AF_INET)
  783. #endif /* USE_IPV6 */
  784. *port = ntohs(from.sin_port);
  785. }
  786. /* Set up all the normal socket crap */
  787. setsock(new_sock, (binary ? SOCK_BINARY : 0));
  788. sdprintf("Answered socket %d: %s", new_sock, caller);
  789. return new_sock;
  790. }
  791. /* Like open_telnet, but uses ip & port specifications of dcc
  792. Take a longip and make into dotted form
  793. */
  794. int open_telnet_dcc(int sock, char *ip, char *port)
  795. {
  796. port_t p;
  797. unsigned long addr;
  798. char sv[100] = "";
  799. unsigned char c[4] = "";
  800. #ifdef DEBUG_IPV6
  801. debug1("open_telnet_dcc %s", ip);
  802. #endif /* DEBUG_IPV6 */
  803. if (port != NULL)
  804. p = atoi(port);
  805. else
  806. p = 2000;
  807. #ifdef USE_IPV6
  808. if (sockprotocol(sock) == AF_INET6) {
  809. # ifdef DEBUG_IPV6
  810. debug0("open_telnet_dcc, af_inet6!");
  811. # endif /* DEBUG_IPV6 */
  812. strlcpy(sv, ip, sizeof(sv));
  813. } else {
  814. #endif /* USE_IPV6 */
  815. if (ip != NULL)
  816. addr = my_atoul(ip);
  817. else
  818. addr = 0L;
  819. if (addr < (1 << 24))
  820. return -3; /* fake address */
  821. c[0] = (addr >> 24) & 0xff;
  822. c[1] = (addr >> 16) & 0xff;
  823. c[2] = (addr >> 8) & 0xff;
  824. c[3] = addr & 0xff;
  825. simple_snprintf(sv, sizeof(sv), "%u.%u.%u.%u", c[0], c[1], c[2], c[3]);
  826. #ifdef USE_IPV6
  827. }
  828. /* strcpy(sv,hostnamefromip(addr)); */
  829. # ifdef DEBUG_IPV6
  830. debug3("open_telnet_raw %s %d %d", sv, sock, p);
  831. # endif /* DEBUG_IPV6 */
  832. #endif /* USE_IPV6 */
  833. return open_telnet_raw(sock, sv, p, 0);
  834. }
  835. /* Attempts to read from all the sockets in socklist
  836. * fills s with up to 511 bytes if available, and returns the array index
  837. *
  838. * on EOF: returns -1, with socket in len
  839. * on socket error: returns -2
  840. * if nothing is ready: returns -3
  841. */
  842. static int sockread(char *s, int *len)
  843. {
  844. fd_set fd;
  845. int fds = 0, i, fdtmp, x;
  846. struct timeval t;
  847. int grab = SGRAB + 1;
  848. egg_timeval_t howlong;
  849. if (timer_get_shortest(&howlong)) {
  850. /* No timer, default to 1 second. */
  851. t.tv_sec = 1;
  852. t.tv_usec = 0;
  853. }
  854. else {
  855. t.tv_sec = howlong.sec;
  856. t.tv_usec = howlong.usec;
  857. }
  858. FD_ZERO(&fd);
  859. for (i = 0; i < MAXSOCKS; i++) {
  860. if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_VIRTUAL))) {
  861. if ((socklist[i].sock == STDOUT) && !backgrd)
  862. fdtmp = STDIN;
  863. else
  864. fdtmp = socklist[i].sock;
  865. if (fdtmp > fds)
  866. fds = fdtmp;
  867. FD_SET(fdtmp, &fd);
  868. }
  869. }
  870. fds++;
  871. x = select(fds, &fd, NULL, NULL, &t);
  872. if (x > 0) {
  873. /* Something happened */
  874. for (i = 0; i < MAXSOCKS; i++) {
  875. if ((!(socklist[i].flags & SOCK_UNUSED)) && ((FD_ISSET(socklist[i].sock, &fd)) ||
  876. ((socklist[i].sock == STDOUT) && (!backgrd) && (FD_ISSET(STDIN, &fd))))) {
  877. if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
  878. /* Listening socket -- don't read, just return activity */
  879. /* Same for connection attempt */
  880. /* (for strong connections, require a read to succeed first) */
  881. if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
  882. /* Hang around to get the return code from proxy */
  883. grab = 10;
  884. } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
  885. debug1("net: connect! sock %d", socklist[i].sock);
  886. s[0] = 0;
  887. *len = 0;
  888. return i;
  889. }
  890. } else if (socklist[i].flags & SOCK_PASS) {
  891. s[0] = 0;
  892. *len = 0;
  893. return i;
  894. }
  895. errno = 0;
  896. if ((socklist[i].sock == STDOUT) && !backgrd)
  897. x = read(STDIN, s, grab);
  898. else
  899. x = read(socklist[i].sock, s, grab);
  900. if (x <= 0) { /* eof */
  901. if (errno != EAGAIN) { /* EAGAIN happens when the operation would block
  902. on a non-blocking socket, if the socket is going
  903. to die, it will die later, otherwise it will connect */
  904. *len = socklist[i].sock;
  905. socklist[i].flags &= ~SOCK_CONNECT;
  906. debug1("net: eof!(read) socket %d", socklist[i].sock);
  907. return -1;
  908. } else {
  909. debug3("sockread EAGAIN: %d %d (%s)", socklist[i].sock, errno, strerror(errno));
  910. continue; /* EAGAIN */
  911. }
  912. }
  913. s[x] = 0;
  914. *len = x;
  915. if (socklist[i].flags & SOCK_PROXYWAIT) {
  916. debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
  917. socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
  918. switch (s[1]) {
  919. case 90: /* Success */
  920. s[0] = 0;
  921. *len = 0;
  922. return i;
  923. case 91: /* Failed */
  924. errno = ECONNREFUSED;
  925. break;
  926. case 92: /* No identd */
  927. case 93: /* Identd said wrong username */
  928. /* A better error message would be "socks misconfigured"
  929. * or "identd not working" but this is simplest.
  930. */
  931. errno = ENETUNREACH;
  932. break;
  933. }
  934. *len = socklist[i].sock;
  935. return -1;
  936. }
  937. return i;
  938. }
  939. }
  940. } else if (x == -1)
  941. return -2; /* socket error */
  942. else {
  943. s[0] = 0;
  944. *len = 0;
  945. }
  946. return -3;
  947. }
  948. /* sockgets: buffer and read from sockets
  949. *
  950. * Attempts to read from all registered sockets for up to one second. if
  951. * after one second, no complete data has been received from any of the
  952. * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
  953. * if there is returnable data received from a socket, the data will be
  954. * in 's' (null-terminated if non-binary), the length will be returned
  955. * in len, and the socket number will be returned.
  956. * normal sockets have their input buffered, and each call to sockgets
  957. * will return one line terminated with a '\n'. binary sockets are not
  958. * buffered and return whatever coems in as soon as it arrives.
  959. * listening sockets will return an empty string when a connection comes in.
  960. * connecting sockets will return an empty string on a successful connect,
  961. * or EOF on a failed connect.
  962. * if an EOF is detected from any of the sockets, that socket number will be
  963. * put in len, and -1 will be returned.
  964. * the maximum length of the string returned is 512 (including null)
  965. *
  966. * Returns -4 if we handled something that shouldn't be handled by the
  967. * dcc functions. Simply ignore it.
  968. */
  969. int sockgets(char *s, int *len)
  970. {
  971. char xx[SGRAB + 4] = "", *p = NULL, *px = NULL;
  972. int ret;
  973. for (int i = 0; i < MAXSOCKS; i++) {
  974. /* Check for stored-up data waiting to be processed */
  975. if (!(socklist[i].flags & SOCK_UNUSED) && !(socklist[i].flags & SOCK_BUFFER) && (socklist[i].inbuf != NULL)) {
  976. if (!(socklist[i].flags & SOCK_BINARY)) {
  977. /* look for \r too cos windows can't follow RFCs */
  978. p = strchr(socklist[i].inbuf, '\n');
  979. if (p == NULL)
  980. p = strchr(socklist[i].inbuf, '\r');
  981. if (p != NULL) {
  982. *p = 0;
  983. if (strlen(socklist[i].inbuf) > SGRAB)
  984. socklist[i].inbuf[SGRAB] = 0;
  985. strlcpy(s, socklist[i].inbuf, SGRAB + 10); //buf@main.c
  986. size_t psiz = strlen(p + 1) + 1;
  987. px = (char *) my_calloc(1, psiz);
  988. strlcpy(px, p + 1, psiz);
  989. free(socklist[i].inbuf);
  990. if (px[0])
  991. socklist[i].inbuf = px;
  992. else {
  993. free(px);
  994. socklist[i].inbuf = NULL;
  995. }
  996. /* Strip CR if this was CR/LF combo */
  997. if (strlen(s) && s[strlen(s) - 1] == '\r')
  998. s[strlen(s) - 1] = 0;
  999. if (s[0] && socklist[i].encstatus)
  1000. link_read(i, s, (size_t *) len);
  1001. *len = strlen(s);
  1002. return socklist[i].sock;
  1003. }
  1004. } else {
  1005. /* i dont think any of this is *ever* called */
  1006. /* Handling buffered binary data (must have been SOCK_BUFFER before). */
  1007. if (socklist[i].inbuflen <= SGRAB) {
  1008. *len = socklist[i].inbuflen;
  1009. memcpy(s, socklist[i].inbuf, socklist[i].inbuflen);
  1010. free(socklist[i].inbuf);
  1011. socklist[i].inbuf = NULL;
  1012. socklist[i].inbuflen = 0;
  1013. } else {
  1014. /* Split up into chunks of SGRAB bytes. */
  1015. *len = SGRAB;
  1016. memcpy(s, socklist[i].inbuf, *len);
  1017. memmove(socklist[i].inbuf, socklist[i].inbuf + *len, *len);
  1018. socklist[i].inbuflen -= *len;
  1019. socklist[i].inbuf = (char *) my_realloc(socklist[i].inbuf, socklist[i].inbuflen);
  1020. }
  1021. return socklist[i].sock;
  1022. }
  1023. }
  1024. /* Also check any sockets that might have EOF'd during write */
  1025. if (!(socklist[i].flags & SOCK_UNUSED)
  1026. && (socklist[i].flags & SOCK_EOFD)) {
  1027. s[0] = 0;
  1028. *len = socklist[i].sock;
  1029. return -1;
  1030. }
  1031. }
  1032. /* No pent-up data of any worth -- down to business */
  1033. *len = 0;
  1034. ret = sockread(xx, len);
  1035. if (ret < 0) {
  1036. s[0] = 0;
  1037. return ret;
  1038. }
  1039. /* Binary, listening and passed on sockets don't get buffered. */
  1040. if (socklist[ret].flags & SOCK_CONNECT) {
  1041. if (socklist[ret].flags & SOCK_STRONGCONN) {
  1042. socklist[ret].flags &= ~SOCK_STRONGCONN;
  1043. /* Buffer any data that came in, for future read. */
  1044. socklist[ret].inbuflen = *len;
  1045. socklist[ret].inbuf = (char *) my_calloc(1, *len + 1);
  1046. /* It might be binary data. You never know. */
  1047. memcpy(socklist[ret].inbuf, xx, *len);
  1048. socklist[ret].inbuf[*len] = 0;
  1049. }
  1050. socklist[ret].flags &= ~SOCK_CONNECT;
  1051. s[0] = 0;
  1052. return socklist[ret].sock;
  1053. }
  1054. if (socklist[ret].flags & SOCK_BINARY) {
  1055. memcpy(s, xx, *len);
  1056. return socklist[ret].sock;
  1057. }
  1058. if ((socklist[ret].flags & SOCK_LISTEN) || (socklist[ret].flags & SOCK_PASS))
  1059. return socklist[ret].sock;
  1060. if (socklist[ret].flags & SOCK_BUFFER) {
  1061. socklist[ret].inbuf = (char *) my_realloc(socklist[ret].inbuf,
  1062. socklist[ret].inbuflen + *len + 1);
  1063. memcpy(socklist[ret].inbuf + socklist[ret].inbuflen, xx, *len);
  1064. socklist[ret].inbuflen += *len;
  1065. /* We don't know whether it's binary data. Make sure normal strings
  1066. will be handled properly later on too. */
  1067. socklist[ret].inbuf[socklist[ret].inbuflen] = 0;
  1068. return -4; /* Ignore this one. */
  1069. }
  1070. /* Might be necessary to prepend stored-up data! */
  1071. if (socklist[ret].inbuf != NULL) {
  1072. p = socklist[ret].inbuf;
  1073. size_t bufsiz = strlen(p) + strlen(xx) + 1;
  1074. socklist[ret].inbuf = (char *) my_calloc(1, bufsiz);
  1075. strlcpy(socklist[ret].inbuf, p, bufsiz);
  1076. strlcat(socklist[ret].inbuf, xx, bufsiz);
  1077. free(p);
  1078. if (strlen(socklist[ret].inbuf) < (SGRAB + 2)) {
  1079. strlcpy(xx, socklist[ret].inbuf, sizeof(xx));
  1080. free(socklist[ret].inbuf);
  1081. socklist[ret].inbuf = NULL;
  1082. socklist[ret].inbuflen = 0;
  1083. } else {
  1084. p = socklist[ret].inbuf;
  1085. socklist[ret].inbuflen = strlen(p) - SGRAB;
  1086. socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
  1087. strlcpy(socklist[ret].inbuf, p + SGRAB, socklist[ret].inbuflen + 1);
  1088. *(p + SGRAB) = 0;
  1089. strlcpy(xx, p, sizeof(xx));
  1090. free(p);
  1091. /* (leave the rest to be post-pended later) */
  1092. }
  1093. }
  1094. bool data = 0;
  1095. /* Look for EOL marker; if it's there, i have something to show */
  1096. p = strchr(xx, '\n');
  1097. if (p == NULL)
  1098. p = strchr(xx, '\r');
  1099. if (p != NULL) {
  1100. *p = 0;
  1101. strlcpy(s, xx, SGRAB + 10); //buf@main.c
  1102. memmove(xx, p + 1, strlen(p + 1) + 1);
  1103. /* if (s[0] && strlen(s) && (s[strlen(s) - 1] == '\r')) */
  1104. if (strlen(s) && s[strlen(s) - 1] == '\r')
  1105. s[strlen(s) - 1] = 0;
  1106. data = 1; /* DCC_CHAT may now need to process a blank line */
  1107. /* NO! */
  1108. /* if (!s[0]) strcpy(s," "); */
  1109. } else {
  1110. s[0] = 0;
  1111. if (strlen(xx) >= SGRAB) {
  1112. /* String is too long, so just insert fake \n */
  1113. strlcpy(s, xx, SGRAB + 10); //buf@main.c
  1114. xx[0] = 0;
  1115. data = 1;
  1116. }
  1117. }
  1118. if (s[0] && socklist[ret].encstatus)
  1119. link_read(ret, s, (size_t *) len);
  1120. *len = strlen(s);
  1121. /* Anything left that needs to be saved? */
  1122. if (!xx[0]) {
  1123. if (data)
  1124. return socklist[ret].sock;
  1125. else
  1126. return -3;
  1127. }
  1128. /* Prepend old data back */
  1129. if (socklist[ret].inbuf != NULL) {
  1130. p = socklist[ret].inbuf;
  1131. socklist[ret].inbuflen = strlen(p) + strlen(xx);
  1132. socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
  1133. strlcpy(socklist[ret].inbuf, xx, socklist[ret].inbuflen + 1);
  1134. strlcat(socklist[ret].inbuf, p, socklist[ret].inbuflen + 1);
  1135. free(p);
  1136. } else {
  1137. socklist[ret].inbuflen = strlen(xx);
  1138. socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
  1139. strlcpy(socklist[ret].inbuf, xx, socklist[ret].inbuflen + 1);
  1140. }
  1141. if (data) {
  1142. return socklist[ret].sock;
  1143. } else {
  1144. return -3;
  1145. }
  1146. }
  1147. /* Dump something to a socket
  1148. *
  1149. * NOTE: Do NOT put Contexts in here if you want DEBUG to be meaningful!!
  1150. */
  1151. void tputs(register int z, const char *s, size_t len)
  1152. {
  1153. if (z < 0) /* um... HELLO?! sanity check please! */
  1154. return;
  1155. if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
  1156. if (write(z, s, len) == -1) {
  1157. ;
  1158. }
  1159. return;
  1160. }
  1161. register int x, idx;
  1162. char *p = NULL;
  1163. for (register int i = 0; i < MAXSOCKS; i++) {
  1164. if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
  1165. for (idx = 0; idx < dcc_total; idx++) {
  1166. if (dcc[idx].type && (dcc[idx].sock == z) && dcc[idx].type->name) {
  1167. if (!strncmp(dcc[idx].type->name, "BOT", 3))
  1168. traffic.out_today.bn += len;
  1169. else if (!strcmp(dcc[idx].type->name, "SERVER"))
  1170. traffic.out_today.irc += len;
  1171. else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
  1172. traffic.out_today.dcc += len;
  1173. else if (!strncmp(dcc[idx].type->name, "FILES", 5))
  1174. traffic.out_today.filesys += len;
  1175. else if (!strcmp(dcc[idx].type->name, "SEND"))
  1176. traffic.out_today.trans += len;
  1177. else if (!strncmp(dcc[idx].type->name, "GET", 3))
  1178. traffic.out_today.trans += len;
  1179. else
  1180. traffic.out_today.unknown += len;
  1181. break;
  1182. }
  1183. }
  1184. if (len && socklist[i].encstatus)
  1185. s = link_write(i, s, &len);
  1186. if (socklist[i].outbuf != NULL) {
  1187. /* Already queueing: just add it */
  1188. p = (char *) my_realloc(socklist[i].outbuf, socklist[i].outbuflen + len);
  1189. memcpy(p + socklist[i].outbuflen, s, len);
  1190. socklist[i].outbuf = p;
  1191. socklist[i].outbuflen += len;
  1192. // if (socklist[i].encstatus && s)
  1193. // free(s);
  1194. return;
  1195. }
  1196. /* Try. */
  1197. #ifdef HAVE_ZLIB_H
  1198. /*
  1199. if (socklist[i].gz) {
  1200. FILE *fp;
  1201. fp = gzdopen(z, "wb0");
  1202. x = gzwrite(fp, s, len);
  1203. } else
  1204. */
  1205. #endif /* HAVE_ZLIB_H */
  1206. x = write(z, s, len);
  1207. if (x == -1)
  1208. x = 0;
  1209. if ((size_t) x < len) {
  1210. /* Socket is full, queue it */
  1211. socklist[i].outbuf = (char *) my_calloc(1, len - x);
  1212. memcpy(socklist[i].outbuf, &s[x], len - x);
  1213. socklist[i].outbuflen = len - x;
  1214. }
  1215. // if (socklist[i].encstatus && s)
  1216. // free(s);
  1217. return;
  1218. }
  1219. }
  1220. /* Make sure we don't cause a crash by looping here */
  1221. static int inhere = 0;
  1222. if (!inhere) {
  1223. inhere = 1;
  1224. putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
  1225. if (strlen(s)) {
  1226. char *tmp = strdup(s); /* To null-terminate */
  1227. putlog(LOG_MISC, "*", "!-> '%s'", tmp);
  1228. free(tmp);
  1229. }
  1230. inhere = 0;
  1231. }
  1232. /* if (socklist[i].encstatus > 0)
  1233. free(s);
  1234. */
  1235. }
  1236. int findanysnum(register int sock)
  1237. {
  1238. register int i = 0;
  1239. if (sock != -1)
  1240. for (i = 0; i < MAXSOCKS; i++)
  1241. if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED))
  1242. return i;
  1243. return -1;
  1244. }
  1245. static int findanyidx(register int sock)
  1246. {
  1247. register int j;
  1248. if (sock != -1)
  1249. for (j = 0; j < dcc_total; j++)
  1250. if (dcc[j].type && dcc[j].sock == sock)
  1251. return j;
  1252. return -1;
  1253. }
  1254. /* tputs might queue data for sockets, let's dump as much of it as
  1255. * possible.
  1256. */
  1257. void dequeue_sockets()
  1258. {
  1259. int i, x, z = 0, fds = 0;
  1260. fd_set wfds;
  1261. struct timeval tv;
  1262. /* ^-- start poptix test code, this should avoid writes to sockets not ready to be written to. */
  1263. FD_ZERO(&wfds);
  1264. tv.tv_sec = 0;
  1265. tv.tv_usec = 0; /* we only want to see if it's ready for writing, no need to actually wait.. */
  1266. for (i = 0; i < MAXSOCKS; i++) {
  1267. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].outbuf != NULL) {
  1268. FD_SET(socklist[i].sock, &wfds);
  1269. if (socklist[i].sock > fds)
  1270. fds = socklist[i].sock;
  1271. z = 1;
  1272. }
  1273. }
  1274. if (!z)
  1275. return; /* nothing to write */
  1276. fds++;
  1277. select(fds, NULL, &wfds, NULL, &tv);
  1278. /* end poptix */
  1279. for (i = 0; i < MAXSOCKS; i++) {
  1280. if (!(socklist[i].flags & SOCK_UNUSED) &&
  1281. (socklist[i].outbuf != NULL) && (FD_ISSET(socklist[i].sock, &wfds))) {
  1282. /* Trick tputs into doing the work */
  1283. errno = 0;
  1284. x = write(socklist[i].sock, socklist[i].outbuf, socklist[i].outbuflen);
  1285. if ((x < 0) && (errno != EAGAIN)
  1286. #ifdef EBADSLT
  1287. && (errno != EBADSLT)
  1288. #endif /* EBADSLT */
  1289. #ifdef ENOTCONN
  1290. && (errno != ENOTCONN)
  1291. #endif /* EBADSLT */
  1292. ) {
  1293. /* This detects an EOF during writing */
  1294. debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock, strerror(errno), errno);
  1295. socklist[i].flags |= SOCK_EOFD;
  1296. } else if ((size_t) x == socklist[i].outbuflen) {
  1297. /* If the whole buffer was sent, nuke it */
  1298. free(socklist[i].outbuf);
  1299. socklist[i].outbuf = NULL;
  1300. socklist[i].outbuflen = 0;
  1301. } else if (x > 0) {
  1302. char *p = socklist[i].outbuf;
  1303. /* This removes any sent bytes from the beginning of the buffer */
  1304. socklist[i].outbuf = (char *) my_calloc(1, socklist[i].outbuflen - x);
  1305. memcpy(socklist[i].outbuf, p + x, socklist[i].outbuflen - x);
  1306. socklist[i].outbuflen -= x;
  1307. free(p);
  1308. } else {
  1309. debug3("dequeue_sockets(): errno = %d (%s) on %d", errno, strerror(errno), socklist[i].sock);
  1310. }
  1311. /* All queued data was sent. Call handler if one exists and the
  1312. * dcc entry wants it.
  1313. */
  1314. if (!socklist[i].outbuf) {
  1315. int idx = findanyidx(socklist[i].sock);
  1316. if (idx >= 0 && dcc[idx].type && dcc[idx].type->outdone)
  1317. dcc[idx].type->outdone(idx);
  1318. }
  1319. }
  1320. }
  1321. }
  1322. /*
  1323. * Debugging stuff
  1324. */
  1325. void tell_netdebug(int idx)
  1326. {
  1327. char s[80] = "";
  1328. dprintf(idx, "Open sockets:");
  1329. for (int i = 0; i < MAXSOCKS; i++) {
  1330. if (!(socklist[i].flags & SOCK_UNUSED)) {
  1331. simple_snprintf(s, sizeof(s), " %d", socklist[i].sock);
  1332. if (socklist[i].flags & SOCK_BINARY)
  1333. strlcat(s, " (binary)", sizeof(s));
  1334. if (socklist[i].flags & SOCK_LISTEN)
  1335. strlcat(s, " (listen)", sizeof(s));
  1336. if (socklist[i].flags & SOCK_PASS)
  1337. strlcat(s, " (passed on)", sizeof(s));
  1338. if (socklist[i].flags & SOCK_CONNECT)
  1339. strlcat(s, " (connecting)", sizeof(s));
  1340. if (socklist[i].flags & SOCK_STRONGCONN)
  1341. strlcat(s, " (strong)", sizeof(s));
  1342. if (socklist[i].flags & SOCK_NONSOCK)
  1343. strlcat(s, " (file)", sizeof(s));
  1344. if (socklist[i].inbuf != NULL)
  1345. simple_sprintf(&s[strlen(s)], " (inbuf: %zu)", strlen(socklist[i].inbuf));
  1346. if (socklist[i].outbuf != NULL)
  1347. simple_sprintf(&s[strlen(s)], " (outbuf: %zu)", socklist[i].outbuflen);
  1348. if (socklist[i].host)
  1349. simple_sprintf(&s[strlen(s)], " (%s:%d)", socklist[i].host, socklist[i].port);
  1350. strlcat(s, ",", sizeof(s));
  1351. dprintf(idx, "%s", s);
  1352. }
  1353. }
  1354. dprintf(idx, " done.\n");
  1355. }
  1356. /* Checks wether the referenced socket has data queued.
  1357. *
  1358. * Returns true if the incoming/outgoing (depending on 'type') queues
  1359. * contain data, otherwise false.
  1360. */
  1361. bool sock_has_data(int type, int sock)
  1362. {
  1363. bool ret = 0;
  1364. int i;
  1365. for (i = 0; i < MAXSOCKS; i++)
  1366. if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
  1367. break;
  1368. if (i < MAXSOCKS) {
  1369. switch (type) {
  1370. case SOCK_DATA_OUTGOING:
  1371. ret = (socklist[i].outbuf != NULL);
  1372. break;
  1373. case SOCK_DATA_INCOMING:
  1374. ret = (socklist[i].inbuf != NULL);
  1375. break;
  1376. }
  1377. } else
  1378. debug1("sock_has_data: could not find socket #%d, returning false.", sock);
  1379. return ret;
  1380. }