2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
10 static void sbappendsb(struct sbuf
*sb
, struct mbuf
*m
);
13 sbfree(struct sbuf
*sb
)
19 sbdrop(struct sbuf
*sb
, int num
)
22 * We can only drop how much we have
23 * This should never succeed
29 if(sb
->sb_rptr
>= sb
->sb_data
+ sb
->sb_datalen
)
30 sb
->sb_rptr
-= sb
->sb_datalen
;
35 sbreserve(struct sbuf
*sb
, int size
)
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
);
43 sb
->sb_datalen
= size
;
48 sb
->sb_wptr
= sb
->sb_rptr
= sb
->sb_data
= (char *)malloc(size
);
51 sb
->sb_datalen
= size
;
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)
64 sbappend(struct socket
*so
, struct mbuf
*m
)
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 */
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)
85 sbappendsb(&so
->so_rcv
, m
);
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);
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
113 sbappendsb(&so
->so_rcv
, m
);
115 /* Whatever happened, we free the mbuf */
120 * Copy the data from m into sb
121 * The caller is responsible to make sure there's enough room
124 sbappendsb(struct sbuf
*sb
, struct mbuf
*m
)
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
);
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
);
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
);
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
161 sbcopy(struct sbuf
*sb
, int off
, int len
, char *to
)
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
;
174 off
= (sb
->sb_data
+ sb
->sb_datalen
) - from
;
175 if (off
> len
) off
= len
;
179 memcpy(to
+off
,sb
->sb_data
,len
);