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
);
12 /* Done as a macro in socket.h */
14 * sbspace(struct sockbuff *sb)
16 * return SB_DATALEN - sb->sb_cc;
21 sbfree(struct sbuf
*sb
)
27 sbdrop(struct sbuf
*sb
, int num
)
30 * We can only drop how much we have
31 * This should never succeed
37 if(sb
->sb_rptr
>= sb
->sb_data
+ sb
->sb_datalen
)
38 sb
->sb_rptr
-= sb
->sb_datalen
;
43 sbreserve(struct sbuf
*sb
, int size
)
46 /* Already alloced, realloc if necessary */
47 if (sb
->sb_datalen
!= size
) {
48 sb
->sb_wptr
= sb
->sb_rptr
= sb
->sb_data
= (char *)realloc(sb
->sb_data
, size
);
51 sb
->sb_datalen
= size
;
56 sb
->sb_wptr
= sb
->sb_rptr
= sb
->sb_data
= (char *)malloc(size
);
59 sb
->sb_datalen
= size
;
66 * Try and write() to the socket, whatever doesn't get written
67 * append to the buffer... for a host with a fast net connection,
68 * this prevents an unnecessary copy of the data
69 * (the socket is non-blocking, so we won't hang)
72 sbappend(struct socket
*so
, struct mbuf
*m
)
76 DEBUG_CALL("sbappend");
77 DEBUG_ARG("so = %lx", (long)so
);
78 DEBUG_ARG("m = %lx", (long)m
);
79 DEBUG_ARG("m->m_len = %d", m
->m_len
);
81 /* Shouldn't happen, but... e.g. foreign host closes connection */
88 * If there is urgent data, call sosendoob
89 * if not all was sent, sowrite will take care of the rest
90 * (The rest of this function is just an optimisation)
93 sbappendsb(&so
->so_rcv
, m
);
100 * We only write if there's nothing in the buffer,
101 * ottherwise it'll arrive out of order, and hence corrupt
103 if (!so
->so_rcv
.sb_cc
)
104 ret
= slirp_send(so
, m
->m_data
, m
->m_len
, 0);
108 * Nothing was written
109 * It's possible that the socket has closed, but
110 * we don't need to check because if it has closed,
111 * it will be detected in the normal way by soread()
113 sbappendsb(&so
->so_rcv
, m
);
114 } else if (ret
!= m
->m_len
) {
116 * Something was written, but not everything..
117 * sbappendsb the rest
121 sbappendsb(&so
->so_rcv
, m
);
123 /* Whatever happened, we free the mbuf */
128 * Copy the data from m into sb
129 * The caller is responsible to make sure there's enough room
132 sbappendsb(struct sbuf
*sb
, struct mbuf
*m
)
138 if (sb
->sb_wptr
< sb
->sb_rptr
) {
139 n
= sb
->sb_rptr
- sb
->sb_wptr
;
140 if (n
> len
) n
= len
;
141 memcpy(sb
->sb_wptr
, m
->m_data
, n
);
143 /* Do the right edge first */
144 n
= sb
->sb_data
+ sb
->sb_datalen
- sb
->sb_wptr
;
145 if (n
> len
) n
= len
;
146 memcpy(sb
->sb_wptr
, m
->m_data
, n
);
149 /* Now the left edge */
150 nn
= sb
->sb_rptr
- sb
->sb_data
;
151 if (nn
> len
) nn
= len
;
152 memcpy(sb
->sb_data
,m
->m_data
+n
,nn
);
159 if (sb
->sb_wptr
>= sb
->sb_data
+ sb
->sb_datalen
)
160 sb
->sb_wptr
-= sb
->sb_datalen
;
164 * Copy data from sbuf to a normal, straight buffer
165 * Don't update the sbuf rptr, this will be
166 * done in sbdrop when the data is acked
169 sbcopy(struct sbuf
*sb
, int off
, int len
, char *to
)
173 from
= sb
->sb_rptr
+ off
;
174 if (from
>= sb
->sb_data
+ sb
->sb_datalen
)
175 from
-= sb
->sb_datalen
;
177 if (from
< sb
->sb_wptr
) {
178 if (len
> sb
->sb_cc
) len
= sb
->sb_cc
;
182 off
= (sb
->sb_data
+ sb
->sb_datalen
) - from
;
183 if (off
> len
) off
= len
;
187 memcpy(to
+off
,sb
->sb_data
,len
);