int grabline(void) { char *ptr; int zap; int i; zap = -1; i = 1; ptr = uri; while(i < 96) { zap = sockread(sock, ptr, 1); if(zap < 0) { break; } if(zap > 0) { if(*ptr == 0x0a) break; if(*ptr != 0x0d) { i++; ptr++; } } } *ptr = 0x00; return zap; }
/* * woo.. no args? we use CurrentChan, CurrentNick and CurrentUser. */ void greet(void) { Strp *sp,**pp; char linebuf[MSGLEN],readbuf[MSGLEN]; char *str; int fd,sz; pp = ¤t->sendq; while(*pp) pp = &(*pp)->next; if (CurrentUser->x.x.greetfile) { if ((fd = open(CurrentUser->greet,O_RDONLY)) < 0) return; sz = sizeof(Strp) + 9 + strlen(CurrentNick); memset(readbuf,0,sizeof(readbuf)); while(TRUE) { str = sockread(fd,readbuf,linebuf); if (str) { *pp = sp = (Strp*)Calloc(sz + strlen(str)); /* Calloc sets to zero sp->next = NULL; */ pp = &sp->next; sprintf(sp->p,"NOTICE %s :%s",CurrentNick,str); } else if (errno != EAGAIN) break; } close(fd); } else if (CurrentUser->x.x.randline) { if ((str = randstring(CurrentUser->greet))) goto single_line; return; } else { str = CurrentUser->greet; single_line: *pp = sp = (Strp*)Calloc(sizeof(Strp) + 13 + Strlen(CurrentChan->name,CurrentNick,str,NULL)); sprintf(sp->p,"PRIVMSG %s :[%s] %s",CurrentChan->name,CurrentNick,str); /* Calloc sets to zero sp->next = NULL; */ } }
int grabfile() { int zap; int i; FILE *dl; zap = -1; i = 1; while(strlen(uri)) { zap = grabline(); if(zap < 0) break; } if(zap > -1) { /* read in file and save it */ dl = fopen(local, "wb"); if(!dl) { zap = -1; perror(local); } else { while(i > -1) { i = sockread(sock, uri, 96); if(i > 0) { printf("."); fflush(stdout); zap = fwrite(uri, 1, i, dl); if(zap < 0) break; } } fclose(dl); if(zap > -1) { printf("Done\n"); } } } return(zap); }
/* * read a packet from the wire, and decrypt it. Increment the global * seq_no return NULL on failure */ u_char * read_packet(void) { HDR hdr; u_char *pkt, *data; int len; char *tkey; if (debug & DEBUG_PACKET_FLAG) report(LOG_DEBUG, "Waiting for packet"); /* read a packet header */ len = sockread(session.sock, (u_char *)&hdr, TAC_PLUS_HDR_SIZE, cfg_get_readtimeout()); if (len != TAC_PLUS_HDR_SIZE) { report(LOG_DEBUG, "Read %d bytes from %s %s, expecting %d", len, session.peer, session.port, TAC_PLUS_HDR_SIZE); return(NULL); } session.peerflags = hdr.flags; if ((hdr.version & TAC_PLUS_MAJOR_VER_MASK) != TAC_PLUS_MAJOR_VER) { report(LOG_ERR, "%s: Illegal major version specified: found %d wanted " "%d\n", session.peer, hdr.version, TAC_PLUS_MAJOR_VER); return(NULL); } /* get memory for the packet */ len = TAC_PLUS_HDR_SIZE + ntohl(hdr.datalength); if ((ntohl(hdr.datalength) & ~0xffffUL) || (len < TAC_PLUS_HDR_SIZE) || (len > 0x10000)) { report(LOG_ERR, "%s: Illegal data size: %lu\n", session.peer, ntohl(hdr.datalength)); return(NULL); } pkt = (u_char *)tac_malloc(len); /* initialise the packet */ memcpy(pkt, &hdr, TAC_PLUS_HDR_SIZE); /* the data start here */ data = pkt + TAC_PLUS_HDR_SIZE; /* read the rest of the packet data */ if (sockread(session.sock, data, ntohl(hdr.datalength), cfg_get_readtimeout()) != ntohl(hdr.datalength)) { report(LOG_ERR, "%s: start_session: bad socket read", session.peer); free(pkt); return(NULL); } session.seq_no++; /* should now equal that of incoming packet */ session.last_exch = time(NULL); if (session.seq_no != hdr.seq_no) { report(LOG_ERR, "%s: Illegal session seq # %d != packet seq # %d", session.peer, session.seq_no, hdr.seq_no); free(pkt); return(NULL); } /* decrypt the data portion */ tkey = cfg_get_host_key(session.peerip); if (tkey == NULL && !STREQ(session.peer, session.peerip)) { tkey = cfg_get_host_prompt(session.peer); } if (tkey == NULL) tkey = session.key; if (md5_xor((HDR *)pkt, data, tkey)) { report(LOG_ERR, "%s: start_session error decrypting data", session.peer); free(pkt); return(NULL); } if (debug & DEBUG_PACKET_FLAG) report(LOG_DEBUG, "Read %s size=%d", summarise_incoming_packet_type(pkt), len); session.version = hdr.version; return(pkt); }