source: rtems/cpukit/libfs/src/nfsclient/src/sock_mbuf.c @ fb664a2

4.11
Last change on this file since fb664a2 was fb664a2, checked in by Sebastian Huber <sebastian.huber@…>, on Dec 16, 2013 at 9:55:27 AM

nfsclient: Move defines to cover all header files

  • Property mode set to 100644
File size: 7.7 KB
Line 
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#define _KERNEL
82#define __BSD_VISIBLE   1
83
84#include <string.h>
85#include <stdarg.h>
86#include <stdio.h>
87
88#include <rtems.h>
89#include <rtems/libio.h>
90#include <rtems/error.h>
91
92#include <rtems/rtems_bsdnet.h>
93
94#include <sys/errno.h>
95#include <sys/types.h>
96#include <sys/param.h>
97#include <sys/mbuf.h>
98#include <sys/socket.h>
99#include <sys/socketvar.h>
100#include <sys/protosw.h>
101#include <sys/proc.h>
102#include <sys/fcntl.h>
103#include <sys/filio.h>
104
105#include <net/if.h>
106#include <net/route.h>
107
108struct socket *rtems_bsdnet_fdToSocket(int fd);
109
110/*
111 * Package system call argument into mbuf.
112 *
113 * (unfortunately, the original is not public)
114 */
115static int
116sockaddrtombuf (struct mbuf **mp, const struct sockaddr *buf, int buflen)
117{
118struct mbuf *m;
119struct sockaddr *sa;
120
121        if ((u_int)buflen > MLEN)
122                return (EINVAL);
123
124        rtems_bsdnet_semaphore_obtain();
125        m = m_get(M_WAIT, MT_SONAME);
126        rtems_bsdnet_semaphore_release();
127
128        if (m == NULL)
129                return (ENOBUFS);
130        m->m_len = buflen;
131        memcpy (mtod(m, caddr_t), buf, buflen);
132        *mp = m;
133        sa = mtod(m, struct sockaddr *);
134        sa->sa_len = buflen;
135
136        return 0;
137}
138
139static void
140dummyproc(caddr_t ext_buf, u_int ext_size)
141{
142}
143
144/*
145 * send data by simply allocating an MBUF packet
146 * header and pointing it to our data region.
147 *
148 * Optionally, the caller may supply 'reference'
149 * and 'free' procs. (The latter may call the
150 * user back once the networking stack has
151 * released the buffer).
152 *
153 * The callbacks are provided with the 'closure'
154 * pointer and the 'buflen' argument.
155 */
156ssize_t
157sendto_nocpy (
158                int s,
159                const void *buf, size_t buflen,
160                int flags,
161                const struct sockaddr *toaddr, int tolen,
162                void *closure,
163                void (*freeproc)(caddr_t, u_int),
164                void (*refproc)(caddr_t, u_int)
165)
166{
167        int           error;
168        struct socket *so;
169        struct mbuf   *to, *m;
170        int           ret = -1;
171
172        rtems_bsdnet_semaphore_obtain ();
173        if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
174                rtems_bsdnet_semaphore_release ();
175                return -1;
176        }
177
178        error = sockaddrtombuf (&to, toaddr, tolen);
179        if (error) {
180                errno = error;
181                rtems_bsdnet_semaphore_release ();
182                return -1;
183        }
184
185        MGETHDR(m, M_WAIT, MT_DATA);
186        m->m_pkthdr.len   = 0;
187        m->m_pkthdr.rcvif =  (struct ifnet *) 0;
188
189        m->m_flags       |= M_EXT;
190        m->m_ext.ext_buf  = closure ? closure : (void*)buf;
191        m->m_ext.ext_size = buflen;
192        /* we _must_ supply non-null procs; otherwise,
193         * the kernel code assumes it's a mbuf cluster
194         */
195        m->m_ext.ext_free = freeproc ? freeproc : dummyproc;
196        m->m_ext.ext_ref  = refproc  ? refproc  : dummyproc;
197        m->m_pkthdr.len  += buflen;
198        m->m_len          = buflen;
199        m->m_data                 = (void*)buf;
200
201        error = sosend (so, to, NULL, m, NULL, flags);
202        if (error) {
203                if (/*auio.uio_resid != len &&*/ (error == EINTR || error == EWOULDBLOCK))
204                        error = 0;
205        }
206        if (error)
207                errno = error;
208        else
209                ret = buflen;
210        if (to)
211                m_freem(to);
212        rtems_bsdnet_semaphore_release ();
213        return (ret);
214}
215
216
217/*
218 * receive data in an 'mbuf chain'.
219 * The chain must be released once the
220 * data has been extracted:
221 *
222 *   rtems_bsdnet_semaphore_obtain();
223 *      m_freem(chain);
224 *   rtems_bsdnet_semaphore_release();
225 */
226ssize_t
227recv_mbuf_from(int s, struct mbuf **ppm, long len, struct sockaddr *fromaddr, int *fromlen)
228{
229        int ret = -1;
230        int error;
231        struct uio auio;
232        struct socket *so;
233        struct mbuf *from = NULL;
234
235        memset(&auio, 0, sizeof(auio));
236        *ppm = 0;
237
238        rtems_bsdnet_semaphore_obtain ();
239        if ((so = rtems_bsdnet_fdToSocket (s)) == NULL) {
240                rtems_bsdnet_semaphore_release ();
241                return -1;
242        }
243/*      auio.uio_iov = mp->msg_iov;
244        auio.uio_iovcnt = mp->msg_iovlen;
245        auio.uio_segflg = UIO_USERSPACE;
246        auio.uio_rw = UIO_READ;
247        auio.uio_offset = 0;
248*/
249        auio.uio_resid = len;
250        error = soreceive (so, &from, &auio, (struct mbuf **) ppm,
251                        (struct mbuf **)NULL,
252                        NULL);
253        if (error) {
254                if (auio.uio_resid != len && (error == EINTR || error == EWOULDBLOCK))
255                        error = 0;
256        }
257        if (error) {
258                errno = error;
259        }
260        else {
261                ret = len - auio.uio_resid;
262                if (fromaddr) {
263                        len = *fromlen;
264                        if ((len <= 0) || (from == NULL)) {
265                                len = 0;
266                        }
267                        else {
268                                if (len > from->m_len)
269                                        len = from->m_len;
270                                memcpy (fromaddr, mtod(from, caddr_t), len);
271                        }
272                        *fromlen = len;
273                }
274        }
275        if (from)
276                m_freem (from);
277        if (error && *ppm) {
278                m_freem(*ppm);
279                *ppm = 0;
280        }
281        rtems_bsdnet_semaphore_release ();
282        return (ret);
283}
Note: See TracBrowser for help on using the repository browser.