]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/addr_parsing.c
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
19 #if defined(__FreeBSD__) || defined(_AIX)
20 #include <sys/socket.h>
21 #include <netinet/in.h>
27 int safe_cat(char **pstr
, int *plen
, int pos
, const char *str2
)
29 int len2
= strlen(str2
);
31 //printf("safe_cat '%s' max %d pos %d '%s' len %d\n", *pstr, *plen, pos, str2, len2);
32 while (*plen
< pos
+ len2
+ 1) {
35 void *_realloc
= NULL
;
36 if ((_realloc
= realloc(*pstr
, (size_t)*plen
)) == NULL
) {
37 printf("Out of memory\n");
40 *pstr
= (char *)_realloc
;
42 //printf("safe_cat '%s' max %d pos %d '%s' len %d\n", *pstr, *plen, pos, str2, len2);
45 strncpy((*pstr
)+pos
, str2
, len2
);
46 (*pstr
)[pos
+len2
] = '\0';
51 char *resolve_addrs(const char *orig_str
)
54 char *tok
, *saveptr
= NULL
;
56 char *buf
= strdup(orig_str
);
57 const char *delim
= ",; ";
60 new_str
= (char *)malloc(len
);
68 tok
= strtok_r(buf
, delim
, &saveptr
);
72 struct addrinfo
*res
, *ores
;
73 char *firstcolon
, *lastcolon
, *bracecolon
;
77 firstcolon
= strchr(tok
, ':');
78 lastcolon
= strrchr(tok
, ':');
79 bracecolon
= strstr(tok
, "]:");
82 if (firstcolon
&& firstcolon
== lastcolon
) {
83 /* host:port or a.b.c.d:port */
85 port_str
= firstcolon
+ 1;
86 } else if (bracecolon
) {
88 port_str
= bracecolon
+ 1;
92 if (port_str
&& !*port_str
)
96 tok
[strlen(tok
)-1] == ']') {
97 tok
[strlen(tok
)-1] = 0;
102 //printf("name '%s' port '%s'\n", tok, port_str);
104 memset(&hint
, 0, sizeof(hint
));
105 hint
.ai_family
= AF_UNSPEC
;
106 hint
.ai_socktype
= SOCK_STREAM
;
107 hint
.ai_protocol
= IPPROTO_TCP
;
109 r
= getaddrinfo(tok
, port_str
, &hint
, &res
);
111 printf("server name not found: %s (%s)\n", tok
,
118 /* build resolved addr list */
121 char host
[40], port
[40];
122 getnameinfo(res
->ai_addr
, res
->ai_addrlen
,
125 NI_NUMERICSERV
| NI_NUMERICHOST
);
126 /*printf(" host %s port %s flags %d family %d socktype %d proto %d sanonname %s\n",
128 res->ai_flags, res->ai_family, res->ai_socktype, res->ai_protocol,
129 res->ai_canonname);*/
130 if (res
->ai_family
== AF_INET6
)
131 brackets
= 1; /* always surround ipv6 addrs with brackets */
133 pos
= safe_cat(&new_str
, &len
, pos
, "[");
134 pos
= safe_cat(&new_str
, &len
, pos
, host
);
136 pos
= safe_cat(&new_str
, &len
, pos
, "]");
138 pos
= safe_cat(&new_str
, &len
, pos
, ":");
139 pos
= safe_cat(&new_str
, &len
, pos
, port
);
143 pos
= safe_cat(&new_str
, &len
, pos
, ",");
147 tok
= strtok_r(NULL
, delim
, &saveptr
);
149 pos
= safe_cat(&new_str
, &len
, pos
, ",");
153 //printf("new_str is '%s'\n", new_str);