5 static inline bool uip_dhcp_is_discovery(struct uip_dhcp
*dhcp
)
7 return (dhcp
->option
[2] == UIP_DHCP_DISCOVER
&&
8 dhcp
->option
[1] == UIP_DHCP_TAG_MSG_TYPE_LEN
&&
9 dhcp
->option
[0] == UIP_DHCP_TAG_MSG_TYPE
);
12 static inline bool uip_dhcp_is_request(struct uip_dhcp
*dhcp
)
14 return (dhcp
->option
[2] == UIP_DHCP_REQUEST
&&
15 dhcp
->option
[1] == UIP_DHCP_TAG_MSG_TYPE_LEN
&&
16 dhcp
->option
[0] == UIP_DHCP_TAG_MSG_TYPE
);
19 bool uip_udp_is_dhcp(struct uip_udp
*udp
)
21 struct uip_dhcp
*dhcp
;
23 if (ntohs(udp
->sport
) != UIP_DHCP_PORT_CLIENT
||
24 ntohs(udp
->dport
) != UIP_DHCP_PORT_SERVER
)
27 dhcp
= (struct uip_dhcp
*)udp
;
29 if (ntohl(dhcp
->magic_cookie
) != UIP_DHCP_MAGIC_COOKIE
)
35 int uip_dhcp_get_dns(struct uip_info
*info
)
37 char key
[256], val
[256];
44 fp
= fopen("/etc/resolv.conf", "r");
49 if (fscanf(fp
, "%s %s\n", key
, val
) != 2)
51 if (strncmp("domain", key
, 6) == 0)
52 info
->domain_name
= strndup(val
, UIP_DHCP_MAX_DOMAIN_NAME_LEN
);
53 else if (strncmp("nameserver", key
, 10) == 0) {
54 if (!inet_aton(val
, &addr
))
56 ip
= ntohl(addr
.s_addr
);
57 if (n
< UIP_DHCP_MAX_DNS_SERVER_NR
)
58 info
->dns_ip
[n
++] = ip
;
68 static int uip_dhcp_fill_option_name_and_server(struct uip_info
*info
, u8
*opt
, int i
)
74 if (info
->domain_name
) {
75 domain_name_len
= strlen(info
->domain_name
);
76 opt
[i
++] = UIP_DHCP_TAG_DOMAIN_NAME
;
77 opt
[i
++] = domain_name_len
;
78 memcpy(&opt
[i
], info
->domain_name
, domain_name_len
);
82 for (n
= 0; n
< UIP_DHCP_MAX_DNS_SERVER_NR
; n
++) {
83 if (info
->dns_ip
[n
] == 0)
85 opt
[i
++] = UIP_DHCP_TAG_DNS_SERVER
;
86 opt
[i
++] = UIP_DHCP_TAG_DNS_SERVER_LEN
;
87 addr
= (u32
*)&opt
[i
];
88 *addr
= htonl(info
->dns_ip
[n
]);
89 i
+= UIP_DHCP_TAG_DNS_SERVER_LEN
;
94 static int uip_dhcp_fill_option(struct uip_info
*info
, struct uip_dhcp
*dhcp
, int reply_msg_type
)
102 opt
[i
++] = UIP_DHCP_TAG_MSG_TYPE
;
103 opt
[i
++] = UIP_DHCP_TAG_MSG_TYPE_LEN
;
104 opt
[i
++] = reply_msg_type
;
106 opt
[i
++] = UIP_DHCP_TAG_SERVER_ID
;
107 opt
[i
++] = UIP_DHCP_TAG_SERVER_ID_LEN
;
108 addr
= (u32
*)&opt
[i
];
109 *addr
= htonl(info
->host_ip
);
110 i
+= UIP_DHCP_TAG_SERVER_ID_LEN
;
112 opt
[i
++] = UIP_DHCP_TAG_LEASE_TIME
;
113 opt
[i
++] = UIP_DHCP_TAG_LEASE_TIME_LEN
;
114 addr
= (u32
*)&opt
[i
];
115 *addr
= htonl(UIP_DHCP_LEASE_TIME
);
116 i
+= UIP_DHCP_TAG_LEASE_TIME_LEN
;
118 opt
[i
++] = UIP_DHCP_TAG_SUBMASK
;
119 opt
[i
++] = UIP_DHCP_TAG_SUBMASK_LEN
;
120 addr
= (u32
*)&opt
[i
];
121 *addr
= htonl(info
->guest_netmask
);
122 i
+= UIP_DHCP_TAG_SUBMASK_LEN
;
124 opt
[i
++] = UIP_DHCP_TAG_ROUTER
;
125 opt
[i
++] = UIP_DHCP_TAG_ROUTER_LEN
;
126 addr
= (u32
*)&opt
[i
];
127 *addr
= htonl(info
->host_ip
);
128 i
+= UIP_DHCP_TAG_ROUTER_LEN
;
130 i
= uip_dhcp_fill_option_name_and_server(info
, opt
, i
);
132 opt
[i
++] = UIP_DHCP_TAG_END
;
137 static int uip_dhcp_make_pkg(struct uip_info
*info
, struct uip_udp_socket
*sk
, struct uip_buf
*buf
, u8 reply_msg_type
)
139 struct uip_dhcp
*dhcp
;
141 dhcp
= (struct uip_dhcp
*)buf
->eth
;
145 dhcp
->your_ip
= htonl(info
->guest_ip
);
146 dhcp
->server_ip
= htonl(info
->host_ip
);
149 uip_dhcp_fill_option(info
, dhcp
, reply_msg_type
);
151 sk
->sip
= htonl(info
->guest_ip
);
152 sk
->dip
= htonl(info
->host_ip
);
153 sk
->sport
= htons(UIP_DHCP_PORT_CLIENT
);
154 sk
->dport
= htons(UIP_DHCP_PORT_SERVER
);
159 int uip_tx_do_ipv4_udp_dhcp(struct uip_tx_arg
*arg
)
161 struct uip_udp_socket sk
;
162 struct uip_dhcp
*dhcp
;
163 struct uip_info
*info
;
167 dhcp
= (struct uip_dhcp
*)arg
->eth
;
169 if (uip_dhcp_is_discovery(dhcp
))
170 reply_msg_type
= UIP_DHCP_OFFER
;
171 else if (uip_dhcp_is_request(dhcp
))
172 reply_msg_type
= UIP_DHCP_ACK
;
176 buf
= uip_buf_clone(arg
);
182 uip_dhcp_make_pkg(info
, &sk
, buf
, reply_msg_type
);
187 uip_udp_make_pkg(info
, &sk
, buf
, NULL
, UIP_DHCP_MAX_PAYLOAD_LEN
);
190 * Send data received from socket to guest
192 uip_buf_set_used(info
, buf
);