1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Sock Mbuf |
---|
5 | * @ingroup libfs |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * NOTE: |
---|
10 | * This is derived from libnetworking/rtems/rtems_syscall.c |
---|
11 | * |
---|
12 | * RTEMS/libnetworking LICENSING restrictions may apply |
---|
13 | * |
---|
14 | * Author (modifications only): |
---|
15 | * Copyright: 2002, Stanford University and |
---|
16 | * Till Straumann, <strauman@slac.stanford.edu> |
---|
17 | * Licensing: 'LICENSE.NET' file in the RTEMS top source directory |
---|
18 | * for more information. |
---|
19 | * |
---|
20 | * The RTEMS TCP/IP stack is a port of the FreeBSD TCP/IP stack. The following |
---|
21 | * copyright and licensing information applies to this code. |
---|
22 | * |
---|
23 | * This code is found under the c/src/libnetworking directory but does not |
---|
24 | * constitute the entire contents of that subdirectory. |
---|
25 | * |
---|
26 | * |
---|
27 | * Copyright (c) 1980, 1983, 1988, 1993 |
---|
28 | * The Regents of the University of California. All rights reserved. |
---|
29 | * |
---|
30 | * Redistribution and use in source and binary forms, with or without |
---|
31 | * modification, are permitted provided that the following conditions |
---|
32 | * are met: |
---|
33 | * 1. Redistributions of source code must retain the above copyright |
---|
34 | * notice, this list of conditions and the following disclaimer. |
---|
35 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
36 | * notice, this list of conditions and the following disclaimer in the |
---|
37 | * documentation and/or other materials provided with the distribution. |
---|
38 | * 3. All advertising materials mentioning features or use of this software |
---|
39 | * must display the following acknowledgment: |
---|
40 | * This product includes software developed by the University of |
---|
41 | * California, Berkeley and its contributors. |
---|
42 | * 4. Neither the name of the University nor the names of its contributors |
---|
43 | * may be used to endorse or promote products derived from this software |
---|
44 | * without specific prior written permission. |
---|
45 | * |
---|
46 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
47 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
48 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
49 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
50 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
51 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
52 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
53 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
54 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
55 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
56 | * SUCH DAMAGE. |
---|
57 | |
---|
58 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
---|
59 | |
---|
60 | * Permission to use, copy, modify, and distribute this software for any |
---|
61 | * purpose with or without fee is hereby granted, provided that the above |
---|
62 | * copyright notice and this permission notice appear in all copies, and that |
---|
63 | * the name of Digital Equipment Corporation not be used in advertising or |
---|
64 | * publicity pertaining to distribution of the document or software without |
---|
65 | * specific, written prior permission. |
---|
66 | |
---|
67 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
---|
68 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES |
---|
69 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT |
---|
70 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
---|
71 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
---|
72 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
---|
73 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
---|
74 | * SOFTWARE. |
---|
75 | */ |
---|
76 | |
---|
77 | #if HAVE_CONFIG_H |
---|
78 | #include "config.h" |
---|
79 | #endif |
---|
80 | |
---|
81 | #include <string.h> |
---|
82 | #include <stdarg.h> |
---|
83 | #include <stdio.h> |
---|
84 | |
---|
85 | #include <rtems.h> |
---|
86 | #include <rtems/libio.h> |
---|
87 | #include <rtems/error.h> |
---|
88 | |
---|
89 | #define _KERNEL |
---|
90 | #define __BSD_VISIBLE 1 |
---|
91 | #include <rtems/rtems_bsdnet.h> |
---|
92 | |
---|
93 | #include <sys/errno.h> |
---|
94 | #include <sys/types.h> |
---|
95 | #include <sys/param.h> |
---|
96 | #include <sys/mbuf.h> |
---|
97 | #include <sys/socket.h> |
---|
98 | #include <sys/socketvar.h> |
---|
99 | #include <sys/protosw.h> |
---|
100 | #include <sys/proc.h> |
---|
101 | #include <sys/fcntl.h> |
---|
102 | #include <sys/filio.h> |
---|
103 | |
---|
104 | #include <net/if.h> |
---|
105 | #include <net/route.h> |
---|
106 | |
---|
107 | struct socket *rtems_bsdnet_fdToSocket(int fd); |
---|
108 | |
---|
109 | /* |
---|
110 | * Package system call argument into mbuf. |
---|
111 | * |
---|
112 | * (unfortunately, the original is not public) |
---|
113 | */ |
---|
114 | static int |
---|
115 | sockaddrtombuf (struct mbuf **mp, const struct sockaddr *buf, int buflen) |
---|
116 | { |
---|
117 | struct mbuf *m; |
---|
118 | struct sockaddr *sa; |
---|
119 | |
---|
120 | if ((u_int)buflen > MLEN) |
---|
121 | return (EINVAL); |
---|
122 | |
---|
123 | rtems_bsdnet_semaphore_obtain(); |
---|
124 | m = m_get(M_WAIT, MT_SONAME); |
---|
125 | rtems_bsdnet_semaphore_release(); |
---|
126 | |
---|
127 | if (m == NULL) |
---|
128 | return (ENOBUFS); |
---|
129 | m->m_len = buflen; |
---|
130 | memcpy (mtod(m, caddr_t), buf, buflen); |
---|
131 | *mp = m; |
---|
132 | sa = mtod(m, struct sockaddr *); |
---|
133 | sa->sa_len = buflen; |
---|
134 | |
---|
135 | return 0; |
---|
136 | } |
---|
137 | |
---|
138 | static void |
---|
139 | dummyproc(caddr_t ext_buf, u_int ext_size) |
---|
140 | { |
---|
141 | } |
---|
142 | |
---|
143 | /* |
---|
144 | * send data by simply allocating an MBUF packet |
---|
145 | * header and pointing it to our data region. |
---|
146 | * |
---|
147 | * Optionally, the caller may supply 'reference' |
---|
148 | * and 'free' procs. (The latter may call the |
---|
149 | * user back once the networking stack has |
---|
150 | * released the buffer). |
---|
151 | * |
---|
152 | * The callbacks are provided with the 'closure' |
---|
153 | * pointer and the 'buflen' argument. |
---|
154 | */ |
---|
155 | ssize_t |
---|
156 | sendto_nocpy ( |
---|
157 | int s, |
---|
158 | const void *buf, size_t buflen, |
---|
159 | int flags, |
---|
160 | const struct sockaddr *toaddr, int tolen, |
---|
161 | void *closure, |
---|
162 | void (*freeproc)(caddr_t, u_int), |
---|
163 | void (*refproc)(caddr_t, u_int) |
---|
164 | ) |
---|
165 | { |
---|
166 | int error; |
---|
167 | struct socket *so; |
---|
168 | struct mbuf *to, *m; |
---|
169 | int ret = -1; |
---|
170 | |
---|
171 | rtems_bsdnet_semaphore_obtain (); |
---|
172 | if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) { |
---|
173 | rtems_bsdnet_semaphore_release (); |
---|
174 | return -1; |
---|
175 | } |
---|
176 | |
---|
177 | error = sockaddrtombuf (&to, toaddr, tolen); |
---|
178 | if (error) { |
---|
179 | errno = error; |
---|
180 | rtems_bsdnet_semaphore_release (); |
---|
181 | return -1; |
---|
182 | } |
---|
183 | |
---|
184 | MGETHDR(m, M_WAIT, MT_DATA); |
---|
185 | m->m_pkthdr.len = 0; |
---|
186 | m->m_pkthdr.rcvif = (struct ifnet *) 0; |
---|
187 | |
---|
188 | m->m_flags |= M_EXT; |
---|
189 | m->m_ext.ext_buf = closure ? closure : (void*)buf; |
---|
190 | m->m_ext.ext_size = buflen; |
---|
191 | /* we _must_ supply non-null procs; otherwise, |
---|
192 | * the kernel code assumes it's a mbuf cluster |
---|
193 | */ |
---|
194 | m->m_ext.ext_free = freeproc ? freeproc : dummyproc; |
---|
195 | m->m_ext.ext_ref = refproc ? refproc : dummyproc; |
---|
196 | m->m_pkthdr.len += buflen; |
---|
197 | m->m_len = buflen; |
---|
198 | m->m_data = (void*)buf; |
---|
199 | |
---|
200 | error = sosend (so, to, NULL, m, NULL, flags); |
---|
201 | if (error) { |
---|
202 | if (/*auio.uio_resid != len &&*/ (error == EINTR || error == EWOULDBLOCK)) |
---|
203 | error = 0; |
---|
204 | } |
---|
205 | if (error) |
---|
206 | errno = error; |
---|
207 | else |
---|
208 | ret = buflen; |
---|
209 | if (to) |
---|
210 | m_freem(to); |
---|
211 | rtems_bsdnet_semaphore_release (); |
---|
212 | return (ret); |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | /* |
---|
217 | * receive data in an 'mbuf chain'. |
---|
218 | * The chain must be released once the |
---|
219 | * data has been extracted: |
---|
220 | * |
---|
221 | * rtems_bsdnet_semaphore_obtain(); |
---|
222 | * m_freem(chain); |
---|
223 | * rtems_bsdnet_semaphore_release(); |
---|
224 | */ |
---|
225 | ssize_t |
---|
226 | recv_mbuf_from(int s, struct mbuf **ppm, long len, struct sockaddr *fromaddr, int *fromlen) |
---|
227 | { |
---|
228 | int ret = -1; |
---|
229 | int error; |
---|
230 | struct uio auio; |
---|
231 | struct socket *so; |
---|
232 | struct mbuf *from = NULL; |
---|
233 | |
---|
234 | memset(&auio, 0, sizeof(auio)); |
---|
235 | *ppm = 0; |
---|
236 | |
---|
237 | rtems_bsdnet_semaphore_obtain (); |
---|
238 | if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) { |
---|
239 | rtems_bsdnet_semaphore_release (); |
---|
240 | return -1; |
---|
241 | } |
---|
242 | /* auio.uio_iov = mp->msg_iov; |
---|
243 | auio.uio_iovcnt = mp->msg_iovlen; |
---|
244 | auio.uio_segflg = UIO_USERSPACE; |
---|
245 | auio.uio_rw = UIO_READ; |
---|
246 | auio.uio_offset = 0; |
---|
247 | */ |
---|
248 | auio.uio_resid = len; |
---|
249 | error = soreceive (so, &from, &auio, (struct mbuf **) ppm, |
---|
250 | (struct mbuf **)NULL, |
---|
251 | NULL); |
---|
252 | if (error) { |
---|
253 | if (auio.uio_resid != len && (error == EINTR || error == EWOULDBLOCK)) |
---|
254 | error = 0; |
---|
255 | } |
---|
256 | if (error) { |
---|
257 | errno = error; |
---|
258 | } |
---|
259 | else { |
---|
260 | ret = len - auio.uio_resid; |
---|
261 | if (fromaddr) { |
---|
262 | len = *fromlen; |
---|
263 | if ((len <= 0) || (from == NULL)) { |
---|
264 | len = 0; |
---|
265 | } |
---|
266 | else { |
---|
267 | if (len > from->m_len) |
---|
268 | len = from->m_len; |
---|
269 | memcpy (fromaddr, mtod(from, caddr_t), len); |
---|
270 | } |
---|
271 | *fromlen = len; |
---|
272 | } |
---|
273 | } |
---|
274 | if (from) |
---|
275 | m_freem (from); |
---|
276 | if (error && *ppm) { |
---|
277 | m_freem(*ppm); |
---|
278 | *ppm = 0; |
---|
279 | } |
---|
280 | rtems_bsdnet_semaphore_release (); |
---|
281 | return (ret); |
---|
282 | } |
---|