net.c 48 KB

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