Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/SharedDependencies/Sources/libslirp/include/socket.h
2 views
1
/* SPDX-License-Identifier: BSD-3-Clause */
2
/*
3
* Copyright (c) 1995 Danny Gasparovski.
4
*/
5
6
#ifndef SLIRP_SOCKET_H
7
#define SLIRP_SOCKET_H
8
9
#include <string.h>
10
11
#ifndef _WIN32
12
#include <sys/un.h>
13
#endif
14
15
#include "misc.h"
16
#include "sbuf.h"
17
18
#define SO_EXPIRE 240000
19
#define SO_EXPIREFAST 10000
20
21
/* Helps unify some in/in6 routines. */
22
union in4or6_addr {
23
struct in_addr addr4;
24
struct in6_addr addr6;
25
};
26
typedef union in4or6_addr in4or6_addr;
27
28
/*
29
* Our socket structure
30
*/
31
32
union slirp_sockaddr {
33
struct sockaddr sa;
34
struct sockaddr_storage ss;
35
struct sockaddr_in sin;
36
struct sockaddr_in6 sin6;
37
};
38
39
struct socket {
40
struct socket *so_next, *so_prev; /* For a linked list of sockets */
41
42
int s; /* The actual socket */
43
int s_aux; /* An auxiliary socket for miscellaneous use. Currently used to
44
* reserve OS ports in UNIX-to-inet translation. */
45
struct gfwd_list *guestfwd;
46
47
int pollfds_idx; /* GPollFD GArray index */
48
49
Slirp *slirp; /* managing slirp instance */
50
51
/* XXX union these with not-yet-used sbuf params */
52
struct mbuf *so_m; /* Pointer to the original SYN packet,
53
* for non-blocking connect()'s, and
54
* PING reply's */
55
struct tcpiphdr *so_ti; /* Pointer to the original ti within
56
* so_mconn, for non-blocking connections */
57
uint32_t so_urgc;
58
union slirp_sockaddr fhost; /* Foreign host */
59
#define so_faddr fhost.sin.sin_addr
60
#define so_fport fhost.sin.sin_port
61
#define so_faddr6 fhost.sin6.sin6_addr
62
#define so_fport6 fhost.sin6.sin6_port
63
#define so_ffamily fhost.ss.ss_family
64
65
union slirp_sockaddr lhost; /* Local host */
66
#define so_laddr lhost.sin.sin_addr
67
#define so_lport lhost.sin.sin_port
68
#define so_laddr6 lhost.sin6.sin6_addr
69
#define so_lport6 lhost.sin6.sin6_port
70
#define so_lfamily lhost.ss.ss_family
71
72
uint8_t so_iptos; /* Type of service */
73
uint8_t so_emu; /* Is the socket emulated? */
74
75
uint8_t so_type; /* Protocol of the socket. May be 0 if loading old
76
* states. */
77
int32_t so_state; /* internal state flags SS_*, below */
78
79
struct tcpcb *so_tcpcb; /* pointer to TCP protocol control block */
80
unsigned so_expire; /* When the socket will expire */
81
82
int so_queued; /* Number of packets queued from this socket */
83
int so_nqueued; /* Number of packets queued in a row
84
* Used to determine when to "downgrade" a session
85
* from fastq to batchq */
86
87
struct sbuf so_rcv; /* Receive buffer */
88
struct sbuf so_snd; /* Send buffer */
89
};
90
91
92
/*
93
* Socket state bits. (peer means the host on the Internet,
94
* local host means the host on the other end of the modem)
95
*/
96
#define SS_NOFDREF 0x001 /* No fd reference */
97
98
#define SS_ISFCONNECTING \
99
0x002 /* Socket is connecting to peer (non-blocking connect()'s) */
100
#define SS_ISFCONNECTED 0x004 /* Socket is connected to peer */
101
#define SS_FCANTRCVMORE \
102
0x008 /* Socket can't receive more from peer (for half-closes) */
103
#define SS_FCANTSENDMORE \
104
0x010 /* Socket can't send more to peer (for half-closes) */
105
#define SS_FWDRAIN \
106
0x040 /* We received a FIN, drain data and set SS_FCANTSENDMORE */
107
108
#define SS_CTL 0x080
109
#define SS_FACCEPTCONN \
110
0x100 /* Socket is accepting connections from a host on the internet */
111
#define SS_FACCEPTONCE \
112
0x200 /* If set, the SS_FACCEPTCONN socket will die after one accept */
113
114
#define SS_PERSISTENT_MASK 0xf000 /* Unremovable state bits */
115
#define SS_HOSTFWD 0x1000 /* Socket describes host->guest forwarding */
116
#define SS_INCOMING \
117
0x2000 /* Connection was initiated by a host on the internet */
118
#define SS_HOSTFWD_V6ONLY 0x4000 /* Only bind on v6 addresses */
119
120
/* Check that two addresses are equal */
121
static inline int sockaddr_equal(const struct sockaddr_storage *a,
122
const struct sockaddr_storage *b)
123
{
124
if (a->ss_family != b->ss_family) {
125
return 0;
126
}
127
128
switch (a->ss_family) {
129
case AF_INET: {
130
const struct sockaddr_in *a4 = (const struct sockaddr_in *)a;
131
const struct sockaddr_in *b4 = (const struct sockaddr_in *)b;
132
return a4->sin_addr.s_addr == b4->sin_addr.s_addr &&
133
a4->sin_port == b4->sin_port;
134
}
135
case AF_INET6: {
136
const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *)a;
137
const struct sockaddr_in6 *b6 = (const struct sockaddr_in6 *)b;
138
return (in6_equal(&a6->sin6_addr, &b6->sin6_addr) &&
139
a6->sin6_port == b6->sin6_port);
140
}
141
#ifndef _WIN32
142
case AF_UNIX: {
143
const struct sockaddr_un *aun = (const struct sockaddr_un *)a;
144
const struct sockaddr_un *bun = (const struct sockaddr_un *)b;
145
return strncmp(aun->sun_path, bun->sun_path, sizeof(aun->sun_path)) == 0;
146
}
147
#endif
148
default:
149
g_assert_not_reached();
150
}
151
152
return 0;
153
}
154
155
/* Get the size of an address */
156
static inline socklen_t sockaddr_size(const struct sockaddr_storage *a)
157
{
158
switch (a->ss_family) {
159
case AF_INET:
160
return sizeof(struct sockaddr_in);
161
case AF_INET6:
162
return sizeof(struct sockaddr_in6);
163
#ifndef _WIN32
164
case AF_UNIX:
165
return sizeof(struct sockaddr_un);
166
#endif
167
default:
168
g_assert_not_reached();
169
}
170
}
171
172
/* Copy an address */
173
static inline void sockaddr_copy(struct sockaddr *dst, socklen_t dstlen, const struct sockaddr *src, socklen_t srclen)
174
{
175
socklen_t len = sockaddr_size((const struct sockaddr_storage *) src);
176
g_assert(len <= srclen);
177
g_assert(len <= dstlen);
178
memcpy(dst, src, len);
179
}
180
181
/* Find the socket corresponding to lhost & fhost, trying last as a guess */
182
struct socket *solookup(struct socket **last, struct socket *head,
183
struct sockaddr_storage *lhost, struct sockaddr_storage *fhost);
184
/* Create a new socket */
185
struct socket *socreate(Slirp *, int);
186
/* Release a socket */
187
void sofree(struct socket *);
188
/* Receive the available data from the Internet socket and queue it on the sb */
189
int soread(struct socket *);
190
/* Receive the available OOB data from the Internet socket and try to send it immediately */
191
int sorecvoob(struct socket *);
192
/* Send OOB data to the Internet socket */
193
int sosendoob(struct socket *);
194
/* Send data to the Internet socket */
195
int sowrite(struct socket *);
196
/* Receive the available data from the Internet UDP socket, and send it to the guest */
197
void sorecvfrom(struct socket *);
198
/* Send data to the Internet UDP socket */
199
int sosendto(struct socket *, struct mbuf *);
200
/* Listen for incoming TCPv4 connections on this haddr+hport */
201
struct socket *tcp_listen(Slirp *, uint32_t haddr, unsigned hport, uint32_t laddr, unsigned lport, int flags);
202
/*
203
* Listen for incoming TCP connections on this haddr
204
* On failure errno contains the reason.
205
*/
206
struct socket *tcpx_listen(Slirp *slirp,
207
const struct sockaddr *haddr, socklen_t haddrlen,
208
const struct sockaddr *laddr, socklen_t laddrlen,
209
int flags);
210
/* Note that the socket is connecting */
211
void soisfconnecting(register struct socket *);
212
/* Note that the socket is connected */
213
void soisfconnected(register struct socket *);
214
/*
215
* Set write drain mode
216
* Set CANTSENDMORE once all data has been write()n
217
*/
218
void sofwdrain(struct socket *);
219
struct iovec; /* For win32 */
220
/* Prepare iov for storing into the sb */
221
size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np);
222
/* Get data from the buffer and queue it on the sb */
223
int soreadbuf(struct socket *so, const char *buf, int size);
224
225
/* Translate addr into host addr when it is a virtual address, before sending to the Internet */
226
int sotranslate_out(struct socket *, struct sockaddr_storage *);
227
/* Translate addr into virtual address when it is host, before sending to the guest */
228
void sotranslate_in(struct socket *, struct sockaddr_storage *);
229
/* Translate connections from localhost to the real hostname */
230
void sotranslate_accept(struct socket *);
231
/* Drop num bytes from the reading end of the socket */
232
void sodrop(struct socket *, int num);
233
/* Forwarding a connection to the guest, try to find the guest address to use, fill lhost with it */
234
int soassign_guest_addr_if_needed(struct socket *so);
235
236
#endif /* SLIRP_SOCKET_H */
237
238