blob: 5a1ccbfadfe6bd35145cad6cb7c289d77dda0f35 [file] [log] [blame]
bellardf0cbd3e2004-04-22 00:10:48 +00001/*
2 * Copyright (c) 1995 Danny Gasparovski.
ths5fafdf22007-09-16 21:08:06 +00003 *
4 * Please read the file COPYRIGHT for the
bellardf0cbd3e2004-04-22 00:10:48 +00005 * terms and conditions of the copyright.
6 */
7
8#include <slirp.h>
9
blueswir19634d902007-10-26 19:01:16 +000010static void sbappendsb(struct sbuf *sb, struct mbuf *m);
11
bellardf0cbd3e2004-04-22 00:10:48 +000012void
blueswir1511d2b12009-03-07 15:32:56 +000013sbfree(struct sbuf *sb)
bellardf0cbd3e2004-04-22 00:10:48 +000014{
15 free(sb->sb_data);
16}
17
18void
blueswir1511d2b12009-03-07 15:32:56 +000019sbdrop(struct sbuf *sb, int num)
bellardf0cbd3e2004-04-22 00:10:48 +000020{
ths5fafdf22007-09-16 21:08:06 +000021 /*
bellardf0cbd3e2004-04-22 00:10:48 +000022 * We can only drop how much we have
ths5fafdf22007-09-16 21:08:06 +000023 * This should never succeed
bellardf0cbd3e2004-04-22 00:10:48 +000024 */
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;
ths3b46e622007-09-17 08:09:54 +000031
bellardf0cbd3e2004-04-22 00:10:48 +000032}
33
34void
blueswir1511d2b12009-03-07 15:32:56 +000035sbreserve(struct sbuf *sb, int size)
bellardf0cbd3e2004-04-22 00:10:48 +000036{
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;
46 }
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;
54 }
55}
56
57/*
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)
62 */
63void
blueswir1511d2b12009-03-07 15:32:56 +000064sbappend(struct socket *so, struct mbuf *m)
bellardf0cbd3e2004-04-22 00:10:48 +000065{
66 int ret = 0;
ths5fafdf22007-09-16 21:08:06 +000067
bellardf0cbd3e2004-04-22 00:10:48 +000068 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);
ths5fafdf22007-09-16 21:08:06 +000072
bellardf0cbd3e2004-04-22 00:10:48 +000073 /* Shouldn't happen, but... e.g. foreign host closes connection */
74 if (m->m_len <= 0) {
75 m_free(m);
76 return;
77 }
ths5fafdf22007-09-16 21:08:06 +000078
bellardf0cbd3e2004-04-22 00:10:48 +000079 /*
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)
83 */
84 if (so->so_urgc) {
85 sbappendsb(&so->so_rcv, m);
86 m_free(m);
87 sosendoob(so);
88 return;
89 }
ths5fafdf22007-09-16 21:08:06 +000090
bellardf0cbd3e2004-04-22 00:10:48 +000091 /*
92 * We only write if there's nothing in the buffer,
93 * ottherwise it'll arrive out of order, and hence corrupt
94 */
95 if (!so->so_rcv.sb_cc)
aliguorie1c5a2b2009-01-08 19:18:21 +000096 ret = slirp_send(so, m->m_data, m->m_len, 0);
ths5fafdf22007-09-16 21:08:06 +000097
bellardf0cbd3e2004-04-22 00:10:48 +000098 if (ret <= 0) {
ths5fafdf22007-09-16 21:08:06 +000099 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000100 * 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()
104 */
105 sbappendsb(&so->so_rcv, m);
106 } else if (ret != m->m_len) {
107 /*
108 * Something was written, but not everything..
109 * sbappendsb the rest
110 */
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);
117}
118
119/*
120 * Copy the data from m into sb
121 * The caller is responsible to make sure there's enough room
122 */
blueswir19634d902007-10-26 19:01:16 +0000123static void
124sbappendsb(struct sbuf *sb, struct mbuf *m)
bellardf0cbd3e2004-04-22 00:10:48 +0000125{
126 int len, n, nn;
ths5fafdf22007-09-16 21:08:06 +0000127
bellardf0cbd3e2004-04-22 00:10:48 +0000128 len = m->m_len;
129
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;
146 }
147 }
148
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;
153}
154
155/*
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
159 */
160void
blueswir1511d2b12009-03-07 15:32:56 +0000161sbcopy(struct sbuf *sb, int off, int len, char *to)
bellardf0cbd3e2004-04-22 00:10:48 +0000162{
163 char *from;
ths5fafdf22007-09-16 21:08:06 +0000164
bellardf0cbd3e2004-04-22 00:10:48 +0000165 from = sb->sb_rptr + off;
166 if (from >= sb->sb_data + sb->sb_datalen)
167 from -= sb->sb_datalen;
168
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);
180 }
181}