OSDN Git Service

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