Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/include/socket.h
2 views
/* SPDX-License-Identifier: BSD-3-Clause */1/*2* Copyright (c) 1995 Danny Gasparovski.3*/45#ifndef SLIRP_SOCKET_H6#define SLIRP_SOCKET_H78#include <string.h>910#ifndef _WIN3211#include <sys/un.h>12#endif1314#include "misc.h"15#include "sbuf.h"1617#define SO_EXPIRE 24000018#define SO_EXPIREFAST 100001920/* Helps unify some in/in6 routines. */21union in4or6_addr {22struct in_addr addr4;23struct in6_addr addr6;24};25typedef union in4or6_addr in4or6_addr;2627/*28* Our socket structure29*/3031union slirp_sockaddr {32struct sockaddr sa;33struct sockaddr_storage ss;34struct sockaddr_in sin;35struct sockaddr_in6 sin6;36};3738struct socket {39struct socket *so_next, *so_prev; /* For a linked list of sockets */4041int s; /* The actual socket */42int s_aux; /* An auxiliary socket for miscellaneous use. Currently used to43* reserve OS ports in UNIX-to-inet translation. */44struct gfwd_list *guestfwd;4546int pollfds_idx; /* GPollFD GArray index */4748Slirp *slirp; /* managing slirp instance */4950/* XXX union these with not-yet-used sbuf params */51struct mbuf *so_m; /* Pointer to the original SYN packet,52* for non-blocking connect()'s, and53* PING reply's */54struct tcpiphdr *so_ti; /* Pointer to the original ti within55* so_mconn, for non-blocking connections */56uint32_t so_urgc;57union slirp_sockaddr fhost; /* Foreign host */58#define so_faddr fhost.sin.sin_addr59#define so_fport fhost.sin.sin_port60#define so_faddr6 fhost.sin6.sin6_addr61#define so_fport6 fhost.sin6.sin6_port62#define so_ffamily fhost.ss.ss_family6364union slirp_sockaddr lhost; /* Local host */65#define so_laddr lhost.sin.sin_addr66#define so_lport lhost.sin.sin_port67#define so_laddr6 lhost.sin6.sin6_addr68#define so_lport6 lhost.sin6.sin6_port69#define so_lfamily lhost.ss.ss_family7071uint8_t so_iptos; /* Type of service */72uint8_t so_emu; /* Is the socket emulated? */7374uint8_t so_type; /* Protocol of the socket. May be 0 if loading old75* states. */76int32_t so_state; /* internal state flags SS_*, below */7778struct tcpcb *so_tcpcb; /* pointer to TCP protocol control block */79unsigned so_expire; /* When the socket will expire */8081int so_queued; /* Number of packets queued from this socket */82int so_nqueued; /* Number of packets queued in a row83* Used to determine when to "downgrade" a session84* from fastq to batchq */8586struct sbuf so_rcv; /* Receive buffer */87struct sbuf so_snd; /* Send buffer */88};899091/*92* Socket state bits. (peer means the host on the Internet,93* local host means the host on the other end of the modem)94*/95#define SS_NOFDREF 0x001 /* No fd reference */9697#define SS_ISFCONNECTING \980x002 /* Socket is connecting to peer (non-blocking connect()'s) */99#define SS_ISFCONNECTED 0x004 /* Socket is connected to peer */100#define SS_FCANTRCVMORE \1010x008 /* Socket can't receive more from peer (for half-closes) */102#define SS_FCANTSENDMORE \1030x010 /* Socket can't send more to peer (for half-closes) */104#define SS_FWDRAIN \1050x040 /* We received a FIN, drain data and set SS_FCANTSENDMORE */106107#define SS_CTL 0x080108#define SS_FACCEPTCONN \1090x100 /* Socket is accepting connections from a host on the internet */110#define SS_FACCEPTONCE \1110x200 /* If set, the SS_FACCEPTCONN socket will die after one accept */112113#define SS_PERSISTENT_MASK 0xf000 /* Unremovable state bits */114#define SS_HOSTFWD 0x1000 /* Socket describes host->guest forwarding */115#define SS_INCOMING \1160x2000 /* Connection was initiated by a host on the internet */117#define SS_HOSTFWD_V6ONLY 0x4000 /* Only bind on v6 addresses */118119/* Check that two addresses are equal */120static inline int sockaddr_equal(const struct sockaddr_storage *a,121const struct sockaddr_storage *b)122{123if (a->ss_family != b->ss_family) {124return 0;125}126127switch (a->ss_family) {128case AF_INET: {129const struct sockaddr_in *a4 = (const struct sockaddr_in *)a;130const struct sockaddr_in *b4 = (const struct sockaddr_in *)b;131return a4->sin_addr.s_addr == b4->sin_addr.s_addr &&132a4->sin_port == b4->sin_port;133}134case AF_INET6: {135const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *)a;136const struct sockaddr_in6 *b6 = (const struct sockaddr_in6 *)b;137return (in6_equal(&a6->sin6_addr, &b6->sin6_addr) &&138a6->sin6_port == b6->sin6_port);139}140#ifndef _WIN32141case AF_UNIX: {142const struct sockaddr_un *aun = (const struct sockaddr_un *)a;143const struct sockaddr_un *bun = (const struct sockaddr_un *)b;144return strncmp(aun->sun_path, bun->sun_path, sizeof(aun->sun_path)) == 0;145}146#endif147default:148g_assert_not_reached();149}150151return 0;152}153154/* Get the size of an address */155static inline socklen_t sockaddr_size(const struct sockaddr_storage *a)156{157switch (a->ss_family) {158case AF_INET:159return sizeof(struct sockaddr_in);160case AF_INET6:161return sizeof(struct sockaddr_in6);162#ifndef _WIN32163case AF_UNIX:164return sizeof(struct sockaddr_un);165#endif166default:167g_assert_not_reached();168}169}170171/* Copy an address */172static inline void sockaddr_copy(struct sockaddr *dst, socklen_t dstlen, const struct sockaddr *src, socklen_t srclen)173{174socklen_t len = sockaddr_size((const struct sockaddr_storage *) src);175g_assert(len <= srclen);176g_assert(len <= dstlen);177memcpy(dst, src, len);178}179180/* Find the socket corresponding to lhost & fhost, trying last as a guess */181struct socket *solookup(struct socket **last, struct socket *head,182struct sockaddr_storage *lhost, struct sockaddr_storage *fhost);183/* Create a new socket */184struct socket *socreate(Slirp *, int);185/* Release a socket */186void sofree(struct socket *);187/* Receive the available data from the Internet socket and queue it on the sb */188int soread(struct socket *);189/* Receive the available OOB data from the Internet socket and try to send it immediately */190int sorecvoob(struct socket *);191/* Send OOB data to the Internet socket */192int sosendoob(struct socket *);193/* Send data to the Internet socket */194int sowrite(struct socket *);195/* Receive the available data from the Internet UDP socket, and send it to the guest */196void sorecvfrom(struct socket *);197/* Send data to the Internet UDP socket */198int sosendto(struct socket *, struct mbuf *);199/* Listen for incoming TCPv4 connections on this haddr+hport */200struct socket *tcp_listen(Slirp *, uint32_t haddr, unsigned hport, uint32_t laddr, unsigned lport, int flags);201/*202* Listen for incoming TCP connections on this haddr203* On failure errno contains the reason.204*/205struct socket *tcpx_listen(Slirp *slirp,206const struct sockaddr *haddr, socklen_t haddrlen,207const struct sockaddr *laddr, socklen_t laddrlen,208int flags);209/* Note that the socket is connecting */210void soisfconnecting(register struct socket *);211/* Note that the socket is connected */212void soisfconnected(register struct socket *);213/*214* Set write drain mode215* Set CANTSENDMORE once all data has been write()n216*/217void sofwdrain(struct socket *);218struct iovec; /* For win32 */219/* Prepare iov for storing into the sb */220size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np);221/* Get data from the buffer and queue it on the sb */222int soreadbuf(struct socket *so, const char *buf, int size);223224/* Translate addr into host addr when it is a virtual address, before sending to the Internet */225int sotranslate_out(struct socket *, struct sockaddr_storage *);226/* Translate addr into virtual address when it is host, before sending to the guest */227void sotranslate_in(struct socket *, struct sockaddr_storage *);228/* Translate connections from localhost to the real hostname */229void sotranslate_accept(struct socket *);230/* Drop num bytes from the reading end of the socket */231void sodrop(struct socket *, int num);232/* Forwarding a connection to the guest, try to find the guest address to use, fill lhost with it */233int soassign_guest_addr_if_needed(struct socket *so);234235#endif /* SLIRP_SOCKET_H */236237238