2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
11 /* Done as a macro in socket.h */
13 * sbspace(struct sockbuff *sb)
15 * return SB_DATALEN - sb->sb_cc;
32 * We can only drop how much we have
33 * This should never succeed
39 if(sb
->sb_rptr
>= sb
->sb_data
+ sb
->sb_datalen
)
40 sb
->sb_rptr
-= sb
->sb_datalen
;
50 /* Already alloced, realloc if necessary */
51 if (sb
->sb_datalen
!= size
) {
52 sb
->sb_wptr
= sb
->sb_rptr
= sb
->sb_data
= (char *)realloc(sb
->sb_data
, size
);
55 sb
->sb_datalen
= size
;
60 sb
->sb_wptr
= sb
->sb_rptr
= sb
->sb_data
= (char *)malloc(size
);
63 sb
->sb_datalen
= size
;
70 * Try and write() to the socket, whatever doesn't get written
71 * append to the buffer... for a host with a fast net connection,
72 * this prevents an unnecessary copy of the data
73 * (the socket is non-blocking, so we won't hang)
82 DEBUG_CALL("sbappend");
83 DEBUG_ARG("so = %lx", (long)so
);
84 DEBUG_ARG("m = %lx", (long)m
);
85 DEBUG_ARG("m->m_len = %d", m
->m_len
);
87 /* Shouldn't happen, but... e.g. foreign host closes connection */
94 * If there is urgent data, call sosendoob
95 * if not all was sent, sowrite will take care of the rest
96 * (The rest of this function is just an optimisation)
99 sbappendsb(&so
->so_rcv
, m
);
106 * We only write if there's nothing in the buffer,
107 * ottherwise it'll arrive out of order, and hence corrupt
109 if (!so
->so_rcv
.sb_cc
)
110 ret
= write(so
->s
, m
->m_data
, m
->m_len
);
114 * Nothing was written
115 * It's possible that the socket has closed, but
116 * we don't need to check because if it has closed,
117 * it will be detected in the normal way by soread()
119 sbappendsb(&so
->so_rcv
, m
);
120 } else if (ret
!= m
->m_len
) {
122 * Something was written, but not everything..
123 * sbappendsb the rest
127 sbappendsb(&so
->so_rcv
, m
);
129 /* Whatever happened, we free the mbuf */
134 * Copy the data from m into sb
135 * The caller is responsible to make sure there's enough room
146 if (sb
->sb_wptr
< sb
->sb_rptr
) {
147 n
= sb
->sb_rptr
- sb
->sb_wptr
;
148 if (n
> len
) n
= len
;
149 memcpy(sb
->sb_wptr
, m
->m_data
, n
);
151 /* Do the right edge first */
152 n
= sb
->sb_data
+ sb
->sb_datalen
- sb
->sb_wptr
;
153 if (n
> len
) n
= len
;
154 memcpy(sb
->sb_wptr
, m
->m_data
, n
);
157 /* Now the left edge */
158 nn
= sb
->sb_rptr
- sb
->sb_data
;
159 if (nn
> len
) nn
= len
;
160 memcpy(sb
->sb_data
,m
->m_data
+n
,nn
);
167 if (sb
->sb_wptr
>= sb
->sb_data
+ sb
->sb_datalen
)
168 sb
->sb_wptr
-= sb
->sb_datalen
;
172 * Copy data from sbuf to a normal, straight buffer
173 * Don't update the sbuf rptr, this will be
174 * done in sbdrop when the data is acked
177 sbcopy(sb
, off
, len
, to
)
185 from
= sb
->sb_rptr
+ off
;
186 if (from
>= sb
->sb_data
+ sb
->sb_datalen
)
187 from
-= sb
->sb_datalen
;
189 if (from
< sb
->sb_wptr
) {
190 if (len
> sb
->sb_cc
) len
= sb
->sb_cc
;
194 off
= (sb
->sb_data
+ sb
->sb_datalen
) - from
;
195 if (off
> len
) off
= len
;
199 memcpy(to
+off
,sb
->sb_data
,len
);