2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
8 #include "qemu/osdep.h"
10 #include <qemu/main-loop.h>
12 static void sbappendsb(struct sbuf
*sb
, struct mbuf
*m
);
15 sbfree(struct sbuf
*sb
)
21 sbdrop(struct sbuf
*sb
, int num
)
23 int limit
= sb
->sb_datalen
/ 2;
26 * We can only drop how much we have
27 * This should never succeed
33 if(sb
->sb_rptr
>= sb
->sb_data
+ sb
->sb_datalen
)
34 sb
->sb_rptr
-= sb
->sb_datalen
;
36 if (sb
->sb_cc
< limit
&& sb
->sb_cc
+ num
>= limit
) {
42 sbreserve(struct sbuf
*sb
, int size
)
45 /* Already alloced, realloc if necessary */
46 if (sb
->sb_datalen
!= size
) {
47 sb
->sb_wptr
= sb
->sb_rptr
= sb
->sb_data
= (char *)realloc(sb
->sb_data
, size
);
50 sb
->sb_datalen
= size
;
55 sb
->sb_wptr
= sb
->sb_rptr
= sb
->sb_data
= (char *)malloc(size
);
58 sb
->sb_datalen
= size
;
65 * Try and write() to the socket, whatever doesn't get written
66 * append to the buffer... for a host with a fast net connection,
67 * this prevents an unnecessary copy of the data
68 * (the socket is non-blocking, so we won't hang)
71 sbappend(struct socket
*so
, struct mbuf
*m
)
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
);
80 /* Shouldn't happen, but... e.g. foreign host closes connection */
87 * If there is urgent data, call sosendoob
88 * if not all was sent, sowrite will take care of the rest
89 * (The rest of this function is just an optimisation)
92 sbappendsb(&so
->so_rcv
, m
);
99 * We only write if there's nothing in the buffer,
100 * ottherwise it'll arrive out of order, and hence corrupt
102 if (!so
->so_rcv
.sb_cc
)
103 ret
= slirp_send(so
, m
->m_data
, m
->m_len
, 0);
107 * Nothing was written
108 * It's possible that the socket has closed, but
109 * we don't need to check because if it has closed,
110 * it will be detected in the normal way by soread()
112 sbappendsb(&so
->so_rcv
, m
);
113 } else if (ret
!= m
->m_len
) {
115 * Something was written, but not everything..
116 * sbappendsb the rest
120 sbappendsb(&so
->so_rcv
, m
);
122 /* Whatever happened, we free the mbuf */
127 * Copy the data from m into sb
128 * The caller is responsible to make sure there's enough room
131 sbappendsb(struct sbuf
*sb
, struct mbuf
*m
)
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
);
142 /* Do the right edge first */
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
);
148 /* Now the left edge */
149 nn
= sb
->sb_rptr
- sb
->sb_data
;
150 if (nn
> len
) nn
= len
;
151 memcpy(sb
->sb_data
,m
->m_data
+n
,nn
);
158 if (sb
->sb_wptr
>= sb
->sb_data
+ sb
->sb_datalen
)
159 sb
->sb_wptr
-= sb
->sb_datalen
;
163 * Copy data from sbuf to a normal, straight buffer
164 * Don't update the sbuf rptr, this will be
165 * done in sbdrop when the data is acked
168 sbcopy(struct sbuf
*sb
, int off
, int len
, char *to
)
172 from
= sb
->sb_rptr
+ off
;
173 if (from
>= sb
->sb_data
+ sb
->sb_datalen
)
174 from
-= sb
->sb_datalen
;
176 if (from
< sb
->sb_wptr
) {
177 if (len
> sb
->sb_cc
) len
= sb
->sb_cc
;
181 off
= (sb
->sb_data
+ sb
->sb_datalen
) - from
;
182 if (off
> len
) off
= len
;
186 memcpy(to
+off
,sb
->sb_data
,len
);