From de6d83270b0523124d130d9ce4befe6f02a59c9e Mon Sep 17 00:00:00 2001 From: epochqwert Date: Sat, 16 May 2015 03:25:59 -0500 Subject: added support for middle-man scripts, like... export segserver=|ssl_connect :) or maybe eventually segserver=|OTR --- libirc.c | 106 +++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 66 insertions(+), 40 deletions(-) diff --git a/libirc.c b/libirc.c index c21867c..975225d 100644 --- a/libirc.c +++ b/libirc.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "irc.h" //#define DEBUG "epoch" //nick or channel to send debug info to. @@ -20,49 +21,74 @@ int main(int argc,char *argv[]) { int serverConnect(char *serv,char *port) { int rv; int fd=0; + int s[2]; + int pid; + char *name[3]; struct addrinfo hints, *servinfo, *p=0; - memset(&hints,0,sizeof hints); - hints.ai_family=AF_INET; - hints.ai_socktype=SOCK_STREAM; - if((fd=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0) { - perror("socket"); - return -1; - } -/* - for(try_ipv4=0;try_ipv4 < 2;try_ipv4++) { - if(!(he=gethostbyname2( - try_ipv4 - ?inet_aton(serv,&saddr) - ?inet_ntoa(saddr) - :serv - :inet_pton(AF_INET6,serv,&saddr6) - ?inet_ntop(AF_INET6,&saddr6,buf,SILLYLIMIT) - :serv - ,try_ipv4?AF_INET:AF_INET6))) return -1; - - for(;*(he->h_addr_list);he->h_addr_list++) { - printf("trying to connect to %s:%s attempt #%d\n",serv,port,n); - n++; -*/ -// if((rv=getaddrinfo(he->h_addr_list,port,&hints,&servinfo)) != 0) { - if((rv=getaddrinfo(serv,port,&hints,&servinfo)) != 0) { - fprintf(stderr,"error resolving '%s'.\n",serv); - return -1; - } - for(p=servinfo;p;p=p->ai_next) { - if(connect(fd,p->ai_addr, p->ai_addrlen) < 0) { - perror("connect"); - continue; - } else { - return fd; + if(!serv || !port) return -1; + if(*serv != '|') { + memset(&hints,0,sizeof hints); + hints.ai_family=AF_INET; + hints.ai_socktype=SOCK_STREAM; + if((fd=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0) { + perror("socket"); + return -1; + } + /* + for(try_ipv4=0;try_ipv4 < 2;try_ipv4++) { + if(!(he=gethostbyname2( + try_ipv4 + ?inet_aton(serv,&saddr) + ?inet_ntoa(saddr) + :serv + :inet_pton(AF_INET6,serv,&saddr6) + ?inet_ntop(AF_INET6,&saddr6,buf,SILLYLIMIT) + :serv + ,try_ipv4?AF_INET:AF_INET6))) return -1; + for(;*(he->h_addr_list);he->h_addr_list++) { + printf("trying to connect to %s:%s attempt #%d\n",serv,port,n); + n++; + */ + // if((rv=getaddrinfo(he->h_addr_list,port,&hints,&servinfo)) != 0) { + if((rv=getaddrinfo(serv,port,&hints,&servinfo)) != 0) { + fprintf(stderr,"error resolving '%s'.\n",serv); + return -1; } - } - //printf("trying a differnt address...\n"); + for(p=servinfo;p;p=p->ai_next) { + if(connect(fd,p->ai_addr, p->ai_addrlen) < 0) { + perror("connect"); + continue; + } else { + return fd; + } + } + //printf("trying a differnt address...\n"); + //} + //printf("trying a different AF...\n"); //} - //printf("trying a different AF...\n"); - //} - //printf("well, shit. how'd I get here?\n"); - return -1; + //printf("well, shit. how'd I get here?\n"); + return -1; + } else { + name[0]=serv+1; + name[1]=port; + name[2]=0; + socketpair(PF_LOCAL,SOCK_STREAM,0,s); + switch(pid=fork()) { + case 0://child + close(0); + close(1); + //close(2); + dup(s[1]); + dup(s[1]); + //dup(s[1]); + execv(name[0],name); + case -1://error + return -1; + default://parent + return s[0]; + } + return -1; + } } int fdlen(int *fds) { -- cgit v1.2.3