X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2Firc_src%2Fmain.c;h=b4892e7975f665209d0b07ef8b9ced834a72f4a2;hb=4dcdedff0ff1494189773c2d724f76a0291ca155;hp=119e025d8bcea9b6aaf0a2ffd36d52ef5f6ae69c;hpb=0c1bf884877e4b89eb224e91627508d42ca70974;p=tpg%2Facess2.git diff --git a/Usermode/Applications/irc_src/main.c b/Usermode/Applications/irc_src/main.c index 119e025d..b4892e79 100644 --- a/Usermode/Applications/irc_src/main.c +++ b/Usermode/Applications/irc_src/main.c @@ -38,20 +38,31 @@ int main(int argc, const char *argv[], const char *envp[]) memset(&srv, 0, sizeof(srv)); + // Parse Command line + // - Sets the server configuration globals if( (tmp = ParseArguments(argc, argv)) ) { return tmp; } + // Connect to the remove server srv.FD = OpenTCP( gsRemoteAddress, giRemotePort ); if( srv.FD == -1 ) { fprintf(stderr, "Unable to create socket\n"); return -1; } + printf("Connection opened\n"); + ProcessIncoming(&srv); + writef(srv.FD, "USER %s %s %s : %s\n", gsUsername, gsHostname, gsRemoteAddress, gsRealName); - writef(srv.FD, "NICK %s", gsNickname); + writef(srv.FD, "NICK %s\n", gsNickname); - ProcessIncoming(&srv); + printf("Processing\n"); + + for( ;; ) + { + ProcessIncoming(&srv); + } close(srv.FD); return 0; @@ -81,6 +92,8 @@ char *GetValue(char *Src, int *Ofs) char *ret = Src + pos; char *end; + if( !Src ) return NULL; + while( *ret == ' ' ) ret ++; end = strchr(ret, ' '); @@ -99,7 +112,7 @@ char *GetValue(char *Src, int *Ofs) */ void ParseServerLine(tServer *Server, char *Line) { - int pos; + int pos = 0; char *ident, *cmd; if( *Line == ':' ) { // Message @@ -138,10 +151,12 @@ void ProcessIncoming(tServer *Server) // process it line by line // ioctl#8 on a TCP client gets the number of bytes in the recieve buffer // - Used to avoid blocking - while( ioctl(Server->FD, 8, NULL) ) + #if NON_BLOCK_READ + while( (len = ioctl(Server->FD, 8, NULL)) > 0 ) { + #endif // Read data - len = read(Server->FD, BUFSIZ - Server->ReadPos, Server->InBuf + Server->ReadPos); + len = read(Server->FD, BUFSIZ - Server->ReadPos, &Server->InBuf[Server->ReadPos]); Server->InBuf[Server->ReadPos + len] = '\0'; // Break into lines @@ -156,13 +171,19 @@ void ProcessIncoming(tServer *Server) // Handle incomplete lines if( ptr - Server->InBuf < len + Server->ReadPos ) { - Server->ReadPos = ptr - Server->InBuf; + // Update the read position + // InBuf ReadPos ptr ReadPos+len + // | old | new used | new unused | + Server->ReadPos = len + Server->ReadPos - (ptr - Server->InBuf); + // Copy stuff back (moving "new unused" to the start of the buffer) memcpy(Server->InBuf, ptr, Server->ReadPos); } else { Server->ReadPos = 0; } + #if NON_BLOCK_READ } + #endif } /** @@ -211,6 +232,8 @@ int OpenTCP(const char *AddressString, short PortNumber) return -1; } + printf("iface = '%s'\n", iface); + // Open client socket // TODO: Move this out to libnet? { @@ -223,16 +246,20 @@ int OpenTCP(const char *AddressString, short PortNumber) free(iface); if( fd == -1 ) { + fprintf(stderr, "Unable to open TCP Client for reading\n"); return -1; } - + // Set remote port and address + printf("Setting port and remote address\n"); ioctl(fd, 5, &PortNumber); ioctl(fd, 6, addrBuffer); // Connect + printf("Initiating connection\n"); if( ioctl(fd, 7, NULL) == 0 ) { // Shouldn't happen :( + fprintf(stderr, "Unable to start connection\n"); return -1; }