| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544 |
- /*
- * Copyright (C) 1997 Robey Pointer
- * Copyright (C) 1999 - 2002 Eggheads Development Team
- * Copyright (C) 2002 - 2008 Bryan Drewery
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- /*
- * net.c -- handles:
- * all raw network i/o
- *
- */
- #include <fcntl.h>
- #include "common.h"
- #include "net.h"
- #include "socket.h"
- #include "misc.h"
- #include "main.h"
- #include "debug.h"
- #include "dccutil.h"
- #include "enclink.h"
- #include "egg_timer.h"
- #include "traffic.h"
- #include "adns.h"
- #include <bdlib/src/String.h>
- #include <bdlib/src/Stream.h>
- #include <limits.h>
- #include <string.h>
- #include <netdb.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <setjmp.h>
- #if HAVE_SYS_SELECT_H
- # include <sys/select.h>
- #endif /* HAVE_SYS_SELECT_H */
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #include <sys/stat.h>
- #if HAVE_UNISTD_H
- # include <unistd.h>
- #endif /* HAVE_UNITSTD_H */
- extern egg_traffic_t traffic;
- union sockaddr_union cached_myip4_so;
- #ifdef USE_IPV6
- union sockaddr_union cached_myip6_so;
- #endif /* USE_IPV6 */
- bool cached_ip = 0; /* Set to 1 after cache_my_ip is called */
- bool identd_hack = 0; /* identd_open() won't work on most servers, dont even bother warning. */
- char botuser[21] = ""; /* Username of the user running the bot */
- int socks_total = 0; /* total number of sockets */
- sock_list *socklist = NULL; /* Enough to be safe */
- int MAXSOCKS = 0;
- jmp_buf alarmret; /* Env buffer for alarm() returns */
- /* This *MUST* be an ip */
- char firewall[121] = ""; /* Socks server for firewall */
- port_t firewallport = 1080; /* Default port of Sock4/5 firewalls */
- /* Types of proxy */
- #define PROXY_SOCKS 1
- #define PROXY_SUN 2
- #define PROXY_HTTP 3
- /* I need an UNSIGNED long for dcc type stuff
- */
- unsigned long my_atoul(const char *s)
- {
- unsigned long ret = 0;
- while ((*s >= '0') && (*s <= '9')) {
- ret *= 10;
- ret += ((*s) - '0');
- ++s;
- }
- return ret;
- }
- /* get the protocol used on a socket */
- int sockprotocol(int sock)
- {
- struct sockaddr sa;
- socklen_t socklen = sizeof(sa);
- bzero(&sa, socklen);
- if (getsockname(sock, &sa, &socklen))
- return -1;
- else
- return sa.sa_family;
- }
- /* AF_INET-independent resolving routine */
- static int get_ip(char *hostname, union sockaddr_union *so)
- {
- if (!hostname || (hostname && !hostname[0]))
- return 1;
- memset(so, 0, sizeof(union sockaddr_union));
- debug1("get_ip(%s)", hostname);
- #ifdef USE_IPV6
- struct addrinfo hints, *ai = NULL, *res = NULL;
- int error = 0;
- memset(&hints, 0, sizeof(struct addrinfo));
- hints.ai_socktype = SOCK_STREAM;
- if ((error = getaddrinfo(hostname, NULL, &hints, &res))) {
- if (res)
- freeaddrinfo(res);
- return error;
- }
- error = 1;
- for (ai = res; ai != NULL; ai = ai->ai_next) {
- if ((ai->ai_family == AF_INET6) || (ai->ai_family == AF_INET)) {
- memcpy(so, ai->ai_addr, ai->ai_addrlen);
- error = 0;
- break;
- }
- }
- if (res)
- freeaddrinfo(res);
- return error;
- #else
- struct hostent *hp = NULL;
- if (!(hp = gethostbyname(hostname)))
- return -1;
- memcpy(&so->sin.sin_addr, hp->h_addr, 4);
- so->sin.sin_family = AF_INET;
- return 0;
- #endif /* USE_IPV6 */
- }
- /* Initialize the socklist
- */
- void init_net()
- {
- MAXSOCKS = max_dcc + 10;
- if (socklist)
- socklist = (sock_list *) my_realloc((void *) socklist, sizeof(sock_list) * MAXSOCKS);
- else
- socklist = (sock_list *) my_calloc(1, sizeof(sock_list) * MAXSOCKS);
- for (int i = 0; i < MAXSOCKS; i++) {
- bzero(&socklist[i], sizeof(socklist[i]));
- socklist[i].flags = SOCK_UNUSED;
- socklist[i].sock = -1;
- }
- }
- /* Get my ipv? ip
- */
- char *myipstr(int af_type)
- {
- if (cached_ip) {
- #ifdef USE_IPV6
- if (af_type == AF_INET6) {
- static char s[UHOSTLEN + 1] = "";
- inet_ntop(AF_INET6, &cached_myip6_so.sin6.sin6_addr, s, 119);
- s[120] = 0;
- return s;
- } else
- #endif /* USE_IPV6 */
- if (af_type == AF_INET) {
- static char s[UHOSTLEN + 1] = "";
- inet_ntop(AF_INET, &cached_myip4_so.sin.sin_addr, s, 119);
- s[120] = 0;
- return s;
- }
- }
- return "";
- }
- /* Get my ip number
- */
- in_addr_t getmyip() {
- return cached_myip4_so.sin.sin_addr.s_addr;
- }
- /* see if it's necessary to set inaddr_any... because if we can't resolve, we die anyway */
- void cache_my_ip()
- {
- #ifdef no
- cached_myip6_so.sin6.sin6_family = AF_INET6;
- cached_myip6_so.sin6.sin6_addr = in6addr_any;
- cached_myip4_so.sin.sin_family = AF_INET;
- cached_myip4_so.sin.sin_addr.s_addr = INADDR_ANY;
- #endif
- int error = 0;
- debug0("cache_my_ip()");
- memset(&cached_myip4_so, 0, sizeof(union sockaddr_union));
- #ifdef USE_IPV6
- bool any = 0;
- memset(&cached_myip6_so, 0, sizeof(union sockaddr_union));
- if (conf.bot->net.ip6) {
- if (get_ip(conf.bot->net.ip6, &cached_myip6_so))
- any = 1;
- } else if (conf.bot->net.host6) {
- if (get_ip(conf.bot->net.host6, &cached_myip6_so))
- any = 1;
- } else
- any = 1;
- if (any) {
- cached_myip6_so.sin6.sin6_family = AF_INET6;
- cached_myip6_so.sin6.sin6_addr = in6addr_any;
- }
- #endif /* USE_IPV6 */
- if (conf.bot->net.ip) {
- if (get_ip(conf.bot->net.ip, &cached_myip4_so))
- error = 1;
- } else if (conf.bot->net.host) {
- if (get_ip(conf.bot->net.host, &cached_myip4_so))
- error = 2;
- } else {
- /*
- char s[121] = "";
- gethostname(s, sizeof(s));
- if (get_ip(s, &cached_myip4_so)) {
- */
- /* error = 3; */
- cached_myip4_so.sin.sin_family = AF_INET;
- cached_myip4_so.sin.sin_addr.s_addr = INADDR_ANY;
- // }
- }
- if (error) {
- putlog(LOG_DEBUG, "*", "Hostname self-lookup error: %d", error);
- fatal("Hostname self-lookup failed.", 0);
- }
- cached_ip = 1;
- }
- /* Sets/Unsets options for a specific socket.
- *
- * Returns: 0 - on success
- * -1 - socket not found
- * -2 - illegal operation
- */
- int sockoptions(int sock, int operation, int sock_options)
- {
- for (int i = 0; i < MAXSOCKS; i++) {
- if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
- if (operation == EGG_OPTION_SET)
- socklist[i].flags |= sock_options;
- else if (operation == EGG_OPTION_UNSET)
- socklist[i].flags &= ~sock_options;
- else
- return -2;
- return 0;
- }
- }
- return -1;
- }
- int
- sock_read(bd::Stream& stream)
- {
- int fd = -1;
- bd::String buf, type;
- while (stream.tell() < stream.length()) {
- buf = stream.getline().chomp();
- if (buf == STR("+sock"))
- return fd;
- type = newsplit(buf);
- if (type == STR("sock")) {
- int sock = atoi(newsplit(buf).c_str()), options = atoi(newsplit(buf).c_str());
- fd = allocsock(sock, options);
- }
- if (fd >= 0) {
- #ifdef USE_IPV6
- if (type == STR("af"))
- socklist[fd].af = atoi(buf.c_str());
- #endif
- if (type == STR("host"))
- socklist[fd].host = strdup(buf.c_str());
- if (type == STR("port"))
- socklist[fd].port = atoi(buf.c_str());
- }
- }
- return -1;
- }
- void
- sock_write(bd::Stream &stream, int fd)
- {
- if (socklist[fd].sock > 0) {
- bd::String buf;
- stream << buf.printf(STR("-sock\n"));
- stream << buf.printf(STR("sock %d %d\n"), socklist[fd].sock, socklist[fd].flags);
- #ifdef USE_IPV6
- stream << buf.printf(STR("af %u\n"), socklist[fd].af);
- #endif
- if (socklist[fd].host)
- stream << buf.printf(STR("host %s\n"), socklist[fd].host);
- if (socklist[fd].port)
- stream << buf.printf(STR("port %d\n"), socklist[fd].port);
- stream << buf.printf(STR("+sock\n"));
- }
- }
- /* Return a free entry in the socket entry
- */
- int allocsock(int sock, int options)
- {
- for (int i = 0; i < MAXSOCKS; i++) {
- if (socklist[i].flags & SOCK_UNUSED) {
- /* yay! there is table space */
- socklist[i].inbuf = socklist[i].outbuf = NULL;
- socklist[i].inbuflen = socklist[i].outbuflen = 0;
- socklist[i].flags = options;
- socklist[i].sock = sock;
- socklist[i].encstatus = 0;
- socklist[i].enclink = -1;
- socklist[i].gz = 0;
- bzero(&(socklist[i].okey), ENC_KEY_LEN + 1);
- bzero(&(socklist[i].ikey), ENC_KEY_LEN + 1);
- socks_total++;
- sdprintf("allocsock(%d) = %d", i, sock);
- return i;
- }
- }
- fatal("Socket table is full!", 0);
- return -1; /* Never reached */
- }
- /* Request a normal socket for i/o
- */
- void setsock(int sock, int options)
- {
- int i = allocsock(sock, options);
- bool parm;
- if (((sock != STDOUT) || backgrd) && !(socklist[i].flags & SOCK_NONSOCK)) {
- parm = 1;
- setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *) &parm, sizeof(int));
- parm = 0;
- setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &parm, sizeof(int));
- }
- if (options & SOCK_LISTEN) {
- /* Tris says this lets us grab the same port again next time */
- parm = 1;
- setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &parm, sizeof(int));
- }
- /* Yay async i/o ! */
- fcntl(sock, F_SETFL, O_NONBLOCK);
- }
- #ifdef USE_IPV6
- int real_getsock(int options, int af_def, const char *fname, int line)
- {
- #else
- int real_getsock(int options, const char *fname, int line)
- {
- int af_def = AF_INET;
- #endif /* USE_IPV6 */
- int sock = -1;
- if (!af_def)
- af_def = AF_INET;
- sock = socket(af_def, SOCK_STREAM, 0);
- if (sock >= 0)
- setsock(sock, options);
- else if (!identd_hack)
- putlog(LOG_WARNING, "*", "Warning: Can't create new socket! (%s:%d): %s", fname, line, strerror(errno));
- else if (identd_hack)
- identd_hack = 0;
- return sock;
- }
- /* Done with a socket
- */
- void real_killsock(register int sock, const char *file, int line)
- {
- if (sock < 0) {
- putlog(LOG_ERRORS, "*", "Attempt to kill socket -1 %s:%d", file, line);
- return;
- }
- for (register int i = 0; i < MAXSOCKS; i++) {
- if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
- close(socklist[i].sock);
- if (socklist[i].inbuf != NULL) {
- free(socklist[i].inbuf);
- socklist[i].inbuf = NULL;
- }
- if (socklist[i].outbuf != NULL) {
- free(socklist[i].outbuf);
- socklist[i].outbuf = NULL;
- socklist[i].outbuflen = 0;
- }
- if (socklist[i].host)
- free(socklist[i].host);
- bzero(&socklist[i], sizeof(socklist[i]));
- socklist[i].flags = SOCK_UNUSED;
- socks_total--;
- sdprintf("killsock(%d, %s, %d) (socklist: %d)", sock, file, line, i);
- return;
- }
- }
- putlog(LOG_MISC, "*", "Attempt to kill un-allocated socket %d %s:%d !!", sock, file, line);
- }
- /* Send connection request to proxy
- */
- static int proxy_connect(int sock, const char *ip, port_t port, int proxy_type)
- {
- sdprintf("proxy_connect(%d, %s, %d, %d)", sock, ip, port, proxy_type);
- #ifdef USE_IPV6
- unsigned char x[32] = "";
- int af_ty = sockprotocol(sock);
- #else
- unsigned char x[10] = "";
- #endif /* USE_IPV6 */
- char s[256] = "";
- /* socks proxy */
- if (proxy_type == PROXY_SOCKS) {
- /* numeric IP? */
- if (is_dotted_ip(ip)) {
- in_addr_t ipaddr = ((in_addr_t) inet_addr(ip));
- memcpy(x, &ipaddr, 4);
- } else { /* if not resolved, resolve it with blocking calls.. (shouldn't happen ever) */
- return -1;
- }
- for (int i = 0; i < MAXSOCKS; i++)
- if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
- socklist[i].flags |= SOCK_PROXYWAIT;
- #ifdef USE_IPV6
- if (af_ty == AF_INET6)
- simple_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), x[0], x[1], x[2], x[3],
- x[4], x[5], x[6], x[7], x[9], x[9], x[10], x[11], x[12],
- x[13], x[14], x[15], botuser);
- else
- #endif /* USE_IPV6 */
- simple_snprintf(s, sizeof s, "\004\001%c%c%c%c%c%c%s", (port >> 8) % 256,
- (port % 256), x[0], x[1], x[2], x[3], botuser);
- tputs(sock, s, strlen(botuser) + 9);
- } else if (proxy_type == PROXY_SUN) {
- simple_snprintf(s, sizeof s, "%s %d\n", ip, port);
- tputs(sock, s, strlen(s));
- } else if (proxy_type == PROXY_HTTP) {
- simple_snprintf(s, sizeof s, "CONNECT %s:%d\n\n", ip, port);
- tputs(sock, s, strlen(s));
- }
- return sock;
- }
- /* FIXME: REPLACE WITH SOCK_NAME() */
- void initialize_sockaddr(int af_type, const char *host, port_t port, union sockaddr_union *so)
- {
- bzero(so, sizeof(*so));
- so->sa.sa_family = af_type;
- if (af_type == AF_INET) {
- so->sin.sin_family = AF_INET;
- if (host) {
- inet_pton(AF_INET, host, &so->sin.sin_addr);
- so->sin.sin_port = htons(port);
- } else {
- so->sin.sin_addr.s_addr = getmyip();
- }
- #ifdef USE_IPV6
- } else {
- so->sin6.sin6_family = AF_INET6;
- if (host) {
- inet_pton(AF_INET6, host, &so->sin6.sin6_addr);
- so->sin6.sin6_port = htons(port);
- } else {
- memcpy(&so->sin6.sin6_addr, &cached_myip6_so.sin6.sin6_addr, 16);
- }
- #endif /* USE_IPV6 */
- }
- }
- /* Starts a connection attempt to a socket
- *
- * If given a normal hostname, this will be resolved to the corresponding
- * IP address first. PLEASE try to use the non-blocking dns functions
- * instead and then call this function with the IP address to avoid blocking.
- *
- * returns <0 if connection refused:
- * -1 strerror()/errno type error
- * -2 can't resolve hostname
- */
- int open_telnet_raw(int sock, const char *ipIn, port_t sport, bool proxy_on, int identd)
- {
- static port_t port = 0;
- union sockaddr_union so;
- char ip[121] = "";
- int is_resolved = 0;
- volatile int proxy_type = 0, proxy = proxy_on;
- /* firewall? use socks */
- if (proxy) {
- switch (firewall[0]) {
- case '!':
- proxy_type = PROXY_SUN;
- strlcpy(ip, &firewall[1], sizeof(ip));
- break;
- case '@':
- proxy_type = PROXY_HTTP;
- strlcpy(ip, &firewall[1], sizeof(ip));
- break;
- default:
- proxy_type = PROXY_SOCKS;
- strlcpy(ip, firewall, sizeof(ip));
- break;
- }
- port = firewallport;
- } else {
- proxy_type = 0;
- strlcpy(ip, ipIn, sizeof(ip));
- port = sport;
- }
- socklen_t socklen;
- if (sport) {
- /* figure out which ip to bind to locally (v4 or v6) based on what the host ip is .. */
- if ((is_resolved = is_dotted_ip(ip))) { /* already resolved */
- /* bind to our cached ip for v4/v6 depending on what the ip is */
- initialize_sockaddr(is_resolved, NULL, 0, &so);
- if (bind(sock, &so.sa, SIZEOF_SOCKADDR(so)) < 0) {
- putlog(LOG_DEBUG, "*", "Failed to bind to socket %d: %s", sock, strerror(errno));
- killsock(sock);
- return -1;
- }
- /* initialize so for connect using the host/port */
- initialize_sockaddr(is_resolved, ip, port, &so);
- } else { /* if not resolved, resolve it with blocking calls.. (shouldn't happen ever) */
- sdprintf("WARNING: open_telnet_raw(%s,%d) was passed an unresolved hostname.", ip, port);
- killsock(sock);
- return -1;
- }
- socklen = SIZEOF_SOCKADDR(so);
- } else { // Unix domain socket
- so.sock_un.sun_family = AF_UNIX;
- strcpy(so.sock_un.sun_path, ip);
- socklen = SUN_LEN(&so.sock_un);
- }
- for (int i = 0; i < MAXSOCKS; i++) {
- if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock)) {
- socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT;
- socklist[i].host = strdup(ipIn);
- socklist[i].port = port;
- break;
- }
- }
- if (identd && sport) //Only open identd if not a unix domain socket
- identd_open(myipstr(is_resolved), ipIn, identd);
- int rc = -1;
- /* make the connect attempt */
- rc = connect(sock, (struct sockaddr *)&so.sa, socklen);
- if (rc < 0) {
- if (errno == EINPROGRESS) {
- debug3("net: connect(%d, %s, %d)", sock, ipIn, sport);
- /* Firewall? announce connect attempt to proxy */
- if (proxy)
- return proxy_connect(sock, ipIn, sport, proxy_type);
- return sock; /* async success! */
- } else {
- sdprintf("connect(%d, %s, %d) failed: %s", sock, ip, sport, strerror(errno));
- killsock(sock);
- return -1;
- }
- }
- /* Synchronous? :/ */
- debug3("net: (BLOCKING) connect(%d, %s, %d)", sock, ipIn, sport);
- if (proxy)
- return proxy_connect(sock, ipIn, sport, proxy_type);
- return sock;
- }
- /* Ordinary non-binary connection attempt */
- int open_telnet(const char *ip, port_t port, bool proxy, int identd)
- {
- int sock = -1;
-
- #ifdef USE_IPV6
- sock = getsock(0, is_dotted_ip(ip));
- #else
- sock = getsock(0);
- #endif /* USE_IPV6 */
- if (sock >= 0)
- return open_telnet_raw(sock, ip, port, proxy, identd);
- return -1;
- }
- /* Returns a socket number for a listening socket that will accept any
- * connection on a certain address -- port # is returned in port
- *
- * 'addr' is ignored if af_def is AF_INET6 -poptix (02/03/03)
- */
- #ifdef USE_IPV6
- int open_address_listen(const char* ip, int af_def, port_t *port)
- #else
- int open_address_listen(const char* ip, port_t *port)
- #endif /* USE_IPV6 */
- {
- // if (firewall[0]) {
- // /* FIXME: can't do listen port thru firewall yet */
- // putlog(LOG_MISC, "*", "!! Cant open a listen port (you are using a firewall)");
- // return -1;
- // }
- int sock = 0;
- socklen_t addrlen;
- union sockaddr_union name;
- #ifdef USE_IPV6
- if (af_def == AF_INET6) {
- struct sockaddr_in6 name6;
- sock = getsock(SOCK_LISTEN, af_def);
- if (sock < 0)
- return -1;
- debug2("Opening listen socket on port %d with AF_INET6, sock: %d", *port, sock);
- bzero((char *) &name6, sizeof(name6));
- name6.sin6_family = af_def;
- name6.sin6_port = htons(*port); /* 0 = just assign us a port */
- /* memcpy(&name6.sin6_addr, &in6addr_any, 16); */ /* this is the only way to get ipv6+ipv4 in 1 socket */
- memcpy(&name6.sin6_addr, &cached_myip6_so.sin6.sin6_addr, 16);
- if (bind(sock, (struct sockaddr *) &name6, sizeof(name6)) < 0) {
- if (!(identd_hack && *port == 113))
- putlog(LOG_DEBUG, "*", "Failed to bind to socket %d for listen on port %d: %s", sock, *port, strerror(errno));
- killsock(sock);
- return -1;
- }
- addrlen = sizeof(name6);
- if (getsockname(sock, (struct sockaddr *) &name6, &addrlen) < 0) {
- if (!(identd_hack && *port == 113))
- putlog(LOG_DEBUG, "*", "Failed to getsockname on socket %d for listen on port %d: %s", sock, *port, strerror(errno));
- killsock(sock);
- return -1;
- }
- *port = ntohs(name6.sin6_port);
- if (listen(sock, 1) < 0) {
- if (!(identd_hack && *port == 113))
- putlog(LOG_DEBUG, "*", "Failed to listen on socket %d for listen on port %d: %s", sock, *port, strerror(errno));
- killsock(sock);
- return -1;
- }
- } else {
- sock = getsock(SOCK_LISTEN, af_def);
- #else
- sock = getsock(SOCK_LISTEN);
- #endif /* USE_IPV6 */
- if (sock < 0)
- return -1;
- if (af_def == AF_UNIX)
- debug2("Opening listen socket on %s, sock: %d", ip, sock);
- else
- debug3("Opening listen socket on %s:%d with AF_INET, sock: %d", ip, *port, sock);
- bzero((char *) &name, sizeof(struct sockaddr *));
- if (af_def == AF_UNIX) {
- name.sock_un.sun_family = AF_UNIX;
- strcpy(name.sock_un.sun_path, ip);
- unlink(name.sock_un.sun_path);
- addrlen = SUN_LEN(&name.sock_un);
- } else {
- name.sin.sin_family = af_def;
- name.sin.sin_port = htons(*port); /* 0 = just assign us a port */
- name.sin.sin_addr.s_addr = inet_addr(ip);
- addrlen = sizeof(struct sockaddr_in);
- }
- if (bind(sock, (struct sockaddr *) &name, addrlen) < 0) {
- if (!(identd_hack && *port == 113)) {
- if (af_def == AF_UNIX)
- putlog(LOG_DEBUG, "*", "Failed to bind to socket %d for listen on %s: %s", sock, ip, strerror(errno));
- else
- putlog(LOG_DEBUG, "*", "Failed to bind to socket %d for listen on %s:%d: %s", sock, ip, *port, strerror(errno));
- }
- killsock(sock);
- return -1;
- }
- if (af_def != AF_UNIX) {
- /* what port are we on? */
- if (getsockname(sock, (struct sockaddr *) &name, &addrlen) < 0) {
- if (!(identd_hack && *port == 113))
- putlog(LOG_DEBUG, "*", "Failed to getsockname on socket %d for listen on port %d: %s", sock, *port, strerror(errno));
- killsock(sock);
- return -1;
- }
- *port = ntohs(name.sin.sin_port);
- }
- if (listen(sock, 1) < 0) {
- if (!(identd_hack && *port == 113) && af_def != AF_UNIX)
- putlog(LOG_DEBUG, "*", "Failed to listen on socket %d for on port %d: %s", sock, *port, strerror(errno));
- killsock(sock);
- return -1;
- }
- #ifdef USE_IPV6
- }
- #endif /* USE_IPV6 */
- if (af_def == AF_UNIX)
- debug2("Opened listen socket on %s, sock: %d", ip, sock);
- else
- debug3("Opened listen socket on %s:%d with AF_INET, sock: %d", ip, *port, sock);
- return sock;
- }
- /* Returns a socket number for a listening socket that will accept any
- * connection -- port # is returned in port
- */
- int open_listen(port_t *port)
- {
- #ifdef USE_IPV6
- return open_address_listen(iptostr(getmyip()), AF_INET, port);
- #else
- return open_address_listen(iptostr(getmyip()), port);
- #endif /* USE_IPV6 */
- }
- /* Same as above, except this one can be called with an AF_ type
- * the above is being left in for compatibility, and should NOT LONGER BE USED IN THE CORE CODE.
- */
- int open_listen_by_af(port_t *port, int af_def)
- {
- #ifdef USE_IPV6
- return open_address_listen(iptostr(getmyip()), af_def, port);
- #else
- return -1;
- #endif /* USE_IPV6 */
- }
- int open_listen_addr_by_af(const char *ip, port_t *port, int af_def)
- {
- if (!ip)
- ip = iptostr(getmyip());
- #ifdef USE_IPV6
- return open_address_listen(ip, af_def, port);
- #else
- return -1;
- #endif /* USE_IPV6 */
- }
- /* Returns the given network byte order IP address in the
- * dotted format - "##.##.##.##"
- */
- char *iptostr(in_addr_t ip)
- {
- static char ipbuf[32];
- struct in_addr a;
- a.s_addr = ip;
- return (char *) inet_ntop(AF_INET, &a, ipbuf, sizeof(ipbuf));
- }
- /* Short routine to answer a connect received on a socket made previously
- * by open_listen ... returns hostname of the caller & the new socket
- * does NOT dispose of old "public" socket!
- */
- int answer(int sock, char *caller, in_addr_t *ip, port_t *port, int binary)
- {
- int new_sock;
- socklen_t addrlen;
- struct sockaddr_in from;
- #ifdef USE_IPV6
- int af_ty = sockprotocol(sock);
- struct sockaddr_in6 from6;
- bzero(&from6, sizeof(struct sockaddr_in6));
- if (af_ty == AF_INET6) {
- addrlen = sizeof(from6);
- new_sock = accept(sock, (struct sockaddr *) &from6, &addrlen);
- } else {
- #endif /* USE_IPV6 */
- addrlen = sizeof(struct sockaddr);
- new_sock = accept(sock, (struct sockaddr *) &from, &addrlen);
- #ifdef USE_IPV6
- }
- #endif /* USE_IPV6 */
-
- if (new_sock < 0)
- return -1;
- if (ip != NULL) {
- #ifdef USE_IPV6
- /* Detect IPv4 in IPv6 mapped address .... */
- if (af_ty == AF_INET6 && (!IN6_IS_ADDR_V4MAPPED(&from6.sin6_addr))) {
- inet_ntop(AF_INET6, &from6.sin6_addr, caller, 119);
- caller[120] = 0;
- *ip = 0L;
- } else if (IN6_IS_ADDR_V4MAPPED(&from6.sin6_addr)) { /* ...and convert it to plain (AF_INET) IPv4 address (openssh) */
- struct sockaddr_in *from4 = (struct sockaddr_in *)&from6;
- struct in_addr addr;
- memcpy(&addr, ((char *)&from6.sin6_addr) + 12, sizeof(addr));
- memset(&from, 0, sizeof(from));
- from4->sin_family = AF_INET;
- addrlen = sizeof(*from4);
- memcpy(&from4->sin_addr, &addr, sizeof(addr));
- *ip = from4->sin_addr.s_addr;
- strlcpy(caller, iptostr(*ip), 121);
- *ip = ntohl(*ip);
- } else {
- #endif /* USE_IPV6 */
- if (af_ty == AF_UNIX) {
- struct sockaddr_un sock_un;
- socklen_t socklen = sizeof(sock_un);
- bzero(&sock_un, socklen);
- getsockname(sock, (struct sockaddr*) &sock_un, &socklen);
- strcpy(caller, sock_un.sun_path);
- *port = 0;
- } else {
- *ip = from.sin_addr.s_addr;
- strlcpy(caller, iptostr(*ip), 121);
- *ip = ntohl(*ip);
- }
- #ifdef USE_IPV6
- }
- #endif /* USE_IPV6 */
- }
- if (port != NULL) {
- #ifdef USE_IPV6
- if (af_ty == AF_INET6)
- *port = ntohs(from6.sin6_port);
- else if (af_ty == AF_INET)
- #endif /* USE_IPV6 */
- *port = ntohs(from.sin_port);
- }
- /* Set up all the normal socket crap */
- setsock(new_sock, (binary ? SOCK_BINARY : 0));
- sdprintf("Answered socket %d: %s", new_sock, caller);
- return new_sock;
- }
- /* Like open_telnet, but uses ip & port specifications of dcc
- Take a longip and make into dotted form
- */
- int open_telnet_dcc(int sock, char *ip, char *port)
- {
- port_t p;
- unsigned long addr;
- char sv[100] = "";
- unsigned char c[4] = "";
- #ifdef DEBUG_IPV6
- debug1("open_telnet_dcc %s", ip);
- #endif /* DEBUG_IPV6 */
- if (port != NULL)
- p = atoi(port);
- else
- p = 2000;
- #ifdef USE_IPV6
- if (sockprotocol(sock) == AF_INET6) {
- # ifdef DEBUG_IPV6
- debug0("open_telnet_dcc, af_inet6!");
- # endif /* DEBUG_IPV6 */
- strlcpy(sv, ip, sizeof(sv));
- } else {
- #endif /* USE_IPV6 */
- if (ip != NULL)
- addr = my_atoul(ip);
- else
- addr = 0L;
- if (addr < (1 << 24))
- return -3; /* fake address */
- c[0] = (addr >> 24) & 0xff;
- c[1] = (addr >> 16) & 0xff;
- c[2] = (addr >> 8) & 0xff;
- c[3] = addr & 0xff;
- simple_snprintf(sv, sizeof(sv), "%u.%u.%u.%u", c[0], c[1], c[2], c[3]);
- #ifdef USE_IPV6
- }
- /* strcpy(sv,hostnamefromip(addr)); */
- # ifdef DEBUG_IPV6
- debug3("open_telnet_raw %s %d %d", sv, sock, p);
- # endif /* DEBUG_IPV6 */
- #endif /* USE_IPV6 */
- return open_telnet_raw(sock, sv, p, 0);
- }
- /* Attempts to read from all the sockets in socklist
- * fills s with up to 511 bytes if available, and returns the array index
- *
- * on EOF: returns -1, with socket in len
- * on socket error: returns -2
- * if nothing is ready: returns -3
- */
- static int sockread(char *s, int *len)
- {
- fd_set fd;
- int fds = 0, i, fdtmp, x;
- struct timeval t;
- int grab = SGRAB + 1;
- egg_timeval_t howlong;
- if (timer_get_shortest(&howlong)) {
- /* No timer, default to 1 second. */
- t.tv_sec = 1;
- t.tv_usec = 0;
- }
- else {
- t.tv_sec = howlong.sec;
- t.tv_usec = howlong.usec;
- }
- FD_ZERO(&fd);
-
- for (i = 0; i < MAXSOCKS; i++) {
- if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_VIRTUAL))) {
- if ((socklist[i].sock == STDOUT) && !backgrd)
- fdtmp = STDIN;
- else
- fdtmp = socklist[i].sock;
- if (fdtmp > fds)
- fds = fdtmp;
- FD_SET(fdtmp, &fd);
- }
- }
- fds++;
- x = select(fds, &fd, NULL, NULL, &t);
- if (x > 0) {
- /* Something happened */
- for (i = 0; i < MAXSOCKS; i++) {
- if ((!(socklist[i].flags & SOCK_UNUSED)) && ((FD_ISSET(socklist[i].sock, &fd)) ||
- ((socklist[i].sock == STDOUT) && (!backgrd) && (FD_ISSET(STDIN, &fd))))) {
- if (socklist[i].flags & (SOCK_LISTEN | SOCK_CONNECT)) {
- /* Listening socket -- don't read, just return activity */
- /* Same for connection attempt */
- /* (for strong connections, require a read to succeed first) */
- if (socklist[i].flags & SOCK_PROXYWAIT) { /* drummer */
- /* Hang around to get the return code from proxy */
- grab = 10;
- } else if (!(socklist[i].flags & SOCK_STRONGCONN)) {
- debug1("net: connect! sock %d", socklist[i].sock);
- s[0] = 0;
- *len = 0;
- return i;
- }
- } else if (socklist[i].flags & SOCK_PASS) {
- s[0] = 0;
- *len = 0;
- return i;
- }
- errno = 0;
- if ((socklist[i].sock == STDOUT) && !backgrd)
- x = read(STDIN, s, grab);
- else
- x = read(socklist[i].sock, s, grab);
- if (x <= 0) { /* eof */
- if (errno != EAGAIN) { /* EAGAIN happens when the operation would block
- on a non-blocking socket, if the socket is going
- to die, it will die later, otherwise it will connect */
- *len = socklist[i].sock;
- socklist[i].flags &= ~SOCK_CONNECT;
- debug1("net: eof!(read) socket %d", socklist[i].sock);
- return -1;
- } else {
- debug3("sockread EAGAIN: %d %d (%s)", socklist[i].sock, errno, strerror(errno));
- continue; /* EAGAIN */
- }
- }
- s[x] = 0;
- *len = x;
- if (socklist[i].flags & SOCK_PROXYWAIT) {
- debug2("net: socket: %d proxy errno: %d", socklist[i].sock, s[1]);
- socklist[i].flags &= ~(SOCK_CONNECT | SOCK_PROXYWAIT);
- switch (s[1]) {
- case 90: /* Success */
- s[0] = 0;
- *len = 0;
- return i;
- case 91: /* Failed */
- errno = ECONNREFUSED;
- break;
- case 92: /* No identd */
- case 93: /* Identd said wrong username */
- /* A better error message would be "socks misconfigured"
- * or "identd not working" but this is simplest.
- */
- errno = ENETUNREACH;
- break;
- }
- *len = socklist[i].sock;
- return -1;
- }
- return i;
- }
- }
- } else if (x == -1)
- return -2; /* socket error */
- else {
- s[0] = 0;
- *len = 0;
- }
- return -3;
- }
- /* sockgets: buffer and read from sockets
- *
- * Attempts to read from all registered sockets for up to one second. if
- * after one second, no complete data has been received from any of the
- * sockets, 's' will be empty, 'len' will be 0, and sockgets will return -3.
- * if there is returnable data received from a socket, the data will be
- * in 's' (null-terminated if non-binary), the length will be returned
- * in len, and the socket number will be returned.
- * normal sockets have their input buffered, and each call to sockgets
- * will return one line terminated with a '\n'. binary sockets are not
- * buffered and return whatever coems in as soon as it arrives.
- * listening sockets will return an empty string when a connection comes in.
- * connecting sockets will return an empty string on a successful connect,
- * or EOF on a failed connect.
- * if an EOF is detected from any of the sockets, that socket number will be
- * put in len, and -1 will be returned.
- * the maximum length of the string returned is 512 (including null)
- *
- * Returns -4 if we handled something that shouldn't be handled by the
- * dcc functions. Simply ignore it.
- */
- int sockgets(char *s, int *len)
- {
- char xx[SGRAB + 4] = "", *p = NULL, *px = NULL;
- int ret;
- for (int i = 0; i < MAXSOCKS; i++) {
- /* Check for stored-up data waiting to be processed */
- if (!(socklist[i].flags & SOCK_UNUSED) && !(socklist[i].flags & SOCK_BUFFER) && (socklist[i].inbuf != NULL)) {
- if (!(socklist[i].flags & SOCK_BINARY)) {
- /* look for \r too cos windows can't follow RFCs */
- p = strchr(socklist[i].inbuf, '\n');
- if (p == NULL)
- p = strchr(socklist[i].inbuf, '\r');
- if (p != NULL) {
- *p = 0;
- if (strlen(socklist[i].inbuf) > SGRAB)
- socklist[i].inbuf[SGRAB] = 0;
- strlcpy(s, socklist[i].inbuf, SGRAB + 10); //buf@main.c
- size_t psiz = strlen(p + 1) + 1;
- px = (char *) my_calloc(1, psiz);
- strlcpy(px, p + 1, psiz);
- free(socklist[i].inbuf);
- if (px[0])
- socklist[i].inbuf = px;
- else {
- free(px);
- socklist[i].inbuf = NULL;
- }
- /* Strip CR if this was CR/LF combo */
- if (strlen(s) && s[strlen(s) - 1] == '\r')
- s[strlen(s) - 1] = 0;
- if (s[0] && socklist[i].encstatus)
- link_read(i, s, (size_t *) len);
-
- *len = strlen(s);
- return socklist[i].sock;
- }
- } else {
- /* i dont think any of this is *ever* called */
- /* Handling buffered binary data (must have been SOCK_BUFFER before). */
- if (socklist[i].inbuflen <= SGRAB) {
- *len = socklist[i].inbuflen;
- memcpy(s, socklist[i].inbuf, socklist[i].inbuflen);
- free(socklist[i].inbuf);
- socklist[i].inbuf = NULL;
- socklist[i].inbuflen = 0;
- } else {
- /* Split up into chunks of SGRAB bytes. */
- *len = SGRAB;
- memcpy(s, socklist[i].inbuf, *len);
- memmove(socklist[i].inbuf, socklist[i].inbuf + *len, *len);
- socklist[i].inbuflen -= *len;
- socklist[i].inbuf = (char *) my_realloc(socklist[i].inbuf, socklist[i].inbuflen);
- }
- return socklist[i].sock;
- }
- }
- /* Also check any sockets that might have EOF'd during write */
- if (!(socklist[i].flags & SOCK_UNUSED)
- && (socklist[i].flags & SOCK_EOFD)) {
- s[0] = 0;
- *len = socklist[i].sock;
- return -1;
- }
- }
- /* No pent-up data of any worth -- down to business */
- *len = 0;
- ret = sockread(xx, len);
- if (ret < 0) {
- s[0] = 0;
- return ret;
- }
- /* Binary, listening and passed on sockets don't get buffered. */
- if (socklist[ret].flags & SOCK_CONNECT) {
- if (socklist[ret].flags & SOCK_STRONGCONN) {
- socklist[ret].flags &= ~SOCK_STRONGCONN;
- /* Buffer any data that came in, for future read. */
- socklist[ret].inbuflen = *len;
- socklist[ret].inbuf = (char *) my_calloc(1, *len + 1);
- /* It might be binary data. You never know. */
- memcpy(socklist[ret].inbuf, xx, *len);
- socklist[ret].inbuf[*len] = 0;
- }
- socklist[ret].flags &= ~SOCK_CONNECT;
- s[0] = 0;
- return socklist[ret].sock;
- }
- if (socklist[ret].flags & SOCK_BINARY) {
- memcpy(s, xx, *len);
- return socklist[ret].sock;
- }
- if ((socklist[ret].flags & SOCK_LISTEN) || (socklist[ret].flags & SOCK_PASS))
- return socklist[ret].sock;
- if (socklist[ret].flags & SOCK_BUFFER) {
- socklist[ret].inbuf = (char *) my_realloc(socklist[ret].inbuf,
- socklist[ret].inbuflen + *len + 1);
- memcpy(socklist[ret].inbuf + socklist[ret].inbuflen, xx, *len);
- socklist[ret].inbuflen += *len;
- /* We don't know whether it's binary data. Make sure normal strings
- will be handled properly later on too. */
- socklist[ret].inbuf[socklist[ret].inbuflen] = 0;
- return -4; /* Ignore this one. */
- }
- /* Might be necessary to prepend stored-up data! */
- if (socklist[ret].inbuf != NULL) {
- p = socklist[ret].inbuf;
- size_t bufsiz = strlen(p) + strlen(xx) + 1;
- socklist[ret].inbuf = (char *) my_calloc(1, bufsiz);
- strlcpy(socklist[ret].inbuf, p, bufsiz);
- strlcat(socklist[ret].inbuf, xx, bufsiz);
- free(p);
- if (strlen(socklist[ret].inbuf) < (SGRAB + 2)) {
- strlcpy(xx, socklist[ret].inbuf, sizeof(xx));
- free(socklist[ret].inbuf);
- socklist[ret].inbuf = NULL;
- socklist[ret].inbuflen = 0;
- } else {
- p = socklist[ret].inbuf;
- socklist[ret].inbuflen = strlen(p) - SGRAB;
- socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
- strlcpy(socklist[ret].inbuf, p + SGRAB, socklist[ret].inbuflen + 1);
- *(p + SGRAB) = 0;
- strlcpy(xx, p, sizeof(xx));
- free(p);
- /* (leave the rest to be post-pended later) */
- }
- }
- bool data = 0;
- /* Look for EOL marker; if it's there, i have something to show */
- p = strchr(xx, '\n');
- if (p == NULL)
- p = strchr(xx, '\r');
- if (p != NULL) {
- *p = 0;
- strlcpy(s, xx, SGRAB + 10); //buf@main.c
- memmove(xx, p + 1, strlen(p + 1) + 1);
- /* if (s[0] && strlen(s) && (s[strlen(s) - 1] == '\r')) */
- if (strlen(s) && s[strlen(s) - 1] == '\r')
- s[strlen(s) - 1] = 0;
- data = 1; /* DCC_CHAT may now need to process a blank line */
- /* NO! */
- /* if (!s[0]) strcpy(s," "); */
- } else {
- s[0] = 0;
- if (strlen(xx) >= SGRAB) {
- /* String is too long, so just insert fake \n */
- strlcpy(s, xx, SGRAB + 10); //buf@main.c
- xx[0] = 0;
- data = 1;
- }
- }
- if (s[0] && socklist[ret].encstatus)
- link_read(ret, s, (size_t *) len);
- *len = strlen(s);
- /* Anything left that needs to be saved? */
- if (!xx[0]) {
- if (data)
- return socklist[ret].sock;
- else
- return -3;
- }
- /* Prepend old data back */
- if (socklist[ret].inbuf != NULL) {
- p = socklist[ret].inbuf;
- socklist[ret].inbuflen = strlen(p) + strlen(xx);
- socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
- strlcpy(socklist[ret].inbuf, xx, socklist[ret].inbuflen + 1);
- strlcat(socklist[ret].inbuf, p, socklist[ret].inbuflen + 1);
- free(p);
- } else {
- socklist[ret].inbuflen = strlen(xx);
- socklist[ret].inbuf = (char *) my_calloc(1, socklist[ret].inbuflen + 1);
- strlcpy(socklist[ret].inbuf, xx, socklist[ret].inbuflen + 1);
- }
- if (data) {
- return socklist[ret].sock;
- } else {
- return -3;
- }
- }
- /* Dump something to a socket
- *
- * NOTE: Do NOT put Contexts in here if you want DEBUG to be meaningful!!
- */
- void tputs(register int z, const char *s, size_t len)
- {
- if (z < 0) /* um... HELLO?! sanity check please! */
- return;
- if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
- if (write(z, s, len) == -1) {
- ;
- }
- return;
- }
- register int x, idx;
- char *p = NULL;
- for (register int i = 0; i < MAXSOCKS; i++) {
- if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
- for (idx = 0; idx < dcc_total; idx++) {
- if (dcc[idx].type && (dcc[idx].sock == z) && dcc[idx].type->name) {
- if (!strncmp(dcc[idx].type->name, "BOT", 3))
- traffic.out_today.bn += len;
- else if (!strcmp(dcc[idx].type->name, "SERVER"))
- traffic.out_today.irc += len;
- else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
- traffic.out_today.dcc += len;
- else if (!strncmp(dcc[idx].type->name, "FILES", 5))
- traffic.out_today.filesys += len;
- else if (!strcmp(dcc[idx].type->name, "SEND"))
- traffic.out_today.trans += len;
- else if (!strncmp(dcc[idx].type->name, "GET", 3))
- traffic.out_today.trans += len;
- else
- traffic.out_today.unknown += len;
- break;
- }
- }
- if (len && socklist[i].encstatus)
- s = link_write(i, s, &len);
-
- if (socklist[i].outbuf != NULL) {
- /* Already queueing: just add it */
- p = (char *) my_realloc(socklist[i].outbuf, socklist[i].outbuflen + len);
- memcpy(p + socklist[i].outbuflen, s, len);
- socklist[i].outbuf = p;
- socklist[i].outbuflen += len;
- // if (socklist[i].encstatus && s)
- // free(s);
- return;
- }
- /* Try. */
- #ifdef HAVE_ZLIB_H
- /*
- if (socklist[i].gz) {
- FILE *fp;
- fp = gzdopen(z, "wb0");
- x = gzwrite(fp, s, len);
-
- } else
- */
- #endif /* HAVE_ZLIB_H */
- x = write(z, s, len);
- if (x == -1)
- x = 0;
- if ((size_t) x < len) {
- /* Socket is full, queue it */
- socklist[i].outbuf = (char *) my_calloc(1, len - x);
- memcpy(socklist[i].outbuf, &s[x], len - x);
- socklist[i].outbuflen = len - x;
- }
- // if (socklist[i].encstatus && s)
- // free(s);
- return;
- }
- }
- /* Make sure we don't cause a crash by looping here */
- static int inhere = 0;
- if (!inhere) {
- inhere = 1;
- putlog(LOG_MISC, "*", "!!! writing to nonexistent socket: %d", z);
- if (strlen(s)) {
- char *tmp = strdup(s); /* To null-terminate */
- putlog(LOG_MISC, "*", "!-> '%s'", tmp);
- free(tmp);
- }
- inhere = 0;
- }
- /* if (socklist[i].encstatus > 0)
- free(s);
- */
- }
- int findanysnum(register int sock)
- {
- register int i = 0;
- if (sock != -1)
- for (i = 0; i < MAXSOCKS; i++)
- if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED))
- return i;
- return -1;
- }
- static int findanyidx(register int sock)
- {
- register int j;
- if (sock != -1)
- for (j = 0; j < dcc_total; j++)
- if (dcc[j].type && dcc[j].sock == sock)
- return j;
- return -1;
- }
- /* tputs might queue data for sockets, let's dump as much of it as
- * possible.
- */
- void dequeue_sockets()
- {
- int i, x, z = 0, fds = 0;
- fd_set wfds;
- struct timeval tv;
- /* ^-- start poptix test code, this should avoid writes to sockets not ready to be written to. */
- FD_ZERO(&wfds);
- tv.tv_sec = 0;
- tv.tv_usec = 0; /* we only want to see if it's ready for writing, no need to actually wait.. */
- for (i = 0; i < MAXSOCKS; i++) {
- if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].outbuf != NULL) {
- FD_SET(socklist[i].sock, &wfds);
- if (socklist[i].sock > fds)
- fds = socklist[i].sock;
- z = 1;
- }
- }
- if (!z)
- return; /* nothing to write */
- fds++;
- select(fds, NULL, &wfds, NULL, &tv);
- /* end poptix */
- for (i = 0; i < MAXSOCKS; i++) {
- if (!(socklist[i].flags & SOCK_UNUSED) &&
- (socklist[i].outbuf != NULL) && (FD_ISSET(socklist[i].sock, &wfds))) {
- /* Trick tputs into doing the work */
- errno = 0;
- x = write(socklist[i].sock, socklist[i].outbuf, socklist[i].outbuflen);
- if ((x < 0) && (errno != EAGAIN)
- #ifdef EBADSLT
- && (errno != EBADSLT)
- #endif /* EBADSLT */
- #ifdef ENOTCONN
- && (errno != ENOTCONN)
- #endif /* EBADSLT */
- ) {
- /* This detects an EOF during writing */
- debug3("net: eof!(write) socket %d (%s,%d)", socklist[i].sock, strerror(errno), errno);
- socklist[i].flags |= SOCK_EOFD;
- } else if ((size_t) x == socklist[i].outbuflen) {
- /* If the whole buffer was sent, nuke it */
- free(socklist[i].outbuf);
- socklist[i].outbuf = NULL;
- socklist[i].outbuflen = 0;
- } else if (x > 0) {
- char *p = socklist[i].outbuf;
- /* This removes any sent bytes from the beginning of the buffer */
- socklist[i].outbuf = (char *) my_calloc(1, socklist[i].outbuflen - x);
- memcpy(socklist[i].outbuf, p + x, socklist[i].outbuflen - x);
- socklist[i].outbuflen -= x;
- free(p);
- } else {
- debug3("dequeue_sockets(): errno = %d (%s) on %d", errno, strerror(errno), socklist[i].sock);
- }
- /* All queued data was sent. Call handler if one exists and the
- * dcc entry wants it.
- */
- if (!socklist[i].outbuf) {
- int idx = findanyidx(socklist[i].sock);
- if (idx >= 0 && dcc[idx].type && dcc[idx].type->outdone)
- dcc[idx].type->outdone(idx);
- }
- }
- }
- }
-
- /*
- * Debugging stuff
- */
- void tell_netdebug(int idx)
- {
- char s[80] = "";
- dprintf(idx, "Open sockets:");
- for (int i = 0; i < MAXSOCKS; i++) {
- if (!(socklist[i].flags & SOCK_UNUSED)) {
- simple_snprintf(s, sizeof(s), " %d", socklist[i].sock);
- if (socklist[i].flags & SOCK_BINARY)
- strlcat(s, " (binary)", sizeof(s));
- if (socklist[i].flags & SOCK_LISTEN)
- strlcat(s, " (listen)", sizeof(s));
- if (socklist[i].flags & SOCK_PASS)
- strlcat(s, " (passed on)", sizeof(s));
- if (socklist[i].flags & SOCK_CONNECT)
- strlcat(s, " (connecting)", sizeof(s));
- if (socklist[i].flags & SOCK_STRONGCONN)
- strlcat(s, " (strong)", sizeof(s));
- if (socklist[i].flags & SOCK_NONSOCK)
- strlcat(s, " (file)", sizeof(s));
- if (socklist[i].inbuf != NULL)
- simple_sprintf(&s[strlen(s)], " (inbuf: %zu)", strlen(socklist[i].inbuf));
- if (socklist[i].outbuf != NULL)
- simple_sprintf(&s[strlen(s)], " (outbuf: %zu)", socklist[i].outbuflen);
- if (socklist[i].host)
- simple_sprintf(&s[strlen(s)], " (%s:%d)", socklist[i].host, socklist[i].port);
- strlcat(s, ",", sizeof(s));
- dprintf(idx, "%s", s);
- }
- }
- dprintf(idx, " done.\n");
- }
- /* Checks wether the referenced socket has data queued.
- *
- * Returns true if the incoming/outgoing (depending on 'type') queues
- * contain data, otherwise false.
- */
- bool sock_has_data(int type, int sock)
- {
- bool ret = 0;
- int i;
- for (i = 0; i < MAXSOCKS; i++)
- if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
- break;
- if (i < MAXSOCKS) {
- switch (type) {
- case SOCK_DATA_OUTGOING:
- ret = (socklist[i].outbuf != NULL);
- break;
- case SOCK_DATA_INCOMING:
- ret = (socklist[i].inbuf != NULL);
- break;
- }
- } else
- debug1("sock_has_data: could not find socket #%d, returning false.", sock);
- return ret;
- }
|