net.c 43 KB

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