mirror of
https://github.com/Telecominfraproject/wlan-ap.git
synced 2025-11-01 19:07:47 +00:00
ucentral-tools: format dhcpdiscover.c with clang-format
Apply clang-format and fix code quality issues found by cppcheck: - Consistent pointer style (char* → char *) - Fix variable shadowing issues - Improve error message formatting - Standardise indentation and brace style Signed-off-by: John Crispin <john@phrozen.org>
This commit is contained in:
@@ -77,8 +77,8 @@ const char* email = "p4u@dabax.net";
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if defined(__linux__)
|
||||
|
||||
@@ -123,7 +123,6 @@ static struct strbuf dat = { AREA_SZ, 0, (char*)dat_area };
|
||||
static int get_msg(int);
|
||||
static int check_ctrl(int);
|
||||
static int put_ctrl(int, int, int);
|
||||
static int put_both(int, int, int, int);
|
||||
static int dl_open(const char *, int, int *);
|
||||
static int dl_bind(int, int, u_char *);
|
||||
long mac_addr_dlpi(const char *, int, u_char *);
|
||||
@@ -247,8 +246,9 @@ int verbose = 0;
|
||||
int prometheus = 0;
|
||||
struct in_addr requested_address;
|
||||
|
||||
static void print_revision(const char* progname, const char* revision) {
|
||||
fprintf(stderr, "Program: %s %s\n", progname, revision);
|
||||
static void print_revision(const char *prog_name, const char *prog_revision)
|
||||
{
|
||||
fprintf(stderr, "Program: %s %s\n", prog_name, prog_revision);
|
||||
}
|
||||
|
||||
int process_arguments(int, char **);
|
||||
@@ -323,8 +323,6 @@ int main(int argc, char** argv)
|
||||
/* determines hardware address on client machine */
|
||||
int get_hardware_address(int sock, char *interface_name)
|
||||
{
|
||||
int i;
|
||||
|
||||
#if defined(__linux__)
|
||||
struct ifreq ifr;
|
||||
|
||||
@@ -334,24 +332,34 @@ int get_hardware_address(int sock, char* interface_name)
|
||||
// If this fails the test will still work since
|
||||
// we do encode the MAC as part of the DHCP frame - tests show it works
|
||||
if (mymac) {
|
||||
int i;
|
||||
for (i = 0; i < MAX_DHCP_CHADDR_LENGTH; ++i)
|
||||
client_hardware_address[i] = my_client_mac[i];
|
||||
int j;
|
||||
for (j = 0; j < MAX_DHCP_CHADDR_LENGTH; ++j)
|
||||
client_hardware_address[j] = my_client_mac[j];
|
||||
memcpy(&ifr.ifr_hwaddr.sa_data, &client_hardware_address[0], 6);
|
||||
if (ioctl(sock, SIOCSIFHWADDR, &ifr) < 0) {
|
||||
fprintf(stderr, "Could not set hardware address of interface '%s'\n", interface_name);
|
||||
fprintf(stderr, "Could not set hardware address of interface '%s'\n",
|
||||
interface_name);
|
||||
// perror("Error");
|
||||
// exit(STATE_UNKNOWN);
|
||||
}
|
||||
} else {
|
||||
/* try and grab hardware address of requested interface */
|
||||
if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
|
||||
fprintf(stderr, "Could not get hardware address of interface '%s'\n", interface_name);
|
||||
fprintf(stderr, "Could not get hardware address of interface '%s'\n",
|
||||
interface_name);
|
||||
exit(STATE_UNKNOWN);
|
||||
}
|
||||
memcpy(&client_hardware_address[0], &ifr.ifr_hwaddr.sa_data, 6);
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
int i;
|
||||
fprintf(stderr, "Hardware address: ");
|
||||
for (i = 0; i < 6; ++i)
|
||||
fprintf(stderr, "%2.2x", client_hardware_address[i]);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
#elif defined(__bsd__)
|
||||
/* King 2004 see ACKNOWLEDGEMENTS */
|
||||
|
||||
@@ -368,22 +376,26 @@ int get_hardware_address(int sock, char* interface_name)
|
||||
mib[4] = NET_RT_IFLIST;
|
||||
|
||||
if ((mib[5] = if_nametoindex(interface_name)) == 0) {
|
||||
fprintf(stderr, "if_nametoindex error - %s.\n", strerror(errno);
|
||||
fprintf(stderr, "if_nametoindex error - %s.\n", strerror(errno));
|
||||
exit(STATE_UNKNOWN);
|
||||
}
|
||||
|
||||
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
|
||||
fprintf(stderr, "Couldn't get hardware address from %s. sysctl 1 error - %s.\n", interface_name, strerror(errno);
|
||||
fprintf(stderr, "Couldn't get hardware address from %s. sysctl 1 error - %s.\n",
|
||||
interface_name, strerror(errno));
|
||||
exit(STATE_UNKNOWN);
|
||||
}
|
||||
|
||||
if ((buf = malloc(len)) == NULL) {
|
||||
fprintf(stderr, "Couldn't get hardware address from interface %s. malloc error - %s.\n", interface_name, strerror(errno);
|
||||
fprintf(stderr,
|
||||
"Couldn't get hardware address from interface %s. malloc error - %s.\n",
|
||||
interface_name, strerror(errno));
|
||||
exit(4);
|
||||
}
|
||||
|
||||
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
|
||||
fprintf(stderr, "Couldn't get hardware address from %s. sysctl 2 error - %s.\n", interface_name, strerror(errno);
|
||||
fprintf(stderr, "Couldn't get hardware address from %s. sysctl 2 error - %s.\n",
|
||||
interface_name, strerror(errno));
|
||||
exit(STATE_UNKNOWN);
|
||||
}
|
||||
|
||||
@@ -393,6 +405,14 @@ int get_hardware_address(int sock, char* interface_name)
|
||||
memcpy(&client_hardware_address[0], ptr, 6);
|
||||
/* King 2004 */
|
||||
|
||||
if (verbose) {
|
||||
int i;
|
||||
fprintf(stderr, "Hardware address: ");
|
||||
for (i = 0; i < 6; ++i)
|
||||
fprintf(stderr, "%2.2x", client_hardware_address[i]);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
#elif defined(__sun__) || defined(__solaris__)
|
||||
|
||||
/* Kompf 2000-2003 see ACKNOWLEDGEMENTS */
|
||||
@@ -403,22 +423,34 @@ int get_hardware_address(int sock, char* interface_name)
|
||||
|
||||
for (p = interface_name; *p && isalpha(*p); p++)
|
||||
/* no-op */;
|
||||
if (p != '\0') {
|
||||
if (*p != '\0') {
|
||||
unit = atoi(p);
|
||||
*p = '\0';
|
||||
strncat(dev, interface_name, 6);
|
||||
} else {
|
||||
fprintf(stderr, "can't find unit number in interface_name (%s) - expecting "
|
||||
"TypeNumber eg lnc0.\n", interface_name);
|
||||
fprintf(stderr,
|
||||
"can't find unit number in interface_name (%s) - expecting "
|
||||
"TypeNumber eg lnc0.\n",
|
||||
interface_name);
|
||||
exit(STATE_UNKNOWN);
|
||||
}
|
||||
stat = mac_addr_dlpi(dev, unit, client_hardware_address);
|
||||
if (stat != 0) {
|
||||
fprintf(stderr, "can't read MAC address from DLPI streams interface for "
|
||||
"device %s unit %d.\n", dev, unit);
|
||||
fprintf(stderr,
|
||||
"can't read MAC address from DLPI streams interface for "
|
||||
"device %s unit %d.\n",
|
||||
dev, unit);
|
||||
exit(STATE_UNKNOWN);
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
int i;
|
||||
fprintf(stderr, "Hardware address: ");
|
||||
for (i = 0; i < 6; ++i)
|
||||
fprintf(stderr, "%2.2x", client_hardware_address[i]);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
#elif defined(__hpux__)
|
||||
|
||||
long stat;
|
||||
@@ -427,24 +459,27 @@ int get_hardware_address(int sock, char* interface_name)
|
||||
|
||||
stat = mac_addr_dlpi(dev, unit, client_hardware_address);
|
||||
if (stat != 0) {
|
||||
fprintf(stderr, "can't read MAC address from DLPI streams interface for "
|
||||
"device %s unit %d.\n", dev, unit);
|
||||
fprintf(stderr,
|
||||
"can't read MAC address from DLPI streams interface for "
|
||||
"device %s unit %d.\n",
|
||||
dev, unit);
|
||||
exit(STATE_UNKNOWN);
|
||||
}
|
||||
/* Kompf 2000-2003 */
|
||||
|
||||
#else
|
||||
fprintf(stderr, "can't get MAC address for this architecture.\n");
|
||||
exit(STATE_UNKNOWN);
|
||||
#endif
|
||||
|
||||
if (verbose) {
|
||||
int i;
|
||||
fprintf(stderr, "Hardware address: ");
|
||||
for (i = 0; i < 6; ++i)
|
||||
fprintf(stderr, "%2.2x", client_hardware_address[i]);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
#else
|
||||
fprintf(stderr, "can't get MAC address for this architecture.\n");
|
||||
exit(STATE_UNKNOWN);
|
||||
#endif
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
@@ -494,7 +529,8 @@ int send_dhcp_discover(int sock)
|
||||
discover_packet.options[3] = '\x63';
|
||||
|
||||
/* DHCP message type is embedded in options field */
|
||||
discover_packet.options[4] = DHCP_OPTION_MESSAGE_TYPE; /* DHCP message type option identifier */
|
||||
discover_packet.options[4] =
|
||||
DHCP_OPTION_MESSAGE_TYPE; /* DHCP message type option identifier */
|
||||
discover_packet.options[5] = '\x01'; /* DHCP message option length in bytes */
|
||||
discover_packet.options[6] = DHCPDISCOVER;
|
||||
|
||||
@@ -513,11 +549,9 @@ int send_dhcp_discover(int sock)
|
||||
|
||||
if (verbose) {
|
||||
fprintf(stderr, "DHCPDISCOVER to %s port %d\n",
|
||||
inet_ntoa(sockaddr_broadcast.sin_addr),
|
||||
ntohs(sockaddr_broadcast.sin_port));
|
||||
inet_ntoa(sockaddr_broadcast.sin_addr), ntohs(sockaddr_broadcast.sin_port));
|
||||
fprintf(stderr, "DHCPDISCOVER XID: %lu (0x%X)\n",
|
||||
(unsigned long)ntohl(discover_packet.xid),
|
||||
ntohl(discover_packet.xid));
|
||||
(unsigned long) ntohl(discover_packet.xid), ntohl(discover_packet.xid));
|
||||
fprintf(stderr, "DHCDISCOVER ciaddr: %s\n", inet_ntoa(discover_packet.ciaddr));
|
||||
fprintf(stderr, "DHCDISCOVER yiaddr: %s\n", inet_ntoa(discover_packet.yiaddr));
|
||||
fprintf(stderr, "DHCDISCOVER siaddr: %s\n", inet_ntoa(discover_packet.siaddr));
|
||||
@@ -538,7 +572,6 @@ int get_dhcp_offer(int sock)
|
||||
{
|
||||
dhcp_packet offer_packet;
|
||||
struct sockaddr_in source;
|
||||
int result = OK;
|
||||
int responses = 0;
|
||||
int x;
|
||||
time_t start_time;
|
||||
@@ -558,8 +591,7 @@ int get_dhcp_offer(int sock)
|
||||
bzero(&source, sizeof(source));
|
||||
bzero(&offer_packet, sizeof(offer_packet));
|
||||
|
||||
result = OK;
|
||||
result = receive_dhcp_packet(&offer_packet, sizeof(offer_packet), sock,
|
||||
int result = receive_dhcp_packet(&offer_packet, sizeof(offer_packet), sock,
|
||||
dhcpoffer_timeout, &source);
|
||||
|
||||
// checks if the source address is the banned one
|
||||
@@ -583,7 +615,8 @@ int get_dhcp_offer(int sock)
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
fprintf(stderr, "DHCPOFFER from IP address %s\n", inet_ntoa(source.sin_addr));
|
||||
fprintf(stderr, "DHCPOFFER from IP address %s\n",
|
||||
inet_ntoa(source.sin_addr));
|
||||
fprintf(stderr, "DHCPOFFER XID: %lu (0x%X)\n",
|
||||
(unsigned long) ntohl(offer_packet.xid), ntohl(offer_packet.xid));
|
||||
}
|
||||
@@ -592,7 +625,9 @@ int get_dhcp_offer(int sock)
|
||||
* discover packet */
|
||||
if (ntohl(offer_packet.xid) != packet_xid) {
|
||||
if (verbose)
|
||||
fprintf(stderr, "DHCPOFFER XID (%lu) did not match DHCPDISCOVER XID (%lu) - "
|
||||
fprintf(
|
||||
stderr,
|
||||
"DHCPOFFER XID (%lu) did not match DHCPDISCOVER XID (%lu) - "
|
||||
"ignoring packet\n",
|
||||
(unsigned long) ntohl(offer_packet.xid),
|
||||
(unsigned long) packet_xid);
|
||||
@@ -617,7 +652,9 @@ int get_dhcp_offer(int sock)
|
||||
|
||||
if (result == ERROR) {
|
||||
if (verbose)
|
||||
fprintf(stderr, "DHCPOFFER hardware address did not match our own - ignoring "
|
||||
fprintf(
|
||||
stderr,
|
||||
"DHCPOFFER hardware address did not match our own - ignoring "
|
||||
"packet\n");
|
||||
continue;
|
||||
}
|
||||
@@ -647,7 +684,8 @@ int send_dhcp_packet(void* buffer, int buffer_size, int sock, struct sockaddr_in
|
||||
{
|
||||
int result;
|
||||
|
||||
result = sendto(sock, (char*)buffer, buffer_size, 0, (struct sockaddr*)dest, sizeof(*dest));
|
||||
result =
|
||||
sendto(sock, (char *) buffer, buffer_size, 0, (struct sockaddr *) dest, sizeof(*dest));
|
||||
|
||||
if (verbose)
|
||||
fprintf(stderr, "send_dhcp_packet result: %d\n", result);
|
||||
@@ -659,11 +697,11 @@ int send_dhcp_packet(void* buffer, int buffer_size, int sock, struct sockaddr_in
|
||||
}
|
||||
|
||||
/* receives a DHCP packet */
|
||||
int receive_dhcp_packet(void* buffer, int buffer_size, int sock, int timeout, struct sockaddr_in* address)
|
||||
int receive_dhcp_packet(void *buffer, int buffer_size, int sock, int timeout,
|
||||
struct sockaddr_in *address)
|
||||
{
|
||||
struct timeval tv;
|
||||
fd_set readfds;
|
||||
int recv_result;
|
||||
socklen_t address_size;
|
||||
struct sockaddr_in source_address;
|
||||
|
||||
@@ -679,14 +717,14 @@ int receive_dhcp_packet(void* buffer, int buffer_size, int sock, int timeout, st
|
||||
if (verbose)
|
||||
fprintf(stderr, "No (more) data received\n");
|
||||
return ERROR;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
/* why do we need to peek first? i don't know, its a hack. without it, the
|
||||
source address of the first packet received was not being interpreted
|
||||
correctly. sigh... */
|
||||
bzero(&source_address, sizeof(source_address));
|
||||
address_size = sizeof(source_address);
|
||||
recv_result = recvfrom(sock, (char*)buffer, buffer_size, MSG_PEEK, (struct sockaddr*)&source_address, &address_size);
|
||||
int recv_result = recvfrom(sock, (char *) buffer, buffer_size, MSG_PEEK,
|
||||
(struct sockaddr *) &source_address, &address_size);
|
||||
|
||||
if (verbose)
|
||||
fprintf(stderr, "recv_result_1: %d\n", recv_result);
|
||||
@@ -759,9 +797,10 @@ int create_dhcp_socket(void)
|
||||
/* bind socket to interface */
|
||||
#if defined(__linux__)
|
||||
strncpy(interface.ifr_ifrn.ifrn_name, network_interface_name, IFNAMSIZ);
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (char *)&interface,
|
||||
sizeof(interface)) < 0) {
|
||||
fprintf(stderr, "Could not bind socket to interface %s. Check your "
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (char *) &interface, sizeof(interface)) <
|
||||
0) {
|
||||
fprintf(stderr,
|
||||
"Could not bind socket to interface %s. Check your "
|
||||
"privileges...\n",
|
||||
network_interface_name);
|
||||
perror("foo");
|
||||
@@ -773,8 +812,10 @@ int create_dhcp_socket(void)
|
||||
|
||||
/* bind the socket */
|
||||
if (bind(sock, (struct sockaddr *) &myname, sizeof(myname)) < 0) {
|
||||
fprintf(stderr, "Could not bind to DHCP socket (port %d)! Check your "
|
||||
"privileges...\n", DHCP_CLIENT_PORT);
|
||||
fprintf(stderr,
|
||||
"Could not bind to DHCP socket (port %d)! Check your "
|
||||
"privileges...\n",
|
||||
DHCP_CLIENT_PORT);
|
||||
exit(STATE_UNKNOWN);
|
||||
}
|
||||
|
||||
@@ -816,60 +857,64 @@ int add_dhcp_offer(struct in_addr source, dhcp_packet* offer_packet)
|
||||
{
|
||||
dhcp_offer *new_offer;
|
||||
int x;
|
||||
int y;
|
||||
unsigned option_type;
|
||||
unsigned option_length;
|
||||
unsigned int y;
|
||||
|
||||
if (offer_packet == NULL)
|
||||
return ERROR;
|
||||
|
||||
/* process all DHCP options present in the packet */
|
||||
for (x = 4; x < MAX_DHCP_OPTIONS_LENGTH;) {
|
||||
|
||||
/* end of options (0 is really just a pad, but bail out anyway) */
|
||||
if ((int)offer_packet->options[x] == -1 || (int)offer_packet->options[x] == 0)
|
||||
if (offer_packet->options[x] == 255 || offer_packet->options[x] == 0)
|
||||
break;
|
||||
|
||||
/* get option type */
|
||||
option_type = offer_packet->options[x++];
|
||||
unsigned option_type = offer_packet->options[x++];
|
||||
|
||||
/* get option length */
|
||||
option_length = offer_packet->options[x++];
|
||||
unsigned option_length = offer_packet->options[x++];
|
||||
|
||||
if (verbose)
|
||||
fprintf(stderr, "Option: %d (0x%02X)\n", option_type, option_length);
|
||||
fprintf(stderr, "Option: %u (0x%02X)\n", option_type, option_length);
|
||||
|
||||
/* get option data */
|
||||
if (option_type == DHCP_OPTION_LEASE_TIME) {
|
||||
memcpy(&dhcp_lease_time, &offer_packet->options[x], sizeof(dhcp_lease_time));
|
||||
memcpy(&dhcp_lease_time, &offer_packet->options[x],
|
||||
sizeof(dhcp_lease_time));
|
||||
dhcp_lease_time = ntohl(dhcp_lease_time);
|
||||
}
|
||||
if (option_type == DHCP_OPTION_RENEWAL_TIME) {
|
||||
memcpy(&dhcp_renewal_time, &offer_packet->options[x], sizeof(dhcp_renewal_time));
|
||||
memcpy(&dhcp_renewal_time, &offer_packet->options[x],
|
||||
sizeof(dhcp_renewal_time));
|
||||
dhcp_renewal_time = ntohl(dhcp_renewal_time);
|
||||
}
|
||||
if (option_type == DHCP_OPTION_REBINDING_TIME) {
|
||||
memcpy(&dhcp_rebinding_time, &offer_packet->options[x], sizeof(dhcp_rebinding_time));
|
||||
memcpy(&dhcp_rebinding_time, &offer_packet->options[x],
|
||||
sizeof(dhcp_rebinding_time));
|
||||
dhcp_rebinding_time = ntohl(dhcp_rebinding_time);
|
||||
}
|
||||
|
||||
/* skip option data we're ignoring */
|
||||
else
|
||||
for (y = 0; y < option_length; y++, x++);
|
||||
for (y = 0; y < option_length; y++, x++)
|
||||
;
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
if (dhcp_lease_time == DHCP_INFINITE_TIME)
|
||||
fprintf(stderr, "Lease Time: Infinite\n");
|
||||
else
|
||||
fprintf(stderr, "Lease Time: %lu seconds\n", (unsigned long)dhcp_lease_time);
|
||||
fprintf(stderr, "Lease Time: %lu seconds\n",
|
||||
(unsigned long) dhcp_lease_time);
|
||||
if (dhcp_renewal_time == DHCP_INFINITE_TIME)
|
||||
fprintf(stderr, "Renewal Time: Infinite\n");
|
||||
else
|
||||
fprintf(stderr, "Renewal Time: %lu seconds\n", (unsigned long)dhcp_renewal_time);
|
||||
fprintf(stderr, "Renewal Time: %lu seconds\n",
|
||||
(unsigned long) dhcp_renewal_time);
|
||||
if (dhcp_rebinding_time == DHCP_INFINITE_TIME)
|
||||
fprintf(stderr, "Rebinding Time: Infinite\n");
|
||||
fprintf(stderr, "Rebinding Time: %lu seconds\n", (unsigned long)dhcp_rebinding_time);
|
||||
fprintf(stderr, "Rebinding Time: %lu seconds\n",
|
||||
(unsigned long) dhcp_rebinding_time);
|
||||
}
|
||||
|
||||
new_offer = (dhcp_offer *) malloc(sizeof(dhcp_offer));
|
||||
@@ -944,25 +989,30 @@ int get_results(void)
|
||||
/* checks responses from requested servers */
|
||||
requested_responses = 0;
|
||||
if (requested_servers > 0) {
|
||||
|
||||
for (temp_server = requested_server_list; temp_server != NULL; temp_server = temp_server->next) {
|
||||
|
||||
for (temp_offer = dhcp_offer_list; temp_offer != NULL; temp_offer = temp_offer->next) {
|
||||
|
||||
for (temp_server = requested_server_list; temp_server != NULL;
|
||||
temp_server = temp_server->next) {
|
||||
for (temp_offer = dhcp_offer_list; temp_offer != NULL;
|
||||
temp_offer = temp_offer->next) {
|
||||
/* get max lease time we were offered */
|
||||
if (temp_offer->lease_time > max_lease_time || temp_offer->lease_time == DHCP_INFINITE_TIME)
|
||||
if (temp_offer->lease_time > max_lease_time ||
|
||||
temp_offer->lease_time == DHCP_INFINITE_TIME)
|
||||
max_lease_time = temp_offer->lease_time;
|
||||
|
||||
/* see if we got the address we requested */
|
||||
if (!memcmp(&requested_address, &temp_offer->offered_address, sizeof(requested_address)))
|
||||
if (!memcmp(&requested_address, &temp_offer->offered_address,
|
||||
sizeof(requested_address)))
|
||||
received_requested_address = TRUE;
|
||||
|
||||
/* see if the servers we wanted a response from talked to us or not */
|
||||
if (!memcmp(&temp_offer->server_address, &temp_server->server_address, sizeof(temp_server->server_address))) {
|
||||
/* see if the servers we wanted a response from talked to us or not
|
||||
*/
|
||||
if (!memcmp(&temp_offer->server_address,
|
||||
&temp_server->server_address,
|
||||
sizeof(temp_server->server_address))) {
|
||||
if (verbose) {
|
||||
fprintf(stderr, "DHCP Server Match: Offerer=%s",
|
||||
inet_ntoa(temp_offer->server_address));
|
||||
fprintf(stderr, " Requested=%s\n", inet_ntoa(temp_server->server_address));
|
||||
fprintf(stderr, " Requested=%s\n",
|
||||
inet_ntoa(temp_server->server_address));
|
||||
}
|
||||
requested_responses++;
|
||||
}
|
||||
@@ -972,14 +1022,16 @@ int get_results(void)
|
||||
|
||||
/* else check and see if we got our requested address from any server */
|
||||
else {
|
||||
for (temp_offer = dhcp_offer_list; temp_offer != NULL; temp_offer = temp_offer->next) {
|
||||
|
||||
for (temp_offer = dhcp_offer_list; temp_offer != NULL;
|
||||
temp_offer = temp_offer->next) {
|
||||
/* get max lease time we were offered */
|
||||
if (temp_offer->lease_time > max_lease_time || temp_offer->lease_time == DHCP_INFINITE_TIME)
|
||||
if (temp_offer->lease_time > max_lease_time ||
|
||||
temp_offer->lease_time == DHCP_INFINITE_TIME)
|
||||
max_lease_time = temp_offer->lease_time;
|
||||
|
||||
/* see if we got the address we requested */
|
||||
if (!memcmp(&requested_address, &temp_offer->offered_address, sizeof(requested_address)))
|
||||
if (!memcmp(&requested_address, &temp_offer->offered_address,
|
||||
sizeof(requested_address)))
|
||||
received_requested_address = TRUE;
|
||||
}
|
||||
}
|
||||
@@ -1007,7 +1059,8 @@ int get_results(void)
|
||||
if (requested_servers > 0)
|
||||
fprintf(stderr, ", %s%d of %d requested servers responded",
|
||||
((requested_responses < requested_servers) && requested_responses > 0)
|
||||
? "only " : "",
|
||||
? "only "
|
||||
: "",
|
||||
requested_responses, requested_servers);
|
||||
|
||||
if (request_specific_address == TRUE)
|
||||
@@ -1047,14 +1100,24 @@ int process_arguments(int argc, char** argv)
|
||||
|
||||
int call_getopt(int argc, char **argv)
|
||||
{
|
||||
int c = 0;
|
||||
int c;
|
||||
int i = 0;
|
||||
struct in_addr ipaddress;
|
||||
|
||||
#ifdef HAVE_GETOPT_H
|
||||
int option_index = 0;
|
||||
int ret;
|
||||
static struct option long_options[] = { { "serverip", required_argument, 0, 's' }, { "requestedip", required_argument, 0, 'r' }, { "timeout", required_argument, 0, 't' }, { "interface", required_argument, 0, 'i' }, { "mac", required_argument, 0, 'm' }, { "bannedip", required_argument, 0, 'b' }, { "verbose", no_argument, 0, 'v' }, { "prometheus", no_argument, 0, 'p' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 } };
|
||||
static struct option long_options[] = { { "serverip", required_argument, 0, 's' },
|
||||
{ "requestedip", required_argument, 0, 'r' },
|
||||
{ "timeout", required_argument, 0, 't' },
|
||||
{ "interface", required_argument, 0, 'i' },
|
||||
{ "mac", required_argument, 0, 'm' },
|
||||
{ "bannedip", required_argument, 0, 'b' },
|
||||
{ "verbose", no_argument, 0, 'v' },
|
||||
{ "prometheus", no_argument, 0, 'p' },
|
||||
{ "version", no_argument, 0, 'V' },
|
||||
{ "help", no_argument, 0, 'h' },
|
||||
{ 0, 0, 0, 0 } };
|
||||
#endif
|
||||
|
||||
while (1) {
|
||||
@@ -1066,7 +1129,7 @@ int call_getopt(int argc, char** argv)
|
||||
|
||||
i++;
|
||||
|
||||
if (c == -1 || c == EOF || c == 1)
|
||||
if (c == EOF || c == 1)
|
||||
break;
|
||||
|
||||
switch (c) {
|
||||
@@ -1081,10 +1144,12 @@ int call_getopt(int argc, char** argv)
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
|
||||
case 'm': /* Our MAC address */
|
||||
{
|
||||
ret = sscanf(optarg, "%x:%x:%x:%x:%x:%x", my_client_mac + 0, my_client_mac + 1, my_client_mac + 2, my_client_mac + 3, my_client_mac + 4, my_client_mac + 5);
|
||||
ret =
|
||||
sscanf(optarg, "%x:%x:%x:%x:%x:%x", my_client_mac + 0,
|
||||
my_client_mac + 1, my_client_mac + 2, my_client_mac + 3,
|
||||
my_client_mac + 4, my_client_mac + 5);
|
||||
if (ret != 6) {
|
||||
usage("Invalid MAC address\n");
|
||||
break;
|
||||
@@ -1165,7 +1230,10 @@ int call_getopt(int argc, char** argv)
|
||||
return i;
|
||||
}
|
||||
|
||||
int validate_arguments(void) { return OK; }
|
||||
int validate_arguments(void)
|
||||
{
|
||||
return OK;
|
||||
}
|
||||
|
||||
#if defined(__sun__) || defined(__solaris__) || defined(__hpux__)
|
||||
|
||||
@@ -1203,7 +1271,8 @@ static int check_ctrl(int prim)
|
||||
{
|
||||
dl_error_ack_t *err_ack = (dl_error_ack_t *) ctl_area;
|
||||
if (err_ack->dl_primitive != prim) {
|
||||
fprintf(stderr, "DLPI stream API failed to get MAC in check_ctrl: %s.\n", strerror(errno);
|
||||
fprintf(stderr, "DLPI stream API failed to get MAC in check_ctrl: %s.\n",
|
||||
strerror(errno));
|
||||
exit(STATE_UNKNOWN);
|
||||
}
|
||||
return 0;
|
||||
@@ -1214,30 +1283,22 @@ static int put_ctrl(int fd, int len, int pri)
|
||||
{
|
||||
ctl.len = len;
|
||||
if (putmsg(fd, &ctl, 0, pri) < 0) {
|
||||
fprintf(stderr, "DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n", strerror(errno);
|
||||
fprintf(stderr, "DLPI stream API failed to get MAC in put_ctrl/putmsg(): %s.\n",
|
||||
strerror(errno));
|
||||
exit(STATE_UNKNOWN);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* put a control + data message on a stream */
|
||||
static int put_both(int fd, int clen, int dlen, int pri)
|
||||
{
|
||||
ctl.len = clen;
|
||||
dat.len = dlen;
|
||||
if (putmsg(fd, &ctl, &dat, pri) < 0) {
|
||||
fprintf(stderr, "DLPI stream API failed to get MAC in put_both/putmsg().\n", strerror(errno);
|
||||
exit(STATE_UNKNOWN);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* open file descriptor and attach */
|
||||
static int dl_open(const char *dev, int unit, int *fd)
|
||||
{
|
||||
dl_attach_req_t *attach_req = (dl_attach_req_t *) ctl_area;
|
||||
if ((*fd = open(dev, O_RDWR)) == -1) {
|
||||
fprintf(stderr, "DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n", dev, strerror(errno);
|
||||
fprintf(stderr,
|
||||
"DLPI stream API failed to get MAC in dl_attach_req/open(%s..): %s.\n", dev,
|
||||
strerror(errno));
|
||||
exit(STATE_UNKNOWN);
|
||||
}
|
||||
attach_req->dl_primitive = DL_ATTACH_REQ;
|
||||
@@ -1261,7 +1322,8 @@ static int dl_bind(int fd, int sap, u_char* addr)
|
||||
put_ctrl(fd, sizeof(dl_bind_req_t), 0);
|
||||
get_msg(fd);
|
||||
if (GOT_ERR == check_ctrl(DL_BIND_ACK)) {
|
||||
fprintf(stderr, "DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n", strerror(errno);
|
||||
fprintf(stderr, "DLPI stream API failed to get MAC in dl_bind/check_ctrl(): %s.\n",
|
||||
strerror(errno));
|
||||
exit(STATE_UNKNOWN);
|
||||
}
|
||||
bcopy((u_char *) bind_ack + bind_ack->dl_addr_offset, addr, bind_ack->dl_addr_length);
|
||||
@@ -1302,10 +1364,9 @@ long mac_addr_dlpi(const char* dev, int unit, u_char* addr)
|
||||
/* print usage help */
|
||||
void print_help(void)
|
||||
{
|
||||
|
||||
print_revision(progname, revision);
|
||||
|
||||
fprintf(stderr, COPYRIGHT, copyright, email);
|
||||
fprintf(stderr, "%s", COPYRIGHT);
|
||||
|
||||
fprintf(stderr, "\n\nThis program checks the existence and more details of a DHCP "
|
||||
"server\n\n");
|
||||
@@ -1341,5 +1402,6 @@ void print_usage(void)
|
||||
{
|
||||
fprintf(stderr, "\
|
||||
Usage: %s [-s serverip] [-r requestedip] [-m clientmac ] [-b bannedip] [-t timeout] [-i interface]\n\
|
||||
[-v] [-p]", progname);
|
||||
[-v] [-p]",
|
||||
progname);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user