Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(749)

Unified Diff: fusl/src/network/if_nameindex.c

Issue 1714623002: [fusl] clang-format fusl (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: headers too Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: fusl/src/network/if_nameindex.c
diff --git a/fusl/src/network/if_nameindex.c b/fusl/src/network/if_nameindex.c
index 2deaef769600e5bb6b67d613eb3b3e1f65597383..db1f3844fc916cffe57e885ab1edb4ad8c9d6b36 100644
--- a/fusl/src/network/if_nameindex.c
+++ b/fusl/src/network/if_nameindex.c
@@ -10,105 +10,109 @@
#define IFADDRS_HASH_SIZE 64
struct ifnamemap {
- unsigned int hash_next;
- unsigned int index;
- unsigned char namelen;
- char name[IFNAMSIZ];
+ unsigned int hash_next;
+ unsigned int index;
+ unsigned char namelen;
+ char name[IFNAMSIZ];
};
struct ifnameindexctx {
- unsigned int num, allocated, str_bytes;
- struct ifnamemap *list;
- unsigned int hash[IFADDRS_HASH_SIZE];
+ unsigned int num, allocated, str_bytes;
+ struct ifnamemap* list;
+ unsigned int hash[IFADDRS_HASH_SIZE];
};
-static int netlink_msg_to_nameindex(void *pctx, struct nlmsghdr *h)
-{
- struct ifnameindexctx *ctx = pctx;
- struct ifnamemap *map;
- struct rtattr *rta;
- unsigned int i;
- int index, type, namelen, bucket;
+static int netlink_msg_to_nameindex(void* pctx, struct nlmsghdr* h) {
+ struct ifnameindexctx* ctx = pctx;
+ struct ifnamemap* map;
+ struct rtattr* rta;
+ unsigned int i;
+ int index, type, namelen, bucket;
- if (h->nlmsg_type == RTM_NEWLINK) {
- struct ifinfomsg *ifi = NLMSG_DATA(h);
- index = ifi->ifi_index;
- type = IFLA_IFNAME;
- rta = NLMSG_RTA(h, sizeof(*ifi));
- } else {
- struct ifaddrmsg *ifa = NLMSG_DATA(h);
- index = ifa->ifa_index;
- type = IFA_LABEL;
- rta = NLMSG_RTA(h, sizeof(*ifa));
- }
- for (; NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) {
- if (rta->rta_type != type) continue;
+ if (h->nlmsg_type == RTM_NEWLINK) {
+ struct ifinfomsg* ifi = NLMSG_DATA(h);
+ index = ifi->ifi_index;
+ type = IFLA_IFNAME;
+ rta = NLMSG_RTA(h, sizeof(*ifi));
+ } else {
+ struct ifaddrmsg* ifa = NLMSG_DATA(h);
+ index = ifa->ifa_index;
+ type = IFA_LABEL;
+ rta = NLMSG_RTA(h, sizeof(*ifa));
+ }
+ for (; NLMSG_RTAOK(rta, h); rta = RTA_NEXT(rta)) {
+ if (rta->rta_type != type)
+ continue;
- namelen = RTA_DATALEN(rta) - 1;
- if (namelen > IFNAMSIZ) return 0;
+ namelen = RTA_DATALEN(rta) - 1;
+ if (namelen > IFNAMSIZ)
+ return 0;
- /* suppress duplicates */
- bucket = index % IFADDRS_HASH_SIZE;
- i = ctx->hash[bucket];
- while (i) {
- map = &ctx->list[i-1];
- if (map->index == index &&
- map->namelen == namelen &&
- memcmp(map->name, RTA_DATA(rta), namelen) == 0)
- return 0;
- i = map->hash_next;
- }
+ /* suppress duplicates */
+ bucket = index % IFADDRS_HASH_SIZE;
+ i = ctx->hash[bucket];
+ while (i) {
+ map = &ctx->list[i - 1];
+ if (map->index == index && map->namelen == namelen &&
+ memcmp(map->name, RTA_DATA(rta), namelen) == 0)
+ return 0;
+ i = map->hash_next;
+ }
- if (ctx->num >= ctx->allocated) {
- size_t a = ctx->allocated ? ctx->allocated * 2 + 1 : 8;
- if (a > SIZE_MAX/sizeof *map) return -1;
- map = realloc(ctx->list, a * sizeof *map);
- if (!map) return -1;
- ctx->list = map;
- ctx->allocated = a;
- }
- map = &ctx->list[ctx->num];
- map->index = index;
- map->namelen = namelen;
- memcpy(map->name, RTA_DATA(rta), namelen);
- ctx->str_bytes += namelen + 1;
- ctx->num++;
- map->hash_next = ctx->hash[bucket];
- ctx->hash[bucket] = ctx->num;
- return 0;
- }
- return 0;
+ if (ctx->num >= ctx->allocated) {
+ size_t a = ctx->allocated ? ctx->allocated * 2 + 1 : 8;
+ if (a > SIZE_MAX / sizeof *map)
+ return -1;
+ map = realloc(ctx->list, a * sizeof *map);
+ if (!map)
+ return -1;
+ ctx->list = map;
+ ctx->allocated = a;
+ }
+ map = &ctx->list[ctx->num];
+ map->index = index;
+ map->namelen = namelen;
+ memcpy(map->name, RTA_DATA(rta), namelen);
+ ctx->str_bytes += namelen + 1;
+ ctx->num++;
+ map->hash_next = ctx->hash[bucket];
+ ctx->hash[bucket] = ctx->num;
+ return 0;
+ }
+ return 0;
}
-struct if_nameindex *if_nameindex()
-{
- struct ifnameindexctx _ctx, *ctx = &_ctx;
- struct if_nameindex *ifs = 0, *d;
- struct ifnamemap *s;
- char *p;
- int i;
- int cs;
+struct if_nameindex* if_nameindex() {
+ struct ifnameindexctx _ctx, *ctx = &_ctx;
+ struct if_nameindex *ifs = 0, *d;
+ struct ifnamemap* s;
+ char* p;
+ int i;
+ int cs;
- pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
- memset(ctx, 0, sizeof(*ctx));
- if (__rtnetlink_enumerate(AF_UNSPEC, AF_INET, netlink_msg_to_nameindex, ctx) < 0) goto err;
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+ memset(ctx, 0, sizeof(*ctx));
+ if (__rtnetlink_enumerate(AF_UNSPEC, AF_INET, netlink_msg_to_nameindex, ctx) <
+ 0)
+ goto err;
- ifs = malloc(sizeof(struct if_nameindex[ctx->num+1]) + ctx->str_bytes);
- if (!ifs) goto err;
+ ifs = malloc(sizeof(struct if_nameindex[ctx->num + 1]) + ctx->str_bytes);
+ if (!ifs)
+ goto err;
- p = (char*)(ifs + ctx->num + 1);
- for (i = ctx->num, d = ifs, s = ctx->list; i; i--, s++, d++) {
- d->if_index = s->index;
- d->if_name = p;
- memcpy(p, s->name, s->namelen);
- p += s->namelen;
- *p++ = 0;
- }
- d->if_index = 0;
- d->if_name = 0;
+ p = (char*)(ifs + ctx->num + 1);
+ for (i = ctx->num, d = ifs, s = ctx->list; i; i--, s++, d++) {
+ d->if_index = s->index;
+ d->if_name = p;
+ memcpy(p, s->name, s->namelen);
+ p += s->namelen;
+ *p++ = 0;
+ }
+ d->if_index = 0;
+ d->if_name = 0;
err:
- pthread_setcancelstate(cs, 0);
- free(ctx->list);
- errno = ENOBUFS;
- return ifs;
+ pthread_setcancelstate(cs, 0);
+ free(ctx->list);
+ errno = ENOBUFS;
+ return ifs;
}

Powered by Google App Engine
This is Rietveld 408576698