net.c 45 KB

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