qemu: Validate spapr-vio addresses
[libvirt/ericb.git] / tests / virportallocatormock.c
blobc0f8e8e8572798a5aa1cd74b5f92397917bfd2e7
1 /*
2 * Copyright (C) 2013-2014 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
19 #include <config.h>
21 #if HAVE_DLFCN_H
22 # include <dlfcn.h>
23 #endif
25 #if defined(__linux__) && defined(RTLD_NEXT)
26 # include "internal.h"
27 # include <sys/socket.h>
28 # include <arpa/inet.h>
29 # include <netinet/in.h>
30 # include <unistd.h>
32 static bool host_has_ipv6;
33 static int (*realsocket)(int domain, int type, int protocol);
35 static void init_syms(void)
37 int fd;
39 if (realsocket)
40 return;
42 realsocket = dlsym(RTLD_NEXT, "socket");
44 if (!realsocket) {
45 fprintf(stderr, "Unable to find 'socket' symbol\n");
46 abort();
49 fd = realsocket(AF_INET6, SOCK_STREAM, 0);
50 if (fd < 0)
51 return;
53 host_has_ipv6 = true;
54 close(fd);
57 int socket(int domain,
58 int type,
59 int protocol)
61 init_syms();
63 if (getenv("LIBVIRT_TEST_IPV4ONLY") && domain == AF_INET6) {
64 errno = EAFNOSUPPORT;
65 return -1;
68 return realsocket(domain, type, protocol);
71 int bind(int sockfd ATTRIBUTE_UNUSED,
72 const struct sockaddr *addr,
73 socklen_t addrlen ATTRIBUTE_UNUSED)
75 struct sockaddr_in saddr;
77 memcpy(&saddr, addr, sizeof(saddr));
79 if (host_has_ipv6 && !getenv("LIBVIRT_TEST_IPV4ONLY")) {
80 if (saddr.sin_port == htons(5900) ||
81 (saddr.sin_family == AF_INET &&
82 saddr.sin_port == htons(5904)) ||
83 (saddr.sin_family == AF_INET6 &&
84 (saddr.sin_port == htons(5905) ||
85 saddr.sin_port == htons(5906)))) {
86 errno = EADDRINUSE;
87 return -1;
89 return 0;
92 if (saddr.sin_port == htons(5900) ||
93 saddr.sin_port == htons(5904) ||
94 saddr.sin_port == htons(5905) ||
95 saddr.sin_port == htons(5906)) {
96 errno = EADDRINUSE;
97 return -1;
100 return 0;
103 #else /* defined(__linux__) && defined(RTLD_NEXT) */
104 /* Nothing to override on other platforms. */
105 #endif