Skip to content

Commit

Permalink
Handle DHCP
Browse files Browse the repository at this point in the history
  • Loading branch information
moratorium08 committed Aug 15, 2018
1 parent 11592b0 commit 3e53da1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 19 deletions.
42 changes: 41 additions & 1 deletion dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,22 @@ void use(int x) {
ipaddrs[x].st = IPUsed;
}

int is_reserverd(unsigned long long haddr, int addr) {
IPAddr ad = ipaddrs[addr];
if (ad.st != IPReserved) return 0;
if (ad.addr != haddr) return 0;
ipaddrs[addr].st = IPUsed;
return 1;
}

int reserve(unsigned long long haddr) {
for (int i = 15; i < 256; i++) {
IPAddr ad = ipaddrs[i];
if (ad.addr == haddr) {
return ip_addr_upper * 0x100 + i;
}
if (ad.st == IPAvailable) {
ipaddrs[i].st = IPUsed;
ipaddrs[i].st = IPReserved;
ipaddrs[i].addr = haddr;
return ip_addr_upper * 0x100 + i;
}
Expand Down Expand Up @@ -77,6 +85,38 @@ DHCP *handle_discover(DHCP *d) {
}

DHCP *handle_request(DHCP *d) {
unsigned int addr = 0;
for (int i = 0; i < d->option_num; i++) {
DHCPOption op = d->options[i];
if (op.type != 50) continue;

for (int i = 0; i < 4; i++) {
addr *= 0x100;
addr += (unsigned char)(op.data[i]);
}

}

printf("%x\n", addr);
if (!is_reserverd(d->chaddr, addr & 0xff)) {
return NULL;
}
DHCP *dhcp = (DHCP *)malloc(sizeof(DHCP));
memset(dhcp, 0, sizeof(DHCP));

dhcp->type = DHCPAck;
dhcp->op = 2;
dhcp->htype = 0x1;
dhcp->hlen = 0x6;
dhcp->ops = d->ops;
dhcp->xid = d->xid;
dhcp->secs = d->secs;
dhcp->flags = d->flags;
dhcp->ciaddr = 0;
dhcp->yiaddr = addr;
dhcp->siaddr = host_ip;
dhcp->giaddr = 0;
dhcp->chaddr = d->chaddr;

return dhcp;
}
48 changes: 30 additions & 18 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,10 @@ int gen_dhcp_packet(DHCP *d, byte * buf) {
save_big_endian(0x0f04, options + 31, 2);
save_big_endian(0x686f6765, options + 33, 4);

save_big_endian(0x3604, options + 37, 2);
save_big_endian(IPADDR, options + 39, 4);


save_big_endian(0xff, options + 37, 1);
save_big_endian(0xff, options + 43, 1);
return 28 + 16 + 64 + 128 + 64;
}

Expand Down Expand Up @@ -932,6 +933,8 @@ int main(int argc, char const *argv[]) {
}
else if (e->type == ETYPE_IPv4) {
print_packet(e);


char buf[1024];
memset(buf, 0, 1024);

Expand All @@ -945,6 +948,7 @@ int main(int argc, char const *argv[]) {
e->ipv4->udp->sender_port = e->ipv4->udp->recver_port;
e->ipv4->udp->recver_port = tmp2;


UDP *udp = e->ipv4->udp;
if (udp->recver_port == 68) {
DHCP *dhcp = parse_dhcp(udp->data);
Expand All @@ -955,27 +959,35 @@ int main(int argc, char const *argv[]) {
dump_data(bufr);
switch(dhcp->type) {
case DHCPDiscover:
printf("Discover!!\n");
d = handle_discover(dhcp);
int size = gen_dhcp_packet(d, dhcp_data);
e->ipv4->udp->length = size;
e->ipv4->udp->data = dhcp_data;
break;
case DHCPRequest:
printf("Request!!\n");
d = handle_request(dhcp);
break;
default:
continue;
}
if (d == NULL) {
printf("oopps");
continue;
}
int size = gen_dhcp_packet(d, dhcp_data);
e->ipv4->udp->length = size;
e->ipv4->udp->data = dhcp_data;

e->ipv4->length = size + 36;
e->ipv4->length = size + 36;

print_dhcp(parse_dhcp(dhcp_data));
print_dhcp(parse_dhcp(dhcp_data));

e->ipv4->sender_addr = IPADDR;
e->ipv4->recver_addr = 0xffffffff;
e->ipv4->sender_addr = IPADDR;
e->ipv4->recver_addr = 0xffffffff;

gen_ethernet_packet(e, buf);
print_packet(e);
write_a_packet(buf);
dump_data(buf);
break;
default:
printf("yata-\n");
break;
}
gen_ethernet_packet(e, buf);
print_packet(e);
write_a_packet(buf);
dump_data(buf);
} else {
gen_ethernet_packet(e, buf);
write_a_packet(buf);
Expand Down

0 comments on commit 3e53da1

Please sign in to comment.