mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-27 12:48:22 +00:00
netd: get dns server from dhcp
This commit is contained in:
+34
-2
@@ -13,6 +13,8 @@
|
||||
#include <errno.h>
|
||||
#include <poll.h>
|
||||
#include <net/route.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifndef SIOCADDRT
|
||||
#define SIOCADDRT 0x890B
|
||||
@@ -57,10 +59,11 @@ typedef struct {
|
||||
#define OPTION_TYPE_DHCPACK 5
|
||||
|
||||
#define OPTION_REQUEST 55
|
||||
#define OPTION_REQUEST_LENGTH 2
|
||||
#define OPTION_REQUEST_LENGTH 3
|
||||
|
||||
#define OPTION_MASK 1
|
||||
#define OPTION_ROUTER 3
|
||||
#define OPTION_DNS 6
|
||||
#define OPTION_LEASETIME 51
|
||||
#define OPTION_IPREQ 50
|
||||
#define OPTION_SERVER 54
|
||||
@@ -68,7 +71,7 @@ typedef struct {
|
||||
|
||||
static uint8_t discoveropts[] = {
|
||||
OPTION_TYPE, OPTION_TYPE_LENGTH, OPTION_TYPE_DHCPDISCOVER,
|
||||
OPTION_REQUEST, OPTION_REQUEST_LENGTH, OPTION_MASK, OPTION_ROUTER,
|
||||
OPTION_REQUEST, OPTION_REQUEST_LENGTH, OPTION_MASK, OPTION_ROUTER, OPTION_DNS,
|
||||
0xff
|
||||
};
|
||||
|
||||
@@ -223,6 +226,7 @@ typedef struct {
|
||||
uint32_t netmask;
|
||||
uint32_t router;
|
||||
uint32_t leasetime;
|
||||
uint32_t dns;
|
||||
} offer_t;
|
||||
|
||||
static int processoffer(offer_t *offer, uint32_t *xid) {
|
||||
@@ -271,6 +275,10 @@ static int processoffer(offer_t *offer, uint32_t *xid) {
|
||||
uint32_t leasetime = optget32(opts, i + 2);
|
||||
offer->leasetime = leasetime;
|
||||
break;
|
||||
case OPTION_DNS:
|
||||
uint32_t dns = optget32(opts, i + 2);
|
||||
offer->dns = dns;
|
||||
break;
|
||||
case OPTION_END:
|
||||
goto leave;
|
||||
}
|
||||
@@ -381,6 +389,7 @@ static void printoffer(offer_t *offer) {
|
||||
printip("server ip", offer->serverip);
|
||||
printip("netmask", offer->netmask);
|
||||
printip("router", offer->router);
|
||||
printip("DNS", offer->dns);
|
||||
printf("%s: lease time: %d seconds\n", name, offer->leasetime);
|
||||
}
|
||||
|
||||
@@ -422,6 +431,28 @@ static void setifip(char *device, uint32_t addr) {
|
||||
logstrerror("failed to set device ip address");
|
||||
}
|
||||
|
||||
static void writedns(uint32_t dns) {
|
||||
char buff[100];
|
||||
int fd = creat("/etc/resolv.conf", 0644);
|
||||
if (fd < 0) {
|
||||
perror("failed to create /etc/resolv.conf");
|
||||
return;
|
||||
}
|
||||
|
||||
struct in_addr addr;
|
||||
addr.s_addr = htonl(dns);
|
||||
char *ipstr = inet_ntoa(addr);
|
||||
|
||||
snprintf(buff, 100, "# generated by netd\nnameserver %s\n", ipstr);
|
||||
|
||||
if (write(fd, buff, strlen(buff)) < 0) {
|
||||
perror("failed to write to /etc/resolv.conf");
|
||||
return;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
name = argv[0];
|
||||
|
||||
@@ -486,6 +517,7 @@ int main(int argc, char *argv[]) {
|
||||
addroute(argv[1], 0, offer.router, 0, 1);
|
||||
addroute(argv[1], offer.clientip, 0, offer.netmask, 10);
|
||||
setifip(argv[1], offer.clientip);
|
||||
writedns(offer.dns);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user