3 * http://www.ietf.org/rfc/rfc2131.txt
11 #include <acess/sys.h>
13 #define FILENAME_MAX 255
14 // --- Translation functions ---
15 static inline uint32_t htonl(uint32_t v)
17 return (((v >> 24) & 0xFF) << 0)
18 | (((v >> 16) & 0xFF) << 8)
19 | (((v >> 8) & 0xFF) << 16)
20 | (((v >> 0) & 0xFF) << 24);
22 static inline uint16_t htons(uint16_t v)
24 return (((v >> 8) & 0xFF) << 0)
25 | (((v >> 0) & 0xFF) << 8);
28 #define ntohl(v) htonl(v)
29 #define ntohs(v) htons(v)
41 #define DHCP_MAGIC 0x63825363
45 uint8_t htype; // 1 = Ethernet
46 uint8_t hlen; // 6 bytes for MAC
47 uint8_t hops; // Hop counting
58 uint32_t dhcp_magic; // 0x63 0x82 0x53 0x63
62 typedef struct sInterface
64 struct sInterface *Next;
71 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 void 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 ) )
149 _SysClose(i->SocketFD);
150 _SysClose(i->IfaceFD);
157 if( _SysTimestamp() > i->Timeout )
166 void Scan_Dir(tInterface **IfaceList, const char *Directory)
168 int dp = _SysOpen(Directory, OPENFLAG_READ);
169 char filename[FILENAME_MAX];
172 fprintf(stderr, "Unable to open directory '%s'\n", Directory);
176 while( _SysReadDir(dp, filename) )
178 if( filename[0] == '.' ) continue ;
179 if( strcmp(filename, "lo") == 0 ) continue ;
181 tInterface *new = malloc(sizeof(tInterface) + strlen(filename)+1);
182 new->Adapter = (void*)(new + 1);
183 strcpy(new->Adapter, filename);
184 new->Next = *IfaceList;
191 int Start_Interface(tInterface *Iface)
194 char path[] = "/Devices/ip/XXXXX/udpc";
195 char addr[4] = {0,0,0,0};
197 // TODO: Check that the adapter is not in use
199 // Request MAC address from network adapter
201 char path[] = "/Devices/ip/adapters/ethXXXX";
202 sprintf(path, "/Devices/ip/adapters/%s", Iface->Adapter);
203 fd = _SysOpen(path, 0);
205 _SysDebug("Unable to open adapter %s", path);
208 _SysIOCtl(fd, 4, Iface->HWAddr);
209 // TODO: Check if ioctl() failed
213 // Initialise an interface, with a dummy IP address (zero)
214 fd = _SysOpen("/Devices/ip", 0);
216 fprintf(stderr, "ERROR: Unable to open '/Devices/ip'\n");
219 Iface->Num = _SysIOCtl(fd, 4, (void*)Iface->Adapter); // Create interface
220 if( Iface->Num == -1 ) {
221 fprintf(stderr, "ERROR: Unable to create new interface\n");
226 // Open new interface
227 snprintf(path, sizeof(path), "/Devices/ip/%i", Iface->Num);
228 Iface->IfaceFD = fd = _SysOpen(path, 0);
230 fprintf(stderr, "ERROR: Unable to open '%s'\n", path);
233 tmp = 4; _SysIOCtl(fd, 4, &tmp); // Set to IPv4
234 _SysIOCtl(fd, 6, addr); // Set address to 0.0.0.0
235 tmp = 0; _SysIOCtl(fd, 7, &tmp); // Set subnet mask to 0
238 snprintf(path, sizeof(path), "/Devices/ip/%i/udp", Iface->Num);
239 Iface->SocketFD = fd = _SysOpen(path, O_RDWR);
241 fprintf(stderr, "ERROR: Unable to open '%s'\n", path);
244 tmp = 68; _SysIOCtl(fd, 4, &tmp); // Local port
245 tmp = 67; _SysIOCtl(fd, 5, &tmp); // Remote port
246 tmp = 0; _SysIOCtl(fd, 7, &tmp); // Remote addr mask bits - we don't care where the reply comes from
247 addr[0] = addr[1] = addr[2] = addr[3] = 255; // 255.255.255.255
248 _SysIOCtl(fd, 8, addr); // Remote address
253 void Send_DHCPRELEASE(tInterface *Iface)
257 void Send_DHCPDISCOVER(tInterface *Iface)
259 uint32_t transaction_id;
260 struct sDHCP_Message *msg;
261 char data[8 + sizeof(struct sDHCP_Message) + 3 + 1];
262 msg = (void*)data + 8;
264 _SysDebug("DHCPDISCOVER to %s", Iface->Adapter);
266 transaction_id = rand();
267 Iface->TransactionID = transaction_id;
269 msg->op = htonb(1); // BOOTREQUEST
270 msg->htype = htonb(1); // 10mb Ethernet
271 msg->hlen = htonb(6); // 6 byte MAC
272 msg->hops = htonb(0); // Hop count so far
273 msg->xid = htonl(transaction_id); // Transaction ID
274 msg->secs = htons(0); // secs - No time has elapsed
275 msg->flags = htons(0x0000); // flags - Broadcast is unset
276 msg->ciaddr = htonl(0); // ciaddr - Zero, as we don't have one yet
277 msg->yiaddr = htonl(0); // yiaddr - Zero?
278 msg->siaddr = htonl(0); // siaddr - Zero? maybe -1
279 msg->giaddr = htonl(0); // giaddr - Zero?
280 memcpy(msg->chaddr, Iface->HWAddr, 6);
282 memset(msg->sname, 0, sizeof(msg->sname)); // Nuke the rest
283 memset(msg->file, 0, sizeof(msg->file)); // Nuke the rest
284 msg->dhcp_magic = htonl(DHCP_MAGIC);
287 msg->options[i++] = 53; // DHCP Message Type
288 msg->options[i++] = 1;
289 msg->options[i++] = 1; // - DHCPDISCOVER
290 msg->options[i++] = 255; // End of list
293 data[0] = 67; data[1] = 0; // Port
294 data[2] = 4; data[3] = 0; // AddrType
295 data[4] = 255; data[5] = 255; data[6] = 255; data[7] = 255;
297 _SysWrite(Iface->SocketFD, data, sizeof(data));
298 Update_State(Iface, STATE_DISCOVER_SENT);
301 void Send_DHCPREQUEST(tInterface *Iface, void *OfferPacket, int TypeOffset)
303 struct sDHCP_Message *msg;
305 msg = (void*) ((char*)OfferPacket) + 8;
307 // Reuses old data :)
313 msg->secs = htons(0); // TODO: Maintain times
314 msg->flags = htons(0);
315 memcpy(msg->chaddr, Iface->HWAddr, 6);
316 memset(msg->sname, 0, sizeof(msg->sname)); // Nuke the rest
317 memset(msg->file, 0, sizeof(msg->file)); // Nuke the rest
320 msg->options[i++] = 53; // Message type = DHCPREQUEST
321 msg->options[i++] = 1;
322 msg->options[i++] = 3;
323 msg->options[i++] = 50; // Requested Address
324 msg->options[i++] = 4;
325 memcpy(msg->options + i, &msg->yiaddr, 4); i += 4;
326 // msg->options[i++] = 54; // Server identifier
327 // msg->options[i++] = 4;
328 // memcpy(msg->options + i, (char*)OfferPacket + 4, 4); i += 4;
329 msg->options[i++] = 255;
331 // Clear last because yiaddr is needed in option setup
332 msg->ciaddr = htonl(0);
333 msg->yiaddr = htonl(0);
334 msg->siaddr = htonl(0);
335 msg->giaddr = htonl(0);
338 ((uint8_t*)OfferPacket)[4] = 255;
339 ((uint8_t*)OfferPacket)[5] = 255;
340 ((uint8_t*)OfferPacket)[6] = 255;
341 ((uint8_t*)OfferPacket)[7] = 255;
343 _SysWrite(Iface->SocketFD, OfferPacket, 8 + sizeof(*msg) + i);
344 Update_State(Iface, STATE_REQUEST_SENT);
347 int Handle_Packet(tInterface *Iface)
350 struct sDHCP_Message *msg = (void*)(data + 8);
353 int dhcp_msg_type = 0, dhcp_msg_type_ofs;
355 void *subnet_mask = NULL;
357 _SysDebug("Doing read on %i", Iface->SocketFD);
358 len = _SysRead(Iface->SocketFD, data, sizeof(data));
359 _SysDebug("len = %i", len);
361 _SysDebug("*msg = {");
362 _SysDebug(" .op = %i", msg->op);
363 _SysDebug(" .htype = %i", msg->htype);
364 _SysDebug(" .ciaddr = 0x%x", ntohl(msg->ciaddr));
365 _SysDebug(" .yiaddr = 0x%x", ntohl(msg->yiaddr));
369 _SysDebug("Not a response message");
373 if( htonl(msg->dhcp_magic) != DHCP_MAGIC ) {
374 _SysDebug("DHCP magic doesn't match (got 0x%x, expected 0x%x)",
375 htonl(msg->dhcp_magic), DHCP_MAGIC);
380 // Check if the packet is related to our requests
381 if( ntohl(msg->xid) != Iface->TransactionID ) {
382 _SysDebug("Transaction ID mis-match, ignoring (0x%x != 0x%x)",
383 ntohl(msg->xid), Iface->TransactionID);
386 if( memcmp(msg->chaddr, Iface->HWAddr, 6) != 0 ) {
387 _SysDebug("Hardware address mis-match, ignoring");
393 while( i < len - sizeof(*msg) - 8 && msg->options[i] != 255 )
395 if( msg->options[i] == 0 ) {
399 _SysDebug("Option %i, %i bytes long", msg->options[i], msg->options[i+1]);
400 switch(msg->options[i])
403 subnet_mask = &msg->options[i+2];
406 router = &msg->options[i+2];
409 dhcp_msg_type_ofs = i;
410 dhcp_msg_type = msg->options[i+2];
413 i += msg->options[i+1]+2;
416 _SysDebug("dhcp_msg_type = %i", dhcp_msg_type);
418 switch( dhcp_msg_type )
420 case 1: // DHCPDISCOVER - wut?
423 // Send out request for this address
424 if( Iface->State != STATE_DISCOVER_SENT ) {
425 _SysDebug("Ignoring DHCPOFFER when not in STATE_DISCOVER_SENT");
428 Send_DHCPREQUEST(Iface, data, dhcp_msg_type_ofs);
430 case 3: // DHCPREQUEST - wut?
432 case 4: // DHCPDECLINE - ?
435 SetAddress(Iface, &msg->yiaddr, subnet_mask, router);
436 // Return 1 to remove from list
442 void Handle_Timeout(tInterface *Iface)
446 case STATE_DISCOVER_SENT:
447 Send_DHCPDISCOVER(Iface);
450 _SysDebug("Timeout with state = %i", Iface->State);
455 void Update_State(tInterface *Iface, int newState)
457 if( Iface->State != newState )
459 Iface->Timeout = _SysTimestamp() + 500;
460 Iface->State = newState;
464 // TODO: Exponential backoff
465 Iface->Timeout = _SysTimestamp() + 3000;
466 _SysDebug("State %i repeated, timeout is 3000ms now", newState);
470 void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router)
474 // Translate the mask
477 uint8_t *mask = Mask;
479 _SysDebug("Mask %i.%i.%i.%i", mask[0], mask[1], mask[2], mask[3]);
480 for( i = 0; i < 4 && mask[i] == 0xFF; i ++ ) ;
490 case 0x00: mask_bits += 0; break;
491 case 0x80: mask_bits += 1; break;
492 case 0xC0: mask_bits += 2; break;
493 case 0xE0: mask_bits += 3; break;
494 case 0xF0: mask_bits += 4; break;
495 case 0xF8: mask_bits += 5; break;
496 case 0xFC: mask_bits += 6; break;
497 case 0xFE: mask_bits += 7; break;
506 uint8_t *addr = Addr;
507 _SysDebug("Addr %i.%i.%i.%i/%i", addr[0], addr[1], addr[2], addr[3], mask_bits);
509 printf("Assigned %i.%i.%i.%i/%i to IF#%i (%s)\n",
510 addr[0], addr[1], addr[2], addr[3], mask_bits,
511 Iface->Num, Iface->Adapter
515 _SysIOCtl(Iface->IfaceFD, 6, Addr);
516 _SysIOCtl(Iface->IfaceFD, 7, &mask_bits);
520 uint8_t *addr = Router;
521 _SysDebug("Router %i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
525 fd = _SysOpen("/Devices/ip/routes/4:00000000:0:0", OPENFLAG_CREATE);
527 fprintf(stderr, "ERROR: Unable to open default route\n");
530 char ifname[snprintf(NULL,0,"%i",Iface->Num)+1];
531 sprintf(ifname, "%i", Iface->Num);
532 _SysIOCtl(fd, _SysIOCtl(fd, 3, "set_nexthop"), Router);
533 _SysIOCtl(fd, _SysIOCtl(fd, 3, "set_interface"), ifname);