Usermode/AxWin3 - Cleaning up and separating code
[tpg/acess2.git] / Usermode / Applications / dhcpclient_src / main.c
index aaf8de7..858a61d 100644 (file)
@@ -2,7 +2,6 @@
  * 
  * http://www.ietf.org/rfc/rfc2131.txt
  */
-#include <unistd.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
@@ -91,7 +90,6 @@ int main(int argc, char *argv[])
 {
        tInterface      *ifaces = NULL, *i;
 
-       // TODO: Scan /Devices and search for network adapters
        if( argc > 2 ) {
                fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
                return -1;
@@ -132,9 +130,9 @@ int main(int argc, char *argv[])
                        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;
                }
 
@@ -208,11 +206,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);
        }
        
@@ -222,7 +223,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;
@@ -236,20 +245,19 @@ 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
 
        // Open UDP Client
        snprintf(path, sizeof(path), "/Devices/ip/%i/udp", Iface->Num);
-       Iface->SocketFD = fd = _SysOpen(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; _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 = 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
        _SysIOCtl(fd, 8, addr); // Remote address
        
@@ -300,7 +308,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;
 
-       _SysWrite(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);
 }
 
@@ -526,7 +537,7 @@ void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router)
        _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]);

UCC git Repository :: git.ucc.asn.au