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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/utils/nidlist.c
36 * Author: Jim Garlick <garlick@llnl.gov>
44 #include <netinet/in.h>
45 #include <sys/socket.h>
58 static void nl_oom(void)
60 fprintf(stderr, "%s: out of memory\n", prog);
64 NIDList nl_create(void)
68 if (!(nl = malloc(sizeof(struct nl_struct))))
71 if (!(nl->nids = malloc(nl->len * sizeof(char *))))
78 void nl_destroy(NIDList nl)
82 for (i = 0; i < nl->count; i++)
88 static void nl_grow(NIDList nl, int n)
91 if (!(nl->nids = realloc(nl->nids, nl->len * sizeof(char *))))
95 void nl_add(NIDList nl, char *nid)
99 if (!(cp = strdup(nid)))
101 if (nl->count == nl->len)
102 nl_grow(nl, NL_CHUNK);
103 nl->nids[nl->count++] = cp;
106 int nl_count(NIDList nl)
111 static char *nl_nid_addr(char *nid)
115 if (!(addr = strdup(nid)))
117 if ((p = strchr(addr, '@')))
123 static int nl_nid_parse_addr(char *addr)
127 for (o = strlen(addr); o > 0; o--)
128 if (!isdigit(addr[o - 1]))
134 static int nl_cmp_addr(char *nid1, char *nid2, int *cflagp)
136 char *p1 = nl_nid_addr(nid1);
137 char *p2 = nl_nid_addr(nid2);
138 int res, o1, o2, cflag = 0;
140 o1 = nl_nid_parse_addr(p1);
141 o2 = nl_nid_parse_addr(p2);
143 if (o1 == o2 && (res = strncmp(p1, p2, o1)) == 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 if ((res = nl_cmp_lnet(*(char **)p1, *(char **)p2)) == 0)
169 res = nl_cmp_addr(*(char **)p1, *(char **)p2, NULL);
173 void nl_sort(NIDList nl)
175 qsort(nl->nids, nl->count, sizeof(char *), nl_cmp);
178 void nl_uniq(NIDList nl)
182 for (i = 1; i < nl->count; i++) {
183 if (!strcmp(nl->nids[i], nl->nids[i - 1])) {
185 for (j = i; j < nl->count - 1; j++)
186 nl->nids[j] = nl->nids[j + 1];
193 static char *nl_nid_lookup_ipaddr(char *nid)
195 struct addrinfo *ai, *aip;
196 char name[NI_MAXHOST] = "";
197 char *p, *addr, *lnet = NULL, *res = NULL;
200 addr = nl_nid_addr(nid);
201 if (sscanf(addr, "%d.%d.%d.%d", &x, &x, &x, &x) == 4) {
202 if ((p = strchr(nid, '@')))
204 if (getaddrinfo(addr, NULL, NULL, &ai) == 0) {
205 for (aip = ai; aip != NULL; aip = aip->ai_next) {
206 if (getnameinfo(aip->ai_addr, aip->ai_addrlen,
207 name, sizeof(name), NULL, 0,
208 NI_NAMEREQD | NI_NOFQDN) == 0) {
209 if ((p = strchr(name, '.')))
211 len = strlen(name) + strlen(lnet) + 2;
212 if (!(res = malloc(len)))
214 snprintf(res, len, "%s@%s", name, lnet);
226 void nl_lookup_ip(NIDList nl)
231 for (i = 0; i < nl->count; i++) {
232 if ((new = nl_nid_lookup_ipaddr(nl->nids[i]))) {
239 char *nl_string(NIDList nl, char *sep)
241 int seplen = strlen(sep);
245 for (i = 0; i < nl->count; i++)
246 len += strlen(nl->nids[i]) + seplen;
247 if (!(s = malloc(len)))
250 for (i = 0; i < nl->count; i++) {
253 strcat(s, nl->nids[i]);
258 static void nl_strxcat(char *s, char **nids, int len)
261 char *base, *p, *lnet = NULL, *savedn = NULL;
263 if ((p = strchr(nids[0], '@')))
265 base = nl_nid_addr(nids[0]);
266 o = nl_nid_parse_addr(base);
268 for (i = 0; i < len; i++) {
269 char *addr = nl_nid_addr(nids[i]);
270 int n = strtoul(&addr[o], NULL, 10);
273 sprintf(s + strlen(s), "%s[%s", base, &addr[o]);
275 if (n == lastn + 1) {
278 if (!(savedn = strdup(&addr[o])))
282 sprintf(s + strlen(s), "-%s", savedn);
286 sprintf(s + strlen(s), ",%s", &addr[o]);
291 sprintf(s + strlen(s), "-%s", savedn);
296 sprintf(s + strlen(s), "@%s", lnet);
304 char *nl_xstring(NIDList nl, char *sep)
306 int seplen = strlen(sep);
307 int cflag, i, j, len = 1;
310 for (i = 0; i < nl->count; i++)
311 len += strlen(nl->nids[i]) + seplen;
312 if (!(s = malloc(len)))
315 for (i = 0; i < nl->count; i++) {
318 for (j = i + 1; j < nl->count; j++) {
319 if (nl_cmp_lnet(nl->nids[i], nl->nids[j]) != 0)
321 (void)nl_cmp_addr(nl->nids[i], nl->nids[j], &cflag);
326 nl_strxcat(s, &nl->nids[i], j - i);
328 strcat(s, nl->nids[i]);