bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1995 Danny Gasparovski. |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
| 4 | * Please read the file COPYRIGHT for the |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 5 | * terms and conditions of the copyright. |
| 6 | */ |
| 7 | |
Markus Armbruster | 2a6a407 | 2016-06-29 13:47:03 +0200 | [diff] [blame] | 8 | #ifndef SLIRP_SOCKET_H |
| 9 | #define SLIRP_SOCKET_H |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 10 | |
| 11 | #define SO_EXPIRE 240000 |
| 12 | #define SO_EXPIREFAST 10000 |
| 13 | |
| 14 | /* |
| 15 | * Our socket structure |
| 16 | */ |
| 17 | |
| 18 | struct socket { |
| 19 | struct socket *so_next,*so_prev; /* For a linked list of sockets */ |
| 20 | |
| 21 | int s; /* The actual socket */ |
| 22 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 23 | int pollfds_idx; /* GPollFD GArray index */ |
| 24 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 25 | Slirp *slirp; /* managing slirp instance */ |
| 26 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 27 | /* XXX union these with not-yet-used sbuf params */ |
| 28 | struct mbuf *so_m; /* Pointer to the original SYN packet, |
| 29 | * for non-blocking connect()'s, and |
| 30 | * PING reply's */ |
| 31 | struct tcpiphdr *so_ti; /* Pointer to the original ti within |
| 32 | * so_mconn, for non-blocking connections */ |
| 33 | int so_urgc; |
Guillaume Subiron | eae303f | 2015-12-19 22:24:58 +0100 | [diff] [blame] | 34 | union { /* foreign host */ |
| 35 | struct sockaddr_storage ss; |
| 36 | struct sockaddr_in sin; |
Guillaume Subiron | 15d62af | 2016-03-15 10:31:20 +0100 | [diff] [blame] | 37 | struct sockaddr_in6 sin6; |
Guillaume Subiron | eae303f | 2015-12-19 22:24:58 +0100 | [diff] [blame] | 38 | } fhost; |
| 39 | #define so_faddr fhost.sin.sin_addr |
| 40 | #define so_fport fhost.sin.sin_port |
Guillaume Subiron | 15d62af | 2016-03-15 10:31:20 +0100 | [diff] [blame] | 41 | #define so_faddr6 fhost.sin6.sin6_addr |
| 42 | #define so_fport6 fhost.sin6.sin6_port |
Guillaume Subiron | eae303f | 2015-12-19 22:24:58 +0100 | [diff] [blame] | 43 | #define so_ffamily fhost.ss.ss_family |
| 44 | |
| 45 | union { /* local host */ |
| 46 | struct sockaddr_storage ss; |
| 47 | struct sockaddr_in sin; |
Guillaume Subiron | 15d62af | 2016-03-15 10:31:20 +0100 | [diff] [blame] | 48 | struct sockaddr_in6 sin6; |
Guillaume Subiron | eae303f | 2015-12-19 22:24:58 +0100 | [diff] [blame] | 49 | } lhost; |
| 50 | #define so_laddr lhost.sin.sin_addr |
| 51 | #define so_lport lhost.sin.sin_port |
Guillaume Subiron | 15d62af | 2016-03-15 10:31:20 +0100 | [diff] [blame] | 52 | #define so_laddr6 lhost.sin6.sin6_addr |
| 53 | #define so_lport6 lhost.sin6.sin6_port |
Guillaume Subiron | eae303f | 2015-12-19 22:24:58 +0100 | [diff] [blame] | 54 | #define so_lfamily lhost.ss.ss_family |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 55 | |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 56 | uint8_t so_iptos; /* Type of service */ |
| 57 | uint8_t so_emu; /* Is the socket emulated? */ |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 58 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 59 | u_char so_type; /* Type of socket, UDP or TCP */ |
| 60 | int so_state; /* internal state flags SS_*, below */ |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 61 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 62 | struct tcpcb *so_tcpcb; /* pointer to TCP protocol control block */ |
| 63 | u_int so_expire; /* When the socket will expire */ |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 64 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 65 | int so_queued; /* Number of packets queued from this socket */ |
| 66 | int so_nqueued; /* Number of packets queued in a row |
| 67 | * Used to determine when to "downgrade" a session |
| 68 | * from fastq to batchq */ |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 69 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 70 | struct sbuf so_rcv; /* Receive buffer */ |
| 71 | struct sbuf so_snd; /* Send buffer */ |
| 72 | void * extra; /* Extra pointer */ |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | /* |
| 77 | * Socket state bits. (peer means the host on the Internet, |
| 78 | * local host means the host on the other end of the modem) |
| 79 | */ |
| 80 | #define SS_NOFDREF 0x001 /* No fd reference */ |
| 81 | |
| 82 | #define SS_ISFCONNECTING 0x002 /* Socket is connecting to peer (non-blocking connect()'s) */ |
| 83 | #define SS_ISFCONNECTED 0x004 /* Socket is connected to peer */ |
| 84 | #define SS_FCANTRCVMORE 0x008 /* Socket can't receive more from peer (for half-closes) */ |
| 85 | #define SS_FCANTSENDMORE 0x010 /* Socket can't send more to peer (for half-closes) */ |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 86 | #define SS_FWDRAIN 0x040 /* We received a FIN, drain data and set SS_FCANTSENDMORE */ |
| 87 | |
| 88 | #define SS_CTL 0x080 |
| 89 | #define SS_FACCEPTCONN 0x100 /* Socket is accepting connections from a host on the internet */ |
| 90 | #define SS_FACCEPTONCE 0x200 /* If set, the SS_FACCEPTCONN socket will die after one accept */ |
| 91 | |
Jan Kiszka | f932b6c | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 92 | #define SS_PERSISTENT_MASK 0xf000 /* Unremovable state bits */ |
Jan Kiszka | 6dd5ffb | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 93 | #define SS_HOSTFWD 0x1000 /* Socket describes host->guest forwarding */ |
Jan Kiszka | 4a82347 | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 94 | #define SS_INCOMING 0x2000 /* Connection was initiated by a host on the internet */ |
Jan Kiszka | f932b6c | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 95 | |
Guillaume Subiron | 8a87f12 | 2015-12-19 22:25:01 +0100 | [diff] [blame] | 96 | static inline int sockaddr_equal(struct sockaddr_storage *a, |
| 97 | struct sockaddr_storage *b) |
| 98 | { |
| 99 | if (a->ss_family != b->ss_family) { |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | switch (a->ss_family) { |
| 104 | case AF_INET: |
| 105 | { |
| 106 | struct sockaddr_in *a4 = (struct sockaddr_in *) a; |
| 107 | struct sockaddr_in *b4 = (struct sockaddr_in *) b; |
| 108 | return a4->sin_addr.s_addr == b4->sin_addr.s_addr |
| 109 | && a4->sin_port == b4->sin_port; |
| 110 | } |
Guillaume Subiron | 0d6ff71 | 2016-03-15 10:31:19 +0100 | [diff] [blame] | 111 | case AF_INET6: |
| 112 | { |
| 113 | struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) a; |
| 114 | struct sockaddr_in6 *b6 = (struct sockaddr_in6 *) b; |
| 115 | return (in6_equal(&a6->sin6_addr, &b6->sin6_addr) |
| 116 | && a6->sin6_port == b6->sin6_port); |
| 117 | } |
Guillaume Subiron | 8a87f12 | 2015-12-19 22:25:01 +0100 | [diff] [blame] | 118 | default: |
| 119 | g_assert_not_reached(); |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
Samuel Thibault | 0d48dfe | 2016-04-28 18:53:08 +0200 | [diff] [blame] | 125 | static inline socklen_t sockaddr_size(struct sockaddr_storage *a) |
| 126 | { |
| 127 | switch (a->ss_family) { |
| 128 | case AF_INET: |
| 129 | return sizeof(struct sockaddr_in); |
| 130 | case AF_INET6: |
| 131 | return sizeof(struct sockaddr_in6); |
| 132 | default: |
| 133 | g_assert_not_reached(); |
| 134 | } |
| 135 | } |
| 136 | |
Guillaume Subiron | a5fd24a | 2015-12-19 22:25:00 +0100 | [diff] [blame] | 137 | struct socket *solookup(struct socket **, struct socket *, |
Guillaume Subiron | 8a87f12 | 2015-12-19 22:25:01 +0100 | [diff] [blame] | 138 | struct sockaddr_storage *, struct sockaddr_storage *); |
Guillaume Subiron | a5fd24a | 2015-12-19 22:25:00 +0100 | [diff] [blame] | 139 | struct socket *socreate(Slirp *); |
Blue Swirl | 6cb9c6d | 2009-07-01 19:11:17 +0000 | [diff] [blame] | 140 | void sofree(struct socket *); |
| 141 | int soread(struct socket *); |
Steven Luo | bfb1ac1 | 2016-04-06 22:04:32 -0700 | [diff] [blame] | 142 | int sorecvoob(struct socket *); |
Blue Swirl | 6cb9c6d | 2009-07-01 19:11:17 +0000 | [diff] [blame] | 143 | int sosendoob(struct socket *); |
| 144 | int sowrite(struct socket *); |
| 145 | void sorecvfrom(struct socket *); |
| 146 | int sosendto(struct socket *, struct mbuf *); |
Stefan Weil | b6dce92 | 2010-07-22 22:15:23 +0200 | [diff] [blame] | 147 | struct socket * tcp_listen(Slirp *, uint32_t, u_int, uint32_t, u_int, |
Blue Swirl | 6cb9c6d | 2009-07-01 19:11:17 +0000 | [diff] [blame] | 148 | int); |
| 149 | void soisfconnecting(register struct socket *); |
| 150 | void soisfconnected(register struct socket *); |
| 151 | void sofwdrain(struct socket *); |
blueswir1 | b9e82a5 | 2009-04-05 18:03:31 +0000 | [diff] [blame] | 152 | struct iovec; /* For win32 */ |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 153 | size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np); |
| 154 | int soreadbuf(struct socket *so, const char *buf, int size); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 155 | |
Guillaume Subiron | 5379229 | 2015-12-19 22:24:59 +0100 | [diff] [blame] | 156 | void sotranslate_out(struct socket *, struct sockaddr_storage *); |
| 157 | void sotranslate_in(struct socket *, struct sockaddr_storage *); |
| 158 | void sotranslate_accept(struct socket *); |
| 159 | |
| 160 | |
Markus Armbruster | 2a6a407 | 2016-06-29 13:47:03 +0200 | [diff] [blame] | 161 | #endif /* SLIRP_SOCKET_H */ |