2004-04-22 00:10:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1995 Danny Gasparovski.
|
2007-09-16 21:08:06 +00:00
|
|
|
*
|
|
|
|
* Please read the file COPYRIGHT for the
|
2004-04-22 00:10:48 +00:00
|
|
|
* terms and conditions of the copyright.
|
|
|
|
*/
|
|
|
|
|
2016-06-29 11:47:03 +00:00
|
|
|
#ifndef SLIRP_SOCKET_H
|
|
|
|
#define SLIRP_SOCKET_H
|
2004-04-22 00:10:48 +00:00
|
|
|
|
|
|
|
#define SO_EXPIRE 240000
|
|
|
|
#define SO_EXPIREFAST 10000
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Our socket structure
|
|
|
|
*/
|
|
|
|
|
2017-02-20 18:50:18 +00:00
|
|
|
union slirp_sockaddr {
|
|
|
|
struct sockaddr_storage ss;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
};
|
|
|
|
|
2004-04-22 00:10:48 +00:00
|
|
|
struct socket {
|
|
|
|
struct socket *so_next,*so_prev; /* For a linked list of sockets */
|
|
|
|
|
|
|
|
int s; /* The actual socket */
|
|
|
|
|
2013-02-20 10:28:28 +00:00
|
|
|
int pollfds_idx; /* GPollFD GArray index */
|
|
|
|
|
2009-06-24 12:42:31 +00:00
|
|
|
Slirp *slirp; /* managing slirp instance */
|
|
|
|
|
2004-04-22 00:10:48 +00:00
|
|
|
/* XXX union these with not-yet-used sbuf params */
|
|
|
|
struct mbuf *so_m; /* Pointer to the original SYN packet,
|
|
|
|
* for non-blocking connect()'s, and
|
|
|
|
* PING reply's */
|
|
|
|
struct tcpiphdr *so_ti; /* Pointer to the original ti within
|
|
|
|
* so_mconn, for non-blocking connections */
|
2017-04-03 17:18:42 +00:00
|
|
|
uint32_t so_urgc;
|
2017-02-20 18:50:18 +00:00
|
|
|
union slirp_sockaddr fhost; /* Foreign host */
|
2015-12-19 21:24:58 +00:00
|
|
|
#define so_faddr fhost.sin.sin_addr
|
|
|
|
#define so_fport fhost.sin.sin_port
|
2016-03-15 09:31:20 +00:00
|
|
|
#define so_faddr6 fhost.sin6.sin6_addr
|
|
|
|
#define so_fport6 fhost.sin6.sin6_port
|
2015-12-19 21:24:58 +00:00
|
|
|
#define so_ffamily fhost.ss.ss_family
|
|
|
|
|
2017-02-20 18:50:18 +00:00
|
|
|
union slirp_sockaddr lhost; /* Local host */
|
2015-12-19 21:24:58 +00:00
|
|
|
#define so_laddr lhost.sin.sin_addr
|
|
|
|
#define so_lport lhost.sin.sin_port
|
2016-03-15 09:31:20 +00:00
|
|
|
#define so_laddr6 lhost.sin6.sin6_addr
|
|
|
|
#define so_lport6 lhost.sin6.sin6_port
|
2015-12-19 21:24:58 +00:00
|
|
|
#define so_lfamily lhost.ss.ss_family
|
2007-09-17 08:09:54 +00:00
|
|
|
|
2010-07-22 20:15:23 +00:00
|
|
|
uint8_t so_iptos; /* Type of service */
|
|
|
|
uint8_t so_emu; /* Is the socket emulated? */
|
2007-09-17 08:09:54 +00:00
|
|
|
|
2017-04-03 17:18:42 +00:00
|
|
|
uint8_t so_type; /* Type of socket, UDP or TCP */
|
|
|
|
int32_t so_state; /* internal state flags SS_*, below */
|
2007-09-17 08:09:54 +00:00
|
|
|
|
2004-04-22 00:10:48 +00:00
|
|
|
struct tcpcb *so_tcpcb; /* pointer to TCP protocol control block */
|
|
|
|
u_int so_expire; /* When the socket will expire */
|
2007-09-17 08:09:54 +00:00
|
|
|
|
2004-04-22 00:10:48 +00:00
|
|
|
int so_queued; /* Number of packets queued from this socket */
|
|
|
|
int so_nqueued; /* Number of packets queued in a row
|
|
|
|
* Used to determine when to "downgrade" a session
|
|
|
|
* from fastq to batchq */
|
2007-09-16 21:08:06 +00:00
|
|
|
|
2004-04-22 00:10:48 +00:00
|
|
|
struct sbuf so_rcv; /* Receive buffer */
|
|
|
|
struct sbuf so_snd; /* Send buffer */
|
|
|
|
void * extra; /* Extra pointer */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Socket state bits. (peer means the host on the Internet,
|
|
|
|
* local host means the host on the other end of the modem)
|
|
|
|
*/
|
|
|
|
#define SS_NOFDREF 0x001 /* No fd reference */
|
|
|
|
|
|
|
|
#define SS_ISFCONNECTING 0x002 /* Socket is connecting to peer (non-blocking connect()'s) */
|
|
|
|
#define SS_ISFCONNECTED 0x004 /* Socket is connected to peer */
|
|
|
|
#define SS_FCANTRCVMORE 0x008 /* Socket can't receive more from peer (for half-closes) */
|
|
|
|
#define SS_FCANTSENDMORE 0x010 /* Socket can't send more to peer (for half-closes) */
|
|
|
|
#define SS_FWDRAIN 0x040 /* We received a FIN, drain data and set SS_FCANTSENDMORE */
|
|
|
|
|
|
|
|
#define SS_CTL 0x080
|
|
|
|
#define SS_FACCEPTCONN 0x100 /* Socket is accepting connections from a host on the internet */
|
|
|
|
#define SS_FACCEPTONCE 0x200 /* If set, the SS_FACCEPTCONN socket will die after one accept */
|
|
|
|
|
2009-06-24 12:42:29 +00:00
|
|
|
#define SS_PERSISTENT_MASK 0xf000 /* Unremovable state bits */
|
2009-06-24 12:42:29 +00:00
|
|
|
#define SS_HOSTFWD 0x1000 /* Socket describes host->guest forwarding */
|
2009-06-24 12:42:29 +00:00
|
|
|
#define SS_INCOMING 0x2000 /* Connection was initiated by a host on the internet */
|
2009-06-24 12:42:29 +00:00
|
|
|
|
2015-12-19 21:25:01 +00:00
|
|
|
static inline int sockaddr_equal(struct sockaddr_storage *a,
|
|
|
|
struct sockaddr_storage *b)
|
|
|
|
{
|
|
|
|
if (a->ss_family != b->ss_family) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (a->ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
struct sockaddr_in *a4 = (struct sockaddr_in *) a;
|
|
|
|
struct sockaddr_in *b4 = (struct sockaddr_in *) b;
|
|
|
|
return a4->sin_addr.s_addr == b4->sin_addr.s_addr
|
|
|
|
&& a4->sin_port == b4->sin_port;
|
|
|
|
}
|
2016-03-15 09:31:19 +00:00
|
|
|
case AF_INET6:
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) a;
|
|
|
|
struct sockaddr_in6 *b6 = (struct sockaddr_in6 *) b;
|
|
|
|
return (in6_equal(&a6->sin6_addr, &b6->sin6_addr)
|
|
|
|
&& a6->sin6_port == b6->sin6_port);
|
|
|
|
}
|
2015-12-19 21:25:01 +00:00
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-28 16:53:08 +00:00
|
|
|
static inline socklen_t sockaddr_size(struct sockaddr_storage *a)
|
|
|
|
{
|
|
|
|
switch (a->ss_family) {
|
|
|
|
case AF_INET:
|
|
|
|
return sizeof(struct sockaddr_in);
|
|
|
|
case AF_INET6:
|
|
|
|
return sizeof(struct sockaddr_in6);
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-19 21:25:00 +00:00
|
|
|
struct socket *solookup(struct socket **, struct socket *,
|
2015-12-19 21:25:01 +00:00
|
|
|
struct sockaddr_storage *, struct sockaddr_storage *);
|
2015-12-19 21:25:00 +00:00
|
|
|
struct socket *socreate(Slirp *);
|
2009-07-01 19:11:17 +00:00
|
|
|
void sofree(struct socket *);
|
|
|
|
int soread(struct socket *);
|
2016-04-07 05:04:32 +00:00
|
|
|
int sorecvoob(struct socket *);
|
2009-07-01 19:11:17 +00:00
|
|
|
int sosendoob(struct socket *);
|
|
|
|
int sowrite(struct socket *);
|
|
|
|
void sorecvfrom(struct socket *);
|
|
|
|
int sosendto(struct socket *, struct mbuf *);
|
2010-07-22 20:15:23 +00:00
|
|
|
struct socket * tcp_listen(Slirp *, uint32_t, u_int, uint32_t, u_int,
|
2009-07-01 19:11:17 +00:00
|
|
|
int);
|
|
|
|
void soisfconnecting(register struct socket *);
|
|
|
|
void soisfconnected(register struct socket *);
|
|
|
|
void sofwdrain(struct socket *);
|
2009-04-05 18:03:31 +00:00
|
|
|
struct iovec; /* For win32 */
|
2009-01-08 19:18:21 +00:00
|
|
|
size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np);
|
|
|
|
int soreadbuf(struct socket *so, const char *buf, int size);
|
2004-04-22 00:10:48 +00:00
|
|
|
|
slirp: Factorizing address translation
This patch factorizes some duplicate code into a new function,
sotranslate_out(). This function perform the address translation when a
packet is transmitted to the host network. If the packet is destinated
to the host, the loopback address is used, and if the packet is
destinated to the virtual DNS, the real DNS address is used. This code
is just a copy of the existent, but factorized and ready to manage the
IPv6 case.
On the same model, the major part of udp_output() code is moved into a
new sotranslate_in(). This function is directly used in sorecvfrom(),
like sotranslate_out() in sosendto().
udp_output() becoming useless, it is removed and udp_output2() is
renamed into udp_output(). This adds consistency with the udp6_output()
function introduced by further patches.
Lastly, this factorizes some duplicate code into sotranslate_accept(), which
performs the address translation when a connection is established on the host
for port forwarding: if it comes from localhost, the host virtual address is
used instead.
This prepares for IPv6 support.
Signed-off-by: Guillaume Subiron <maethor@subiron.org>
Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
2015-12-19 21:24:59 +00:00
|
|
|
void sotranslate_out(struct socket *, struct sockaddr_storage *);
|
|
|
|
void sotranslate_in(struct socket *, struct sockaddr_storage *);
|
|
|
|
void sotranslate_accept(struct socket *);
|
|
|
|
|
|
|
|
|
2016-06-29 11:47:03 +00:00
|
|
|
#endif /* SLIRP_SOCKET_H */
|