Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/include/util.h
2 views
/* SPDX-License-Identifier: MIT */1/*2* Copyright (c) 2003-2008 Fabrice Bellard3* Copyright (c) 2010-2019 Red Hat, Inc.4*5* Permission is hereby granted, free of charge, to any person obtaining a copy6* of this software and associated documentation files (the "Software"), to deal7* in the Software without restriction, including without limitation the rights8* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9* copies of the Software, and to permit persons to whom the Software is10* furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included in13* all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* 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 IN21* THE SOFTWARE.22*/23#ifndef UTIL_H_24#define UTIL_H_2526#include <glib.h>2728#include <stdlib.h>29#include <stdio.h>30#include <assert.h>31#include <errno.h>32#include <sys/types.h>33#include <sys/stat.h>34#include <inttypes.h>3536#ifdef _WIN3237#include <winsock2.h>38#include <windows.h>39#include <ws2tcpip.h>40#else41#include <unistd.h>42#include <sys/socket.h>43#include <netinet/tcp.h>44#include <netinet/in.h>45#endif4647#include "libslirp.h"4849#ifdef __GNUC__50#define SLIRP_PACKED_BEGIN51#if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))52#define SLIRP_PACKED_END __attribute__((gcc_struct, packed))53#else54#define SLIRP_PACKED_END __attribute__((packed))55#endif56#elif defined(_MSC_VER)57#define SLIRP_PACKED_BEGIN __pragma(pack(push, 1))58#define SLIRP_PACKED_END __pragma(pack(pop))59#endif6061#ifndef DIV_ROUND_UP62#define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))63#endif6465#ifndef container_of66#define container_of(ptr, type, member) \67((type *) (((char *)(ptr)) - offsetof(type, member)))68#endif6970#ifndef G_SIZEOF_MEMBER71#define G_SIZEOF_MEMBER(type, member) sizeof(((type *)0)->member)72#endif7374#if defined(_WIN32) /* CONFIG_IOVEC */75#if !defined(IOV_MAX) /* XXX: to avoid duplicate with QEMU osdep.h */76struct iovec {77void *iov_base;78size_t iov_len;79};80#endif81#else82#include <sys/uio.h>83#endif8485#define stringify(s) tostring(s)86#define tostring(s) #s8788#define SCALE_MS 10000008990#define ETH_ALEN 691#define ETH_ADDRSTRLEN 18 /* "xx:xx:xx:xx:xx:xx", with trailing NUL */92#define ETH_HLEN 1493#define ETH_MINLEN 6094#define ETH_P_IP (0x0800) /* Internet Protocol packet */95#define ETH_P_ARP (0x0806) /* Address Resolution packet */96#define ETH_P_IPV6 (0x86dd)97#define ETH_P_VLAN (0x8100)98#define ETH_P_DVLAN (0x88a8)99#define ETH_P_NCSI (0x88f8)100#define ETH_P_UNKNOWN (0xffff)101102/* FIXME: remove me when made standalone */103#ifdef _WIN32104#undef accept105#undef bind106#undef closesocket107#undef connect108#undef getpeername109#undef getsockname110#undef getsockopt111#undef ioctlsocket112#undef listen113#undef recv114#undef recvfrom115#undef send116#undef sendto117#undef setsockopt118#undef shutdown119#undef socket120#endif121122#ifdef _WIN32123#define connect slirp_connect_wrap124int slirp_connect_wrap(int fd, const struct sockaddr *addr, int addrlen);125#define listen slirp_listen_wrap126int slirp_listen_wrap(int fd, int backlog);127#define bind slirp_bind_wrap128int slirp_bind_wrap(int fd, const struct sockaddr *addr, int addrlen);129#define socket slirp_socket_wrap130int slirp_socket_wrap(int domain, int type, int protocol);131#define accept slirp_accept_wrap132int slirp_accept_wrap(int fd, struct sockaddr *addr, int *addrlen);133#define shutdown slirp_shutdown_wrap134int slirp_shutdown_wrap(int fd, int how);135#define getpeername slirp_getpeername_wrap136int slirp_getpeername_wrap(int fd, struct sockaddr *addr, int *addrlen);137#define getsockname slirp_getsockname_wrap138int slirp_getsockname_wrap(int fd, struct sockaddr *addr, int *addrlen);139#define send slirp_send_wrap140slirp_ssize_t slirp_send_wrap(int fd, const void *buf, size_t len, int flags);141#define sendto slirp_sendto_wrap142slirp_ssize_t slirp_sendto_wrap(int fd, const void *buf, size_t len, int flags,143const struct sockaddr *dest_addr, int addrlen);144#define recv slirp_recv_wrap145slirp_ssize_t slirp_recv_wrap(int fd, void *buf, size_t len, int flags);146#define recvfrom slirp_recvfrom_wrap147slirp_ssize_t slirp_recvfrom_wrap(int fd, void *buf, size_t len, int flags,148struct sockaddr *src_addr, int *addrlen);149#define closesocket slirp_closesocket_wrap150int slirp_closesocket_wrap(int fd);151#define ioctlsocket slirp_ioctlsocket_wrap152int slirp_ioctlsocket_wrap(int fd, int req, void *val);153#define getsockopt slirp_getsockopt_wrap154int slirp_getsockopt_wrap(int sockfd, int level, int optname, void *optval,155int *optlen);156#define setsockopt slirp_setsockopt_wrap157int slirp_setsockopt_wrap(int sockfd, int level, int optname,158const void *optval, int optlen);159#define inet_aton slirp_inet_aton160int slirp_inet_aton(const char *cp, struct in_addr *ia);161#else162#define closesocket(s) close(s)163#define ioctlsocket(s, r, v) ioctl(s, r, v)164#endif165166int slirp_socket(int domain, int type, int protocol);167void slirp_set_nonblock(int fd);168169static inline int slirp_socket_set_v6only(int fd, int v)170{171return setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &v, sizeof(v));172}173174static inline int slirp_socket_set_nodelay(int fd)175{176int v = 1;177return setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));178}179180static inline int slirp_socket_set_fast_reuse(int fd)181{182#ifndef _WIN32183int v = 1;184return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v));185#else186/* Enabling the reuse of an endpoint that was used by a socket still in187* TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows188* fast reuse is the default and SO_REUSEADDR does strange things. So we189* don't have to do anything here. More info can be found at:190* http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */191return 0;192#endif193}194195void slirp_pstrcpy(char *buf, int buf_size, const char *str);196197int slirp_fmt(char *str, size_t size, const char *format, ...) G_GNUC_PRINTF(3, 4);198int slirp_fmt0(char *str, size_t size, const char *format, ...) G_GNUC_PRINTF(3, 4);199200/*201* Pretty print a MAC address into out_str.202* As a convenience returns out_str.203*/204const char *slirp_ether_ntoa(const uint8_t *addr, char *out_str,205size_t out_str_len);206207#endif208209210