git.ucc.asn.au
/
tpg
/
acess2.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Usermode/libc++ - Fix off by one in map indexing
[tpg/acess2.git]
/
Usermode
/
Applications
/
dhcpclient_src
/
main.c
diff --git
a/Usermode/Applications/dhcpclient_src/main.c
b/Usermode/Applications/dhcpclient_src/main.c
index
aaf8de7
..
d6f4cda
100644
(file)
--- a/
Usermode/Applications/dhcpclient_src/main.c
+++ b/
Usermode/Applications/dhcpclient_src/main.c
@@
-2,13
+2,20
@@
*
* http://www.ietf.org/rfc/rfc2131.txt
*/
*
* http://www.ietf.org/rfc/rfc2131.txt
*/
-#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <net.h>
#include <acess/sys.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <net.h>
#include <acess/sys.h>
+#include <errno.h>
+
+enum {
+ UDP_IOCTL_GETSETLPORT = 4,
+ UDP_IOCTL_GETSETRPORT,
+ UDP_IOCTL_GETSETRMASK,
+ UDP_IOCTL_SETRADDR,
+};
#define FILENAME_MAX 255
// --- Translation functions ---
#define FILENAME_MAX 255
// --- Translation functions ---
@@
-70,7
+77,8
@@
typedef struct sInterface
uint32_t TransactionID;
char HWAddr[6];
uint32_t TransactionID;
char HWAddr[6];
-
+
+ int64_t StartTime;
int64_t Timeout;
int nTimeouts;
} tInterface;
int64_t Timeout;
int nTimeouts;
} tInterface;
@@
-89,9
+97,8
@@
void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router);
// === CODE ===
int main(int argc, char *argv[])
{
// === 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 <interface>\n", argv[0]);
return -1;
if( argc > 2 ) {
fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
return -1;
@@
-106,12
+113,13
@@
int main(int argc, char *argv[])
Scan_Dir( &ifaces, "/Devices/ip/adapters" );
}
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;
}
i->State = STATE_PREINIT;
{
if( Start_Interface(i) < 0 ) {
return -1;
}
i->State = STATE_PREINIT;
+ i->StartTime = _SysTimestamp();
// Send request
Send_DHCPDISCOVER(i);
// Send request
Send_DHCPDISCOVER(i);
@@
-121,27
+129,26
@@
int main(int argc, char *argv[])
{
int maxfd;
fd_set fds;
{
int maxfd;
fd_set fds;
- tInterface *p;
int64_t timeout = 1000;
maxfd = 0;
FD_ZERO(&fds);
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;
}
{
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)
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) )
{
{
if( FD_ISSET(i->SocketFD, &fds) )
{
@@
-153,7
+160,7
@@
int main(int argc, char *argv[])
{
if( Handle_Timeout(i) )
{
{
if( Handle_Timeout(i) )
{
- fprintf(stderr, "%s timed out\n", i->Adapter);
+ fprintf(stderr, "
DHCP on
%s timed out\n", i->Adapter);
goto _remove;
}
}
goto _remove;
}
}
@@
-208,11
+215,14
@@
int Start_Interface(tInterface *Iface)
sprintf(path, "/Devices/ip/adapters/%s", Iface->Adapter);
fd = _SysOpen(path, 0);
if(fd == -1) {
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;
}
_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);
}
_SysClose(fd);
}
@@
-222,7
+232,15
@@
int Start_Interface(tInterface *Iface)
fprintf(stderr, "ERROR: Unable to open '/Devices/ip'\n");
return -1;
}
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;
if( Iface->Num == -1 ) {
fprintf(stderr, "ERROR: Unable to create new interface\n");
return -1;
@@
-236,22
+254,21
@@
int Start_Interface(tInterface *Iface)
fprintf(stderr, "ERROR: Unable to open '%s'\n", path);
return -1;
}
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);
_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, O
PENFLAG_READ|OPENFLAG_WRITE
);
if( fd == -1 ) {
fprintf(stderr, "ERROR: Unable to open '%s'\n", path);
return -1;
}
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 = 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
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;
}
return 0;
}
@@
-284,6
+301,7
@@
void Send_DHCPDISCOVER(tInterface *Iface)
msg->siaddr = htonl(0); // siaddr - Zero? maybe -1
msg->giaddr = htonl(0); // giaddr - Zero?
memcpy(msg->chaddr, Iface->HWAddr, 6);
msg->siaddr = htonl(0); // siaddr - Zero? maybe -1
msg->giaddr = htonl(0); // giaddr - Zero?
memcpy(msg->chaddr, Iface->HWAddr, 6);
+ memset(msg->chaddr+6, 0, sizeof(msg->chaddr)-6);
memset(msg->sname, 0, sizeof(msg->sname)); // Nuke the rest
memset(msg->file, 0, sizeof(msg->file)); // Nuke the rest
memset(msg->sname, 0, sizeof(msg->sname)); // Nuke the rest
memset(msg->file, 0, sizeof(msg->file)); // Nuke the rest
@@
-293,14
+311,19
@@
void Send_DHCPDISCOVER(tInterface *Iface)
msg->options[i++] = 53; // DHCP Message Type
msg->options[i++] = 1;
msg->options[i++] = 1; // - DHCPDISCOVER
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;
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;
- _SysWrite(Iface->SocketFD, data, sizeof(data));
+ i = _SysWrite(Iface->SocketFD, data, sizeof(data));
+ if( i != sizeof(data) ) {
+ _SysDebug("_SysWrite failed (%i != %i): %s", i, sizeof(data),
+ strerror(errno));
+ }
Update_State(Iface, STATE_DISCOVER_SENT);
}
Update_State(Iface, STATE_DISCOVER_SENT);
}
@@
-310,14
+333,19
@@
void Send_DHCPREQUEST(tInterface *Iface, void *OfferPacket, int TypeOffset)
int i;
msg = (void*) ((char*)OfferPacket) + 8;
int i;
msg = (void*) ((char*)OfferPacket) + 8;
+ if( msg->xid != htonl(Iface->TransactionID) ) {
+ _SysDebug("DHCPREQUEST: Transaction ID mismatch");
+ return ;
+ }
+
// Reuses old data :)
// Reuses old data :)
- msg->op =
1
;
- msg->htype =
1
;
- msg->hlen =
6
;
- msg->hops =
0
;
- msg->xid =
msg->xid
;
- msg->secs = htons(
0);
// TODO: Maintain times
- msg->flags = htons(0);
+ msg->op =
htonb(1)
;
+ msg->htype =
htonb(1)
;
+ msg->hlen =
htonb(6)
;
+ msg->hops =
htonb(0)
;
+ msg->xid =
htonl(Iface->TransactionID)
;
+ msg->secs = htons(
(_SysTimestamp()-Iface->StartTime+499)/1000 );
// TODO: Maintain times
+ msg->flags = htons(0
x0000
);
memcpy(msg->chaddr, Iface->HWAddr, 6);
memset(msg->sname, 0, sizeof(msg->sname)); // Nuke the rest
memset(msg->file, 0, sizeof(msg->file)); // Nuke the rest
memcpy(msg->chaddr, Iface->HWAddr, 6);
memset(msg->sname, 0, sizeof(msg->sname)); // Nuke the rest
memset(msg->file, 0, sizeof(msg->file)); // Nuke the rest
@@
-360,7
+388,7
@@
int Handle_Packet(tInterface *Iface)
void *router = NULL;
void *subnet_mask = NULL;
void *router = NULL;
void *subnet_mask = NULL;
- _SysDebug("Doing read on %
i"
, Iface->SocketFD);
+ _SysDebug("Doing read on %
s %i", Iface->Adapter
, Iface->SocketFD);
len = _SysRead(Iface->SocketFD, data, sizeof(data));
_SysDebug("len = %i", len);
len = _SysRead(Iface->SocketFD, data, sizeof(data));
_SysDebug("len = %i", len);
@@
-452,11
+480,15
@@
int Handle_Timeout(tInterface *Iface)
switch(Iface->State)
{
case STATE_DISCOVER_SENT:
switch(Iface->State)
{
case STATE_DISCOVER_SENT:
+ fprintf(stderr, "DHCPDISCOVER timeout on %s, trying again\n", Iface->Adapter);
Send_DHCPDISCOVER(Iface);
break;
Send_DHCPDISCOVER(Iface);
break;
+ case STATE_REQUEST_SENT:
+ fprintf(stderr, "DHCPREQUEST timeout on %s\n", Iface->Adapter);
+ return 1;
default:
default:
- _SysDebug("Timeout with state = %i
", Iface->State
);
-
break
;
+ _SysDebug("Timeout with state = %i
(%s)", Iface->State, Iface->Adapter
);
+
return 1
;
}
return 0;
}
}
return 0;
}
@@
-474,7
+506,9
@@
void Update_State(tInterface *Iface, int newState)
// TODO: Exponential backoff
Iface->Timeout = _SysTimestamp() + 3000;
Iface->nTimeouts ++;
// TODO: Exponential backoff
Iface->Timeout = _SysTimestamp() + 3000;
Iface->nTimeouts ++;
- _SysDebug("State %i repeated, timeout is 3000ms now", newState);
+ _SysDebug("%s: State %i repeated, timeout is 3000ms now",
+ Iface->Adapter,
+ newState);
}
}
}
}
@@
-526,7
+560,7
@@
void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router)
_SysIOCtl(Iface->IfaceFD, 6, Addr);
_SysIOCtl(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]);
{
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