Move get_clientid_string() to cfg.rl.

This commit is contained in:
Nicholas J. Kain 2023-02-13 00:18:16 -05:00
parent aa684baad7
commit d9e0afdc7c
No known key found for this signature in database
4 changed files with 664 additions and 629 deletions

1214
cfg.c

File diff suppressed because it is too large Load Diff

38
cfg.rl
View File

@ -9,6 +9,7 @@
#include <limits.h> #include <limits.h>
#include "ndhc-defines.h" #include "ndhc-defines.h"
#include "cfg.h" #include "cfg.h"
#include "sys.h"
#include "arp.h" #include "arp.h"
#include "ndhc.h" #include "ndhc.h"
#include "ifchd.h" #include "ifchd.h"
@ -17,6 +18,43 @@
#include "nk/privs.h" #include "nk/privs.h"
#include "nk/io.h" #include "nk/io.h"
static bool xisxdigit(int c)
{
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
}
static bool is_string_hwaddr(const char *str, size_t slen)
{
return slen == 17 && str[2] == ':' && str[5] == ':' && str[8] == ':' &&
str[11] == ':' && str[14] == ':' &&
xisxdigit(str[0]) && xisxdigit(str[1]) && xisxdigit(str[3]) &&
xisxdigit(str[4]) && xisxdigit(str[6]) && xisxdigit(str[7]) &&
xisxdigit(str[9]) && xisxdigit(str[10]) && xisxdigit(str[12]) &&
xisxdigit(str[13]) && xisxdigit(str[15]) && xisxdigit(str[16]);
}
static int get_clientid_string(const char *str, size_t slen)
{
if (!slen)
return -1;
if (!is_string_hwaddr(str, slen)) {
client_config.clientid[0] = 0;
memcpy(client_config.clientid + 1, str,
min_size_t(slen, sizeof client_config.clientid - 1));
client_config.clientid_len = slen + 1;
return 0;
}
uint8_t mac[6];
for (size_t i = 0; i < sizeof mac; ++i)
mac[i] = strtol(str+i*3, (char **)0, 16);
client_config.clientid[0] = 1; // Ethernet MAC type
memcpy(client_config.clientid + 1, mac,
min_size_t(sizeof mac, sizeof client_config.clientid - 1));
client_config.clientid_len = 7;
return 1;
}
static void copy_cmdarg(char *dest, const char *src, static void copy_cmdarg(char *dest, const char *src,
size_t destlen, const char *argname) size_t destlen, const char *argname)
{ {

40
ndhc.c
View File

@ -197,46 +197,6 @@ static void setup_signals_ndhc(void)
suicide("sigaction failed"); suicide("sigaction failed");
} }
static bool xisxdigit(int c)
{
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
}
static int is_string_hwaddr(const char *str, size_t slen)
{
if (slen == 17 && str[2] == ':' && str[5] == ':' && str[8] == ':' &&
str[11] == ':' && str[14] == ':' &&
xisxdigit(str[0]) && xisxdigit(str[1]) && xisxdigit(str[3]) &&
xisxdigit(str[4]) && xisxdigit(str[6]) && xisxdigit(str[7]) &&
xisxdigit(str[9]) && xisxdigit(str[10]) && xisxdigit(str[12]) &&
xisxdigit(str[13]) && xisxdigit(str[15]) && xisxdigit(str[16])
)
return 1;
return 0;
}
int get_clientid_string(const char *str, size_t slen)
{
if (!slen)
return -1;
if (!is_string_hwaddr(str, slen)) {
client_config.clientid[0] = 0;
memcpy(client_config.clientid + 1, str,
min_size_t(slen, sizeof client_config.clientid - 1));
client_config.clientid_len = slen + 1;
return 0;
}
uint8_t mac[6];
for (size_t i = 0; i < sizeof mac; ++i)
mac[i] = strtol(str+i*3, (char **)0, 16);
client_config.clientid[0] = 1; // Ethernet MAC type
memcpy(client_config.clientid + 1, mac,
min_size_t(sizeof mac, sizeof client_config.clientid - 1));
client_config.clientid_len = 7;
return 1;
}
static void fail_if_state_dir_dne(void) static void fail_if_state_dir_dne(void)
{ {
if (strlen(state_dir) == 0) if (strlen(state_dir) == 0)

1
ndhc.h
View File

@ -83,7 +83,6 @@ bool carrier_isup(void);
void set_client_addr(const char *v); void set_client_addr(const char *v);
void show_usage(void); void show_usage(void);
void signal_exit(int status); void signal_exit(int status);
int get_clientid_string(const char *str, size_t slen);
void print_version(void); void print_version(void);
static inline void advance_xid(struct client_state_t *cs) { static inline void advance_xid(struct client_state_t *cs) {