diff --git a/CMakeLists.txt b/CMakeLists.txt index a8058a0..fb527d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,8 +77,8 @@ ENDMACRO(COMPARE_VERSION_STRINGS) #################################### -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -fno-strict-overflow -pedantic -Wall -Wextra -Wimplicit-fallthrough=0 -Wformat=2 -Wformat-nonliteral -Wformat-security -Wshadow -Wpointer-arith -Wmissing-prototypes -Wunused-const-variable=0 -D_GNU_SOURCE -DNK_USE_CAPABILITY") -set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -fno-strict-overflow -pedantic -Wall -Wextra -Wimplicit-fallthrough=0 -Wformat-security -Wshadow -Wpointer-arith -Wmissing-prototypes -Wunused-const-variable=0 -D_GNU_SOURCE -DNK_USE_CAPABILITY") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -fno-strict-overflow -pedantic -Wall -Wextra -Wimplicit-fallthrough=0 -Wformat=2 -Wformat-nonliteral -Wformat-security -Wshadow -Wpointer-arith -Wmissing-prototypes -Wunused-const-variable=0 -Wsign-conversion -D_GNU_SOURCE -DNK_USE_CAPABILITY") +set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -fno-strict-overflow -pedantic -Wall -Wextra -Wimplicit-fallthrough=0 -Wformat-security -Wshadow -Wpointer-arith -Wmissing-prototypes -Wunused-const-variable=0 -Wsign-conversion -D_GNU_SOURCE -DNK_USE_CAPABILITY") if (WIN32) set(OSNAME "Win32") diff --git a/src/arp.c b/src/arp.c index df07e91..f910cdf 100644 --- a/src/arp.c +++ b/src/arp.c @@ -395,9 +395,10 @@ static int arp_is_query_reply(struct arpMsg am[static 1]) static int arp_gen_probe_wait(struct client_state_t cs[static 1]) { + int range = arp_probe_max - arp_probe_min; + if (range < 1000) range = 1000; // This is not a uniform distribution but it doesn't matter here. - return arp_probe_min + (nk_random_u32(&cs->rnd_state) & 0x7fffffffu) - % (arp_probe_max - arp_probe_min); + return arp_probe_min + (int)(nk_random_u32(&cs->rnd_state) % (unsigned)range); } int arp_defense_timeout(struct client_state_t cs[static 1], long long nowts) diff --git a/src/cfg.rl b/src/cfg.rl index 524f2ee..789368e 100644 --- a/src/cfg.rl +++ b/src/cfg.rl @@ -169,7 +169,7 @@ struct cfgparse { } } action rfkill_idx { - uint32_t t = atoi(ccfg.buf); + uint32_t t = (uint32_t)atoi(ccfg.buf); client_config.rfkillIdx = t; client_config.enable_rfkill = true; } @@ -250,7 +250,7 @@ static void parse_cfgfile(const char fname[static 1]) if (rc == 0) { l[lc] = '\n'; rc = 1; reached_eof = true; // Emulate a LF to terminate the line. } - lc += rc; + lc += (size_t)rc; size_t lstart = 0, lend = 0, consumed = 0; for (; lend < lc; ++lend) { @@ -350,7 +350,7 @@ void parse_cmdline(int argc, char *argv[]) snl = snprintf(argb + argbl, sizeof argb - argbl, "%s", argv[i]); if (snl < 0 || (size_t)snl >= sizeof argb) suicide("error parsing command line option: option too long"); - argbl += snl; + argbl += (size_t)snl; } if (argbl == 0) return; diff --git a/src/dhcp.c b/src/dhcp.c index d81aadc..9aa29c4 100644 --- a/src/dhcp.c +++ b/src/dhcp.c @@ -106,8 +106,14 @@ static ssize_t send_dhcp_unicast(struct client_state_t cs[static 1], client_config.interface, __func__); goto out_fd; } + const size_t el = (size_t)endloc + 1; + if (el > sizeof payload->options) { + log_error("%s: (%s) Invalid value of endloc. Not sending.", + client_config.interface, __func__); + goto out_fd; + } size_t payload_len = - sizeof *payload - (sizeof payload->options - 1 - endloc); + sizeof *payload - (sizeof payload->options - el); if (!carrier_isup()) { log_error("%s: (%s) carrier down; write would fail", client_config.interface, __func__); @@ -213,8 +219,7 @@ static ssize_t get_raw_packet(struct client_state_t cs[static 1], } size_t l = iphdrlen - sizeof packet.ip - sizeof packet.udp; if (l > sizeof *payload) { - log_error("%s: Packet received that is too long (%zu bytes).", - l); + log_error("%s: Packet received that is too long (%zu bytes).", l); return -2; } if (packet.udp.check && !udp_checksum(&packet)) { @@ -225,7 +230,7 @@ static ssize_t get_raw_packet(struct client_state_t cs[static 1], if (srcaddr) *srcaddr = packet.ip.saddr; memcpy(payload, &packet.data, l); - return l; + return (ssize_t)l; } // Broadcast a DHCP message using a raw socket. @@ -247,7 +252,14 @@ static ssize_t send_dhcp_raw(struct dhcpmsg payload[static 1]) close(fd); return ret; } - size_t padding = sizeof payload->options - 1 - endloc; + const size_t el = (size_t)endloc + 1; + if (el > sizeof payload->options) { + log_error("%s: (%s) Invalid value of endloc. Not sending.", + client_config.interface, __func__); + close(fd); + return ret; + } + size_t padding = sizeof payload->options - el; size_t iud_len = sizeof(struct ip_udp_dhcp_packet) - padding; size_t ud_len = sizeof(struct udp_dhcp_packet) - padding; @@ -415,7 +427,7 @@ static void add_options_vendor_hostname(struct dhcpmsg packet[static 1]) } // Initialize a DHCP client packet that will be sent to a server -static void init_packet(struct dhcpmsg packet[static 1], char type) +static void init_packet(struct dhcpmsg packet[static 1], uint8_t type) { packet->op = 1; // BOOTREQUEST (client) packet->htype = 1; // ETH_10MB diff --git a/src/ifchange.c b/src/ifchange.c index 98106e4..011f70c 100644 --- a/src/ifchange.c +++ b/src/ifchange.c @@ -49,14 +49,14 @@ static struct dhcpmsg cfg_packet; // Copy of the current configuration packet. static int ifcmd_raw(char buf[static 1], size_t buflen, const char optname[static 1], - char *optdata, ssize_t optlen) + char *optdata, size_t optlen) { if (!optdata) { log_warning("%s: (%s) '%s' option has no data", client_config.interface, __func__, optname); return -1; } - if (optlen > INT_MAX || optlen < 0) { + if (optlen > INT_MAX) { log_warning("%s: (%s) '%s' option optlen out of bounds", client_config.interface, __func__, optname); return -1; @@ -80,14 +80,14 @@ static int ifcmd_raw(char buf[static 1], size_t buflen, static int ifcmd_bytes(char buf[static 1], size_t buflen, const char optname[static 1], - uint8_t *optdata, ssize_t optlen) + uint8_t *optdata, size_t optlen) { return ifcmd_raw(buf, buflen, optname, (char *)optdata, optlen); } static int ifcmd_u8(char buf[static 1], size_t buflen, const char optname[static 1], - uint8_t *optdata, ssize_t optlen) + uint8_t *optdata, size_t optlen) { if (!optdata || optlen < 1) return -1; @@ -101,7 +101,7 @@ static int ifcmd_u8(char buf[static 1], size_t buflen, static int ifcmd_u16(char buf[static 1], size_t buflen, const char optname[static 1], - uint8_t *optdata, ssize_t optlen) + uint8_t *optdata, size_t optlen) { if (!optdata || optlen < 2) return -1; @@ -117,12 +117,12 @@ static int ifcmd_u16(char buf[static 1], size_t buflen, static int ifcmd_s32(char buf[static 1], size_t buflen, const char optname[static 1], - uint8_t *optdata, ssize_t optlen) + uint8_t *optdata, size_t optlen) { if (!optdata || optlen < 4) return -1; char numbuf[16]; - int32_t v; + uint32_t v; memcpy(&v, optdata, 4); v = ntohl(v); ssize_t olen = snprintf(numbuf, sizeof numbuf, "%d", v); @@ -133,7 +133,7 @@ static int ifcmd_s32(char buf[static 1], size_t buflen, static int ifcmd_ip(char buf[static 1], size_t buflen, const char optname[static 1], - uint8_t *optdata, ssize_t optlen) + uint8_t *optdata, size_t optlen) { if (!optdata || optlen < 4) return -1; @@ -144,7 +144,7 @@ static int ifcmd_ip(char buf[static 1], size_t buflen, static int ifcmd_iplist(char out[static 1], size_t outlen, const char optname[static 1], - uint8_t *optdata, ssize_t optlen) + uint8_t *optdata, size_t optlen) { char buf[2048]; char ipbuf[INET_ADDRSTRLEN]; @@ -159,20 +159,20 @@ static int ifcmd_iplist(char out[static 1], size_t outlen, if (wc < 0 || (size_t)wc >= sizeof buf) return -1; optoff += 4; - bufoff += wc; - while (optlen - optoff >= 4) { + bufoff += (size_t)wc; + while (optlen >= 4 + optoff) { inet_ntop(AF_INET, optdata + optoff, ipbuf, sizeof ipbuf); wc = snprintf(buf + bufoff, sizeof buf, ",%s", ipbuf); if (wc < 0 || (size_t)wc >= sizeof buf) return -1; optoff += 4; - bufoff += wc; + bufoff += (size_t)wc; } return ifcmd_raw(out, outlen, optname, buf, strlen(buf)); } static int ifchd_cmd(char b[static 1], size_t bl, uint8_t *od, - ssize_t ol, uint8_t code) + size_t ol, uint8_t code) { switch (code) { case DCODE_ROUTER: return ifcmd_ip(b, bl, "routr", od, ol); @@ -196,7 +196,7 @@ static int ifchwrite(const char buf[static 1], size_t count) { ssize_t r = safe_write(ifchSock[0], buf, count); if (r < 0 || (size_t)r != count) { - log_error("%s: (%s) write failed: %d", client_config.interface); + log_error("%s: (%s) write failed: %d", client_config.interface, __func__, r); return -1; } char data[256], control[256]; @@ -255,7 +255,7 @@ static size_t send_client_ip(char out[static 1], size_t olen, { uint8_t optdata[MAX_DOPT_SIZE], olddata[MAX_DOPT_SIZE]; char ip[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN], bc[INET_ADDRSTRLEN]; - ssize_t optlen, oldlen; + size_t optlen, oldlen; bool change_ipaddr = false; bool have_subnet = false; bool change_subnet = false; @@ -309,14 +309,14 @@ static size_t send_client_ip(char out[static 1], size_t olen, memset(out, 0, olen); return 0; } - return snlen; + return (size_t)snlen; } static size_t send_cmd(char out[static 1], size_t olen, struct dhcpmsg packet[static 1], uint8_t code) { uint8_t optdata[MAX_DOPT_SIZE], olddata[MAX_DOPT_SIZE]; - ssize_t optlen, oldlen; + size_t optlen, oldlen; optlen = get_dhcp_opt(packet, code, optdata, sizeof optdata); if (!optlen) @@ -325,7 +325,7 @@ static size_t send_cmd(char out[static 1], size_t olen, if (oldlen == optlen && !memcmp(optdata, olddata, optlen)) return 0; int r = ifchd_cmd(out, olen, optdata, optlen, code); - return r > 0 ? r : 0; + return r > 0 ? (size_t)r : 0; } int ifchange_bind(struct client_state_t cs[static 1], diff --git a/src/ifchd-parse.rl b/src/ifchd-parse.rl index 0f7e53c..8aa31c9 100644 --- a/src/ifchd-parse.rl +++ b/src/ifchd-parse.rl @@ -43,26 +43,26 @@ action XSt { arg_start = p; } action IpEn { - arg_len = p - arg_start; - if (arg_len < sizeof ip4_addr) { + ptrdiff_t arg_len = p - arg_start; + if (arg_len > 0 && (size_t)arg_len < sizeof ip4_addr) { have_ip = true; - memcpy(ip4_addr, arg_start, arg_len); + memcpy(ip4_addr, arg_start, (size_t)arg_len); } ip4_addr[arg_len] = 0; } action SnEn { - arg_len = p - arg_start; - if (arg_len < sizeof ip4_subnet) { + ptrdiff_t arg_len = p - arg_start; + if (arg_len > 0 && (size_t)arg_len < sizeof ip4_subnet) { have_subnet = true; - memcpy(ip4_subnet, arg_start, arg_len); + memcpy(ip4_subnet, arg_start, (size_t)arg_len); } ip4_subnet[arg_len] = 0; } action BcEn { - arg_len = p - arg_start; - if (arg_len < sizeof ip4_bcast) { + ptrdiff_t arg_len = p - arg_start; + if (arg_len > 0 && (size_t)arg_len < sizeof ip4_bcast) { have_ip = true; - memcpy(ip4_bcast, arg_start, arg_len); + memcpy(ip4_bcast, arg_start, (size_t)arg_len); } ip4_bcast[arg_len] = 0; } @@ -85,7 +85,6 @@ static int perform_ip4set(const char buf[static 1], size_t len) const char *pe = p + len; const char *eof = pe; const char *arg_start; - size_t arg_len; int cs = 0; bool have_ip = false; bool have_subnet = false; @@ -119,11 +118,12 @@ static int perform_ip4set(const char buf[static 1], size_t len) action Reset { cl.state = STATE_NOTHING; } action ArgSt { arg_start = p; } action ArgEn { - arg_len = p - arg_start; - if (arg_len > sizeof tb - 1) { + ptrdiff_t al = p - arg_start; + if (al < 0 || (size_t)al > sizeof tb - 1) { log_line("command argument would overflow"); return -99; } + arg_len = (size_t)al; memcpy(tb, arg_start, arg_len); tb[arg_len] = 0; } @@ -144,9 +144,11 @@ static int perform_ip4set(const char buf[static 1], size_t len) case STATE_WINS: pr = perform_wins(tb, arg_len); break; case STATE_CARRIER: pr = perform_carrier(); break; default: + arg_len = 0; log_line("error: invalid state in dispatch_work"); return -99; } + arg_len = 0; if (pr == -99) return -99; cmdf |= pr; @@ -210,13 +212,14 @@ int execute_buffer(const char newbuf[static 1]) const char *p = buf; const char *pe = p + init_siz; const char *arg_start; - size_t arg_len; + size_t arg_len = 0; int cs = 0; %% write init; %% write exec; - size_t bytes_left = pe - p; + ptrdiff_t blt = pe - p; + size_t bytes_left = blt >= 0 ? (size_t)blt : 0; if (bytes_left > 0) { size_t taken = init_siz - bytes_left; ssize_t ilen = snprintf(cl.ibuf, sizeof cl.ibuf, "%s", buf + taken); diff --git a/src/ifchd.c b/src/ifchd.c index 8467410..88f2d55 100644 --- a/src/ifchd.c +++ b/src/ifchd.c @@ -95,7 +95,7 @@ static int write_append_fd(int to_fd, int from_fd, const char descr[static 1]) } char buf[4096]; - size_t from_fd_len = lse; + size_t from_fd_len = (size_t)lse; while (from_fd_len > 0) { const size_t to_read = from_fd_len <= sizeof buf ? from_fd_len : sizeof buf; ssize_t r = safe_read(from_fd, buf, to_read); diff --git a/src/ifset.c b/src/ifset.c index 3b7aa01..d32dae0 100644 --- a/src/ifset.c +++ b/src/ifset.c @@ -149,7 +149,7 @@ static ssize_t rtnl_do_send(int fd, const uint8_t *sbuf, size_t slen, return -1; } -static ssize_t rtnl_if_flags_send(int fd, int type, int ifi_flags) +static ssize_t rtnl_if_flags_send(int fd, int type, unsigned ifi_flags) { uint8_t request[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(sizeof(struct ifinfomsg))]; @@ -199,7 +199,8 @@ static ssize_t rtnl_addr_broadcast_send(int fd, int type, int ifa_flags, ifaddrmsg->ifa_prefixlen = prefixlen; ifaddrmsg->ifa_flags = ifa_flags; ifaddrmsg->ifa_scope = ifa_scope; - ifaddrmsg->ifa_index = client_config.ifindex; + // Linux is inconsistent about the type of ifindex. + ifaddrmsg->ifa_index = (uint32_t)client_config.ifindex; if (ipaddr) { if (nl_add_rtattr(header, sizeof request, IFA_LOCAL, @@ -316,8 +317,7 @@ static int link_flags_get(int fd, uint32_t flags[static 1]) ret = nl_recv_buf(fd, nlbuf, sizeof nlbuf); if (ret < 0) return -2; - if (nl_foreach_nlmsg(nlbuf, ret, seq, 0, link_flags_get_do, - &ipx) < 0) + if (nl_foreach_nlmsg(nlbuf, (size_t)ret, seq, 0, link_flags_get_do, &ipx) < 0) return -3; } while (ret > 0); if (ipx.got_flags) { @@ -444,14 +444,14 @@ static int ipbcpfx_clear_others(int fd, uint32_t ipaddr, uint32_t bcast, .prefixlen = prefixlen, .already_ok = false }; ssize_t ret; uint32_t seq = ifset_nl_seq++; - if (nl_sendgetaddr4(fd, seq, client_config.ifindex) < 0) + if (nl_sendgetaddr4(fd, seq, (uint32_t)client_config.ifindex) < 0) return -1; do { ret = nl_recv_buf(fd, nlbuf, sizeof nlbuf); if (ret < 0) return -2; - if (nl_foreach_nlmsg(nlbuf, ret, seq, 0, + if (nl_foreach_nlmsg(nlbuf, (size_t)ret, seq, 0, ipbcpfx_clear_others_do, &ipx) < 0) return -3; } while (ret > 0); diff --git a/src/ndhc.c b/src/ndhc.c index 6ab6735..fc73b6e 100644 --- a/src/ndhc.c +++ b/src/ndhc.c @@ -80,7 +80,7 @@ struct client_state_t cs = { .listenFd = -1, .arpFd = -1, .nlFd = -1, - .nlPortId = -1, + .nlPortId = 0, .rfkillFd = -1, .dhcp_wake_ts = -1, .routerArp = "\0\0\0\0\0\0", @@ -357,7 +357,7 @@ static void do_ndhc_work(void) // We can't do anything while the iface is disabled, anyway. // Suspend might cause link state change notifications to be // missed, so we use a non-infinite timeout. - timeout = 2000 + nk_random_u32(&cs.rnd_state) % 3000; + timeout = 2000 + (int)(nk_random_u32(&cs.rnd_state) % 3000); continue; } @@ -370,7 +370,7 @@ static void do_ndhc_work(void) arp_wake_ts <= nowts, sev_signal); if (dhcp_ok == COR_ERROR) { - timeout = 2000 + nk_random_u32(&cs.rnd_state) % 3000; + timeout = 2000 + (int)(nk_random_u32(&cs.rnd_state) % 3000); continue; } diff --git a/src/ndhc.h b/src/ndhc.h index a23a8f3..77230ff 100644 --- a/src/ndhc.h +++ b/src/ndhc.h @@ -40,7 +40,7 @@ struct client_state_t { long long dhcp_wake_ts; int ifDeconfig; // Set if the interface has already been deconfigured. int epollFd, signalFd, listenFd, arpFd, nlFd, rfkillFd; - int nlPortId; + uint32_t nlPortId; unsigned int num_dhcp_requests; uint32_t clientAddr, serverAddr, srcAddr, routerAddr; uint32_t lease, xid; diff --git a/src/netlink.c b/src/netlink.c index e12f1bf..bc9db0f 100644 --- a/src/netlink.c +++ b/src/netlink.c @@ -99,8 +99,7 @@ int nl_event_get(struct client_state_t cs[static 1]) ret = nl_recv_buf(cs->nlFd, nlbuf, sizeof nlbuf); if (ret < 0) break; - if (nl_foreach_nlmsg(nlbuf, ret, 0, cs->nlPortId, nl_process_msgs, 0) - < 0) + if (nl_foreach_nlmsg(nlbuf, (size_t)ret, 0, cs->nlPortId, nl_process_msgs, 0) < 0) break; } while (ret > 0); return nl_process_msgs_return; @@ -155,7 +154,7 @@ static int handle_getifdata(int fd, uint32_t seq) ret = nl_recv_buf(fd, nlbuf, sizeof nlbuf); if (ret < 0) return -1; - if (nl_foreach_nlmsg(nlbuf, ret, seq, 0, + if (nl_foreach_nlmsg(nlbuf, (size_t)ret, seq, 0, do_handle_getifdata, &got_ifdata) < 0) return -1; } while (ret > 0); diff --git a/src/nl.c b/src/nl.c index c84fd4d..1bfe3d5 100644 --- a/src/nl.c +++ b/src/nl.c @@ -148,7 +148,7 @@ int nl_foreach_nlmsg(char buf[static 1], size_t blen, uint32_t seq, uint32_t por return 0; } -static int nl_sendgetlink_do(int fd, int seq, int ifindex, int by_ifindex) +static int nl_sendgetlink_do(int fd, uint32_t seq, int ifindex, int by_ifindex) { char nlbuf[512]; struct nlmsghdr *nlh = (struct nlmsghdr *)nlbuf; @@ -182,17 +182,17 @@ static int nl_sendgetlink_do(int fd, int seq, int ifindex, int by_ifindex) return 0; } -int nl_sendgetlinks(int fd, int seq) +int nl_sendgetlinks(int fd, uint32_t seq) { return nl_sendgetlink_do(fd, seq, 0, 0); } -int nl_sendgetlink(int fd, int seq, int ifindex) +int nl_sendgetlink(int fd, uint32_t seq, int ifindex) { return nl_sendgetlink_do(fd, seq, ifindex, 1); } -static int nl_sendgetaddr_do(int fd, int seq, int ifindex, int by_ifindex, +static int nl_sendgetaddr_do(int fd, uint32_t seq, uint32_t ifindex, int by_ifindex, int afamily, int by_afamily) { char nlbuf[512]; @@ -228,37 +228,37 @@ static int nl_sendgetaddr_do(int fd, int seq, int ifindex, int by_ifindex, return 0; } -int nl_sendgetaddrs(int fd, int seq) +int nl_sendgetaddrs(int fd, uint32_t seq) { return nl_sendgetaddr_do(fd, seq, 0, 0, 0, 0); } -int nl_sendgetaddrs4(int fd, int seq) +int nl_sendgetaddrs4(int fd, uint32_t seq) { return nl_sendgetaddr_do(fd, seq, 0, 0, AF_INET, 1); } -int nl_sendgetaddrs6(int fd, int seq) +int nl_sendgetaddrs6(int fd, uint32_t seq) { return nl_sendgetaddr_do(fd, seq, 0, 0, AF_INET6, 1); } -int nl_sendgetaddr(int fd, int seq, int ifindex) +int nl_sendgetaddr(int fd, uint32_t seq, uint32_t ifindex) { return nl_sendgetaddr_do(fd, seq, ifindex, 1, 0, 0); } -int nl_sendgetaddr4(int fd, int seq, int ifindex) +int nl_sendgetaddr4(int fd, uint32_t seq, uint32_t ifindex) { return nl_sendgetaddr_do(fd, seq, ifindex, 1, AF_INET, 1); } -int nl_sendgetaddr6(int fd, int seq, int ifindex) +int nl_sendgetaddr6(int fd, uint32_t seq, uint32_t ifindex) { return nl_sendgetaddr_do(fd, seq, ifindex, 1, AF_INET6, 1); } -int nl_open(int nltype, int nlgroup, int *nlportid) +int nl_open(int nltype, unsigned nlgroup, uint32_t *nlportid) { int fd; fd = socket(AF_NETLINK, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, nltype); diff --git a/src/nl.h b/src/nl.h index a17ce36..d227228 100644 --- a/src/nl.h +++ b/src/nl.h @@ -54,15 +54,15 @@ typedef void (*nlmsg_foreach_fn)(const struct nlmsghdr *, void *); int nl_foreach_nlmsg(char *buf, size_t blen, uint32_t seq, uint32_t portid, nlmsg_foreach_fn pfn, void *fnarg); -int nl_sendgetlinks(int fd, int seq); -int nl_sendgetlink(int fd, int seq, int ifindex); -int nl_sendgetaddr(int fd, int seq, int ifindex); -int nl_sendgetaddr4(int fd, int seq, int ifindex); -int nl_sendgetaddr6(int fd, int seq, int ifindex); -int nl_sendgetaddrs(int fd, int seq); -int nl_sendgetaddrs4(int fd, int seq); -int nl_sendgetaddrs6(int fd, int seq); +int nl_sendgetlinks(int fd, uint32_t seq); +int nl_sendgetlink(int fd, uint32_t seq, int ifindex); +int nl_sendgetaddr(int fd, uint32_t seq, uint32_t ifindex); +int nl_sendgetaddr4(int fd, uint32_t seq, uint32_t ifindex); +int nl_sendgetaddr6(int fd, uint32_t seq, uint32_t ifindex); +int nl_sendgetaddrs(int fd, uint32_t seq); +int nl_sendgetaddrs4(int fd, uint32_t seq); +int nl_sendgetaddrs6(int fd, uint32_t seq); -int nl_open(int nltype, int nlgroup, int *nlportid); +int nl_open(int nltype, unsigned nlgroup, uint32_t *nlportid); #endif /* NK_NL_H_ */ diff --git a/src/options.c b/src/options.c index ab663c7..96d6058 100644 --- a/src/options.c +++ b/src/options.c @@ -31,6 +31,7 @@ #include #include #include "nk/log.h" +#include #include "options.h" @@ -74,10 +75,10 @@ static int overload_value(const struct dhcpmsg * const packet) return ol; // ol == 0 } -static void do_get_dhcp_opt(const uint8_t *sbuf, ssize_t slen, uint8_t code, - uint8_t *dbuf, ssize_t dlen, ssize_t *didx) +static void do_get_dhcp_opt(const uint8_t *sbuf, size_t slen, uint8_t code, + uint8_t *dbuf, size_t dlen, size_t *didx) { - ssize_t i = 0; + size_t i = 0; while (i < slen) { if (sbuf[i] == DCODE_PADDING) { ++i; @@ -87,11 +88,11 @@ static void do_get_dhcp_opt(const uint8_t *sbuf, ssize_t slen, uint8_t code, break; if (i >= slen - 2) break; - ssize_t soptsiz = sbuf[i+1]; + size_t soptsiz = sbuf[i+1]; if (sbuf[i] == code) { - if (dlen - *didx < soptsiz) + if (dlen < soptsiz + *didx) return; - if (slen - i - 2 < soptsiz) + if (slen < soptsiz + i + 2) return; memcpy(dbuf + *didx, sbuf+i+2, soptsiz); *didx += soptsiz; @@ -100,11 +101,11 @@ static void do_get_dhcp_opt(const uint8_t *sbuf, ssize_t slen, uint8_t code, } } -ssize_t get_dhcp_opt(const struct dhcpmsg * const packet, uint8_t code, - uint8_t *dbuf, ssize_t dlen) +size_t get_dhcp_opt(const struct dhcpmsg * const packet, uint8_t code, + uint8_t *dbuf, size_t dlen) { int ol = overload_value(packet); - ssize_t didx = 0; + size_t didx = 0; do_get_dhcp_opt(packet->options, sizeof packet->options, code, dbuf, dlen, &didx); if (ol & 1) @@ -121,12 +122,12 @@ ssize_t get_end_option_idx(const struct dhcpmsg * const packet) { for (size_t i = 0; i < sizeof packet->options; ++i) { if (packet->options[i] == DCODE_END) - return i; + return (ssize_t)i; if (packet->options[i] == DCODE_PADDING) continue; if (i + 1 >= sizeof packet->options) break; - i += packet->options[i+1] + 1; + i += (size_t)packet->options[i+1] + 1; } log_warning("get_end_option_idx: Did not find DCODE_END marker."); return -1; @@ -156,14 +157,14 @@ size_t add_option_string(struct dhcpmsg *packet, uint8_t code, log_warning("add_option_string: Buffer has no DCODE_END marker."); return 0; } - if (end + len >= sizeof packet->options) { + if ((size_t)end + len >= sizeof packet->options) { log_warning("add_option_string: No space for option 0x%02x.", code); return 0; } packet->options[end] = code; packet->options[end+1] = slen; memcpy(packet->options + end + 2, str, slen); - packet->options[end+len] = DCODE_END; + packet->options[(size_t)end+len] = DCODE_END; return len; } @@ -304,10 +305,9 @@ void add_option_hostname(struct dhcpmsg *packet, const char * const hostname, uint32_t get_option_router(const struct dhcpmsg * const packet) { - ssize_t ol; uint32_t ret = 0; uint8_t buf[MAX_DOPT_SIZE]; - ol = get_dhcp_opt(packet, DCODE_ROUTER, buf, sizeof buf); + const size_t ol = get_dhcp_opt(packet, DCODE_ROUTER, buf, sizeof buf); if (ol == sizeof ret) memcpy(&ret, buf, sizeof ret); return ret; @@ -315,10 +315,9 @@ uint32_t get_option_router(const struct dhcpmsg * const packet) uint8_t get_option_msgtype(const struct dhcpmsg * const packet) { - ssize_t ol; uint8_t ret = 0; uint8_t buf[MAX_DOPT_SIZE]; - ol = get_dhcp_opt(packet, DCODE_MSGTYPE, buf, sizeof buf); + const size_t ol = get_dhcp_opt(packet, DCODE_MSGTYPE, buf, sizeof buf); if (ol == sizeof ret) ret = buf[0]; return ret; @@ -326,11 +325,10 @@ uint8_t get_option_msgtype(const struct dhcpmsg * const packet) uint32_t get_option_serverid(const struct dhcpmsg *const packet, int *found) { - ssize_t ol; uint32_t ret = 0; uint8_t buf[MAX_DOPT_SIZE]; *found = 0; - ol = get_dhcp_opt(packet, DCODE_SERVER_ID, buf, sizeof buf); + const size_t ol = get_dhcp_opt(packet, DCODE_SERVER_ID, buf, sizeof buf); if (ol == sizeof ret) { *found = 1; memcpy(&ret, buf, sizeof ret); @@ -340,10 +338,9 @@ uint32_t get_option_serverid(const struct dhcpmsg *const packet, int *found) uint32_t get_option_leasetime(const struct dhcpmsg * const packet) { - ssize_t ol; uint32_t ret = 0; uint8_t buf[MAX_DOPT_SIZE]; - ol = get_dhcp_opt(packet, DCODE_LEASET, buf, sizeof buf); + const size_t ol = get_dhcp_opt(packet, DCODE_LEASET, buf, sizeof buf); if (ol == sizeof ret) { memcpy(&ret, buf, sizeof ret); ret = ntohl(ret); @@ -357,8 +354,6 @@ size_t get_option_clientid(const struct dhcpmsg * const packet, char *cbuf, { if (clen < 1) return 0; - ssize_t ol = get_dhcp_opt(packet, DCODE_CLIENT_ID, - (uint8_t *)cbuf, clen); - return ol > 0 ? ol : 0; + return get_dhcp_opt(packet, DCODE_CLIENT_ID, (uint8_t *)cbuf, clen); } diff --git a/src/options.h b/src/options.h index 17ec2c9..c2d42ba 100644 --- a/src/options.h +++ b/src/options.h @@ -56,8 +56,8 @@ #define MAX_DOPT_SIZE 500 -ssize_t get_dhcp_opt(const struct dhcpmsg * const packet, uint8_t code, - uint8_t *dbuf, ssize_t dlen); +size_t get_dhcp_opt(const struct dhcpmsg * const packet, uint8_t code, + uint8_t *dbuf, size_t dlen); ssize_t get_end_option_idx(const struct dhcpmsg * const packet); size_t add_option_string(struct dhcpmsg *packet, uint8_t code, diff --git a/src/state.c b/src/state.c index 974b2b4..00ce4f0 100644 --- a/src/state.c +++ b/src/state.c @@ -70,7 +70,7 @@ static int delay_timeout(struct client_state_t cs[static 1], size_t numpackets) if (numpackets < sizeof tot) to = tot[numpackets]; // Distribution is a bit biased but it doesn't really matter. - return to * 1000 + (nk_random_u32(&cs->rnd_state) & 0x7fffffffu) % 1000; + return to * 1000 + (int)(nk_random_u32(&cs->rnd_state) % 1000); } static void reinit_shared_deconfig(struct client_state_t cs[static 1])