diff options
-rw-r--r-- | libirc/Makefile | 3 | ||||
-rw-r--r-- | libirc/libirc.c | 53 | ||||
-rw-r--r-- | segfault.c | 53 |
3 files changed, 50 insertions, 59 deletions
diff --git a/libirc/Makefile b/libirc/Makefile index 2ed3299..95c7451 100644 --- a/libirc/Makefile +++ b/libirc/Makefile @@ -9,3 +9,6 @@ $(TARGET): clean: rm -f libirc.so + +install: + cp $(TARGET) /usr/local/lib/$(TARGET) diff --git a/libirc/libirc.c b/libirc/libirc.c index ab93113..c529d10 100644 --- a/libirc/libirc.c +++ b/libirc/libirc.c @@ -7,21 +7,19 @@ #include <netdb.h> #include <sys/socket.h> #include <arpa/inet.h> +#include <unistd.h> -//#define DEBUG //comment me out to disable. +//#define DEBUG "epoch" //nick or channel to send debug info to. #define CHUNK 16 static int serverConnect(char *serv,char *port) { struct addrinfo hints, *servinfo, *p; int rv; + int fd=0; memset(&hints,0,sizeof hints); hints.ai_family=AF_INET; hints.ai_socktype=SOCK_STREAM; - - int fd=0; - - fd=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); - if(fd < 0) { + if((fd=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0) { perror("socket"); return fd; } @@ -36,18 +34,7 @@ static int serverConnect(char *serv,char *port) { } break; } - if(!p) { - return 0; - } - return fd; -} - -static int haschar(char *s,char c) { - int i; - for(i=0;s[i];i++) { - if(s[i]==c) return 1; - } - return 0; + return p?fd:0; } int runit(int fd,void (*line_handler)(),void (*extra_handler)()) { @@ -55,18 +42,13 @@ int runit(int fd,void (*line_handler)(),void (*extra_handler)()) { fd_set master; fd_set readfs; struct timeval timeout; - int fdmax; - int n; - int s; - int i; - int good=0; + int fdmax,n,s,i; char *backlog=malloc(CHUNK+1); - if(!backlog) return 252; - char *t; - char *line=0; + char *t,*line=0; int blsize=CHUNK; int bllen=0; char buffer[CHUNK];//THIS IS *NOT* NULL TERMINATED. + if(!backlog) return 252; FD_ZERO(&master); FD_ZERO(&readfs); FD_SET(fd,&master); @@ -76,26 +58,22 @@ int runit(int fd,void (*line_handler)(),void (*extra_handler)()) { memset(buffer,0,CHUNK); if(fd) { int done=0; -// printf("starting main loop.\n"); while(!done) { extra_handler(fd); readfs=master; timeout.tv_sec=0; timeout.tv_usec=1000; -// printf("trying select. %d\n",time(0)); if( select(fdmax+1,&readfs,0,0,&timeout) == -1 ) { printf("\n!!!It is crashing here!!!\n\n"); perror("select"); return 1; } if(FD_ISSET(fd,&readfs)) { -// printf("An fd is set!!!\n"); if((n=recv(fd,buffer,CHUNK,0)) <= 0) {//read CHUNK bytes fprintf(stderr,"recv: %d\n",n); perror("recv"); return 2; } else { -// printf("%d bytes read\n",n); buffer[n]=0;//deff right. if(bllen+n >= blsize) {//this is probably off... blsize+=n; @@ -111,28 +89,22 @@ int runit(int fd,void (*line_handler)(),void (*extra_handler)()) { } memcpy(backlog+bllen,buffer,n); bllen+=n; -// write(1,backlog,bllen); -// write(1,"\n",1); for(i=0,s=0;i<bllen;i++) { if(backlog[i]=='\n') { - line=malloc(i-s+3);//on linux it crashes without the +1 + line=malloc(i-s+3);//on linux it crashes without the +1 +3? weird. when did I do that? if(!line) { printf("ANOTHER malloc error!\n"); exit(254); } memcpy(line,backlog+s,i-s+2); -// printf("NEWLINE!\n"); line[i-s+1]=0;//gotta null terminate this. line_handler expects it. -// printf("libirc: !!! %s !!!\n",line); s=i+1;//the character after the newline. if(!strncmp(line,"PING",4)) { - //write(fd,"PONG ",5); - //write(fd,line+6,strlen(line)-6); - fprintf(fp,"PING %s\r\n",line+6);//a whole FILE * and fdopen JUST for this??? oi... + fprintf(fp,"PONG %s\r\n",line+6);//a whole FILE * and fdopen JUST for this??? oy... fflush(fp); #ifdef DEBUG printf("%s\nPONG %s\n",line,line+6); - write(fd,"PRIVMSG epoch :PONG! w00t!\r\n",28); + write(fd,"PRIVMSG %s :PONG! w00t!\r\n",DEBUG,28); #endif } else if(!strncmp(line,"ERROR",5)) { #ifdef DEBUG @@ -157,9 +129,8 @@ int runit(int fd,void (*line_handler)(),void (*extra_handler)()) { } } } - } else { - return 0; } + return 0; } //:hack.thebackupbox.net 433 * sysbot :Nickname is already in use. @@ -58,7 +58,6 @@ timer_t timer; int lines_sent; unsigned long oldtime; - struct tail { FILE *fp; char *file; @@ -82,9 +81,12 @@ void c_untail(int fd,char *from, char *file); void mywrite(int fd,char *b) { + int r; if(!b) return; if(fd<0) return; - write(fd,b,strlen(b)); + r=write(fd,b,strlen(b)); + if(r == -1) exit(1); + if(r != strlen(b)) exit(2); lines_sent++; } @@ -173,6 +175,13 @@ char *format_magic(int fd,char *from,char *nick,char *orig_fmt,char *arg) { return output; } +void eofd(int fd) { + char b; + if(lseek(fd,0,SEEK_END) == -1) { + while(read(fd,&b,1) > 0);//this is used on named pipes usually. + } +} + void eofp(FILE *fp) { if(fseek(fp,0,SEEK_END) == -1) { while(fgetc(fp) != -1);//this is used on named pipes usually. @@ -283,7 +292,9 @@ void file_tail(int fd,char *from,char *file,char *args,char opt) { privmsg(fd,from,tmp); } else { fcntl(fileno(tailf[i].fp),F_SETFL,O_NONBLOCK); - if(!(opt & TAILO_BEGIN)) fseek(tailf[i].fp,0,SEEK_END); + if(!(opt & TAILO_BEGIN)) { + eofp(tailf[i].fp); + } tailf[i].to=malloc(strlen(from)+1); if(!tailf[i].to) { mywrite(fd,"QUIT :malloc error 3!!!\r\n"); @@ -568,7 +579,9 @@ void c_leetuntail(int fd,char *from,char *line) { if(tailf[i].fp && !strcmp(tailf[i].file,file)) { //c_untail(fd,tailf[i].to,file); eofp(tailf[i].fp); - fclose(tailf[i].fp); + if(fclose(tailf[i].fp) == -1) { + privmsg(fd,from,"well, shit. fclose failed somehow."); + } tailf[i].fp=0; free(tailf[i].to); free(tailf[i].file); @@ -583,7 +596,9 @@ void c_leetuntail(int fd,char *from,char *line) { } else { frmN=atoi(frm); if(frmN < MAXTAILS && tailf[frmN].fp) { - fclose(tailf[frmN].fp); + if(fclose(tailf[frmN].fp) == -1) { + privmsg(fd,from,"well shit. fclose failed. #2"); + } tailf[frmN].fp=0; free(tailf[frmN].to); free(tailf[frmN].file); @@ -613,7 +628,9 @@ void c_untail(int fd,char *from, char *file) { for(i=0;i<MAXTAILS;i++) { if(tailf[i].fp) { if(!strcmp(from,tailf[i].to) && !strcmp(file,tailf[i].file)) { - fclose(tailf[i].fp); + if(fclose(tailf[i].fp) == -1) { + privmsg(fd,from,"fclose failed. #3"); + } tailf[i].fp=0; free(tailf[i].to); free(tailf[i].file); @@ -641,6 +658,7 @@ char append_file(int fd,char *from,char *file,char *line,unsigned short nl) { privmsg(fd,from,strerror(errno)); return 0; } + if(!(fp=fdopen(fdd,"a"))) { snprintf(tmp,sizeof(tmp)-1,"Couldn't fdopen file (%s) fd:%d for appending.",file,fdd); privmsg(fd,from,tmp); @@ -648,18 +666,17 @@ char append_file(int fd,char *from,char *file,char *line,unsigned short nl) { return 0; } fcntl(fileno(fp),F_SETFL,O_NONBLOCK); - fseek(fp,0,SEEK_END);//is this right for named pipes? - //if(fprintf(fp,"%s%s",line,(char *) - // ( nl != 0 ? - // &nl : - // (unsigned short *)"" ) - if(fprintf(fp,"%s\n",line - ) < 0) { - privmsg(fd,from,"error writing to file."); - return 0; - } - fflush(fp);//??? - fflush(fp); + eofp(fp); +/* mywrite(fileno(fp),line); + mywrite(fileno(fp),"\n");*/ + fprintf(fp,"%s\n",line); +/* + fcntl(fdd,F_SETFL,O_NONBLOCK); + eofd(fdd); + mywrite(fdd,line); + mywrite(fdd,"\n"); +*/ +// close(fdd); fclose(fp); return 1; } |