3 * http://www.ietf.org/rfc/rfc2131.txt
11 #define FILENAME_MAX 255
12 // --- Translation functions ---
13 static inline uint32_t htonl(uint32_t v)
15 return (((v >> 24) & 0xFF) << 0)
16 | (((v >> 16) & 0xFF) << 8)
17 | (((v >> 8) & 0xFF) << 16)
18 | (((v >> 0) & 0xFF) << 24);
20 static inline uint16_t htons(uint16_t v)
22 return (((v >> 8) & 0xFF) << 0)
23 | (((v >> 0) & 0xFF) << 8);
26 #define ntohl(v) htonl(v)
27 #define ntohs(v) htons(v)
39 #define DHCP_MAGIC 0x63825363
43 uint8_t htype; // 1 = Ethernet
44 uint8_t hlen; // 6 bytes for MAC
45 uint8_t hops; // Hop counting
56 uint32_t dhcp_magic; // 0x63 0x82 0x53 0x63
60 typedef struct sInterface
62 struct sInterface *Next;
69 uint32_t TransactionID;
76 int main(int argc, char *argv[]);
77 void Scan_Dir(tInterface **IfaceList, const char *Directory);
78 int Start_Interface(tInterface *Iface);
79 void Send_DHCPDISCOVER(tInterface *Iface);
80 void Send_DHCPREQUEST(tInterface *Iface, void *OfferBuffer, int TypeOffset);
81 int Handle_Packet(tInterface *Iface);
82 void Handle_Timeout(tInterface *Iface);
83 void Update_State(tInterface *Iface, int newState);
84 void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router);
87 int main(int argc, char *argv[])
89 tInterface *ifaces = NULL, *i;
91 // TODO: Scan /Devices and search for network adapters
93 fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
98 ifaces = malloc(sizeof(tInterface));
100 ifaces->Adapter = argv[1];
103 Scan_Dir( &ifaces, "/Devices/ip/adapters" );
106 for( i = ifaces; i; i = i->Next )
108 if( Start_Interface(i) < 0 ) {
111 i->State = STATE_PREINIT;
114 Send_DHCPDISCOVER(i);
122 int64_t timeout = 1000;
126 for( i = ifaces; i; i = i->Next )
128 FD_SET(i->SocketFD, &fds);
129 if(maxfd < i->SocketFD) maxfd = i->SocketFD;
132 if( select(maxfd+1, &fds, NULL, NULL, &timeout) < 0 )
134 // TODO: Check error result
138 _SysDebug("select() returned");
140 // Check for changes (with magic to allow inline deletion)
141 for( p = (void*)&ifaces, i = ifaces; i; p=i,i = i->Next )
143 if( FD_ISSET(i->SocketFD, &fds) )
145 if( Handle_Packet( i ) )
155 if( _SysTimestamp() > i->Timeout )
164 void Scan_Dir(tInterface **IfaceList, const char *Directory)
166 int dp = open(Directory, OPENFLAG_READ);
167 char filename[FILENAME_MAX];
170 fprintf(stderr, "Unable to open directory '%s'\n", Directory);
173 while( readdir(dp, filename) )
175 if( filename[0] == '.' ) continue ;
176 if( strcmp(filename, "lo") == 0 ) continue ;
178 tInterface *new = malloc(sizeof(tInterface) + strlen(filename)+1);
179 new->Adapter = (void*)(new + 1);
180 strcpy(new->Adapter, filename);
181 new->Next = *IfaceList;
188 int Start_Interface(tInterface *Iface)
191 char path[] = "/Devices/ip/XXXXX/udpc";
192 char addr[4] = {0,0,0,0};
194 // TODO: Check that the adapter is not in use
196 // Request MAC address from network adapter
198 char path[] = "/Devices/ip/adapters/ethXXXX";
199 sprintf(path, "/Devices/ip/adapters/%s", Iface->Adapter);
202 _SysDebug("Unable to open adapter %s", path);
205 ioctl(fd, 4, Iface->HWAddr);
206 // TODO: Check if ioctl() failed
210 // Initialise an interface, with a dummy IP address (zero)
211 fd = open("/Devices/ip", 0);
213 fprintf(stderr, "ERROR: Unable to open '/Devices/ip'\n");
216 Iface->Num = ioctl(fd, 4, (void*)Iface->Adapter); // Create interface
217 if( Iface->Num == -1 ) {
218 fprintf(stderr, "ERROR: Unable to create new interface\n");
223 // Open new interface
224 snprintf(path, sizeof(path), "/Devices/ip/%i", Iface->Num);
225 Iface->IfaceFD = fd = open(path, 0);
227 fprintf(stderr, "ERROR: Unable to open '%s'\n", path);
230 tmp = 4; ioctl(fd, 4, &tmp); // Set to IPv4
231 ioctl(fd, 6, addr); // Set address to 0.0.0.0
232 tmp = 0; ioctl(fd, 7, &tmp); // Set subnet mask to 0
235 snprintf(path, sizeof(path), "/Devices/ip/%i/udp", Iface->Num);
236 Iface->SocketFD = fd = open(path, O_RDWR);
238 fprintf(stderr, "ERROR: Unable to open '%s'\n", path);
241 tmp = 68; ioctl(fd, 4, &tmp); // Local port
242 tmp = 67; ioctl(fd, 5, &tmp); // Remote port
243 tmp = 0; ioctl(fd, 7, &tmp); // Remote addr mask bits - we don't care where the reply comes from
244 addr[0] = addr[1] = addr[2] = addr[3] = 255; // 255.255.255.255
245 ioctl(fd, 8, addr); // Remote address
250 void Send_DHCPRELEASE(tInterface *Iface)
254 void Send_DHCPDISCOVER(tInterface *Iface)
256 uint32_t transaction_id;
257 struct sDHCP_Message *msg;
258 char data[8 + sizeof(struct sDHCP_Message) + 3 + 1];
259 msg = (void*)data + 8;
261 _SysDebug("DHCPDISCOVER to %s", Iface->Adapter);
263 transaction_id = rand();
264 Iface->TransactionID = transaction_id;
266 msg->op = htonb(1); // BOOTREQUEST
267 msg->htype = htonb(1); // 10mb Ethernet
268 msg->hlen = htonb(6); // 6 byte MAC
269 msg->hops = htonb(0); // Hop count so far
270 msg->xid = htonl(transaction_id); // Transaction ID
271 msg->secs = htons(0); // secs - No time has elapsed
272 msg->flags = htons(0x0000); // flags - Broadcast is unset
273 msg->ciaddr = htonl(0); // ciaddr - Zero, as we don't have one yet
274 msg->yiaddr = htonl(0); // yiaddr - Zero?
275 msg->siaddr = htonl(0); // siaddr - Zero? maybe -1
276 msg->giaddr = htonl(0); // giaddr - Zero?
277 memcpy(msg->chaddr, Iface->HWAddr, 6);
279 memset(msg->sname, 0, sizeof(msg->sname)); // Nuke the rest
280 memset(msg->file, 0, sizeof(msg->file)); // Nuke the rest
281 msg->dhcp_magic = htonl(DHCP_MAGIC);
284 msg->options[i++] = 53; // DHCP Message Type
285 msg->options[i++] = 1;
286 msg->options[i++] = 1; // - DHCPDISCOVER
287 msg->options[i++] = 255; // End of list
290 data[0] = 67; data[1] = 0; // Port
291 data[2] = 4; data[3] = 0; // AddrType
292 data[4] = 255; data[5] = 255; data[6] = 255; data[7] = 255;
294 write(Iface->SocketFD, data, sizeof(data));
295 Update_State(Iface, STATE_DISCOVER_SENT);
298 void Send_DHCPREQUEST(tInterface *Iface, void *OfferPacket, int TypeOffset)
300 struct sDHCP_Message *msg;
302 msg = (void*) ((char*)OfferPacket) + 8;
304 // Reuses old data :)
310 msg->secs = htons(0); // TODO: Maintain times
311 msg->flags = htons(0);
312 memcpy(msg->chaddr, Iface->HWAddr, 6);
313 memset(msg->sname, 0, sizeof(msg->sname)); // Nuke the rest
314 memset(msg->file, 0, sizeof(msg->file)); // Nuke the rest
317 msg->options[i++] = 53; // Message type = DHCPREQUEST
318 msg->options[i++] = 1;
319 msg->options[i++] = 3;
320 msg->options[i++] = 50; // Requested Address
321 msg->options[i++] = 4;
322 memcpy(msg->options + i, &msg->yiaddr, 4); i += 4;
323 // msg->options[i++] = 54; // Server identifier
324 // msg->options[i++] = 4;
325 // memcpy(msg->options + i, (char*)OfferPacket + 4, 4); i += 4;
326 msg->options[i++] = 255;
328 // Clear last because yiaddr is needed in option setup
329 msg->ciaddr = htonl(0);
330 msg->yiaddr = htonl(0);
331 msg->siaddr = htonl(0);
332 msg->giaddr = htonl(0);
335 ((uint8_t*)OfferPacket)[4] = 255;
336 ((uint8_t*)OfferPacket)[5] = 255;
337 ((uint8_t*)OfferPacket)[6] = 255;
338 ((uint8_t*)OfferPacket)[7] = 255;
340 write(Iface->SocketFD, OfferPacket, 8 + sizeof(*msg) + i);
341 Update_State(Iface, STATE_REQUEST_SENT);
344 int Handle_Packet(tInterface *Iface)
347 struct sDHCP_Message *msg = (void*)(data + 8);
350 int dhcp_msg_type = 0, dhcp_msg_type_ofs;
352 void *subnet_mask = NULL;
354 _SysDebug("Doing read on %i", Iface->SocketFD);
355 len = read(Iface->SocketFD, data, sizeof(data));
356 _SysDebug("len = %i", len);
358 _SysDebug("*msg = {");
359 _SysDebug(" .op = %i", msg->op);
360 _SysDebug(" .htype = %i", msg->htype);
361 _SysDebug(" .ciaddr = 0x%x", ntohl(msg->ciaddr));
362 _SysDebug(" .yiaddr = 0x%x", ntohl(msg->yiaddr));
366 _SysDebug("Not a response message");
370 if( htonl(msg->dhcp_magic) != DHCP_MAGIC ) {
371 _SysDebug("DHCP magic doesn't match (got 0x%x, expected 0x%x)",
372 htonl(msg->dhcp_magic), DHCP_MAGIC);
377 // Check if the packet is related to our requests
378 if( ntohl(msg->xid) != Iface->TransactionID ) {
379 _SysDebug("Transaction ID mis-match, ignoring (0x%x != 0x%x)",
380 ntohl(msg->xid), Iface->TransactionID);
383 if( memcmp(msg->chaddr, Iface->HWAddr, 6) != 0 ) {
384 _SysDebug("Hardware address mis-match, ignoring");
390 while( i < len - sizeof(*msg) - 8 && msg->options[i] != 255 )
392 if( msg->options[i] == 0 ) {
396 _SysDebug("Option %i, %i bytes long", msg->options[i], msg->options[i+1]);
397 switch(msg->options[i])
400 subnet_mask = &msg->options[i+2];
403 router = &msg->options[i+2];
406 dhcp_msg_type_ofs = i;
407 dhcp_msg_type = msg->options[i+2];
410 i += msg->options[i+1]+2;
413 _SysDebug("dhcp_msg_type = %i", dhcp_msg_type);
415 switch( dhcp_msg_type )
417 case 1: // DHCPDISCOVER - wut?
420 // Send out request for this address
421 if( Iface->State != STATE_DISCOVER_SENT ) {
422 _SysDebug("Ignoring DHCPOFFER when not in STATE_DISCOVER_SENT");
425 Send_DHCPREQUEST(Iface, data, dhcp_msg_type_ofs);
427 case 3: // DHCPREQUEST - wut?
429 case 4: // DHCPDECLINE - ?
432 SetAddress(Iface, &msg->yiaddr, subnet_mask, router);
433 // Return 1 to remove from list
439 void Handle_Timeout(tInterface *Iface)
443 case STATE_DISCOVER_SENT:
444 Send_DHCPDISCOVER(Iface);
447 _SysDebug("Timeout with state = %i", Iface->State);
452 void Update_State(tInterface *Iface, int newState)
454 if( Iface->State != newState )
456 Iface->Timeout = _SysTimestamp() + 500;
457 Iface->State = newState;
461 // TODO: Exponential backoff
462 Iface->Timeout = _SysTimestamp() + 3000;
463 _SysDebug("State %i repeated, timeout is 3000ms now", newState);
467 void SetAddress(tInterface *Iface, void *Addr, void *Mask, void *Router)
471 // Translate the mask
474 uint8_t *mask = Mask;
476 _SysDebug("Mask %i.%i.%i.%i", mask[0], mask[1], mask[2], mask[3]);
477 for( i = 0; i < 4 && mask[i] == 0xFF; i ++ ) ;
487 case 0x00: mask_bits += 0; break;
488 case 0x80: mask_bits += 1; break;
489 case 0xC0: mask_bits += 2; break;
490 case 0xE0: mask_bits += 3; break;
491 case 0xF0: mask_bits += 4; break;
492 case 0xF8: mask_bits += 5; break;
493 case 0xFC: mask_bits += 6; break;
494 case 0xFE: mask_bits += 7; break;
503 uint8_t *addr = Addr;
504 _SysDebug("Addr %i.%i.%i.%i/%i", addr[0], addr[1], addr[2], addr[3], mask_bits);
506 printf("Assigned %i.%i.%i.%i/%i to IF#%i (%s)\n",
507 addr[0], addr[1], addr[2], addr[3], mask_bits,
508 Iface->Num, Iface->Adapter
512 ioctl(Iface->IfaceFD, 6, Addr);
513 ioctl(Iface->IfaceFD, 7, &mask_bits);
517 uint8_t *addr = Router;
518 _SysDebug("Router %i.%i.%i.%i", addr[0], addr[1], addr[2], addr[3]);
522 fd = open("/Devices/ip/routes/4:00000000:0:0", OPENFLAG_CREATE);
524 fprintf(stderr, "ERROR: Unable to open default route\n");
527 char ifname[snprintf(NULL,0,"%i",Iface->Num)+1];
528 sprintf(ifname, "%i", Iface->Num);
529 ioctl(fd, ioctl(fd, 3, "set_nexthop"), Router);
530 ioctl(fd, ioctl(fd, 3, "set_interface"), ifname);