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.
27 * This file is part of Lustre, http://www.lustre.org/
29 * lustre/utils/nidlist.c
31 * Author: Jim Garlick <garlick@llnl.gov>
39 #include <netinet/in.h>
40 #include <sys/socket.h>
51 static void nl_oom(void)
53 fprintf(stderr, "%s: out of memory\n", prog);
57 NIDList nl_create(void)
61 nl = malloc(sizeof(struct nl_struct));
65 nl->nids = malloc(nl->len * sizeof(char *));
73 void nl_destroy(NIDList nl)
77 for (i = 0; i < nl->count; i++)
83 static void nl_grow(NIDList nl, int n)
86 nl->nids = realloc(nl->nids, nl->len * sizeof(char *));
91 void nl_add(NIDList nl, char *nid)
98 if (nl->count == nl->len)
99 nl_grow(nl, NL_CHUNK);
100 nl->nids[nl->count++] = cp;
103 int nl_count(NIDList nl)
108 static char *nl_nid_addr(char *nid)
115 p = strchr(addr, '@');
122 static int nl_nid_parse_addr(char *addr)
126 for (o = strlen(addr); o > 0; o--)
127 if (!isdigit(addr[o - 1]))
133 static int nl_cmp_addr(char *nid1, char *nid2, int *cflagp)
135 char *p1 = nl_nid_addr(nid1);
136 char *p2 = nl_nid_addr(nid2);
137 int res, o1, o2, cflag = 0;
139 o1 = nl_nid_parse_addr(p1);
140 o2 = nl_nid_parse_addr(p2);
142 res = strncmp(p1, p2, o1);
143 if (o1 == o2 && res == 0) {
144 res = strtoul(&p1[o1], NULL, 10) - strtoul(&p2[o2], NULL, 10);
145 if (cflagp && strlen(&p1[o1]) > 0 && strlen(&p2[o2]) > 0)
148 res = strcmp(p1, p2);
156 static int nl_cmp_lnet(char *nid1, char *nid2)
158 char *s1 = strchr(nid1, '@');
159 char *s2 = strchr(nid2, '@');
161 return strcmp(s1 ? s1 + 1 : "", s2 ? s2 + 1 : "");
164 static int nl_cmp(const void *p1, const void *p2)
168 res = nl_cmp_lnet(*(char **)p1, *(char **)p2);
170 res = nl_cmp_addr(*(char **)p1, *(char **)p2, NULL);
174 void nl_sort(NIDList nl)
176 qsort(nl->nids, nl->count, sizeof(char *), nl_cmp);
179 void nl_uniq(NIDList nl)
183 for (i = 1; i < nl->count; i++) {
184 if (!strcmp(nl->nids[i], nl->nids[i - 1])) {
186 for (j = i; j < nl->count - 1; j++)
187 nl->nids[j] = nl->nids[j + 1];
194 static char *nl_nid_lookup_ipaddr(char *nid)
196 struct addrinfo *ai, *aip;
197 char name[NI_MAXHOST] = "";
198 char *p, *addr, *lnet = NULL, *res = NULL;
201 addr = nl_nid_addr(nid);
202 if (sscanf(addr, "%d.%d.%d.%d", &x, &x, &x, &x) == 4) {
203 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 p = strchr(name, '.');
214 len = strlen(name) + 2;
221 snprintf(res, len, "%s@%s",
224 snprintf(res, len, "%s", name);
236 void nl_lookup_ip(NIDList nl)
241 for (i = 0; i < nl->count; i++) {
242 new = nl_nid_lookup_ipaddr(nl->nids[i]);
250 char *nl_string(NIDList nl, char *sep)
252 int seplen = strlen(sep);
256 for (i = 0; i < nl->count; i++)
257 len += strlen(nl->nids[i]) + seplen;
262 for (i = 0; i < nl->count; i++) {
264 strncat(s, sep, len);
265 strncat(s, nl->nids[i], len);
270 static void nl_strxcat(char *s, char **nids, int len, const int max_len)
273 char *base, *p, *lnet = NULL, *savedn = NULL;
275 p = strchr(nids[0], '@');
278 base = nl_nid_addr(nids[0]);
279 o = nl_nid_parse_addr(base);
281 for (i = 0; i < len; i++) {
282 char *addr = nl_nid_addr(nids[i]);
283 int n = strtoul(&addr[o], NULL, 10);
286 snprintf(s + strlen(s), max_len, "%s[%s", base,
289 if (n == lastn + 1) {
292 savedn = strdup(&addr[o]);
297 snprintf(s + strlen(s),
303 snprintf(s + strlen(s), max_len - strlen(s),
309 snprintf(s + strlen(s), max_len - strlen(s),
315 snprintf(s + strlen(s), max_len - strlen(s),
324 char *nl_xstring(NIDList nl, char *sep)
326 int seplen = strlen(sep);
327 int cflag, i, j, len = 1;
330 for (i = 0; i < nl->count; i++)
331 len += strlen(nl->nids[i]) + seplen;
336 for (i = 0; i < nl->count; i++) {
338 strncat(s, sep, len);
339 for (j = i + 1; j < nl->count; j++) {
340 if (nl_cmp_lnet(nl->nids[i], nl->nids[j]) != 0)
342 (void)nl_cmp_addr(nl->nids[i], nl->nids[j], &cflag);
347 nl_strxcat(s, &nl->nids[i], j - i, len);
349 strncat(s, nl->nids[i], len);