Usermode/GUITerminal - Backspace and over-greedy fix
[tpg/acess2.git] / Usermode / Applications / dhcpclient_src / main.c
index 5b122d8..73e85d8 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>
@@ -34,7 +33,7 @@ enum eStates
        STATE_PREINIT,
        STATE_DISCOVER_SENT,
        STATE_REQUEST_SENT,
-       STATE_COMPLETE
+       STATE_COMPLETE,
 };
 
 // === STRUCTURES ===
@@ -72,6 +71,7 @@ typedef struct sInterface
        char    HWAddr[6];
        
        int64_t Timeout;
+        int    nTimeouts;
 } tInterface;
 
 // === PROTOTYPES ===
@@ -80,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);
 
@@ -145,19 +145,24 @@ int main(int argc, char *argv[])
                        if( FD_ISSET(i->SocketFD, &fds) )
                        {
                                if( Handle_Packet( i ) )
-                               {
-                                       _SysClose(i->SocketFD);
-                                       _SysClose(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;
@@ -236,7 +241,7 @@ int Start_Interface(tInterface *Iface)
 
        // 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;
@@ -294,7 +299,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);
 }
 
@@ -439,8 +447,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:
@@ -450,6 +460,7 @@ void Handle_Timeout(tInterface *Iface)
                _SysDebug("Timeout with state = %i", Iface->State);
                break;
        }
+       return 0;
 }
 
 void Update_State(tInterface *Iface, int newState)
@@ -458,11 +469,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);
        }
 }
@@ -515,7 +528,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