X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2Fdhcpclient_src%2Fmain.c;h=affe1fd171706cc54aa861b09d74d1b31222f9c2;hb=5393d8b418bbfe48509ef3f40070175bca9bf71a;hp=73e85d8ef5c731b41f6df35fc7258a3e0cf365d8;hpb=b0da731b2d89b9dd58de2c98eaf6218a41a21920;p=tpg%2Facess2.git diff --git a/Usermode/Applications/dhcpclient_src/main.c b/Usermode/Applications/dhcpclient_src/main.c index 73e85d8e..affe1fd1 100644 --- a/Usermode/Applications/dhcpclient_src/main.c +++ b/Usermode/Applications/dhcpclient_src/main.c @@ -8,6 +8,14 @@ #include #include #include +#include + +enum { + UDP_IOCTL_GETSETLPORT = 4, + UDP_IOCTL_GETSETRPORT, + UDP_IOCTL_GETSETRMASK, + UDP_IOCTL_SETRADDR, +}; #define FILENAME_MAX 255 // --- Translation functions --- @@ -88,9 +96,8 @@ void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router); // === CODE === int main(int argc, char *argv[]) { - tInterface *ifaces = NULL, *i; + tInterface *ifaces = NULL; - // TODO: Scan /Devices and search for network adapters if( argc > 2 ) { fprintf(stderr, "Usage: %s \n", argv[0]); return -1; @@ -105,7 +112,7 @@ int main(int argc, char *argv[]) Scan_Dir( &ifaces, "/Devices/ip/adapters" ); } - for( i = ifaces; i; i = i->Next ) + for( tInterface *i = ifaces; i; i = i->Next ) { if( Start_Interface(i) < 0 ) { return -1; @@ -120,27 +127,26 @@ int main(int argc, char *argv[]) { int maxfd; fd_set fds; - tInterface *p; int64_t timeout = 1000; maxfd = 0; FD_ZERO(&fds); - for( i = ifaces; i; i = i->Next ) + for( tInterface *i = ifaces; i; i = i->Next ) { FD_SET(i->SocketFD, &fds); if(maxfd < i->SocketFD) maxfd = i->SocketFD; } - if( select(maxfd+1, &fds, NULL, NULL, &timeout) < 0 ) + if( _SysSelect(maxfd+1, &fds, NULL, NULL, &timeout, 0) < 0 ) { - // TODO: Check error result + perror("_SysSelect returned error"); return -1; } _SysDebug("select() returned"); // Check for changes (with magic to allow inline deletion) - for( p = (void*)&ifaces, i = ifaces; i; p=i,i = i->Next ) + for( tInterface *p = (void*)&ifaces, *i = ifaces; i; p=i,i = i->Next ) { if( FD_ISSET(i->SocketFD, &fds) ) { @@ -152,7 +158,7 @@ int main(int argc, char *argv[]) { if( Handle_Timeout(i) ) { - fprintf(stderr, "%s timed out\n", i->Adapter); + fprintf(stderr, "DHCP on %s timed out\n", i->Adapter); goto _remove; } } @@ -207,11 +213,14 @@ int Start_Interface(tInterface *Iface) sprintf(path, "/Devices/ip/adapters/%s", Iface->Adapter); fd = _SysOpen(path, 0); if(fd == -1) { + perror("Opening adapter"); _SysDebug("Unable to open adapter %s", path); return -1; } - _SysIOCtl(fd, 4, Iface->HWAddr); - // TODO: Check if ioctl() failed + if( _SysIOCtl(fd, 4, Iface->HWAddr) ) { + perror("Getting MAC address"); + return -1; + } _SysClose(fd); } @@ -221,7 +230,15 @@ int Start_Interface(tInterface *Iface) fprintf(stderr, "ERROR: Unable to open '/Devices/ip'\n"); return -1; } - Iface->Num = _SysIOCtl(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; @@ -235,7 +252,6 @@ int Start_Interface(tInterface *Iface) fprintf(stderr, "ERROR: Unable to open '%s'\n", path); return -1; } - tmp = 4; _SysIOCtl(fd, 4, &tmp); // Set to IPv4 _SysIOCtl(fd, 6, addr); // Set address to 0.0.0.0 tmp = 0; _SysIOCtl(fd, 7, &tmp); // Set subnet mask to 0 @@ -246,11 +262,11 @@ int Start_Interface(tInterface *Iface) fprintf(stderr, "ERROR: Unable to open '%s'\n", path); return -1; } - 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 + tmp = 68; _SysIOCtl(fd, UDP_IOCTL_GETSETLPORT, &tmp); // Local port + tmp = 67; _SysIOCtl(fd, UDP_IOCTL_GETSETRPORT, &tmp); // Remote port + tmp = 0; _SysIOCtl(fd, UDP_IOCTL_GETSETRMASK, &tmp); // Remote addr mask bits - don't care source addr[0] = addr[1] = addr[2] = addr[3] = 255; // 255.255.255.255 - _SysIOCtl(fd, 8, addr); // Remote address + _SysIOCtl(fd, UDP_IOCTL_SETRADDR, addr); // Remote address return 0; } @@ -292,16 +308,18 @@ void Send_DHCPDISCOVER(tInterface *Iface) msg->options[i++] = 53; // DHCP Message Type msg->options[i++] = 1; msg->options[i++] = 1; // - DHCPDISCOVER - msg->options[i++] = 255; // End of list + msg->options[i ] = 255; // End of list + // UDP Header data[0] = 67; data[1] = 0; // Port data[2] = 4; data[3] = 0; // AddrType data[4] = 255; data[5] = 255; data[6] = 255; data[7] = 255; i = _SysWrite(Iface->SocketFD, data, sizeof(data)); if( i != sizeof(data) ) { - _SysDebug("_SysWrite failed (%i != %i)", i, sizeof(data)); + _SysDebug("_SysWrite failed (%i != %i): %s", i, sizeof(data), + strerror(errno)); } Update_State(Iface, STATE_DISCOVER_SENT); }