chardev: split up qmp_chardev_open_socket connection code
[qemu/ar7.git] / slirp / util.h
blob4963747aefca46926fcb9e845e8c22c62b7ecfc2
1 /*
2 * Copyright (c) 2003-2008 Fabrice Bellard
3 * Copyright (c) 2010-2019 Red Hat, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
23 #ifndef UTIL_H_
24 #define UTIL_H_
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <assert.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #include <inttypes.h>
36 #ifdef _WIN32
37 #include <winsock2.h>
38 #include <windows.h>
39 #else
40 #include <sys/socket.h>
41 #include <netinet/tcp.h>
42 #include <netinet/in.h>
43 #endif
45 #if defined(_WIN32)
46 # define SLIRP_PACKED __attribute__((gcc_struct, packed))
47 #else
48 # define SLIRP_PACKED __attribute__((packed))
49 #endif
51 #ifndef DIV_ROUND_UP
52 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
53 #endif
55 #ifndef container_of
56 #define container_of(ptr, type, member) __extension__ ({ \
57 void *__mptr = (void *)(ptr); \
58 ((type *)(__mptr - offsetof(type, member))); })
59 #endif
61 #if defined(_WIN32) /* CONFIG_IOVEC */
62 # if !defined(IOV_MAX) /* XXX: to avoid duplicate with QEMU osdep.h */
63 struct iovec {
64 void *iov_base;
65 size_t iov_len;
67 # endif
68 #else
69 #include <sys/uio.h>
70 #endif
72 #define SCALE_MS 1000000
74 #define ETH_ALEN 6
75 #define ETH_HLEN 14
76 #define ETH_P_IP (0x0800) /* Internet Protocol packet */
77 #define ETH_P_ARP (0x0806) /* Address Resolution packet */
78 #define ETH_P_IPV6 (0x86dd)
79 #define ETH_P_VLAN (0x8100)
80 #define ETH_P_DVLAN (0x88a8)
81 #define ETH_P_NCSI (0x88f8)
82 #define ETH_P_UNKNOWN (0xffff)
84 #ifdef _WIN32
85 int slirp_closesocket(int fd);
86 int slirp_ioctlsocket(int fd, int req, void *val);
87 int inet_aton(const char *cp, struct in_addr *ia);
88 #define slirp_getsockopt(sockfd, level, optname, optval, optlen) \
89 getsockopt(sockfd, level, optname, (void *)optval, optlen)
90 #define slirp_setsockopt(sockfd, level, optname, optval, optlen) \
91 setsockopt(sockfd, level, optname, (const void *)optval, optlen)
92 #define slirp_recv(sockfd, buf, len, flags) recv(sockfd, (void *)buf, len, flags)
93 #else
94 #define slirp_setsockopt setsockopt
95 #define slirp_getsockopt getsockopt
96 #define slirp_recv recv
97 #define slirp_closesocket close
98 #define slirp_ioctlsocket ioctl
99 #endif
101 int slirp_socket(int domain, int type, int protocol);
102 void slirp_set_nonblock(int fd);
104 static inline int slirp_socket_set_nodelay(int fd)
106 int v = 1;
107 return slirp_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
110 static inline int slirp_socket_set_fast_reuse(int fd)
112 #ifndef _WIN32
113 int v = 1;
114 return slirp_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v));
115 #else
116 /* Enabling the reuse of an endpoint that was used by a socket still in
117 * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
118 * fast reuse is the default and SO_REUSEADDR does strange things. So we
119 * don't have to do anything here. More info can be found at:
120 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
121 return 0;
122 #endif
125 void slirp_pstrcpy(char *buf, int buf_size, const char *str);
127 #endif