2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
9 #include <qemu/main-loop.h>
11 static void sbappendsb(struct sbuf *sb, struct mbuf *m);
14 sbfree(struct sbuf *sb)
20 sbdrop(struct sbuf *sb, int num)
22 int limit = sb->sb_datalen / 2;
25 * We can only drop how much we have
26 * This should never succeed
32 if(sb->sb_rptr >= sb->sb_data + sb->sb_datalen)
33 sb->sb_rptr -= sb->sb_datalen;
35 if (sb->sb_cc < limit && sb->sb_cc + num >= limit) {
41 sbreserve(struct sbuf *sb, int size)
44 /* Already alloced, realloc if necessary */
45 if (sb->sb_datalen != size) {
46 sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)realloc(sb->sb_data, size);
49 sb->sb_datalen = size;
54 sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)malloc(size);
57 sb->sb_datalen = size;
64 * Try and write() to the socket, whatever doesn't get written
65 * append to the buffer... for a host with a fast net connection,
66 * this prevents an unnecessary copy of the data
67 * (the socket is non-blocking, so we won't hang)
70 sbappend(struct socket *so, struct mbuf *m)
74 DEBUG_CALL("sbappend");
75 DEBUG_ARG("so = %lx", (long)so);
76 DEBUG_ARG("m = %lx", (long)m);
77 DEBUG_ARG("m->m_len = %d", m->m_len);
79 /* Shouldn't happen, but... e.g. foreign host closes connection */
86 * If there is urgent data, call sosendoob
87 * if not all was sent, sowrite will take care of the rest
88 * (The rest of this function is just an optimisation)
91 sbappendsb(&so->so_rcv, m);
98 * We only write if there's nothing in the buffer,
99 * ottherwise it'll arrive out of order, and hence corrupt
101 if (!so->so_rcv.sb_cc)
102 ret = slirp_send(so, m->m_data, m->m_len, 0);
106 * Nothing was written
107 * It's possible that the socket has closed, but
108 * we don't need to check because if it has closed,
109 * it will be detected in the normal way by soread()
111 sbappendsb(&so->so_rcv, m);
112 } else if (ret != m->m_len) {
114 * Something was written, but not everything..
115 * sbappendsb the rest
119 sbappendsb(&so->so_rcv, m);
121 /* Whatever happened, we free the mbuf */
126 * Copy the data from m into sb
127 * The caller is responsible to make sure there's enough room
130 sbappendsb(struct sbuf *sb, struct mbuf *m)
136 if (sb->sb_wptr < sb->sb_rptr) {
137 n = sb->sb_rptr - sb->sb_wptr;
138 if (n > len) n = len;
139 memcpy(sb->sb_wptr, m->m_data, n);
141 /* Do the right edge first */
142 n = sb->sb_data + sb->sb_datalen - sb->sb_wptr;
143 if (n > len) n = len;
144 memcpy(sb->sb_wptr, m->m_data, n);
147 /* Now the left edge */
148 nn = sb->sb_rptr - sb->sb_data;
149 if (nn > len) nn = len;
150 memcpy(sb->sb_data,m->m_data+n,nn);
157 if (sb->sb_wptr >= sb->sb_data + sb->sb_datalen)
158 sb->sb_wptr -= sb->sb_datalen;
162 * Copy data from sbuf to a normal, straight buffer
163 * Don't update the sbuf rptr, this will be
164 * done in sbdrop when the data is acked
167 sbcopy(struct sbuf *sb, int off, int len, char *to)
171 from = sb->sb_rptr + off;
172 if (from >= sb->sb_data + sb->sb_datalen)
173 from -= sb->sb_datalen;
175 if (from < sb->sb_wptr) {
176 if (len > sb->sb_cc) len = sb->sb_cc;
180 off = (sb->sb_data + sb->sb_datalen) - from;
181 if (off > len) off = len;
185 memcpy(to+off,sb->sb_data,len);