static struct if_nameindex * if_nameindex_netlink (void) { struct netlink_handle nh = { 0, 0, 0, NULL, NULL }; struct if_nameindex *idx = NULL; if (__no_netlink_support || __netlink_open (&nh) < 0) return NULL; /* Tell the kernel that we wish to get a list of all active interfaces. Collect all data for every interface. */ if (__netlink_request (&nh, RTM_GETLINK) < 0) goto exit_free; /* Count the interfaces. */ unsigned int nifs = 0; for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next) { struct nlmsghdr *nlh; size_t size = nlp->size; if (nlp->nlh == NULL) continue; /* Walk through all entries we got from the kernel and look, which message type they contain. */ for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size)) { /* Check if the message is what we want. */ if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq) continue; if (nlh->nlmsg_type == NLMSG_DONE) break; /* ok */ if (nlh->nlmsg_type == RTM_NEWLINK) ++nifs; } } idx = malloc ((nifs + 1) * sizeof (struct if_nameindex)); if (idx == NULL) { nomem: __set_errno (ENOBUFS); goto exit_free; } /* Add the interfaces. */ nifs = 0; for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next) { struct nlmsghdr *nlh; size_t size = nlp->size; if (nlp->nlh == NULL) continue; /* Walk through all entries we got from the kernel and look, which message type they contain. */ for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size)) { /* Check if the message is what we want. */ if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq) continue; if (nlh->nlmsg_type == NLMSG_DONE) break; /* ok */ if (nlh->nlmsg_type == RTM_NEWLINK) { struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh); struct rtattr *rta = IFLA_RTA (ifim); size_t rtasize = IFLA_PAYLOAD (nlh); idx[nifs].if_index = ifim->ifi_index; while (RTA_OK (rta, rtasize)) { char *rta_data = RTA_DATA (rta); size_t rta_payload = RTA_PAYLOAD (rta); if (rta->rta_type == IFLA_IFNAME) { idx[nifs].if_name = __strndup (rta_data, rta_payload); if (idx[nifs].if_name == NULL) { idx[nifs].if_index = 0; if_freenameindex (idx); idx = NULL; goto nomem; } break; } rta = RTA_NEXT (rta, rtasize); } ++nifs; } } } idx[nifs].if_index = 0; idx[nifs].if_name = NULL; exit_free: __netlink_free_handle (&nh); __netlink_close (&nh); return idx; }
/* Replace any occurrences of the string STR in ARGZ with WITH, reallocating ARGZ as necessary. If REPLACE_COUNT is non-zero, *REPLACE_COUNT will be incremented by number of replacements performed. */ error_t __argz_replace (char **argz, size_t *argz_len, const char *str, const char *with, unsigned *replace_count) { error_t err = 0; if (str && *str) { char *arg = 0; char *src = *argz; size_t src_len = *argz_len; char *dst = 0; size_t dst_len = 0; int delayed_copy = 1; /* True while we've avoided copying anything. */ size_t str_len = strlen (str), with_len = strlen (with); while (!err && (arg = argz_next (src, src_len, arg))) { char *match = strstr (arg, str); if (match) { char *from = match + str_len; size_t to_len = match - arg; char *to = __strndup (arg, to_len); while (to && from) { str_append (&to, &to_len, with, with_len); if (to) { match = strstr (from, str); if (match) { str_append (&to, &to_len, from, match - from); from = match + str_len; } else { str_append (&to, &to_len, from, strlen (from)); from = 0; } } } if (to) { if (delayed_copy) /* We avoided copying SRC to DST until we found a match; now that we've done so, copy everything from the start of SRC. */ { if (arg > src) err = __argz_append (&dst, &dst_len, src, (arg - src)); delayed_copy = 0; } if (! err) err = __argz_add (&dst, &dst_len, to); free (to); } else err = ENOMEM; if (replace_count) (*replace_count)++; } else if (! delayed_copy) err = __argz_add (&dst, &dst_len, arg); } if (! err) { if (! delayed_copy) /* We never found any instances of str. */ { free (src); *argz = dst; *argz_len = dst_len; } } else if (dst_len > 0) free (dst); } return err; }