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

4.104.114.95
Last change on this file since 58d38a0 was 58d38a0, checked in by Joel Sherrill <joel.sherrill@…>, on 02/26/08 at 19:23:53

2008-02-26 Joel Sherrill <joel.sherrill@…>

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