3 * http://www.ietf.org/rfc/rfc2131.txt
10 #include <acess/sys.h>
12 #define FILENAME_MAX 255
13 // --- Translation functions ---
14 static inline uint32_t htonl(uint32_t v)
16 return (((v >> 24) & 0xFF) << 0)
17 | (((v >> 16) & 0xFF) << 8)
18 | (((v >> 8) & 0xFF) << 16)
19 | (((v >> 0) & 0xFF) << 24);
21 static inline uint16_t htons(uint16_t v)
23 return (((v >> 8) & 0xFF) << 0)
24 | (((v >> 0) & 0xFF) << 8);
27 #define ntohl(v) htonl(v)
28 #define ntohs(v) htons(v)
40 #define DHCP_MAGIC 0x63825363
44 uint8_t htype; // 1 = Ethernet
45 uint8_t hlen; // 6 bytes for MAC
46 uint8_t hops; // Hop counting
57 uint32_t dhcp_magic; // 0x63 0x82 0x53 0x63
61 typedef struct sInterface
63 struct sInterface *Next;
70 uint32_t TransactionID;
78 int main(int argc, char *argv[]);
79 void Scan_Dir(tInterface **IfaceList, const char *Directory);
80 int Start_Interface(tInterface *Iface);
81 void Send_DHCPDISCOVER(tInterface *Iface);
82 void Send_DHCPREQUEST(tInterface *Iface, void *OfferBuffer, int TypeOffset);
83 int Handle_Packet(tInterface *Iface);
84 int Handle_Timeout(tInterface *Iface);
85 void Update_State(tInterface *Iface, int newState);
86 void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router);
89 int main(int argc, char *argv[])
91 tInterface *ifaces = NULL, *i;
93 // TODO: Scan /Devices and search for network adapters
95 fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
100 ifaces = malloc(sizeof(tInterface));
102 ifaces->Adapter = argv[1];
105 Scan_Dir( &ifaces, "/Devices/ip/adapters" );
108 for( i = ifaces; i; i = i->Next )
110 if( Start_Interface(i) < 0 ) {
113 i->State = STATE_PREINIT;
116 Send_DHCPDISCOVER(i);
124 int64_t timeout = 1000;
128 for( i = ifaces; i; i = i->Next )
130 FD_SET(i->SocketFD, &fds);
131 if(maxfd < i->SocketFD) maxfd = i->SocketFD;
134 if( select(maxfd+1, &fds, NULL, NULL, &timeout) < 0 )
136 // TODO: Check error result
140 _SysDebug("select() returned");
142 // Check for changes (with magic to allow inline deletion)
143 for( p = (void*)&ifaces, i = ifaces; i; p=i,i = i->Next )
145 if( FD_ISSET(i->SocketFD, &fds) )
147 if( Handle_Packet( i ) )
151 if( _SysTimestamp() > i->Timeout )
153 if( Handle_Timeout(i) )
155 fprintf(stderr, "%s timed out\n", i->Adapter);
161 _SysClose(i->SocketFD);
162 _SysClose(i->IfaceFD);
171 void Scan_Dir(tInterface **IfaceList, const char *Directory)
173 int dp = _SysOpen(Directory, OPENFLAG_READ);
174 char filename[FILENAME_MAX];
177 fprintf(stderr, "Unable to open directory '%s'\n", Directory);
181 while( _SysReadDir(dp, filename) )
183 if( filename[0] == '.' ) continue ;
184 if( strcmp(filename, "lo") == 0 ) continue ;
186 tInterface *new = malloc(sizeof(tInterface) + strlen(filename)+1);
187 new->Adapter = (void*)(new + 1);
188 strcpy(new->Adapter, filename);
189 new->Next = *IfaceList;
196 int Start_Interface(tInterface *Iface)
199 char path[] = "/Devices/ip/XXXXX/udpc";
200 char addr[4] = {0,0,0,0};
202 // TODO: Check that the adapter is not in use
204 // Request MAC address from network adapter
206 char path[] = "/Devices/ip/adapters/ethXXXX";
207 sprintf(path, "/Devices/ip/adapters/%s", Iface->Adapter);
208 fd = _SysOpen(path, 0);
210 _SysDebug("Unable to open adapter %s", path);
213 _SysIOCtl(fd, 4, Iface->HWAddr);
214 // TODO: Check if ioctl() failed
218 // Initialise an interface, with a dummy IP address (zero)
219 fd = _SysOpen("/Devices/ip", 0);
221 fprintf(stderr, "ERROR: Unable to open '/Devices/ip'\n");
229 ifinfo.Device = Iface->Adapter;
232 Iface->Num = _SysIOCtl(fd, 4, &ifinfo); // Create interface
233 if( Iface->Num == -1 ) {
234 fprintf(stderr, "ERROR: Unable to create new interface\n");
239 // Open new interface
240 snprintf(path, sizeof(path), "/Devices/ip/%i", Iface->Num);
241 Iface->IfaceFD = fd = _SysOpen(path, 0);
243 fprintf(stderr, "ERROR: Unable to open '%s'\n", path);
246 _SysIOCtl(fd, 6, addr); // Set address to 0.0.0.0
247 tmp = 0; _SysIOCtl(fd, 7, &tmp); // Set subnet mask to 0
250 snprintf(path, sizeof(path), "/Devices/ip/%i/udp", Iface->Num);
251 Iface->SocketFD = fd = _SysOpen(path, OPENFLAG_READ|OPENFLAG_WRITE);
253 fprintf(stderr, "ERROR: Unable to open '%s'\n", path);
256 tmp = 68; _SysIOCtl(fd, 4, &tmp); // Local port
257 tmp = 67; _SysIOCtl(fd, 5, &tmp); // Remote port
258 tmp = 0; _SysIOCtl(fd, 7, &tmp); // Remote addr mask bits - we don't care where the reply comes from
259 addr[0] = addr[1] = addr[2] = addr[3] = 255; // 255.255.255.255
260 _SysIOCtl(fd, 8, addr); // Remote address
265 void Send_DHCPRELEASE(tInterface *Iface)
269 void Send_DHCPDISCOVER(tInterface *Iface)
271 uint32_t transaction_id;
272 struct sDHCP_Message *msg;
273 char data[8 + sizeof(struct sDHCP_Message) + 3 + 1];
274 msg = (void*)data + 8;
276 _SysDebug("DHCPDISCOVER to %s", Iface->Adapter);
278 transaction_id = rand();
279 Iface->TransactionID = transaction_id;
281 msg->op = htonb(1); // BOOTREQUEST
282 msg->htype = htonb(1); // 10mb Ethernet
283 msg->hlen = htonb(6); // 6 byte MAC
284 msg->hops = htonb(0); // Hop count so far
285 msg->xid = htonl(transaction_id); // Transaction ID
286 msg->secs = htons(0); // secs - No time has elapsed
287 msg->flags = htons(0x0000); // flags - Broadcast is unset
288 msg->ciaddr = htonl(0); // ciaddr - Zero, as we don't have one yet
289 msg->yiaddr = htonl(0); // yiaddr - Zero?
290 msg->siaddr = htonl(0); // siaddr - Zero? maybe -1
291 msg->giaddr = htonl(0); // giaddr - Zero?
292 memcpy(msg->chaddr, Iface->HWAddr, 6);
294 memset(msg->sname, 0, sizeof(msg->sname)); // Nuke the rest
295 memset(msg->file, 0, sizeof(msg->file)); // Nuke the rest
296 msg->dhcp_magic = htonl(DHCP_MAGIC);
299 msg->options[i++] = 53; // DHCP Message Type
300 msg->options[i++] = 1;
301 msg->options[i++] = 1; // - DHCPDISCOVER
302 msg->options[i++] = 255; // End of list
305 data[0] = 67; data[1] = 0; // Port
306 data[2] = 4; data[3] = 0; // AddrType
307 data[4] = 255; data[5] = 255; data[6] = 255; data[7] = 255;
309 i = _SysWrite(Iface->SocketFD, data, sizeof(data));
310 if( i != sizeof(data) ) {
311 _SysDebug("_SysWrite failed (%i != %i)", i, sizeof(data));
313 Update_State(Iface, STATE_DISCOVER_SENT);
316 void Send_DHCPREQUEST(tInterface *Iface, void *OfferPacket, int TypeOffset)
318 struct sDHCP_Message *msg;
320 msg = (void*) ((char*)OfferPacket) + 8;
322 // Reuses old data :)
328 msg->secs = htons(0); // TODO: Maintain times
329 msg->flags = htons(0);
330 memcpy(msg->chaddr, Iface->HWAddr, 6);
331 memset(msg->sname, 0, sizeof(msg->sname)); // Nuke the rest
332 memset(msg->file, 0, sizeof(msg->file)); // Nuke the rest
335 msg->options[i++] = 53; // Message type = DHCPREQUEST
336 msg->options[i++] = 1;
337 msg->options[i++] = 3;
338 msg->options[i++] = 50; // Requested Address
339 msg->options[i++] = 4;
340 memcpy(msg->options + i, &msg->yiaddr, 4); i += 4;
341 // msg->options[i++] = 54; // Server identifier
342 // msg->options[i++] = 4;
343 // memcpy(msg->options + i, (char*)OfferPacket + 4, 4); i += 4;
344 msg->options[i++] = 255;
346 // Clear last because yiaddr is needed in option setup
347 msg->ciaddr = htonl(0);
348 msg->yiaddr = htonl(0);
349 msg->siaddr = htonl(0);
350 msg->giaddr = htonl(0);
353 ((uint8_t*)OfferPacket)[4] = 255;
354 ((uint8_t*)OfferPacket)[5] = 255;
355 ((uint8_t*)OfferPacket)[6] = 255;
356 ((uint8_t*)OfferPacket)[7] = 255;
358 _SysWrite(Iface->SocketFD, OfferPacket, 8 + sizeof(*msg) + i);
359 Update_State(Iface, STATE_REQUEST_SENT);
362 int Handle_Packet(tInterface *Iface)
365 struct sDHCP_Message *msg = (void*)(data + 8);
368 int dhcp_msg_type = 0, dhcp_msg_type_ofs;
370 void *subnet_mask = NULL;
372 _SysDebug("Doing read on %i", Iface->SocketFD);
373 len = _SysRead(Iface->SocketFD, data, sizeof(data));
374 _SysDebug("len = %i", len);
376 _SysDebug("*msg = {");
377 _SysDebug(" .op = %i", msg->op);
378 _SysDebug(" .htype = %i", msg->htype);
379 _SysDebug(" .ciaddr = 0x%x", ntohl(msg->ciaddr));
380 _SysDebug(" .yiaddr = 0x%x", ntohl(msg->yiaddr));
384 _SysDebug("Not a response message");
388 if( htonl(msg->dhcp_magic) != DHCP_MAGIC ) {
389 _SysDebug("DHCP magic doesn't match (got 0x%x, expected 0x%x)",
390 htonl(msg->dhcp_magic), DHCP_MAGIC);
395 // Check if the packet is related to our requests
396 if( ntohl(msg->xid) != Iface->TransactionID ) {
397 _SysDebug("Transaction ID mis-match, ignoring (0x%x != 0x%x)",
398 ntohl(msg->xid), Iface->TransactionID);
401 if( memcmp(msg->chaddr, Iface->HWAddr, 6) != 0 ) {
402 _SysDebug("Hardware address mis-match, ignoring");
408 while( i < len - sizeof(*msg) - 8 && msg->options[i] != 255 )
410 if( msg->options[i] == 0 ) {
414 _SysDebug("Option %i, %i bytes long", msg->options[i], msg->options[i+1]);
415 switch(msg->options[i])
418 subnet_mask = &msg->options[i+2];
421 router = &msg->options[i+2];
424 dhcp_msg_type_ofs = i;
425 dhcp_msg_type = msg->options[i+2];
428 i += msg->options[i+1]+2;
431 _SysDebug("dhcp_msg_type = %i", dhcp_msg_type);
433 switch( dhcp_msg_type )
435 case 1: // DHCPDISCOVER - wut?
438 // Send out request for this address
439 if( Iface->State != STATE_DISCOVER_SENT ) {
440 _SysDebug("Ignoring DHCPOFFER when not in STATE_DISCOVER_SENT");
443 Send_DHCPREQUEST(Iface, data, dhcp_msg_type_ofs);
445 case 3: // DHCPREQUEST - wut?
447 case 4: // DHCPDECLINE - ?
450 SetAddress(Iface, &msg->yiaddr, subnet_mask, router);
451 // Return 1 to remove from list
457 int Handle_Timeout(tInterface *Iface)
459 if( Iface->nTimeouts == 3 )
463 case STATE_DISCOVER_SENT:
464 Send_DHCPDISCOVER(Iface);
467 _SysDebug("Timeout with state = %i", Iface->State);
473 void Update_State(tInterface *Iface, int newState)
475 if( Iface->State != newState )
477 Iface->Timeout = _SysTimestamp() + 500;
478 Iface->State = newState;
479 Iface->nTimeouts = 0;
483 // TODO: Exponential backoff
484 Iface->Timeout = _SysTimestamp() + 3000;
486 _SysDebug("State %i repeated, timeout is 3000ms now", newState);
490 void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router)
494 // Translate the mask
497 uint8_t *mask = Mask;
499 _SysDebug("Mask %i.%i.%i.%i", mask[0], mask[1], mask[2], mask[3]);
500 for( i = 0; i < 4 && mask[i] == 0xFF; i ++ ) ;
510 case 0x00: mask_bits += 0; break;
511 case 0x80: mask_bits += 1; break;
512 case 0xC0: mask_bits += 2; break;
513 case 0xE0: mask_bits += 3; break;
514 case 0xF0: mask_bits += 4; break;
515 case 0xF8: mask_bits += 5; break;
516 case 0xFC: mask_bits += 6; break;
517 case 0xFE: mask_bits += 7; break;
526 uint8_t *addr = Addr;
527 _SysDebug("Addr %i.%i.%i.%i/%i", addr[0], addr[1], addr[2], addr[3], mask_bits);
529 printf("Assigned %i.%i.%i.%i/%i to IF#%i (%s)\n",
530 addr[0], addr[1], addr[2], addr[3], mask_bits,
531 Iface->Num, Iface->Adapter
535 _SysIOCtl(Iface->IfaceFD, 6, Addr);
536 _SysIOCtl(Iface->IfaceFD, 7, &mask_bits);
540 uint8_t *addr = Router;
541 _SysDebug("Router %i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
545 fd = _SysOpen("/Devices/ip/routes/4:00000000:0:0", OPENFLAG_CREATE);
547 fprintf(stderr, "ERROR: Unable to open default route\n");
550 char ifname[snprintf(NULL,0,"%i",Iface->Num)+1];
551 sprintf(ifname, "%i", Iface->Num);
552 _SysIOCtl(fd, _SysIOCtl(fd, 3, "set_nexthop"), Router);
553 _SysIOCtl(fd, _SysIOCtl(fd, 3, "set_interface"), ifname);