iothread: use signalfd
[qemu/mdroth.git] / slirp / sbuf.c
blob5a1ccbfadfe6bd35145cad6cb7c289d77dda0f35
1 /*
2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
8 #include <slirp.h>
10 static void sbappendsb(struct sbuf *sb, struct mbuf *m);
12 void
13 sbfree(struct sbuf *sb)
15 free(sb->sb_data);
18 void
19 sbdrop(struct sbuf *sb, int num)
22 * We can only drop how much we have
23 * This should never succeed
25 if(num > sb->sb_cc)
26 num = sb->sb_cc;
27 sb->sb_cc -= num;
28 sb->sb_rptr += num;
29 if(sb->sb_rptr >= sb->sb_data + sb->sb_datalen)
30 sb->sb_rptr -= sb->sb_datalen;
34 void
35 sbreserve(struct sbuf *sb, int size)
37 if (sb->sb_data) {
38 /* Already alloced, realloc if necessary */
39 if (sb->sb_datalen != size) {
40 sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)realloc(sb->sb_data, size);
41 sb->sb_cc = 0;
42 if (sb->sb_wptr)
43 sb->sb_datalen = size;
44 else
45 sb->sb_datalen = 0;
47 } else {
48 sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)malloc(size);
49 sb->sb_cc = 0;
50 if (sb->sb_wptr)
51 sb->sb_datalen = size;
52 else
53 sb->sb_datalen = 0;
58 * Try and write() to the socket, whatever doesn't get written
59 * append to the buffer... for a host with a fast net connection,
60 * this prevents an unnecessary copy of the data
61 * (the socket is non-blocking, so we won't hang)
63 void
64 sbappend(struct socket *so, struct mbuf *m)
66 int ret = 0;
68 DEBUG_CALL("sbappend");
69 DEBUG_ARG("so = %lx", (long)so);
70 DEBUG_ARG("m = %lx", (long)m);
71 DEBUG_ARG("m->m_len = %d", m->m_len);
73 /* Shouldn't happen, but... e.g. foreign host closes connection */
74 if (m->m_len <= 0) {
75 m_free(m);
76 return;
80 * If there is urgent data, call sosendoob
81 * if not all was sent, sowrite will take care of the rest
82 * (The rest of this function is just an optimisation)
84 if (so->so_urgc) {
85 sbappendsb(&so->so_rcv, m);
86 m_free(m);
87 sosendoob(so);
88 return;
92 * We only write if there's nothing in the buffer,
93 * ottherwise it'll arrive out of order, and hence corrupt
95 if (!so->so_rcv.sb_cc)
96 ret = slirp_send(so, m->m_data, m->m_len, 0);
98 if (ret <= 0) {
100 * Nothing was written
101 * It's possible that the socket has closed, but
102 * we don't need to check because if it has closed,
103 * it will be detected in the normal way by soread()
105 sbappendsb(&so->so_rcv, m);
106 } else if (ret != m->m_len) {
108 * Something was written, but not everything..
109 * sbappendsb the rest
111 m->m_len -= ret;
112 m->m_data += ret;
113 sbappendsb(&so->so_rcv, m);
114 } /* else */
115 /* Whatever happened, we free the mbuf */
116 m_free(m);
120 * Copy the data from m into sb
121 * The caller is responsible to make sure there's enough room
123 static void
124 sbappendsb(struct sbuf *sb, struct mbuf *m)
126 int len, n, nn;
128 len = m->m_len;
130 if (sb->sb_wptr < sb->sb_rptr) {
131 n = sb->sb_rptr - sb->sb_wptr;
132 if (n > len) n = len;
133 memcpy(sb->sb_wptr, m->m_data, n);
134 } else {
135 /* Do the right edge first */
136 n = sb->sb_data + sb->sb_datalen - sb->sb_wptr;
137 if (n > len) n = len;
138 memcpy(sb->sb_wptr, m->m_data, n);
139 len -= n;
140 if (len) {
141 /* Now the left edge */
142 nn = sb->sb_rptr - sb->sb_data;
143 if (nn > len) nn = len;
144 memcpy(sb->sb_data,m->m_data+n,nn);
145 n += nn;
149 sb->sb_cc += n;
150 sb->sb_wptr += n;
151 if (sb->sb_wptr >= sb->sb_data + sb->sb_datalen)
152 sb->sb_wptr -= sb->sb_datalen;
156 * Copy data from sbuf to a normal, straight buffer
157 * Don't update the sbuf rptr, this will be
158 * done in sbdrop when the data is acked
160 void
161 sbcopy(struct sbuf *sb, int off, int len, char *to)
163 char *from;
165 from = sb->sb_rptr + off;
166 if (from >= sb->sb_data + sb->sb_datalen)
167 from -= sb->sb_datalen;
169 if (from < sb->sb_wptr) {
170 if (len > sb->sb_cc) len = sb->sb_cc;
171 memcpy(to,from,len);
172 } else {
173 /* re-use off */
174 off = (sb->sb_data + sb->sb_datalen) - from;
175 if (off > len) off = len;
176 memcpy(to,from,off);
177 len -= off;
178 if (len)
179 memcpy(to+off,sb->sb_data,len);