X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2Fdhcpclient_src%2Fmain.c;h=5b1d38021ad1c54f516203dd5cc0e2a1891c8d44;hb=94d9540cae85d2f9e145fe4d2986e84b46bbd052;hp=8831cd6e55c2aff4e7daff0f304e86988d5371e9;hpb=d0b4559f2936f6d9f06be0f7c3c51527a480ec0d;p=tpg%2Facess2.git diff --git a/Usermode/Applications/dhcpclient_src/main.c b/Usermode/Applications/dhcpclient_src/main.c index 8831cd6e..5b1d3802 100644 --- a/Usermode/Applications/dhcpclient_src/main.c +++ b/Usermode/Applications/dhcpclient_src/main.c @@ -2,12 +2,12 @@ * * http://www.ietf.org/rfc/rfc2131.txt */ -#include #include #include #include #include #include +#include #define FILENAME_MAX 255 // --- Translation functions --- @@ -33,7 +33,7 @@ enum eStates STATE_PREINIT, STATE_DISCOVER_SENT, STATE_REQUEST_SENT, - STATE_COMPLETE + STATE_COMPLETE, }; // === STRUCTURES === @@ -71,6 +71,7 @@ typedef struct sInterface char HWAddr[6]; int64_t Timeout; + int nTimeouts; } tInterface; // === PROTOTYPES === @@ -79,8 +80,8 @@ void Scan_Dir(tInterface **IfaceList, const char *Directory); int Start_Interface(tInterface *Iface); void Send_DHCPDISCOVER(tInterface *Iface); void Send_DHCPREQUEST(tInterface *Iface, void *OfferBuffer, int TypeOffset); -int Handle_Packet(tInterface *Iface); -void Handle_Timeout(tInterface *Iface); + int Handle_Packet(tInterface *Iface); + int Handle_Timeout(tInterface *Iface); void Update_State(tInterface *Iface, int newState); void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router); @@ -144,19 +145,24 @@ int main(int argc, char *argv[]) if( FD_ISSET(i->SocketFD, &fds) ) { if( Handle_Packet( i ) ) - { - close(i->SocketFD); - close(i->IfaceFD); - p->Next = i->Next; - free(i); - i = p; - } + goto _remove; } if( _SysTimestamp() > i->Timeout ) { - Handle_Timeout(i); + if( Handle_Timeout(i) ) + { + fprintf(stderr, "%s timed out\n", i->Adapter); + goto _remove; + } } + continue ; + _remove: + _SysClose(i->SocketFD); + _SysClose(i->IfaceFD); + p->Next = i->Next; + free(i); + i = p; } } return 0; @@ -164,14 +170,15 @@ int main(int argc, char *argv[]) void Scan_Dir(tInterface **IfaceList, const char *Directory) { - int dp = open(Directory, OPENFLAG_READ); + int dp = _SysOpen(Directory, OPENFLAG_READ); char filename[FILENAME_MAX]; if( dp == -1 ) { fprintf(stderr, "Unable to open directory '%s'\n", Directory); + return ; } - while( SysReadDir(dp, filename) ) + while( _SysReadDir(dp, filename) ) { if( filename[0] == '.' ) continue ; if( strcmp(filename, "lo") == 0 ) continue ; @@ -182,7 +189,7 @@ void Scan_Dir(tInterface **IfaceList, const char *Directory) new->Next = *IfaceList; *IfaceList = new; } - close(dp); + _SysClose(dp); } // RETURN: Client FD @@ -198,52 +205,59 @@ int Start_Interface(tInterface *Iface) { char path[] = "/Devices/ip/adapters/ethXXXX"; sprintf(path, "/Devices/ip/adapters/%s", Iface->Adapter); - fd = open(path, 0); + fd = _SysOpen(path, 0); if(fd == -1) { _SysDebug("Unable to open adapter %s", path); return -1; } - ioctl(fd, 4, Iface->HWAddr); + _SysIOCtl(fd, 4, Iface->HWAddr); // TODO: Check if ioctl() failed - close(fd); + _SysClose(fd); } // Initialise an interface, with a dummy IP address (zero) - fd = open("/Devices/ip", 0); + fd = _SysOpen("/Devices/ip", 0); if( fd == -1 ) { fprintf(stderr, "ERROR: Unable to open '/Devices/ip'\n"); return -1; } - Iface->Num = ioctl(fd, 4, (void*)Iface->Adapter); // Create interface + struct { + const char *Device; + const char *Name; + int Type; + } ifinfo; + ifinfo.Device = Iface->Adapter; + ifinfo.Name = ""; + ifinfo.Type = 4; + Iface->Num = _SysIOCtl(fd, 4, &ifinfo); // Create interface if( Iface->Num == -1 ) { fprintf(stderr, "ERROR: Unable to create new interface\n"); return -1; } - close(fd); + _SysClose(fd); // Open new interface snprintf(path, sizeof(path), "/Devices/ip/%i", Iface->Num); - Iface->IfaceFD = fd = open(path, 0); + Iface->IfaceFD = fd = _SysOpen(path, 0); if( fd == -1 ) { fprintf(stderr, "ERROR: Unable to open '%s'\n", path); return -1; } - tmp = 4; ioctl(fd, 4, &tmp); // Set to IPv4 - ioctl(fd, 6, addr); // Set address to 0.0.0.0 - tmp = 0; ioctl(fd, 7, &tmp); // Set subnet mask to 0 + _SysIOCtl(fd, 6, addr); // Set address to 0.0.0.0 + tmp = 0; _SysIOCtl(fd, 7, &tmp); // Set subnet mask to 0 // Open UDP Client snprintf(path, sizeof(path), "/Devices/ip/%i/udp", Iface->Num); - Iface->SocketFD = fd = open(path, O_RDWR); + Iface->SocketFD = fd = _SysOpen(path, OPENFLAG_READ|OPENFLAG_WRITE); if( fd == -1 ) { fprintf(stderr, "ERROR: Unable to open '%s'\n", path); return -1; } - tmp = 68; ioctl(fd, 4, &tmp); // Local port - tmp = 67; ioctl(fd, 5, &tmp); // Remote port - tmp = 0; ioctl(fd, 7, &tmp); // Remote addr mask bits - we don't care where the reply comes from + tmp = 68; _SysIOCtl(fd, 4, &tmp); // Local port + tmp = 67; _SysIOCtl(fd, 5, &tmp); // Remote port + tmp = 0; _SysIOCtl(fd, 7, &tmp); // Remote addr mask bits - we don't care where the reply comes from addr[0] = addr[1] = addr[2] = addr[3] = 255; // 255.255.255.255 - ioctl(fd, 8, addr); // Remote address + _SysIOCtl(fd, 8, addr); // Remote address return 0; } @@ -292,7 +306,10 @@ void Send_DHCPDISCOVER(tInterface *Iface) data[2] = 4; data[3] = 0; // AddrType data[4] = 255; data[5] = 255; data[6] = 255; data[7] = 255; - write(Iface->SocketFD, data, sizeof(data)); + i = _SysWrite(Iface->SocketFD, data, sizeof(data)); + if( i != sizeof(data) ) { + _SysDebug("_SysWrite failed (%i != %i)", i, sizeof(data)); + } Update_State(Iface, STATE_DISCOVER_SENT); } @@ -338,7 +355,7 @@ void Send_DHCPREQUEST(tInterface *Iface, void *OfferPacket, int TypeOffset) ((uint8_t*)OfferPacket)[6] = 255; ((uint8_t*)OfferPacket)[7] = 255; - write(Iface->SocketFD, OfferPacket, 8 + sizeof(*msg) + i); + _SysWrite(Iface->SocketFD, OfferPacket, 8 + sizeof(*msg) + i); Update_State(Iface, STATE_REQUEST_SENT); } @@ -353,7 +370,7 @@ int Handle_Packet(tInterface *Iface) void *subnet_mask = NULL; _SysDebug("Doing read on %i", Iface->SocketFD); - len = read(Iface->SocketFD, data, sizeof(data)); + len = _SysRead(Iface->SocketFD, data, sizeof(data)); _SysDebug("len = %i", len); _SysDebug("*msg = {"); @@ -437,8 +454,10 @@ int Handle_Packet(tInterface *Iface) return 0; } -void Handle_Timeout(tInterface *Iface) +int Handle_Timeout(tInterface *Iface) { + if( Iface->nTimeouts == 3 ) + return 1; switch(Iface->State) { case STATE_DISCOVER_SENT: @@ -448,6 +467,7 @@ void Handle_Timeout(tInterface *Iface) _SysDebug("Timeout with state = %i", Iface->State); break; } + return 0; } void Update_State(tInterface *Iface, int newState) @@ -456,11 +476,13 @@ void Update_State(tInterface *Iface, int newState) { Iface->Timeout = _SysTimestamp() + 500; Iface->State = newState; + Iface->nTimeouts = 0; } else { // TODO: Exponential backoff Iface->Timeout = _SysTimestamp() + 3000; + Iface->nTimeouts ++; _SysDebug("State %i repeated, timeout is 3000ms now", newState); } } @@ -510,26 +532,26 @@ void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router) ); } - ioctl(Iface->IfaceFD, 6, Addr); - ioctl(Iface->IfaceFD, 7, &mask_bits); + _SysIOCtl(Iface->IfaceFD, 6, Addr); + _SysIOCtl(Iface->IfaceFD, 7, &mask_bits); - if( Router ); + if( Router ) { uint8_t *addr = Router; _SysDebug("Router %i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]); // Set default route int fd; - fd = open("/Devices/ip/routes/4:00000000:0:0", OPENFLAG_CREATE); + fd = _SysOpen("/Devices/ip/routes/4:00000000:0:0", OPENFLAG_CREATE); if(fd == -1) { fprintf(stderr, "ERROR: Unable to open default route\n"); } else { char ifname[snprintf(NULL,0,"%i",Iface->Num)+1]; sprintf(ifname, "%i", Iface->Num); - ioctl(fd, ioctl(fd, 3, "set_nexthop"), Router); - ioctl(fd, ioctl(fd, 3, "set_interface"), ifname); - close(fd); + _SysIOCtl(fd, _SysIOCtl(fd, 3, "set_nexthop"), Router); + _SysIOCtl(fd, _SysIOCtl(fd, 3, "set_interface"), ifname); + _SysClose(fd); } } }