net.c 43 KB

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