dhcpreplay: code shrink, -40 bytes
This commit is contained in:
parent
5fb09656ca
commit
d7b3350eb3
@ -11,17 +11,15 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "dhcpd.h"
|
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
|
|
||||||
/* constants */
|
/* constants */
|
||||||
#define SELECT_TIMEOUT 5 /* select timeout in sec. */
|
#define SELECT_TIMEOUT 5 /* select timeout in sec. */
|
||||||
#define MAX_LIFETIME 2*60 /* lifetime of an xid entry in sec. */
|
#define MAX_LIFETIME 2*60 /* lifetime of an xid entry in sec. */
|
||||||
#define MAX_INTERFACES 9
|
|
||||||
|
|
||||||
/* This list holds information about clients. The xid_* functions manipulate this list. */
|
/* This list holds information about clients. The xid_* functions manipulate this list. */
|
||||||
struct xid_item {
|
struct xid_item {
|
||||||
time_t timestamp;
|
unsigned timestamp;
|
||||||
int client;
|
int client;
|
||||||
uint32_t xid;
|
uint32_t xid;
|
||||||
struct sockaddr_in ip;
|
struct sockaddr_in ip;
|
||||||
@ -41,7 +39,7 @@ static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client
|
|||||||
item->ip = *ip;
|
item->ip = *ip;
|
||||||
item->xid = xid;
|
item->xid = xid;
|
||||||
item->client = client;
|
item->client = client;
|
||||||
item->timestamp = time(NULL);
|
item->timestamp = monotonic_sec();
|
||||||
item->next = dhcprelay_xid_list.next;
|
item->next = dhcprelay_xid_list.next;
|
||||||
dhcprelay_xid_list.next = item;
|
dhcprelay_xid_list.next = item;
|
||||||
|
|
||||||
@ -52,7 +50,7 @@ static void xid_expire(void)
|
|||||||
{
|
{
|
||||||
struct xid_item *item = dhcprelay_xid_list.next;
|
struct xid_item *item = dhcprelay_xid_list.next;
|
||||||
struct xid_item *last = &dhcprelay_xid_list;
|
struct xid_item *last = &dhcprelay_xid_list;
|
||||||
time_t current_time = time(NULL);
|
unsigned current_time = monotonic_sec();
|
||||||
|
|
||||||
while (item != NULL) {
|
while (item != NULL) {
|
||||||
if ((current_time - item->timestamp) > MAX_LIFETIME) {
|
if ((current_time - item->timestamp) > MAX_LIFETIME) {
|
||||||
@ -120,31 +118,32 @@ static int get_dhcp_packet_type(struct dhcpMessage *p)
|
|||||||
*/
|
*/
|
||||||
static char **get_client_devices(char *dev_list, int *client_number)
|
static char **get_client_devices(char *dev_list, int *client_number)
|
||||||
{
|
{
|
||||||
char *s, *list, **client_dev;
|
char *s, **client_dev;
|
||||||
int i, cn;
|
int i, cn;
|
||||||
|
|
||||||
/* copy list */
|
/* copy list */
|
||||||
list = xstrdup(dev_list);
|
dev_list = xstrdup(dev_list);
|
||||||
if (list == NULL) return NULL;
|
|
||||||
|
|
||||||
/* get number of items */
|
/* get number of items, replace ',' with NULs */
|
||||||
for (s = dev_list, cn = 1; *s; s++)
|
s = dev_list;
|
||||||
if (*s == ',')
|
cn = 1;
|
||||||
|
while (*s) {
|
||||||
|
if (*s == ',') {
|
||||||
|
*s = '\0';
|
||||||
cn++;
|
cn++;
|
||||||
|
}
|
||||||
client_dev = xzalloc(cn * sizeof(*client_dev));
|
s++;
|
||||||
|
|
||||||
/* parse list */
|
|
||||||
s = strtok(list, ",");
|
|
||||||
i = 0;
|
|
||||||
while (s != NULL) {
|
|
||||||
client_dev[i++] = xstrdup(s);
|
|
||||||
s = strtok(NULL, ",");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* free copy and exit */
|
|
||||||
free(list);
|
|
||||||
*client_number = cn;
|
*client_number = cn;
|
||||||
|
|
||||||
|
/* create vector of pointers */
|
||||||
|
client_dev = xzalloc(cn * sizeof(*client_dev));
|
||||||
|
client_dev[0] = dev_list;
|
||||||
|
i = 1;
|
||||||
|
while (i != cn) {
|
||||||
|
client_dev[i] = client_dev[i - 1] + strlen(client_dev[i - 1]) + 1;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
return client_dev;
|
return client_dev;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,7 +160,7 @@ static int init_sockets(char **client, int num_clients,
|
|||||||
|
|
||||||
for (i = 1; i < num_clients; i++) {
|
for (i = 1; i < num_clients; i++) {
|
||||||
/* listen for clients on bootps */
|
/* listen for clients on bootps */
|
||||||
fds[i] = listen_socket(/*NADDR_ANY,*/ 67, client[i-1]);
|
fds[i] = listen_socket(/*INADDR_ANY,*/ 67, client[i-1]);
|
||||||
if (fds[i] > n)
|
if (fds[i] > n)
|
||||||
n = fds[i];
|
n = fds[i];
|
||||||
}
|
}
|
||||||
@ -224,8 +223,6 @@ static void pass_back(struct dhcpMessage *p, int packet_len, int *fds)
|
|||||||
|
|
||||||
if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
|
if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
|
||||||
item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
|
item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
|
||||||
if (item->client > MAX_INTERFACES)
|
|
||||||
return;
|
|
||||||
res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*)(&item->ip),
|
res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*)(&item->ip),
|
||||||
sizeof(item->ip));
|
sizeof(item->ip));
|
||||||
if (res != packet_len) {
|
if (res != packet_len) {
|
||||||
@ -285,7 +282,8 @@ static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **cli
|
|||||||
int dhcprelay_main(int argc, char **argv);
|
int dhcprelay_main(int argc, char **argv);
|
||||||
int dhcprelay_main(int argc, char **argv)
|
int dhcprelay_main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int num_sockets, max_socket, fds[MAX_INTERFACES];
|
int num_sockets, max_socket;
|
||||||
|
int *fds;
|
||||||
uint32_t gw_ip;
|
uint32_t gw_ip;
|
||||||
char **clients;
|
char **clients;
|
||||||
struct sockaddr_in server_addr;
|
struct sockaddr_in server_addr;
|
||||||
@ -300,10 +298,10 @@ int dhcprelay_main(int argc, char **argv)
|
|||||||
} else {
|
} else {
|
||||||
bb_show_usage();
|
bb_show_usage();
|
||||||
}
|
}
|
||||||
clients = get_client_devices(argv[1], &num_sockets);
|
|
||||||
if (!clients) return 0;
|
|
||||||
|
|
||||||
|
clients = get_client_devices(argv[1], &num_sockets);
|
||||||
num_sockets++; /* for server socket at fds[0] */
|
num_sockets++; /* for server socket at fds[0] */
|
||||||
|
fds = xmalloc(num_sockets * sizeof(fds[0]));
|
||||||
max_socket = init_sockets(clients, num_sockets, argv[2], fds);
|
max_socket = init_sockets(clients, num_sockets, argv[2], fds);
|
||||||
|
|
||||||
if (read_interface(argv[2], NULL, &gw_ip, NULL))
|
if (read_interface(argv[2], NULL, &gw_ip, NULL))
|
||||||
|
Loading…
Reference in New Issue
Block a user