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

4.115
Last change on this file since 36f3207 was 9b4422a2, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 15:09:24

Remove All CVS Id Strings Possible Using a Script

Script does what is expected and tries to do it as
smartly as possible.

+ remove occurrences of two blank comment lines

next to each other after Id string line removed.

+ remove entire comment blocks which only exited to

contain CVS Ids

+ If the processing left a blank line at the top of

a file, it was removed.

  • Property mode set to 100644
File size: 7.7 KB
Line 
1/*
2 *  NOTE:
3 *    This is derived from libnetworking/rtems/rtems_syscall.c
4 *
5 *    RTEMS/libnetworking LICENSING restrictions may apply
6 *
7 *    Author (modifications only):
8 *    Copyright: 2002, Stanford University and
9 *               Till Straumann, <strauman@slac.stanford.edu>
10 *    Licensing: 'LICENSE.NET' file in the RTEMS top source directory
11 *               for more information.
12 */
13
14/*
15The RTEMS TCP/IP stack is a port of the FreeBSD TCP/IP stack.  The following
16copyright and licensing information applies to this code.
17
18This code is found under the c/src/libnetworking directory but does not
19constitute the entire contents of that subdirectory.
20
21=============================================================================
22
23Copyright (c) 1980, 1983, 1988, 1993
24        The Regents of the University of California.  All rights reserved.
25
26Redistribution and use in source and binary forms, with or without
27modification, are permitted provided that the following conditions
28are met:
291. Redistributions of source code must retain the above copyright
30   notice, this list of conditions and the following disclaimer.
312. Redistributions in binary form must reproduce the above copyright
32   notice, this list of conditions and the following disclaimer in the
33   documentation and/or other materials provided with the distribution.
343. All advertising materials mentioning features or use of this software
35   must display the following acknowledgment:
36        This product includes software developed by the University of
37        California, Berkeley and its contributors.
384. Neither the name of the University nor the names of its contributors
39   may be used to endorse or promote products derived from this software
40   without specific prior written permission.
41
42THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52SUCH DAMAGE.
53
54-
55Portions Copyright (c) 1993 by Digital Equipment Corporation.
56
57Permission to use, copy, modify, and distribute this software for any
58purpose with or without fee is hereby granted, provided that the above
59copyright notice and this permission notice appear in all copies, and that
60the name of Digital Equipment Corporation not be used in advertising or
61publicity pertaining to distribution of the document or software without
62specific, written prior permission.
63
64THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
65WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
66OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
67CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
68DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
69PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
70ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
71SOFTWARE.
72
73=============================================================================
74*/
75
76#if HAVE_CONFIG_H
77#include "config.h"
78#endif
79
80#include <string.h>
81#include <stdarg.h>
82#include <stdio.h>
83
84#include <rtems.h>
85#include <rtems/libio.h>
86#include <rtems/error.h>
87
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.