n = ifc.ifc_len;
for (i = 0; i < n; i+= ifreq_size(*ifrp) ) {
ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
- strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
+ strncpy_IFNAMSIZ(ifr.ifr_name, ifrp->ifr_name);
#ifdef SIOCGIFHWADDR
if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
continue;
char *xstrndup(const char *s, int n) FAST_FUNC;
void overlapping_strcpy(char *dst, const char *src) FAST_FUNC;
char *safe_strncpy(char *dst, const char *src, size_t size) FAST_FUNC;
+char *strncpy_IFNAMSIZ(char *dst, const char *src) FAST_FUNC;
/* Guaranteed to NOT be a macro (smallest code). Saves nearly 2k on uclibc.
* But potentially slow, don't use in one-billion-times loops */
int bb_putchar(int ch) FAST_FUNC;
return fcntl(fd, F_SETFD, FD_CLOEXEC);
}
+char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src)
+{
+#ifndef IFNAMSIZ
+ enum { IFNAMSIZ = 16 };
+#endif
+ return strncpy(dst, src, IFNAMSIZ);
+}
+
/* Convert unsigned long long value into compact 4-char
* representation. Examples: "1234", "1.2k", " 27M", "123T"
* String is not terminated (buf[4] is untouched) */
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
- strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name) - 1);
+ strncpy_IFNAMSIZ(ifr.ifr_name, device);
/* We use ifr.ifr_name in error msg so that problem
* with truncated name will be visible */
ioctl_or_perror_and_die(sock_fd, SIOCGIFINDEX, &ifr, err_str, "not found");
if (!if_indextoname(bridx[i], brname))
bb_perror_msg_and_die("can't get bridge name for index %d", i);
- strncpy(ifr.ifr_name, brname, IFNAMSIZ);
+ strncpy_IFNAMSIZ(ifr.ifr_name, brname);
arm_ioctl(args, BRCTL_GET_BRIDGE_INFO,
(unsigned long) &bi, 0);
if (!*argv) /* all but 'addif/delif' need at least two arguments */
bb_show_usage();
- strncpy(ifr.ifr_name, br, IFNAMSIZ);
+ strncpy_IFNAMSIZ(ifr.ifr_name, br);
if (key == ARG_addif || key == ARG_delif) { /* addif or delif */
brif = *argv;
ifr.ifr_ifindex = if_nametoindex(brif);
{
struct ifreq if_hwaddr;
- strncpy(if_hwaddr.ifr_name, ifname, sizeof(if_hwaddr.ifr_name));
+ strncpy_IFNAMSIZ(if_hwaddr.ifr_name, ifname);
ioctl_or_perror_and_die(s, SIOCGIFHWADDR, &if_hwaddr, "SIOCGIFHWADDR on %s failed", ifname);
memcpy(outpack+6, if_hwaddr.ifr_hwaddr.sa_data, 6);
#if defined(PF_PACKET)
{
struct ifreq ifr;
- strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
xioctl(s, SIOCGIFINDEX, &ifr);
memset(&whereto, 0, sizeof(whereto));
whereto.sll_family = AF_PACKET;
sockfd = xsocket(AF_INET, SOCK_DGRAM, 0);
/* get interface name */
- strncpy(ifr.ifr_name, *argv, IFNAMSIZ);
+ strncpy_IFNAMSIZ(ifr.ifr_name, *argv);
/* Process the remaining arguments. */
while (*++argv != (char *) NULL) {
/* NOINLINEs are placed where it results in smaller code (gcc 4.3.1) */
-static void strncpy_IFNAMSIZ(char *dst, const char *src)
-{
- strncpy(dst, src, IFNAMSIZ);
-}
-
static int ioctl_on_skfd(unsigned request, struct ifreq *ifr)
{
return ioctl(skfd, request, ifr);
}
new = xzalloc(sizeof(*new));
- strncpy(new->name, name, IFNAMSIZ);
+ strncpy_IFNAMSIZ(new->name, name);
nextp = ife ? &ife->next : &int_list;
new->prev = ife;
new->next = *nextp;
skfd = xsocket(AF_INET, SOCK_DGRAM, 0);
- strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
close(skfd);
return -1;
}
ife->flags = ifr.ifr_flags;
- strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
memset(ife->hwaddr, 0, 32);
if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0)
memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
ife->type = ifr.ifr_hwaddr.sa_family;
- strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
ife->metric = 0;
if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0)
ife->metric = ifr.ifr_metric;
- strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
ife->mtu = 0;
if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0)
ife->mtu = ifr.ifr_mtu;
memset(&ife->map, 0, sizeof(struct ifmap));
#ifdef SIOCGIFMAP
- strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
ife->map = ifr.ifr_map;
#endif
#ifdef HAVE_TXQUEUELEN
- strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
ife->tx_queue_len = -1; /* unknown value */
if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0)
ife->tx_queue_len = ifr.ifr_qlen;
ife->tx_queue_len = -1; /* unknown value */
#endif
- strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
ifr.ifr_addr.sa_family = AF_INET;
memset(&ife->addr, 0, sizeof(struct sockaddr));
if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) {
ife->has_ip = 1;
ife->addr = ifr.ifr_addr;
- strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0)
ife->dstaddr = ifr.ifr_dstaddr;
- strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0)
ife->broadaddr = ifr.ifr_broadaddr;
- strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
memset(&ife->netmask, 0, sizeof(struct sockaddr));
if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0)
ife->netmask = ifr.ifr_netmask;
return;
memset(&ifr, 0, sizeof(ifr));
- strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, name);
if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) {
close(s);
return;
struct ifreq ifr;
int fd;
- strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, dev);
fd = get_ctl_fd();
xioctl(fd, SIOCGIFFLAGS, &ifr);
if ((ifr.ifr_flags ^ flags) & mask) {
struct ifreq ifr;
int fd;
- strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
- strncpy(ifr.ifr_newname, newdev, sizeof(ifr.ifr_newname));
+ strncpy_IFNAMSIZ(ifr.ifr_name, dev);
+ strncpy_IFNAMSIZ(ifr.ifr_newname, newdev);
fd = get_ctl_fd();
xioctl(fd, SIOCSIFNAME, &ifr);
close(fd);
s = get_ctl_fd();
memset(&ifr, 0, sizeof(ifr));
- strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, dev);
ifr.ifr_qlen = qlen;
xioctl(s, SIOCSIFTXQLEN, &ifr);
close(s);
s = get_ctl_fd();
memset(&ifr, 0, sizeof(ifr));
- strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, dev);
ifr.ifr_mtu = mtu;
xioctl(s, SIOCSIFMTU, &ifr);
close(s);
s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
memset(&ifr, 0, sizeof(ifr));
- strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, dev);
xioctl(s, SIOCGIFINDEX, &ifr);
memset(&me, 0, sizeof(me));
int alen;
memset(ifr, 0, sizeof(*ifr));
- strncpy(ifr->ifr_name, dev, sizeof(ifr->ifr_name));
+ strncpy_IFNAMSIZ(ifr->ifr_name, dev);
ifr->ifr_hwaddr.sa_family = hatype;
alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/;
struct ifreq ifr;
int fd;
- strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, dev);
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
xioctl(fd, SIOCGIFINDEX, &ifr);
close(fd);
int fd;
int err;
- strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, dev);
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr);
close(fd);
int fd;
int err;
- strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
ifr.ifr_ifru.ifru_data = (void*)p;
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
err = ioctl_or_warn(fd, SIOCGETTUNNEL, &ifr);
int fd;
if (cmd == SIOCCHGTUNNEL && p->name[0]) {
- strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
} else {
- strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
}
ifr.ifr_ifru.ifru_data = (void*)p;
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
int fd;
if (p->name[0]) {
- strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
} else {
- strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
}
ifr.ifr_ifru.ifru_data = (void*)p;
fd = xsocket(AF_INET, SOCK_DGRAM, 0);
int key;
memset(p, 0, sizeof(*p));
- memset(&medium, 0, sizeof(medium));
+ medium[0] = '\0';
p->iph.version = 4;
p->iph.ihl = 5;
p->iph.saddr = get_addr32(*argv);
} else if (key == ARG_dev) {
NEXT_ARG();
- strncpy(medium, *argv, IFNAMSIZ-1);
+ strncpy_IFNAMSIZ(medium, *argv);
} else if (key == ARG_ttl) {
unsigned uval;
NEXT_ARG();
}
if (p->name[0])
duparg2("name", *argv);
- strncpy(p->name, *argv, IFNAMSIZ);
+ strncpy_IFNAMSIZ(p->name, *argv);
if (cmd == SIOCCHGTUNNEL && count == 0) {
struct ip_tunnel_parm old_p;
memset(&old_p, 0, sizeof(old_p));
}
}
-
/* Return value becomes exitcode. It's okay to not return at all */
static int do_add(int cmd, char **argv)
{
struct ifreq ifr;
int tmp;
- strncpy(ifr.ifr_name, name, IFNAMSIZ);
+ strncpy_IFNAMSIZ(ifr.ifr_name, name);
ifr.ifr_ifindex = -1;
tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
close(sock_fd);
/* Find the current interface name and copy it to ifr.ifr_name */
memset(&ifr, 0, sizeof(struct ifreq));
- strncpy(ifr.ifr_name, token[0], sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, token[0]);
#if ENABLE_FEATURE_NAMEIF_EXTENDED
/* Check for driver etc. */
if (devname) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
- strncpy(ifr.ifr_name, devname, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, devname);
xioctl(skfd, SIOGIFINDEX, &ifr);
rt.rtmsg_ifindex = ifr.ifr_ifindex;
}
* SIOCGIFFLAGS stomps over it because the requests
* are returned in a union.)
*/
- strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, ifrp->ifr_name);
if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
if (errno == ENXIO)
continue;
fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
ifr.ifr_addr.sa_family = AF_INET;
- strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, interface);
if (addr) {
if (ioctl_or_perror(fd, SIOCGIFADDR, &ifr,
"is interface %s up and configured?", interface)
if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
} else {
- strncpy(ifr.device1, argv[1], IFNAMSIZ);
+ strncpy_IFNAMSIZ(ifr.device1, argv[1]);
p = argv[2];
/* I suppose one could try to combine some of the function calls below,
// get the interface's ethernet address
//memset(&ifr, 0, sizeof(ifr));
- strncpy(ifr.ifr_name, argv_intf, sizeof(ifr.ifr_name));
+ strncpy_IFNAMSIZ(ifr.ifr_name, argv_intf);
xioctl(sock_fd, SIOCGIFHWADDR, &ifr);
memcpy(ð_addr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);