1
2
3
4
5
6
7
8#include "qemu/osdep.h"
9#include <slirp.h>
10#include <qemu/main-loop.h>
11
12static void sbappendsb(struct sbuf *sb, struct mbuf *m);
13
14void
15sbfree(struct sbuf *sb)
16{
17 free(sb->sb_data);
18}
19
20void
21sbdrop(struct sbuf *sb, int num)
22{
23 int limit = sb->sb_datalen / 2;
24
25
26
27
28
29 if(num > sb->sb_cc)
30 num = sb->sb_cc;
31 sb->sb_cc -= num;
32 sb->sb_rptr += num;
33 if(sb->sb_rptr >= sb->sb_data + sb->sb_datalen)
34 sb->sb_rptr -= sb->sb_datalen;
35
36 if (sb->sb_cc < limit && sb->sb_cc + num >= limit) {
37 qemu_notify_event();
38 }
39}
40
41void
42sbreserve(struct sbuf *sb, int size)
43{
44 if (sb->sb_data) {
45
46 if (sb->sb_datalen != size) {
47 sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)realloc(sb->sb_data, size);
48 sb->sb_cc = 0;
49 if (sb->sb_wptr)
50 sb->sb_datalen = size;
51 else
52 sb->sb_datalen = 0;
53 }
54 } else {
55 sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)malloc(size);
56 sb->sb_cc = 0;
57 if (sb->sb_wptr)
58 sb->sb_datalen = size;
59 else
60 sb->sb_datalen = 0;
61 }
62}
63
64
65
66
67
68
69
70void
71sbappend(struct socket *so, struct mbuf *m)
72{
73 int ret = 0;
74
75 DEBUG_CALL("sbappend");
76 DEBUG_ARG("so = %p", so);
77 DEBUG_ARG("m = %p", m);
78 DEBUG_ARG("m->m_len = %d", m->m_len);
79
80
81 if (m->m_len <= 0) {
82 m_free(m);
83 return;
84 }
85
86
87
88
89
90
91 if (so->so_urgc) {
92 sbappendsb(&so->so_rcv, m);
93 m_free(m);
94 sosendoob(so);
95 return;
96 }
97
98
99
100
101
102 if (!so->so_rcv.sb_cc)
103 ret = slirp_send(so, m->m_data, m->m_len, 0);
104
105 if (ret <= 0) {
106
107
108
109
110
111
112 sbappendsb(&so->so_rcv, m);
113 } else if (ret != m->m_len) {
114
115
116
117
118 m->m_len -= ret;
119 m->m_data += ret;
120 sbappendsb(&so->so_rcv, m);
121 }
122
123 m_free(m);
124}
125
126
127
128
129
130static void
131sbappendsb(struct sbuf *sb, struct mbuf *m)
132{
133 int len, n, nn;
134
135 len = m->m_len;
136
137 if (sb->sb_wptr < sb->sb_rptr) {
138 n = sb->sb_rptr - sb->sb_wptr;
139 if (n > len) n = len;
140 memcpy(sb->sb_wptr, m->m_data, n);
141 } else {
142
143 n = sb->sb_data + sb->sb_datalen - sb->sb_wptr;
144 if (n > len) n = len;
145 memcpy(sb->sb_wptr, m->m_data, n);
146 len -= n;
147 if (len) {
148
149 nn = sb->sb_rptr - sb->sb_data;
150 if (nn > len) nn = len;
151 memcpy(sb->sb_data,m->m_data+n,nn);
152 n += nn;
153 }
154 }
155
156 sb->sb_cc += n;
157 sb->sb_wptr += n;
158 if (sb->sb_wptr >= sb->sb_data + sb->sb_datalen)
159 sb->sb_wptr -= sb->sb_datalen;
160}
161
162
163
164
165
166
167void
168sbcopy(struct sbuf *sb, int off, int len, char *to)
169{
170 char *from;
171
172 from = sb->sb_rptr + off;
173 if (from >= sb->sb_data + sb->sb_datalen)
174 from -= sb->sb_datalen;
175
176 if (from < sb->sb_wptr) {
177 if (len > sb->sb_cc) len = sb->sb_cc;
178 memcpy(to,from,len);
179 } else {
180
181 off = (sb->sb_data + sb->sb_datalen) - from;
182 if (off > len) off = len;
183 memcpy(to,from,off);
184 len -= off;
185 if (len)
186 memcpy(to+off,sb->sb_data,len);
187 }
188}
189