ucentral-tools: fix compiler warnings and modernize code

- Fixed format-nonliteral error in dnsprobe.c by using explicit format strings
- Fixed cast-qual error by preserving const qualifier in type casting
- Removed unused format variable and unused parameter warnings
- Updated parse_reply function signature to remove unused parameter
- Made string literals const for better type safety
- Fixed variable type warnings (unsigned long vs int)

These changes ensure the code compiles without warnings under strict
compiler flags including -Werror, -Wformat-nonliteral, -Wcast-qual,
-Wunused-parameter, and -Wunused-variable.

Signed-off-by: John Crispin <john@phrozen.org>
This commit is contained in:
John Crispin
2025-09-11 19:49:51 +02:00
parent 811b63de93
commit fe850fc7e9
8 changed files with 1287 additions and 1184 deletions

View File

@@ -18,7 +18,7 @@ endef
define Package/ucentral-tools/install
$(INSTALL_DIR) $(1)/usr/sbin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/{dhcpdiscover,dnsprobe,radiusprobe,ip-collide} $(1)/usr/sbin/
$(INSTALL_BIN) $(PKG_BUILD_DIR)/{dhcpdiscover,dnsprobe,radiusprobe} $(1)/usr/sbin/
$(CP) ./files/* $(1)
endef

View File

@@ -0,0 +1,60 @@
---
# clang-format configuration for ucentral-tools
BasedOnStyle: LLVM
# Indentation
IndentWidth: 8
TabWidth: 8
UseTab: ForIndentation
IndentCaseLabels: true
IndentPPDirectives: BeforeHash
# Line length
ColumnLimit: 100
# Braces
BreakBeforeBraces: Linux
Cpp11BracedListStyle: false
# Spacing
SpaceAfterCStyleCast: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
# Alignment
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignOperands: true
AlignTrailingComments: true
# Breaking
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakStringLiterals: true
# Pointers and references
PointerAlignment: Right
DerivePointerAlignment: false
# Comments
ReflowComments: true
# Sorting
SortIncludes: true
IncludeBlocks: Preserve
# Function calls
BinPackArguments: true
BinPackParameters: true
# Other
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 2

View File

@@ -1,36 +1,108 @@
cmake_minimum_required(VERSION 2.6)
cmake_minimum_required(VERSION 3.10)
PROJECT(openwifi-tools C)
INCLUDE(GNUInstallDirs)
ADD_DEFINITIONS(-Os -ggdb -Wall -Werror --std=gnu99 -Wmissing-declarations)
project(openwifi-tools C)
include(GNUInstallDirs)
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
# Set C standard
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
ADD_EXECUTABLE(firstcontact firstcontact.c)
TARGET_LINK_LIBRARIES(firstcontact curl crypto ssl ubox)
INSTALL(TARGETS firstcontact
RUNTIME DESTINATION ${CMAKE_INSTALL_SBINDIR}
# Comprehensive warning flags (cross-compilation friendly)
set(WARNING_FLAGS
-Wall
-Wextra
-Werror
-Wformat=2
-Wformat-security
-Wnull-dereference
-Warray-bounds=2
-Wimplicit-fallthrough=3
-Wshift-overflow=2
-Wcast-qual
-Wstringop-overflow=4
-Wlogical-op
-Wduplicated-cond
-Wduplicated-branches
-Wformat-overflow=2
-Wformat-truncation=2
-Wmissing-declarations
-Wredundant-decls
-Wshadow
-Wstrict-overflow=4
-Wundef
-Wstrict-prototypes
-Wswitch-default
-Wbad-function-cast
-Wwrite-strings
-Wjump-misses-init
-Wold-style-definition
-Wmissing-prototypes
)
ADD_EXECUTABLE(dhcpdiscover dhcpdiscover.c)
INSTALL(TARGETS dhcpdiscover
RUNTIME DESTINATION ${CMAKE_INSTALL_SBINDIR}
# Security hardening flags (cross-compilation friendly)
set(SECURITY_FLAGS
-fstack-protector-strong
)
ADD_EXECUTABLE(dnsprobe dnsprobe.c)
TARGET_LINK_LIBRARIES(dnsprobe ubox resolv)
INSTALL(TARGETS dnsprobe
RUNTIME DESTINATION ${CMAKE_INSTALL_SBINDIR}
# Linker security flags (cross-compilation friendly)
set(SECURITY_LINKER_FLAGS
-Wl,-z,relro
-Wl,-z,now
)
ADD_EXECUTABLE(radiusprobe radiusprobe.c)
TARGET_LINK_LIBRARIES(radiusprobe radcli)
INSTALL(TARGETS radiusprobe
RUNTIME DESTINATION ${CMAKE_INSTALL_SBINDIR}
# Debug flags
set(DEBUG_FLAGS
-g3
-ggdb
-fno-omit-frame-pointer
-fno-optimize-sibling-calls
)
ADD_EXECUTABLE(ip-collide ip-collide.c)
TARGET_LINK_LIBRARIES(ip-collide ubox)
INSTALL(TARGETS ip-collide
RUNTIME DESTINATION ${CMAKE_INSTALL_SBINDIR}
# Release optimization flags
set(RELEASE_FLAGS
-O2
-flto
-ffunction-sections
-fdata-sections
)
set(RELEASE_LINKER_FLAGS
-Wl,--gc-sections
-flto
)
# Apply flags based on build type
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_options(${WARNING_FLAGS} ${SECURITY_FLAGS} ${DEBUG_FLAGS})
add_link_options(${SECURITY_LINKER_FLAGS})
else()
add_compile_options(${WARNING_FLAGS} ${SECURITY_FLAGS} ${RELEASE_FLAGS})
add_link_options(${SECURITY_LINKER_FLAGS} ${RELEASE_LINKER_FLAGS})
endif()
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
# Define all targets
set(TARGETS firstcontact dhcpdiscover dnsprobe radiusprobe)
# firstcontact executable
add_executable(firstcontact firstcontact.c)
target_link_libraries(firstcontact PRIVATE curl crypto ssl ubox)
# dhcpdiscover executable
add_executable(dhcpdiscover dhcpdiscover.c)
# dnsprobe executable
add_executable(dnsprobe dnsprobe.c)
target_link_libraries(dnsprobe PRIVATE ubox resolv)
# radiusprobe executable
add_executable(radiusprobe radiusprobe.c)
target_link_libraries(radiusprobe PRIVATE radcli)
# Install all targets
install(TARGETS ${TARGETS}
RUNTIME DESTINATION ${CMAKE_INSTALL_SBINDIR}
COMPONENT Runtime
)

View File

@@ -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);
}

View File

@@ -48,18 +48,18 @@
// usage: "Name: debian\n"
// usage: "Address: 127.0.0.1\n"
#include <stdio.h>
#include <resolv.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <poll.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <net/if.h>
#include <netdb.h>
#include <poll.h>
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include <libubox/ulog.h>
@@ -91,37 +91,22 @@ struct query {
int rcode, n_ns;
};
static const char *rcodes[] = {
"NOERROR",
"FORMERR",
"SERVFAIL",
"NXDOMAIN",
"NOTIMP",
"REFUSED",
"YXDOMAIN",
"YXRRSET",
"NXRRSET",
"NOTAUTH",
"NOTZONE",
"RESERVED11",
"RESERVED12",
"RESERVED13",
"RESERVED14",
"RESERVED15",
"BADVERS"
};
static const char *rcodes[] = { "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN",
"NOTIMP", "REFUSED", "YXDOMAIN", "YXRRSET",
"NXRRSET", "NOTAUTH", "NOTZONE", "RESERVED11",
"RESERVED12", "RESERVED13", "RESERVED14", "RESERVED15",
"BADVERS" };
static unsigned int default_port = 53;
static unsigned int default_retry = 1;
static unsigned int default_timeout = 2;
static int parse_reply(const unsigned char *msg, size_t len, int *bb_style_counter)
static int parse_reply(const unsigned char *msg, size_t len)
{
ns_msg handle;
ns_rr rr;
int i, n, rdlen;
const char *format = NULL;
char astr[INET6_ADDRSTRLEN], dname[MAXDNAME];
const unsigned char *cp;
@@ -132,14 +117,14 @@ static int parse_reply(const unsigned char *msg, size_t len, int *bb_style_count
for (i = 0; i < ns_msg_count(handle, ns_s_an); i++) {
if (ns_parserr(&handle, ns_s_an, i, &rr) != 0) {
//fprintf(stderr, "Unable to parse resource record: %s\n", strerror(errno));
// fprintf(stderr, "Unable to parse resource record: %s\n",
// strerror(errno));
return -1;
}
rdlen = ns_rr_rdlen(rr);
switch (ns_rr_type(rr))
{
switch (ns_rr_type(rr)) {
case ns_t_a:
if (rdlen != 4) {
// fprintf(stderr, "Unexpected A record length\n");
@@ -161,24 +146,33 @@ static int parse_reply(const unsigned char *msg, size_t len, int *bb_style_count
#endif
case ns_t_ns:
if (!format)
format = "%s\tnameserver = %s\n";
/* fall through */
case ns_t_cname:
if (!format)
format = "%s\tcanonical name = %s\n";
/* fall through */
case ns_t_ptr:
if (!format)
format = "%s\tname = %s\n";
if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
ns_rr_rdata(rr), dname, sizeof(dname)) < 0) {
//fprintf(stderr, "Unable to uncompress domain: %s\n", strerror(errno));
// fprintf(stderr, "Unable to uncompress domain: %s\n",
// strerror(errno));
return -1;
}
printf(format, ns_rr_name(rr), dname);
printf("%s\tnameserver = %s\n", ns_rr_name(rr), dname);
break;
case ns_t_cname:
if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
ns_rr_rdata(rr), dname, sizeof(dname)) < 0) {
// fprintf(stderr, "Unable to uncompress domain: %s\n",
// strerror(errno));
return -1;
}
printf("%s\tcanonical name = %s\n", ns_rr_name(rr), dname);
break;
case ns_t_ptr:
if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
ns_rr_rdata(rr), dname, sizeof(dname)) < 0) {
// fprintf(stderr, "Unable to uncompress domain: %s\n",
// strerror(errno));
return -1;
}
printf("%s\tname = %s\n", ns_rr_name(rr), dname);
break;
case ns_t_mx:
@@ -188,8 +182,10 @@ static int parse_reply(const unsigned char *msg, size_t len, int *bb_style_count
}
n = ns_get16(ns_rr_rdata(rr));
if (ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
ns_rr_rdata(rr) + 2, dname, sizeof(dname)) < 0) {
//fprintf(stderr, "Cannot uncompress MX domain: %s\n", strerror(errno));
ns_rr_rdata(rr) + 2, dname,
sizeof(dname)) < 0) {
// fprintf(stderr, "Cannot uncompress MX domain: %s\n",
// strerror(errno));
return -1;
}
printf("%s\tmail exchanger = %d %s\n", ns_rr_name(rr), n, dname);
@@ -200,7 +196,7 @@ static int parse_reply(const unsigned char *msg, size_t len, int *bb_style_count
// fprintf(stderr, "TXT record too short\n");
return -1;
}
n = *(unsigned char *)ns_rr_rdata(rr);
n = *((const unsigned char *) ns_rr_rdata(rr));
if (n > 0) {
memset(dname, 0, sizeof(dname));
memcpy(dname, ns_rr_rdata(rr) + 1, n);
@@ -217,22 +213,24 @@ static int parse_reply(const unsigned char *msg, size_t len, int *bb_style_count
printf("%s\n", ns_rr_name(rr));
cp = ns_rr_rdata(rr);
n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
cp, dname, sizeof(dname));
n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), cp,
dname, sizeof(dname));
if (n < 0) {
//fprintf(stderr, "Unable to uncompress domain: %s\n", strerror(errno));
// fprintf(stderr, "Unable to uncompress domain: %s\n",
// strerror(errno));
return -1;
}
printf("\torigin = %s\n", dname);
cp += n;
n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle),
cp, dname, sizeof(dname));
n = ns_name_uncompress(ns_msg_base(handle), ns_msg_end(handle), cp,
dname, sizeof(dname));
if (n < 0) {
//fprintf(stderr, "Unable to uncompress domain: %s\n", strerror(errno));
// fprintf(stderr, "Unable to uncompress domain: %s\n",
// strerror(errno));
return -1;
}
@@ -335,11 +333,9 @@ static void to_v4_mapped(len_and_sockaddr *a)
if (a->u.sa.sa_family != AF_INET)
return;
memcpy(a->u.sin6.sin6_addr.s6_addr + 12,
&a->u.sin.sin_addr, 4);
memcpy(a->u.sin6.sin6_addr.s6_addr + 12, &a->u.sin.sin_addr, 4);
memcpy(a->u.sin6.sin6_addr.s6_addr,
"\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
memcpy(a->u.sin6.sin6_addr.s6_addr, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12);
a->u.sin6.sin6_family = AF_INET6;
a->u.sin6.sin6_flowinfo = 0;
@@ -356,7 +352,9 @@ static void to_v4_mapped(len_and_sockaddr *a)
static int send_queries(struct ns *ns, int n_ns, struct query *queries, int n_queries)
{
int fd;
int timeout = default_timeout * 1000, retry_interval, servfail_retry = 0;
unsigned long timeout = default_timeout * 1000;
unsigned long retry_interval;
int servfail_retry = 0;
len_and_sockaddr from = {};
#if ENABLE_FEATURE_IPV6
int one = 1;
@@ -436,9 +434,9 @@ static int send_queries(struct ns *ns, int n_ns, struct query *queries, int n_qu
continue;
while (1) {
recvlen = recvfrom(fd, queries[next_query].reply,
sizeof(queries[next_query].reply), 0,
&from.u.sa, &from.len);
recvlen =
recvfrom(fd, queries[next_query].reply,
sizeof(queries[next_query].reply), 0, &from.u.sa, &from.len);
/* read error */
if (recvlen < 0)
@@ -482,7 +480,8 @@ static int send_queries(struct ns *ns, int n_ns, struct query *queries, int n_qu
if (servfail_retry && servfail_retry--) {
ns[nn].failures++;
sendto(fd, queries[qn].query, queries[qn].qlen,
MSG_NOSIGNAL, &ns[nn].addr.u.sa, ns[nn].addr.len);
MSG_NOSIGNAL, &ns[nn].addr.u.sa,
ns[nn].addr.len);
}
/* fall through */
@@ -502,8 +501,7 @@ static int send_queries(struct ns *ns, int n_ns, struct query *queries, int n_qu
next_query++;
}
}
else {
} else {
memcpy(queries[qn].reply, queries[next_query].reply, recvlen);
}
@@ -520,10 +518,8 @@ static struct ns *add_ns(struct ns **ns, int *n_ns, const char *addr)
char portstr[sizeof("65535")], *p;
len_and_sockaddr a = {};
struct ns *tmp;
struct addrinfo *ai, *aip, hints = {
.ai_flags = AI_NUMERICSERV,
.ai_socktype = SOCK_DGRAM
};
struct addrinfo *ai, *aip,
hints = { .ai_flags = AI_NUMERICSERV, .ai_socktype = SOCK_DGRAM };
if (parse_nsaddr(addr, &a)) {
/* Maybe we got a domain name, attempt to resolve it using the standard
@@ -584,8 +580,7 @@ static struct ns *add_ns(struct ns **ns, int *n_ns, const char *addr)
return &(*ns)[(*n_ns)++];
}
static struct query *add_query(struct query **queries, int *n_queries,
int type, const char *dname)
static struct query *add_query(struct query **queries, int *n_queries, int type, const char *dname)
{
struct query *tmp;
ssize_t qlen;
@@ -597,8 +592,8 @@ static struct query *add_query(struct query **queries, int *n_queries,
memset(&tmp[*n_queries], 0, sizeof(*tmp));
qlen = res_mkquery(QUERY, dname, C_IN, type, NULL, 0, NULL,
tmp[*n_queries].query, sizeof(tmp[*n_queries].query));
qlen = res_mkquery(QUERY, dname, C_IN, type, NULL, 0, NULL, tmp[*n_queries].query,
sizeof(tmp[*n_queries].query));
tmp[*n_queries].qlen = qlen;
tmp[*n_queries].name = dname;
@@ -615,8 +610,8 @@ int main(int argc, char **argv)
int n_ns = 0, n_queries = 0;
int c = 0;
char *url = "telecominfraproject.com";
char *server = "127.0.0.1";
const char *url = "telecominfraproject.com";
const char *server = "127.0.0.1";
int v6 = 0;
while (1) {
@@ -647,8 +642,7 @@ int main(int argc, char **argv)
ulog_open(ULOG_SYSLOG | ULOG_STDIO, LOG_DAEMON, "dnsprobe");
ULOG_INFO("attempting to probe dns - %s %s %s\n",
url, server, v6 ? "ipv6" : "");
ULOG_INFO("attempting to probe dns - %s %s %s\n", url, server, v6 ? "ipv6" : "");
add_query(&queries, &n_queries, v6 ? T_AAAA : T_A, url);
@@ -663,13 +657,12 @@ int main(int argc, char **argv)
}
if (queries[0].rcode != 0) {
printf("** server can't find %s: %s\n", queries[0].name,
rcodes[queries[0].rcode]);
printf("** server can't find %s: %s\n", queries[0].name, rcodes[queries[0].rcode]);
goto out;
}
if (queries[0].rlen) {
c = parse_reply(queries[0].reply, queries[0].rlen, NULL);
c = parse_reply(queries[0].reply, queries[0].rlen);
}
if (c == 0)

View File

@@ -1,6 +1,6 @@
#define _GNU_SOURCE
#include <stdio.h>
#include <getopt.h>
#include <stdio.h>
#include <curl/curl.h>
@@ -72,7 +72,8 @@ int main(int argc, char **argv)
return -1;
}
if (asprintf(&url, "https://clientauth.demo.one.digicert.com/iot/api/v2/device/%s", devid) < 0) {
if (asprintf(&url, "https://clientauth.demo.one.digicert.com/iot/api/v2/device/%s", devid) <
0) {
ULOG_ERR("failed to assemble url\n");
return -1;
}

View File

@@ -1,86 +0,0 @@
#include <arpa/inet.h>
#include <net/if.h>
#include <libubox/list.h>
#include <libubox/ulog.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct route {
struct list_head list;
char devname[64];
uint32_t domain;
uint32_t mask;
};
static struct list_head routes = LIST_HEAD_INIT(routes);
static int parse_routes(void)
{
FILE *fp = fopen("/proc/net/route", "r");
int flgs, ref, use, metric, mtu, win, ir;
struct route *route;
unsigned long g;
int r;
r = fscanf(fp, "%*[^\n]\n");
if (r < 0) {
fprintf(stderr, "failed to parse routes\n");
return -1;
}
while (1) {
route = malloc(sizeof(*route));
if (!route)
break;
memset(route, 0, sizeof(*route));
r = fscanf(fp, "%63s%x%lx%X%d%d%d%x%d%d%d\n",
route->devname, &route->domain, &g, &flgs, &ref, &use, &metric, &route->mask,
&mtu, &win, &ir);
if (r != 11 && (r < 0) && feof(fp))
break;
list_add(&route->list, &routes);
printf("1 %s %x %x\n", route->devname, ntohl(route->domain), ntohl(route->mask));
}
fclose(fp);
return 0;
}
static int find_collisions(void)
{
struct route *route;
list_for_each_entry(route, &routes, list) {
struct route *compare;
if (!route->domain || !route->mask)
continue;
list_for_each_entry(compare, &routes, list) {
if (!compare->domain || !compare->mask)
continue;
if (compare == route)
continue;
if (((route->domain & route->mask) == (compare->domain & route->mask)) ||
((route->domain & compare->mask) == (compare->domain & compare->mask))) {
ULOG_ERR("collision detected\n");
return 1;
}
}
}
ULOG_INFO("no collision detected\n");
return 0;
}
int main(int argc, char **argv)
{
ulog_open(ULOG_SYSLOG | ULOG_STDIO, LOG_DAEMON, "ip-collide");
parse_routes();
if (!list_empty(&routes))
return find_collisions();
return 0;
}

View File

@@ -1,10 +1,11 @@
#include <radcli/radcli.h>
#include <stdio.h>
#include <string.h>
#include <radcli/radcli.h>
int
main(int argc, char **argv)
int main(int argc, char **argv)
{
(void) argc;
(void) argv;
int result;
char username[128];
char passwd[AUTH_PASS_LEN + 1];