4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/utils/nidlist.c
33 * Author: Jim Garlick <garlick@llnl.gov>
41 #include <netinet/in.h>
42 #include <sys/socket.h>
53 static void nl_oom(void)
55 fprintf(stderr, "%s: out of memory\n", prog);
59 NIDList nl_create(void)
63 nl = malloc(sizeof(struct nl_struct));
67 nl->nids = malloc(nl->len * sizeof(char *));
75 void nl_destroy(NIDList nl)
79 for (i = 0; i < nl->count; i++)
85 static void nl_grow(NIDList nl, int n)
88 nl->nids = realloc(nl->nids, nl->len * sizeof(char *));
93 void nl_add(NIDList nl, char *nid)
100 if (nl->count == nl->len)
101 nl_grow(nl, NL_CHUNK);
102 nl->nids[nl->count++] = cp;
105 int nl_count(NIDList nl)
110 static char *nl_nid_addr(char *nid)
117 p = strchr(addr, '@');
124 static int nl_nid_parse_addr(char *addr)
128 for (o = strlen(addr); o > 0; o--)
129 if (!isdigit(addr[o - 1]))
135 static int nl_cmp_addr(char *nid1, char *nid2, int *cflagp)
137 char *p1 = nl_nid_addr(nid1);
138 char *p2 = nl_nid_addr(nid2);
139 int res, o1, o2, cflag = 0;
141 o1 = nl_nid_parse_addr(p1);
142 o2 = nl_nid_parse_addr(p2);
144 res = strncmp(p1, p2, o1);
145 if (o1 == o2 && res == 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 res = nl_cmp_lnet(*(char **)p1, *(char **)p2);
172 res = nl_cmp_addr(*(char **)p1, *(char **)p2, NULL);
176 void nl_sort(NIDList nl)
178 qsort(nl->nids, nl->count, sizeof(char *), nl_cmp);
181 void nl_uniq(NIDList nl)
185 for (i = 1; i < nl->count; i++) {
186 if (!strcmp(nl->nids[i], nl->nids[i - 1])) {
188 for (j = i; j < nl->count - 1; j++)
189 nl->nids[j] = nl->nids[j + 1];
196 static char *nl_nid_lookup_ipaddr(char *nid)
198 struct addrinfo *ai, *aip;
199 char name[NI_MAXHOST] = "";
200 char *p, *addr, *lnet = NULL, *res = NULL;
203 addr = nl_nid_addr(nid);
204 if (sscanf(addr, "%d.%d.%d.%d", &x, &x, &x, &x) == 4) {
205 p = strchr(nid, '@');
208 if (getaddrinfo(addr, NULL, NULL, &ai) == 0) {
209 for (aip = ai; aip != NULL; aip = aip->ai_next) {
210 if (getnameinfo(aip->ai_addr, aip->ai_addrlen,
211 name, sizeof(name), NULL, 0,
212 NI_NAMEREQD | NI_NOFQDN) == 0) {
213 p = strchr(name, '.');
216 len = strlen(name) + 2;
223 snprintf(res, len, "%s@%s",
226 snprintf(res, len, "%s", name);
238 void nl_lookup_ip(NIDList nl)
243 for (i = 0; i < nl->count; i++) {
244 new = nl_nid_lookup_ipaddr(nl->nids[i]);
252 char *nl_string(NIDList nl, char *sep)
254 int seplen = strlen(sep);
258 for (i = 0; i < nl->count; i++)
259 len += strlen(nl->nids[i]) + seplen;
264 for (i = 0; i < nl->count; i++) {
266 snprintf(s + cur, len - cur, "%s%s",
267 i > 0 ? sep : "", nl->nids[i]);
272 static void nl_strxcat(char *s, char **nids, int len, const int max_len)
275 char *base, *p, *lnet = NULL, *savedn = NULL;
277 p = strchr(nids[0], '@');
280 base = nl_nid_addr(nids[0]);
281 o = nl_nid_parse_addr(base);
283 for (i = 0; i < len; i++) {
284 char *addr = nl_nid_addr(nids[i]);
285 int n = strtoul(&addr[o], NULL, 10);
288 snprintf(s + strlen(s), max_len, "%s[%s", base,
291 if (n == lastn + 1) {
294 savedn = strdup(&addr[o]);
299 snprintf(s + strlen(s),
305 snprintf(s + strlen(s), max_len - strlen(s),
311 snprintf(s + strlen(s), max_len - strlen(s),
315 strncat(s, "]", max_len - strlen(s));
317 snprintf(s + strlen(s), max_len - strlen(s),
326 char *nl_xstring(NIDList nl, char *sep)
328 int seplen = strlen(sep);
329 int cflag, i, j, len = 1;
332 for (i = 0; i < nl->count; i++)
333 len += strlen(nl->nids[i]) + seplen;
338 for (i = 0; i < nl->count; i++) {
340 strncat(s, sep, len);
341 for (j = i + 1; j < nl->count; j++) {
342 if (nl_cmp_lnet(nl->nids[i], nl->nids[j]) != 0)
344 (void)nl_cmp_addr(nl->nids[i], nl->nids[j], &cflag);
349 nl_strxcat(s, &nl->nids[i], j - i, len);
351 strncat(s, nl->nids[i], len);