1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2011 New Dream Network
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(__FreeBSD__) || defined(_AIX)
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include <netdb.h>
#define BUF_SIZE 128
int safe_cat(char **pstr, int *plen, int pos, const char *str2)
{
int len2 = strlen(str2);
//printf("safe_cat '%s' max %d pos %d '%s' len %d\n", *pstr, *plen, pos, str2, len2);
while (*plen < pos + len2 + 1) {
*plen += BUF_SIZE;
void *_realloc = realloc(*pstr, (size_t)*plen);
if (!_realloc) {
printf("Out of memory\n");
exit(1);
} else {
*pstr = _realloc;
}
//printf("safe_cat '%s' max %d pos %d '%s' len %d\n", *pstr, *plen, pos, str2, len2);
}
strncpy((*pstr)+pos, str2, len2);
(*pstr)[pos+len2] = '\0';
return pos + len2;
}
char *resolve_addrs(const char *orig_str)
{
int len = BUF_SIZE;
char *new_str = (char *)malloc(len);
if (!new_str) {
return NULL;
}
char *saveptr = NULL;
char *buf = strdup(orig_str);
const char *delim = ",; ";
char *tok = strtok_r(buf, delim, &saveptr);
int pos = 0;
while (tok) {
struct addrinfo hint;
struct addrinfo *res, *ores;
char *firstcolon, *lastcolon, *bracecolon;
int r;
int brackets = 0;
firstcolon = strchr(tok, ':');
lastcolon = strrchr(tok, ':');
bracecolon = strstr(tok, "]:");
char *port_str = 0;
if (firstcolon && firstcolon == lastcolon) {
/* host:port or a.b.c.d:port */
*firstcolon = 0;
port_str = firstcolon + 1;
} else if (bracecolon) {
/* [ipv6addr]:port */
port_str = bracecolon + 1;
*port_str = 0;
port_str++;
}
if (port_str && !*port_str)
port_str = NULL;
if (*tok == '[' &&
tok[strlen(tok)-1] == ']') {
tok[strlen(tok)-1] = 0;
tok++;
brackets = 1;
}
//printf("name '%s' port '%s'\n", tok, port_str);
// FIPS zeroization audit 20191115: this memset is fine.
memset(&hint, 0, sizeof(hint));
hint.ai_family = AF_UNSPEC;
hint.ai_socktype = SOCK_STREAM;
hint.ai_protocol = IPPROTO_TCP;
r = getaddrinfo(tok, port_str, &hint, &res);
if (r < 0) {
printf("server name not found: %s (%s)\n", tok,
gai_strerror(r));
free(new_str);
free(buf);
return 0;
}
/* build resolved addr list */
ores = res;
while (res) {
char host[40], port[40];
getnameinfo(res->ai_addr, res->ai_addrlen,
host, sizeof(host),
port, sizeof(port),
NI_NUMERICSERV | NI_NUMERICHOST);
/*printf(" host %s port %s flags %d family %d socktype %d proto %d sanonname %s\n",
host, port,
res->ai_flags, res->ai_family, res->ai_socktype, res->ai_protocol,
res->ai_canonname);*/
if (res->ai_family == AF_INET6)
brackets = 1; /* always surround ipv6 addrs with brackets */
if (brackets)
pos = safe_cat(&new_str, &len, pos, "[");
pos = safe_cat(&new_str, &len, pos, host);
if (brackets)
pos = safe_cat(&new_str, &len, pos, "]");
if (port_str) {
pos = safe_cat(&new_str, &len, pos, ":");
pos = safe_cat(&new_str, &len, pos, port);
}
res = res->ai_next;
if (res)
pos = safe_cat(&new_str, &len, pos, ",");
}
freeaddrinfo(ores);
tok = strtok_r(NULL, delim, &saveptr);
if (tok)
pos = safe_cat(&new_str, &len, pos, ",");
}
//printf("new_str is '%s'\n", new_str);
free(buf);
return new_str;
}
|