8 #include "qemu_socket.h"
11 # define AI_ADDRCONFIG 0
14 static int sockets_debug = 0;
15 static const int on=1, off=0;
17 static int inet_getport(struct addrinfo *e)
19 struct sockaddr_in *i4;
20 struct sockaddr_in6 *i6;
22 switch (e->ai_family) {
24 i6 = (void*)e->ai_addr;
25 return ntohs(i6->sin6_port);
27 i4 = (void*)e->ai_addr;
28 return ntohs(i4->sin_port);
34 static void inet_setport(struct addrinfo *e, int port)
36 struct sockaddr_in *i4;
37 struct sockaddr_in6 *i6;
39 switch (e->ai_family) {
41 i6 = (void*)e->ai_addr;
42 i6->sin6_port = htons(port);
45 i4 = (void*)e->ai_addr;
46 i4->sin_port = htons(port);
51 static const char *inet_strfamily(int family)
54 case PF_INET6: return "ipv6";
55 case PF_INET: return "ipv4";
56 case PF_UNIX: return "unix";
61 static void inet_print_addrinfo(const char *tag, struct addrinfo *res)
64 char uaddr[INET6_ADDRSTRLEN+1];
67 for (e = res; e != NULL; e = e->ai_next) {
68 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
69 uaddr,INET6_ADDRSTRLEN,uport,32,
70 NI_NUMERICHOST | NI_NUMERICSERV);
71 fprintf(stderr,"%s: getaddrinfo: family %s, host %s, port %s\n",
72 tag, inet_strfamily(e->ai_family), uaddr, uport);
76 int inet_listen(const char *str, char *ostr, int olen,
77 int socktype, int port_offset)
79 struct addrinfo ai,*res,*e;
82 char uaddr[INET6_ADDRSTRLEN+1];
85 int slisten,rc,pos,to,try_next;
87 memset(&ai,0, sizeof(ai));
88 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
89 ai.ai_family = PF_UNSPEC;
90 ai.ai_socktype = socktype;
96 if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
97 fprintf(stderr, "%s: portonly parse error (%s)\n",
101 } else if (str[0] == '[') {
103 if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
104 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
108 ai.ai_family = PF_INET6;
109 } else if (isdigit(str[0])) {
111 if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
112 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
116 ai.ai_family = PF_INET;
119 if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
120 fprintf(stderr, "%s: hostname parse error (%s)\n",
128 h = strstr(opts, ",to=");
129 to = h ? atoi(h+4) : 0;
130 if (strstr(opts, ",ipv4"))
131 ai.ai_family = PF_INET;
132 if (strstr(opts, ",ipv6"))
133 ai.ai_family = PF_INET6;
137 snprintf(port, sizeof(port), "%d", atoi(port) + port_offset);
138 rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
140 fprintf(stderr,"%s: getaddrinfo(%s,%s): %s\n", __FUNCTION__,
141 addr, port, gai_strerror(rc));
145 inet_print_addrinfo(__FUNCTION__, res);
147 /* create socket + bind */
148 for (e = res; e != NULL; e = e->ai_next) {
149 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
150 uaddr,INET6_ADDRSTRLEN,uport,32,
151 NI_NUMERICHOST | NI_NUMERICSERV);
152 slisten = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
154 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
155 inet_strfamily(e->ai_family), strerror(errno));
159 setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
161 if (e->ai_family == PF_INET6) {
162 /* listen on both ipv4 and ipv6 */
163 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,sizeof(off));
168 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
170 fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
171 inet_strfamily(e->ai_family), uaddr, inet_getport(e));
174 try_next = to && (inet_getport(e) <= to + port_offset);
175 if (!try_next || sockets_debug)
176 fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
177 inet_strfamily(e->ai_family), uaddr, inet_getport(e),
180 inet_setport(e, inet_getport(e) + 1);
185 closesocket(slisten);
187 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
192 if (listen(slisten,1) != 0) {
194 closesocket(slisten);
198 if (e->ai_family == PF_INET6) {
199 snprintf(ostr, olen, "[%s]:%d%s", uaddr,
200 inet_getport(e) - port_offset, opts);
202 snprintf(ostr, olen, "%s:%d%s", uaddr,
203 inet_getport(e) - port_offset, opts);
210 int inet_connect(const char *str, int socktype)
212 struct addrinfo ai,*res,*e;
215 char uaddr[INET6_ADDRSTRLEN+1];
219 memset(&ai,0, sizeof(ai));
220 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
221 ai.ai_family = PF_UNSPEC;
222 ai.ai_socktype = socktype;
227 if (2 != sscanf(str,"[%64[^]]]:%32[^,]",addr,port)) {
228 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
232 ai.ai_family = PF_INET6;
233 } else if (isdigit(str[0])) {
235 if (2 != sscanf(str,"%64[0-9.]:%32[^,]",addr,port)) {
236 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
240 ai.ai_family = PF_INET;
243 if (2 != sscanf(str,"%64[^:]:%32[^,]",addr,port)) {
244 fprintf(stderr, "%s: hostname parse error (%s)\n",
251 if (strstr(str, ",ipv4"))
252 ai.ai_family = PF_INET;
253 if (strstr(str, ",ipv6"))
254 ai.ai_family = PF_INET6;
257 if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) {
258 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", gai_strerror(rc),
263 inet_print_addrinfo(__FUNCTION__, res);
265 for (e = res; e != NULL; e = e->ai_next) {
266 if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
267 uaddr,INET6_ADDRSTRLEN,uport,32,
268 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
269 fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
272 sock = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
274 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
275 inet_strfamily(e->ai_family), strerror(errno));
278 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
280 /* connect to peer */
281 if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) {
282 if (sockets_debug || NULL == e->ai_next)
283 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
284 inet_strfamily(e->ai_family),
285 e->ai_canonname, uaddr, uport, strerror(errno));
290 fprintf(stderr, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__,
291 inet_strfamily(e->ai_family),
292 e->ai_canonname, uaddr, uport);
302 int unix_listen(const char *str, char *ostr, int olen)
304 struct sockaddr_un un;
308 sock = socket(PF_UNIX, SOCK_STREAM, 0);
310 perror("socket(unix)");
314 opts = strchr(str, ',');
317 path = malloc(len+1);
318 snprintf(path, len+1, "%.*s", len, str);
322 memset(&un, 0, sizeof(un));
323 un.sun_family = AF_UNIX;
324 if (path && strlen(path)) {
325 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
327 char *tmpdir = getenv("TMPDIR");
328 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
329 tmpdir ? tmpdir : "/tmp");
331 * This dummy fd usage silences the mktemp() unsecure warning.
332 * Using mkstemp() doesn't make things more secure here
333 * though. bind() complains about existing files, so we have
334 * to unlink first and thus re-open the race window. The
335 * worst case possible is bind() failing, i.e. a DoS attack.
337 fd = mkstemp(un.sun_path); close(fd);
339 snprintf(ostr, olen, "%s%s", un.sun_path, opts ? opts : "");
342 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
343 fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
346 if (listen(sock, 1) < 0) {
347 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
352 fprintf(stderr, "bind(unix:%s): OK\n", un.sun_path);
362 int unix_connect(const char *path)
364 struct sockaddr_un un;
367 sock = socket(PF_UNIX, SOCK_STREAM, 0);
369 perror("socket(unix)");
373 memset(&un, 0, sizeof(un));
374 un.sun_family = AF_UNIX;
375 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
376 if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
377 fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
382 fprintf(stderr, "connect(unix:%s): OK\n", path);
388 int unix_listen(const char *path, char *ostr, int olen)
390 fprintf(stderr, "unix sockets are not available on windows\n");
394 int unix_connect(const char *path)
396 fprintf(stderr, "unix sockets are not available on windows\n");