Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* Port to Busybox Copyright (C) 2006 Jesse Dutton <jessedutton@gmail.com> |
| 3 | * |
| 4 | * Licensed under GPL v2, see file LICENSE in this tarball for details. |
| 5 | * |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 6 | * DHCP Relay for 'DHCPv4 Configuration of IPSec Tunnel Mode' support |
| 7 | * Copyright (C) 2002 Mario Strasser <mast@gmx.net>, |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 8 | * Zuercher Hochschule Winterthur, |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 9 | * Netbeat AG |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 10 | * Upstream has GPL v2 or later |
| 11 | */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 12 | #include "common.h" |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 14 | #define SERVER_PORT 67 |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 15 | #define SELECT_TIMEOUT 5 /* select timeout in sec. */ |
| 16 | #define MAX_LIFETIME 2*60 /* lifetime of an xid entry in sec. */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 17 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 18 | /* This list holds information about clients. The xid_* functions manipulate this list. */ |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 19 | struct xid_item { |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 20 | unsigned timestamp; |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 21 | int client; |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 22 | uint32_t xid; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 23 | struct sockaddr_in ip; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 24 | struct xid_item *next; |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 25 | }; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 26 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 27 | #define dhcprelay_xid_list (*(struct xid_item*)&bb_common_bufsiz1) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 28 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 29 | static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 30 | { |
| 31 | struct xid_item *item; |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 33 | /* create new xid entry */ |
| 34 | item = xmalloc(sizeof(struct xid_item)); |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 35 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 36 | /* add xid entry */ |
| 37 | item->ip = *ip; |
| 38 | item->xid = xid; |
| 39 | item->client = client; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 40 | item->timestamp = monotonic_sec(); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 41 | item->next = dhcprelay_xid_list.next; |
| 42 | dhcprelay_xid_list.next = item; |
| 43 | |
| 44 | return item; |
| 45 | } |
| 46 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 47 | static void xid_expire(void) |
| 48 | { |
| 49 | struct xid_item *item = dhcprelay_xid_list.next; |
| 50 | struct xid_item *last = &dhcprelay_xid_list; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 51 | unsigned current_time = monotonic_sec(); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 52 | |
| 53 | while (item != NULL) { |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 54 | if ((current_time - item->timestamp) > MAX_LIFETIME) { |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 55 | last->next = item->next; |
| 56 | free(item); |
| 57 | item = last->next; |
| 58 | } else { |
| 59 | last = item; |
| 60 | item = item->next; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 65 | static struct xid_item *xid_find(uint32_t xid) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 66 | { |
| 67 | struct xid_item *item = dhcprelay_xid_list.next; |
| 68 | while (item != NULL) { |
| 69 | if (item->xid == xid) { |
| 70 | return item; |
| 71 | } |
| 72 | item = item->next; |
| 73 | } |
| 74 | return NULL; |
| 75 | } |
| 76 | |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 77 | static void xid_del(uint32_t xid) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 78 | { |
| 79 | struct xid_item *item = dhcprelay_xid_list.next; |
| 80 | struct xid_item *last = &dhcprelay_xid_list; |
| 81 | while (item != NULL) { |
| 82 | if (item->xid == xid) { |
| 83 | last->next = item->next; |
| 84 | free(item); |
| 85 | item = last->next; |
| 86 | } else { |
| 87 | last = item; |
| 88 | item = item->next; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 93 | /** |
| 94 | * get_dhcp_packet_type - gets the message type of a dhcp packet |
| 95 | * p - pointer to the dhcp packet |
| 96 | * returns the message type on success, -1 otherwise |
| 97 | */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 98 | static int get_dhcp_packet_type(struct dhcp_packet *p) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 99 | { |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 100 | uint8_t *op; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 101 | |
| 102 | /* it must be either a BOOTREQUEST or a BOOTREPLY */ |
| 103 | if (p->op != BOOTREQUEST && p->op != BOOTREPLY) |
| 104 | return -1; |
| 105 | /* get message type option */ |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 106 | op = udhcp_get_option(p, DHCP_MESSAGE_TYPE); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 107 | if (op != NULL) |
| 108 | return op[0]; |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | /** |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 113 | * get_client_devices - parses the devices list |
| 114 | * dev_list - comma separated list of devices |
| 115 | * returns array |
| 116 | */ |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 117 | static char **get_client_devices(char *dev_list, int *client_number) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 118 | { |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 119 | char *s, **client_dev; |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 120 | int i, cn; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 121 | |
| 122 | /* copy list */ |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 123 | dev_list = xstrdup(dev_list); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 124 | |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 125 | /* get number of items, replace ',' with NULs */ |
| 126 | s = dev_list; |
| 127 | cn = 1; |
| 128 | while (*s) { |
| 129 | if (*s == ',') { |
| 130 | *s = '\0'; |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 131 | cn++; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 132 | } |
| 133 | s++; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 134 | } |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 135 | *client_number = cn; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 136 | |
| 137 | /* create vector of pointers */ |
| 138 | client_dev = xzalloc(cn * sizeof(*client_dev)); |
| 139 | client_dev[0] = dev_list; |
| 140 | i = 1; |
| 141 | while (i != cn) { |
| 142 | client_dev[i] = client_dev[i - 1] + strlen(client_dev[i - 1]) + 1; |
| 143 | i++; |
| 144 | } |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 145 | return client_dev; |
| 146 | } |
| 147 | |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 148 | /* Creates listen sockets (in fds) bound to client and server ifaces, |
| 149 | * and returns numerically max fd. |
| 150 | */ |
| 151 | static int init_sockets(char **client_ifaces, int num_clients, |
| 152 | char *server_iface, int *fds) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 153 | { |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 154 | int i, n; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 155 | |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 156 | /* talk to real server on bootps */ |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 157 | fds[0] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, server_iface); |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 158 | n = fds[0]; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 159 | |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 160 | for (i = 1; i < num_clients; i++) { |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 161 | /* listen for clients on bootps */ |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 162 | fds[i] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, client_ifaces[i-1]); |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 163 | if (fds[i] > n) |
| 164 | n = fds[i]; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 165 | } |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 166 | return n; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 169 | /** |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 170 | * pass_to_server() - forwards dhcp packets from client to server |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 171 | * p - packet to send |
| 172 | * client - number of the client |
| 173 | */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 174 | static void pass_to_server(struct dhcp_packet *p, int packet_len, int client, int *fds, |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 175 | struct sockaddr_in *client_addr, struct sockaddr_in *server_addr) |
| 176 | { |
| 177 | int res, type; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 178 | |
| 179 | /* check packet_type */ |
| 180 | type = get_dhcp_packet_type(p); |
| 181 | if (type != DHCPDISCOVER && type != DHCPREQUEST |
| 182 | && type != DHCPDECLINE && type != DHCPRELEASE |
| 183 | && type != DHCPINFORM |
| 184 | ) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | /* create new xid entry */ |
Denys Vlasenko | 243d175 | 2010-07-04 04:26:55 +0200 | [diff] [blame] | 189 | xid_add(p->xid, client_addr, client); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 190 | |
| 191 | /* forward request to LAN (server) */ |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 192 | errno = 0; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 193 | res = sendto(fds[0], p, packet_len, 0, (struct sockaddr*)server_addr, |
| 194 | sizeof(struct sockaddr_in)); |
| 195 | if (res != packet_len) { |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 196 | bb_perror_msg("sendto"); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 201 | * pass_to_client() - forwards dhcp packets from server to client |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 202 | * p - packet to send |
| 203 | */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 204 | static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 205 | { |
| 206 | int res, type; |
| 207 | struct xid_item *item; |
| 208 | |
| 209 | /* check xid */ |
| 210 | item = xid_find(p->xid); |
| 211 | if (!item) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | /* check packet type */ |
| 216 | type = get_dhcp_packet_type(p); |
| 217 | if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY)) |
| 222 | item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST); |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 223 | errno = 0; |
| 224 | res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*) &(item->ip), |
| 225 | sizeof(item->ip)); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 226 | if (res != packet_len) { |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 227 | bb_perror_msg("sendto"); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | |
| 231 | /* remove xid entry */ |
| 232 | xid_del(p->xid); |
| 233 | } |
| 234 | |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 235 | int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 236 | int dhcprelay_main(int argc, char **argv) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 237 | { |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 238 | struct dhcp_packet dhcp_msg; |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 239 | struct sockaddr_in server_addr; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 240 | struct sockaddr_in client_addr; |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 241 | fd_set rfds; |
| 242 | char **client_ifaces; |
| 243 | int *fds; |
| 244 | int num_sockets, max_socket; |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 245 | uint32_t our_nip; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 246 | |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 247 | server_addr.sin_family = AF_INET; |
| 248 | server_addr.sin_port = htons(SERVER_PORT); |
| 249 | |
| 250 | /* dhcprelay client_iface1,client_iface2,... server_iface [server_IP] */ |
| 251 | if (argc == 4) { |
| 252 | if (!inet_aton(argv[3], &server_addr.sin_addr)) |
| 253 | bb_perror_msg_and_die("bad server IP"); |
| 254 | } else if (argc == 3) { |
| 255 | server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); |
| 256 | } else { |
| 257 | bb_show_usage(); |
| 258 | } |
| 259 | |
| 260 | /* Produce list of client ifaces */ |
| 261 | client_ifaces = get_client_devices(argv[1], &num_sockets); |
| 262 | |
| 263 | num_sockets++; /* for server socket at fds[0] */ |
| 264 | fds = xmalloc(num_sockets * sizeof(fds[0])); |
| 265 | |
| 266 | /* Create sockets and bind one to every iface */ |
| 267 | max_socket = init_sockets(client_ifaces, num_sockets, argv[2], fds); |
| 268 | |
| 269 | /* Get our IP on server_iface */ |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 270 | if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL)) |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 271 | return 1; |
| 272 | |
| 273 | /* Main loop */ |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 274 | while (1) { |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 275 | //reinit stuff from time to time? go back to get_client_devices |
| 276 | //every N minutes? |
| 277 | struct timeval tv; |
| 278 | size_t packlen; |
| 279 | socklen_t addr_size; |
| 280 | int i; |
| 281 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 282 | FD_ZERO(&rfds); |
| 283 | for (i = 0; i < num_sockets; i++) |
| 284 | FD_SET(fds[i], &rfds); |
| 285 | tv.tv_sec = SELECT_TIMEOUT; |
| 286 | tv.tv_usec = 0; |
| 287 | if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) { |
| 288 | /* server */ |
| 289 | if (FD_ISSET(fds[0], &rfds)) { |
Denis Vlasenko | 6de8994 | 2008-05-21 07:05:06 +0000 | [diff] [blame] | 290 | packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 291 | if (packlen > 0) { |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 292 | pass_to_client(&dhcp_msg, packlen, fds); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 295 | /* clients */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 296 | for (i = 1; i < num_sockets; i++) { |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 297 | if (!FD_ISSET(fds[i], &rfds)) |
| 298 | continue; |
| 299 | addr_size = sizeof(struct sockaddr_in); |
| 300 | packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0, |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 301 | (struct sockaddr *)(&client_addr), &addr_size); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 302 | if (packlen <= 0) |
| 303 | continue; |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 304 | |
| 305 | /* Get our IP on corresponding client_iface */ |
| 306 | //why? what if server can't route such IP? |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 307 | if (udhcp_read_interface(client_ifaces[i-1], NULL, &dhcp_msg.gateway_nip, NULL)) { |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 308 | /* Fall back to our server_iface's IP */ |
| 309 | //this makes more sense! |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 310 | dhcp_msg.gateway_nip = our_nip; |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 311 | } |
| 312 | //maybe set dhcp_msg.flags |= BROADCAST_FLAG too? |
| 313 | pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | xid_expire(); |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 317 | } /* while (1) */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 318 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 319 | /* return 0; - not reached */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 320 | } |