1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/nidlist.c
38 * Author: Jim Garlick <garlick@llnl.gov>
46 #include <netinet/in.h>
47 #include <sys/socket.h>
60 static void nl_oom(void)
62 fprintf(stderr, "%s: out of memory\n", prog);
66 NIDList nl_create(void)
70 if (!(nl = malloc(sizeof(struct nl_struct))))
73 if (!(nl->nids = malloc(nl->len * sizeof(char *))))
80 void nl_destroy(NIDList nl)
84 for (i = 0; i < nl->count; i++)
90 static void nl_grow(NIDList nl, int n)
93 if (!(nl->nids = realloc(nl->nids, nl->len * sizeof(char *))))
97 void nl_add(NIDList nl, char *nid)
101 if (!(cp = strdup(nid)))
103 if (nl->count == nl->len)
104 nl_grow(nl, NL_CHUNK);
105 nl->nids[nl->count++] = cp;
108 int nl_count(NIDList nl)
113 static char *nl_nid_addr(char *nid)
117 if (!(addr = strdup(nid)))
119 if ((p = strchr(addr, '@')))
125 static int nl_nid_parse_addr(char *addr)
129 for (o = strlen(addr); o > 0; o--)
130 if (!isdigit(addr[o - 1]))
136 static int nl_cmp_addr(char *nid1, char *nid2, int *cflagp)
138 char *p1 = nl_nid_addr(nid1);
139 char *p2 = nl_nid_addr(nid2);
140 int res, o1, o2, cflag = 0;
142 o1 = nl_nid_parse_addr(p1);
143 o2 = nl_nid_parse_addr(p2);
145 if (o1 == o2 && (res = strncmp(p1, p2, o1)) == 0) {
146 res = strtoul(&p1[o1], NULL, 10) - strtoul(&p2[o2], NULL, 10);
147 if (cflagp && strlen(&p1[o1]) > 0 && strlen(&p2[o2]) > 0)
150 res = strcmp(p1, p2);
158 static int nl_cmp_lnet(char *nid1, char *nid2)
160 char *s1 = strchr(nid1, '@');
161 char *s2 = strchr(nid2, '@');
163 return strcmp(s1 ? s1 + 1 : "", s2 ? s2 + 1 : "");
166 static int nl_cmp(const void *p1, const void *p2)
170 if ((res = nl_cmp_lnet(*(char **)p1, *(char **)p2)) == 0)
171 res = nl_cmp_addr(*(char **)p1, *(char **)p2, NULL);
175 void nl_sort(NIDList nl)
177 qsort(nl->nids, nl->count, sizeof(char *), nl_cmp);
180 void nl_uniq(NIDList nl)
184 for (i = 1; i < nl->count; i++) {
185 if (!strcmp(nl->nids[i], nl->nids[i - 1])) {
187 for (j = i; j < nl->count - 1; j++)
188 nl->nids[j] = nl->nids[j + 1];
195 static char *nl_nid_lookup_ipaddr(char *nid)
197 struct addrinfo *ai, *aip;
198 char name[NI_MAXHOST] = "";
199 char *p, *addr, *lnet = NULL, *res = NULL;
202 addr = nl_nid_addr(nid);
203 if (sscanf(addr, "%d.%d.%d.%d", &x, &x, &x, &x) == 4) {
204 if ((p = strchr(nid, '@')))
206 if (getaddrinfo(addr, NULL, NULL, &ai) == 0) {
207 for (aip = ai; aip != NULL; aip = aip->ai_next) {
208 if (getnameinfo(aip->ai_addr, aip->ai_addrlen,
209 name, sizeof(name), NULL, 0,
210 NI_NAMEREQD | NI_NOFQDN) == 0) {
211 if ((p = strchr(name, '.')))
213 len = strlen(name) + strlen(lnet) + 2;
214 if (!(res = malloc(len)))
216 snprintf(res, len, "%s@%s", name, lnet);
228 void nl_lookup_ip(NIDList nl)
233 for (i = 0; i < nl->count; i++) {
234 if ((new = nl_nid_lookup_ipaddr(nl->nids[i]))) {
241 char *nl_string(NIDList nl, char *sep)
243 int seplen = strlen(sep);
247 for (i = 0; i < nl->count; i++)
248 len += strlen(nl->nids[i]) + seplen;
249 if (!(s = malloc(len)))
252 for (i = 0; i < nl->count; i++) {
255 strcat(s, nl->nids[i]);
260 static void nl_strxcat(char *s, char **nids, int len)
263 char *base, *p, *lnet = NULL, *savedn = NULL;
265 if ((p = strchr(nids[0], '@')))
267 base = nl_nid_addr(nids[0]);
268 o = nl_nid_parse_addr(base);
270 for (i = 0; i < len; i++) {
271 char *addr = nl_nid_addr(nids[i]);
272 int n = strtoul(&addr[o], NULL, 10);
275 sprintf(s + strlen(s), "%s[%s", base, &addr[o]);
277 if (n == lastn + 1) {
280 if (!(savedn = strdup(&addr[o])))
284 sprintf(s + strlen(s), "-%s", savedn);
288 sprintf(s + strlen(s), ",%s", &addr[o]);
293 sprintf(s + strlen(s), "-%s", savedn);
298 sprintf(s + strlen(s), "@%s", lnet);
306 char *nl_xstring(NIDList nl, char *sep)
308 int seplen = strlen(sep);
309 int cflag, i, j, len = 1;
312 for (i = 0; i < nl->count; i++)
313 len += strlen(nl->nids[i]) + seplen;
314 if (!(s = malloc(len)))
317 for (i = 0; i < nl->count; i++) {
320 for (j = i + 1; j < nl->count; j++) {
321 if (nl_cmp_lnet(nl->nids[i], nl->nids[j]) != 0)
323 (void)nl_cmp_addr(nl->nids[i], nl->nids[j], &cflag);
328 nl_strxcat(s, &nl->nids[i], j - i);
330 strcat(s, nl->nids[i]);