• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * include/nuttx/net/net.h
3  *
4  *   Copyright (C) 2007, 2009-2014, 2016-2018 Gregory Nutt. All rights
5  *     reserved.
6  *   Author: Gregory Nutt <gnutt@nuttx.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name NuttX nor the names of its contributors may be
19  *    used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  ****************************************************************************/
36 
37 #ifndef __INCLUDE_NUTTX_NET_NET_H
38 #define __INCLUDE_NUTTX_NET_NET_H
39 
40 /****************************************************************************
41  * Included Files
42  ****************************************************************************/
43 
44 #include "vfs_config.h"
45 #ifdef LOSCFG_NET_LWIP_SACK
46 
47 #include <sys/socket.h>
48 #include <stdint.h>
49 
50 #include <stdarg.h>
51 #include <semaphore.h>
52 #include "lwip/sockets.h"
53 
54 /****************************************************************************
55  * Pre-processor Definitions
56  ****************************************************************************/
57 
58 /* Most internal network OS interfaces are not available in the user space in
59  * PROTECTED and KERNEL builds.  In that context, the corresponding
60  * application network interfaces must be used.  The differences between the
61  * two sets of interfaces are:  The internal OS interfaces (1) do not cause
62  * cancellation points and (2) they do not modify the errno variable.
63  *
64  * This is only important when compiling libraries (libc or libnx) that are
65  * used both by the OS (libkc.a and libknx.a) or by the applications
66  * (libuc.a and libunx.a).  The that case, the correct interface must be
67  * used for the build context.
68  *
69  * REVISIT:  In the flat build, the same functions must be used both by
70  * the OS and by applications.  We have to use the normal user functions
71  * in this case or we will fail to set the errno or fail to create the
72  * cancellation point.
73  *
74  * The interfaces accept(), read(), recv(), recvfrom(), write(), send(),
75  * sendto() are all cancellation points.
76  *
77  * REVISIT:  These cancellation points are an issue and may cause
78  * violations:  It use of these internally will cause the calling function
79  * to become a cancellation points!
80  */
81 
82 #if !defined(CONFIG_BUILD_FLAT) && defined(__KERNEL__)
83 #  define _NX_SEND(s,b,l,f)         nx_send(s,b,l,f)
84 #  define _NX_RECV(s,b,l,f)         nx_recv(s,b,l,f)
85 #  define _NX_RECVFROM(s,b,l,f,a,n) nx_recvfrom(s,b,l,f,a,n)
86 #  define _NX_GETERRNO(r)           (-(r))
87 #  define _NX_GETERRVAL(r)          (r)
88 #else
89 #  define _NX_SEND(s,b,l,f)         send(s,b,l,f)
90 #  define _NX_RECV(s,b,l,f)         recv(s,b,l,f)
91 #  define _NX_RECVFROM(s,b,l,f,a,n) recvfrom(s,b,l,f,a,n)
92 #  define _NX_GETERRNO(r)           errno
93 #  define _NX_GETERRVAL(r)          (-errno)
94 #endif
95 
96 /* Socket descriptors are the index into the TCB sockets list, offset by the
97  * following amount. This offset is used to distinguish file descriptors from
98  * socket descriptors
99  */
100 
101 #define __SOCKFD_OFFSET CONFIG_NFILE_DESCRIPTORS
102 
103 /* Capabilities of a socket */
104 
105 #define SOCKCAP_NONBLOCKING (1 << 0)  /* Bit 0: Socket supports non-blocking
106                                        *        operation. */
107 
108 /****************************************************************************
109  * Public Types
110  ****************************************************************************/
111 
112 /* Link layer type.  This type is used with netdev_register in order to
113  * identify the type of the network driver.
114  */
115 
116 enum net_lltype_e
117 {
118   NET_LL_ETHERNET = 0, /* Ethernet */
119   NET_LL_SLIP,         /* Serial Line Internet Protocol (SLIP) */
120   NET_LL_PPP           /* Point-to-Point Protocol (PPP) */
121 };
122 
123 /* This defines a bitmap big enough for one bit for each socket option */
124 
125 typedef uint16_t sockopt_t;
126 
127 /* This defines the storage size of a timeout value.  This effects only
128  * range of supported timeout values.  With an LSB in seciseconds, the
129  * 16-bit maximum of 65535 corresponds to 1 hr 49 min 13.5 sec at decisecond
130  * resolution.
131  */
132 
133 typedef uint16_t socktimeo_t;
134 
135 /* This type defines the type of the socket capabilities set */
136 
137 typedef uint8_t sockcaps_t;
138 
139 /* This callbacks are socket operations that may be performed on a socket of
140  * a given address family.
141  */
142 
143 struct file;    /* Forward reference */
144 struct socket;  /* Forward reference */
145 struct pollfd;  /* Forward reference */
146 
147 struct sock_intf_s
148 {
149   CODE int        (*si_setup)(FAR struct socket *psock, int protocol);
150   CODE sockcaps_t (*si_sockcaps)(FAR struct socket *psock);
151   CODE void       (*si_addref)(FAR struct socket *psock);
152   CODE int        (*si_bind)(FAR struct socket *psock,
153                     FAR const struct sockaddr *addr, socklen_t addrlen);
154   CODE int        (*si_getsockname)(FAR struct socket *psock,
155                     FAR struct sockaddr *addr, FAR socklen_t *addrlen);
156   CODE int        (*si_getpeername)(FAR struct socket *psock,
157                     FAR struct sockaddr *addr, FAR socklen_t *addrlen);
158   CODE int        (*si_listen)(FAR struct socket *psock, int backlog);
159   CODE int        (*si_connect)(FAR struct socket *psock,
160                     FAR const struct sockaddr *addr, socklen_t addrlen);
161   CODE int        (*si_accept)(FAR struct socket *psock,
162                     FAR struct sockaddr *addr, FAR socklen_t *addrlen,
163                     FAR struct socket *newsock);
164   CODE int        (*si_poll)(FAR struct socket *psock,
165                     FAR struct pollfd *fds, bool setup);
166   CODE ssize_t    (*si_send)(FAR struct socket *psock, FAR const void *buf,
167                     size_t len, int flags);
168   CODE ssize_t    (*si_sendto)(FAR struct socket *psock, FAR const void *buf,
169                     size_t len, int flags, FAR const struct sockaddr *to,
170                     socklen_t tolen);
171 #ifdef CONFIG_NET_SENDFILE
172   CODE ssize_t    (*si_sendfile)(FAR struct socket *psock,
173                     FAR struct file *infile, FAR off_t *offset,
174                     size_t count);
175 #endif
176   CODE ssize_t    (*si_recvfrom)(FAR struct socket *psock, FAR void *buf,
177                     size_t len, int flags, FAR struct sockaddr *from,
178                     FAR socklen_t *fromlen);
179   CODE int        (*si_close)(FAR struct socket *psock);
180 #ifdef CONFIG_NET_USRSOCK
181   CODE int        (*si_ioctl)(FAR struct socket *psock, int cmd,
182                     FAR void *arg, size_t arglen);
183 #endif
184 };
185 
186 /* Each socket refers to a connection structure of type FAR void *.  Each
187  * socket type will have a different connection structure type bound to its
188  * sockets.  The fields at the the beginning of each connection type must
189  * begin the same content prologue as struct socket_conn_s and must be cast
190  * compatible with struct socket_conn_s.  Connection-specific content may
191  * then follow the common prologue fields.
192  */
193 
194 struct devif_callback_s;  /* Forward reference */
195 
196 /* This is the internal representation of a socket reference by a file
197  * descriptor.
198  */
199 
200 struct devif_callback_s;  /* Forward reference */
201 
202 struct socket
203 {
204   int16_t       s_crefs;     /* Reference count on the socket */
205   uint8_t       s_domain;    /* IP domain: PF_INET, PF_INET6, or PF_PACKET */
206   uint8_t       s_type;      /* Protocol type: Only SOCK_STREAM or
207                               * SOCK_DGRAM */
208   uint8_t       s_flags;     /* See _SF_* definitions */
209 
210   /* Socket options */
211 
212 #ifdef CONFIG_NET_SOCKOPTS
213   sockopt_t     s_options;   /* Selected socket options */
214   socktimeo_t   s_rcvtimeo;  /* Receive timeout value (in deciseconds) */
215   socktimeo_t   s_sndtimeo;  /* Send timeout value (in deciseconds) */
216 #ifdef CONFIG_NET_SOLINGER
217   socktimeo_t   s_linger;    /* Linger timeout value (in deciseconds) */
218 #endif
219 #endif
220 
221   void     *s_conn;      /* Connection inherits from struct socket_conn_s */
222 
223   /* Socket interface */
224 
225   FAR const struct sock_intf_s *s_sockif;
226 
227 #if defined(CONFIG_NET_TCP_WRITE_BUFFERS) || \
228     defined(CONFIG_NET_UDP_WRITE_BUFFERS)
229   /* Callback instance for TCP send() or UDP sendto() */
230 
231   struct devif_callback_s *s_sndcb;
232 #endif
233 };
234 
235 /* Callback from netdev_foreach() */
236 
237 struct net_driver_s; /* Forward reference. Defined in net/netdev.h */
238 typedef int (*netdev_callback_t)(struct net_driver_s *dev, void *arg);
239 
240 #ifdef CONFIG_NET_NOINTS
241 
242 /* Semaphore based locking for non-interrupt based logic.
243  *
244  * net_lock_t -- Not used.  Only for compatibility
245  */
246 
247 typedef uint8_t net_lock_t; /* Not really used */
248 
249 #else
250 
251 /* Enable/disable locking for interrupt based logic:
252  *
253  * net_lock_t -- The processor specific representation of interrupt state.
254  */
255 
256 #  define net_lock_t        irqstate_t
257 #endif
258 
259 /****************************************************************************
260  * Public Data
261  ****************************************************************************/
262 
263 #ifdef __cplusplus
264 #if __cplusplus
265 extern "C" {
266 #endif /* __cplusplus */
267 #endif /* __cplusplus */
268 
269 /****************************************************************************
270  * Public Function Prototypes
271  ****************************************************************************/
272 
273 /****************************************************************************
274  * Name: net_initialize
275  *
276  * Description:
277  *   This is called from the OS initialization logic at power-up reset in
278  *   order to configure networking data structures.  This is called prior
279  *   to platform-specific driver initialization so that the networking
280  *   subsystem is prepared to deal with network driver initialization
281  *   actions.
282  *
283  *   Actions performed in this initialization phase assume that base OS
284  *   facilities such as semaphores are available but this logic cannot
285  *   depend upon OS resources such as interrupts or timers which are not
286  *   yet available.
287  *
288  * Input Parameters:
289  *   None
290  *
291  * Returned Value:
292  *   None
293  *
294  ****************************************************************************/
295 
296 void net_initialize(void);
297 
298 /****************************************************************************
299  * Critical section management.
300  *
301  * Re-entrant mutex based locking of the network is supported:
302  *
303  *   net_lock()        - Locks the network via a re-entrant mutex.
304  *   net_unlock()      - Unlocks the network.
305  *   net_lockedwait()  - Like pthread_cond_wait() except releases the
306  *                       network momentarily to wait on another semaphore.
307  *   net_ioballoc()    - Like iob_alloc() except releases the network
308  *                       momentarily to wait for an IOB to become
309  *                       available.
310  *
311  ****************************************************************************/
312 
313 /****************************************************************************
314  * Name: net_lock
315  *
316  * Description:
317  *   Take the network lock
318  *
319  * Input Parameters:
320  *   None
321  *
322  * Returned Value:
323  *   Zero (OK) is returned on success; a negated errno value is returned on
324  *   failured (probably -ECANCELED).
325  *
326  ****************************************************************************/
327 
328 int net_lock(void);
329 
330 /****************************************************************************
331  * Name: net_unlock
332  *
333  * Description:
334  *   Release the network lock.
335  *
336  * Input Parameters:
337  *   None
338  *
339  * Returned Value:
340  *   None
341  *
342  ****************************************************************************/
343 
344 void net_unlock(void);
345 
346 /****************************************************************************
347  * Name: net_timedwait
348  *
349  * Description:
350  *   Atomically wait for sem (or a timeout( while temporarily releasing
351  *   the lock on the network.
352  *
353  *   Caution should be utilized.  Because the network lock is relinquished
354  *   during the wait, there could changes in the network state that occur
355  *   before the lock is recovered.  Your design should account for this
356  *   possibility.
357  *
358  * Input Parameters:
359  *   sem     - A reference to the semaphore to be taken.
360  *   abstime - The absolute time to wait until a timeout is declared.
361  *
362  * Returned Value:
363  *   Zero (OK) is returned on success; a negated errno value is returned on
364  *   any failure.
365  *
366  ****************************************************************************/
367 
368 struct timespec;
369 int net_timedwait(sem_t *sem, FAR const struct timespec *abstime);
370 
371 /****************************************************************************
372  * Name: net_lockedwait
373  *
374  * Description:
375  *   Atomically wait for sem while temporarily releasing the network lock.
376  *
377  *   Caution should be utilized.  Because the network lock is relinquished
378  *   during the wait, there could changes in the network state that occur
379  *   before the lock is recovered.  Your design should account for this
380  *   possibility.
381  *
382  * Input Parameters:
383  *   sem - A reference to the semaphore to be taken.
384  *
385  * Returned Value:
386  *   Zero (OK) is returned on success; a negated errno value is returned on
387  *   any failure.
388  *
389  ****************************************************************************/
390 
391 int net_lockedwait(sem_t *sem);
392 
393 /****************************************************************************
394  * Name: net_ioballoc
395  *
396  * Description:
397  *   Allocate an IOB.  If no IOBs are available, then atomically wait for
398  *   for the IOB while temporarily releasing the lock on the network.
399  *
400  *   Caution should be utilized.  Because the network lock is relinquished
401  *   during the wait, there could changes in the network state that occur
402  *   before the lock is recovered.  Your design should account for this
403  *   possibility.
404  *
405  * Input Parameters:
406  *   throttled - An indication of the IOB allocation is "throttled"
407  *
408  * Returned Value:
409  *   A pointer to the newly allocated IOB is returned on success.  NULL is
410  *   returned on any allocation failure.
411  *
412  ****************************************************************************/
413 
414 #ifdef CONFIG_MM_IOB
415 FAR struct iob_s *net_ioballoc(bool throttled, enum iob_user_e consumerid);
416 #endif
417 
418 /****************************************************************************
419  * Name: net_setipid
420  *
421  * Description:
422  *   This function may be used at boot time to set the initial ip_id.
423  *
424  * Assumptions:
425  *
426  ****************************************************************************/
427 
428 void net_setipid(uint16_t id);
429 
430 /****************************************************************************
431  * Name: net_checksd
432  *
433  * Description:
434  *   Check if the socket descriptor is valid for the provided TCB and if it
435  *   supports the requested access.  This trivial operation is part of the
436  *   fdopen() operation when the fdopen() is performed on a socket descriptor.
437  *   It simply performs some sanity checking before permitting the socket
438  *   descriptor to be wrapped as a C FILE stream.
439  *
440  ****************************************************************************/
441 
442 int net_checksd(int fd, int oflags);
443 
444 
445 
446 
447 /****************************************************************************
448  * Name: sockfd_socket
449  *
450  * Description:
451  *   Given a socket descriptor, return the underlying socket structure.
452  *
453  * Input Parameters:
454  *   sockfd - The socket descriptor index o use.
455  *
456  * Returned Value:
457  *   On success, a reference to the socket structure associated with the
458  *   the socket descriptor is returned.  NULL is returned on any failure.
459  *
460  ****************************************************************************/
461 
462 struct socket *sockfd_socket(int sockfd);
463 
464 /****************************************************************************
465  * Name: psock_socket
466  *
467  * Description:
468  *   socket() creates an endpoint for communication and returns a socket
469  *   structure.
470  *
471  * Input Parameters:
472  *   domain   (see sys/socket.h)
473  *   type     (see sys/socket.h)
474  *   protocol (see sys/socket.h)
475  *   psock    A pointer to a user allocated socket structure to be
476  *            initialized.
477  *
478  * Returned Value:
479  *  Returns zero (OK) on success.  On failure, it returns a negated errno
480  *  value to indicate the nature of the error:
481  *
482  *   EACCES
483  *     Permission to create a socket of the specified type and/or protocol
484  *     is denied.
485  *   EAFNOSUPPORT
486  *     The implementation does not support the specified address family.
487  *   EINVAL
488  *     Unknown protocol, or protocol family not available.
489  *   EMFILE
490  *     Process file table overflow.
491  *   ENFILE
492  *     The system limit on the total number of open files has been reached.
493  *   ENOBUFS or ENOMEM
494  *     Insufficient memory is available. The socket cannot be created until
495  *     sufficient resources are freed.
496  *   EPROTONOSUPPORT
497  *     The protocol type or the specified protocol is not supported within
498  *     this domain.
499  *
500  * Assumptions:
501  *
502  ****************************************************************************/
503 
504 int psock_socket(int domain, int type, int protocol,
505                  struct socket *psock);
506 
507 /****************************************************************************
508  * Name: net_close
509  *
510  * Description:
511  *   Performs the close operation on socket descriptors
512  *
513  * Input Parameters:
514  *   sockfd   Socket descriptor of socket
515  *
516  * Returned Value:
517  *  Returns zero (OK) on success.  On failure, it returns a negated errno
518  *  value to indicate the nature of the error.
519  *
520  * Assumptions:
521  *
522  ****************************************************************************/
523 
524 int net_close(int sockfd);
525 
526 /****************************************************************************
527  * Name: psock_close
528  *
529  * Description:
530  *   Performs the close operation on a socket instance
531  *
532  * Input Parameters:
533  *   psock   Socket instance
534  *
535  * Returned Value:
536  *  Returns zero (OK) on success.  On failure, it returns a negated errno
537  *  value to indicate the nature of the error.
538  *
539  ****************************************************************************/
540 
541 int psock_close(struct socket *psock);
542 
543 /****************************************************************************
544  * Name: psock_bind
545  *
546  * Description:
547  *   bind() gives the socket 'psock' the local address 'addr'. 'addr' is
548  *   'addrlen' bytes long. Traditionally, this is called "assigning a name to
549  *   a socket." When a socket is created with socket, it exists in a name
550  *   space (address family) but has no name assigned.
551  *
552  * Input Parameters:
553  *   psock    Socket structure of the socket to bind
554  *   addr     Socket local address
555  *   addrlen  Length of 'addr'
556  *
557  * Returned Value:
558  *  Returns zero (OK) on success.  On failure, it returns a negated errno
559  *  value to indicate the nature of the error.
560  *
561  *   EACCES
562  *     The address is protected, and the user is not the superuser.
563  *   EADDRINUSE
564  *     The given address is already in use.
565  *   EINVAL
566  *     The socket is already bound to an address.
567  *   ENOTSOCK
568  *     psock is a descriptor for a file, not a socket.
569  *
570  * Assumptions:
571  *
572  ****************************************************************************/
573 
574 struct sockaddr; /* Forward reference. See nuttx/include/sys/socket.h */
575 int psock_bind(struct socket *psock, const struct sockaddr *addr,
576                socklen_t addrlen);
577 
578 /****************************************************************************
579  * Name: psock_listen
580  *
581  * Description:
582  *   To accept connections, a socket is first created with psock_socket(), a
583  *   willingness to accept incoming connections and a queue limit for
584  *   incoming connections are specified with psock_listen(), and then the
585  *   connections are accepted with psock_accept(). The psock_listen() call
586  *   applies only to sockets of type SOCK_STREAM or SOCK_SEQPACKET.
587  *
588  * Input Parameters:
589  *   psock    Reference to an internal, boound socket structure.
590  *   backlog  The maximum length the queue of pending connections may grow.
591  *            If a connection request arrives with the queue full, the client
592  *            may receive an error with an indication of ECONNREFUSED or,
593  *            if the underlying protocol supports retransmission, the request
594  *            may be ignored so that retries succeed.
595  *
596  * Returned Value:
597  *  Returns zero (OK) on success.  On failure, it returns a negated errno
598  *  value to indicate the nature of the error.
599  *
600  *   EADDRINUSE
601  *     Another socket is already listening on the same port.
602  *   EOPNOTSUPP
603  *     The socket is not of a type that supports the listen operation.
604  *
605  ****************************************************************************/
606 
607 int psock_listen(FAR struct socket *psock, int backlog);
608 
609 /****************************************************************************
610  * Name: psock_accept
611  *
612  * Description:
613  *   The psock_accept function is used with connection-based socket types
614  *   (SOCK_STREAM, SOCK_SEQPACKET and SOCK_RDM). It extracts the first
615  *   connection request on the queue of pending connections, creates a new
616  *   connected socket with mostly the same properties as 'sockfd', and
617  *   allocates a new socket descriptor for the socket, which is returned. The
618  *   newly created socket is no longer in the listening state. The original
619  *   socket 'sockfd' is unaffected by this call.  Per file descriptor flags
620  *   are not inherited across an psock_accept.
621  *
622  *   The 'sockfd' argument is a socket descriptor that has been created with
623  *   socket(), bound to a local address with bind(), and is listening for
624  *   connections after a call to listen().
625  *
626  *   On return, the 'addr' structure is filled in with the address of the
627  *   connecting entity. The 'addrlen' argument initially contains the size
628  *   of the structure pointed to by 'addr'; on return it will contain the
629  *   actual length of the address returned.
630  *
631  *   If no pending connections are present on the queue, and the socket is
632  *   not marked as non-blocking, psock_accept blocks the caller until a
633  *   connection is present. If the socket is marked non-blocking and no
634  *   pending connections are present on the queue, psock_accept returns
635  *   EAGAIN.
636  *
637  * Input Parameters:
638  *   psock    Reference to the listening socket structure
639  *   addr     Receives the address of the connecting client
640  *   addrlen  Input: allocated size of 'addr', Return: returned size of 'addr'
641  *   newsock  Location to return the accepted socket information.
642  *
643  * Returned Value:
644  *  Returns zero (OK) on success.  On failure, it returns a negated errno
645  *  value to indicate the nature of the error.
646  *
647  * EAGAIN or EWOULDBLOCK
648  *   The socket is marked non-blocking and no connections are present to
649  *   be accepted.
650  * EOPNOTSUPP
651  *   The referenced socket is not of type SOCK_STREAM.
652  * EINTR
653  *   The system call was interrupted by a signal that was caught before
654  *   a valid connection arrived.
655  * ECONNABORTED
656  *   A connection has been aborted.
657  * EINVAL
658  *   Socket is not listening for connections.
659  * EMFILE
660  *   The per-process limit of open file descriptors has been reached.
661  * ENFILE
662  *   The system maximum for file descriptors has been reached.
663  * EFAULT
664  *   The addr parameter is not in a writable part of the user address
665  *   space.
666  * ENOBUFS or ENOMEM
667  *   Not enough free memory.
668  * EPROTO
669  *   Protocol error.
670  * EPERM
671  *   Firewall rules forbid connection.
672  *
673  ****************************************************************************/
674 
675 int psock_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
676                  FAR socklen_t *addrlen, FAR struct socket *newsock);
677 
678 /****************************************************************************
679  * Name: psock_connect
680  *
681  * Description:
682  *   connect() connects the socket referred to by the structure 'psock'
683  *   to the address specified by 'addr'. The addrlen argument specifies
684  *   the size of 'addr'.  The format of the address in 'addr' is
685  *   determined by the address space of the socket 'psock'.
686  *
687  *   If the socket 'psock' is of type SOCK_DGRAM then 'addr' is the address
688  *   to which datagrams are sent by default, and the only address from which
689  *   datagrams are received. If the socket is of type SOCK_STREAM or
690  *   SOCK_SEQPACKET, this call attempts to make a connection to the socket
691  *   that is bound to the address specified by 'addr'.
692  *
693  *   Generally, connection-based protocol sockets may successfully connect()
694  *   only once; connectionless protocol sockets may use connect() multiple
695  *   times to change their association.  Connectionless sockets may dissolve
696  *   the association by connecting to an address with the sa_family member of
697  *   sockaddr set to AF_UNSPEC.
698  *
699  * Input Parameters:
700  *   psock     Pointer to a socket structure initialized by psock_socket()
701  *   addr      Server address (form depends on type of socket)
702  *   addrlen   Length of actual 'addr'
703  *
704  * Returned Value:
705  *  Returns zero (OK) on success.  On failure, it returns a negated errno
706  *  value to indicate the nature of the error.
707  *
708  *     EACCES, EPERM
709  *       The user tried to connect to a broadcast address without having the
710  *       socket broadcast flag enabled or the connection request failed
711  *       because of a local firewall rule.
712  *     EADDRINUSE
713  *       Local address is already in use.
714  *     EAFNOSUPPORT
715  *       The passed address didn't have the correct address family in its
716  *       sa_family field.
717  *     EAGAIN
718  *       No more free local ports or insufficient entries in the routing
719  *       cache.
720  *     EALREADY
721  *       The socket is non-blocking and a previous connection attempt has
722  *       not yet been completed.
723  *     EBADF
724  *       The file descriptor is not a valid index in the descriptor table.
725  *     ECONNREFUSED
726  *       No one listening on the remote address.
727  *     EFAULT
728  *       The socket structure address is outside the user's address space.
729  *     EINPROGRESS
730  *       The socket is non-blocking and the connection cannot be completed
731  *       immediately.
732  *     EINTR
733  *       The system call was interrupted by a signal that was caught.
734  *     EISCONN
735  *       The socket is already connected.
736  *     ENETUNREACH
737  *       Network is unreachable.
738  *     ENOTSOCK
739  *       The file descriptor is not associated with a socket.
740  *     ETIMEDOUT
741  *       Timeout while attempting connection. The server may be too busy
742  *       to accept new connections.
743  *
744  * Assumptions:
745  *
746  ****************************************************************************/
747 
748 int psock_connect(struct socket *psock, const struct sockaddr *addr,
749                   socklen_t addrlen);
750 
751 /****************************************************************************
752  * Name: psock_send
753  *
754  * Description:
755  *   The psock_send() call may be used only when the socket is in a
756  *   connected state (so that the intended recipient is known).  This is an
757  *   internal OS interface.  It is functionally equivalent to send() except
758  *   that:
759  *
760  *   - It is not a cancellation point,
761  *   - It does not modify the errno variable, and
762  *   - I accepts the internal socket structure as an input rather than an
763  *     task-specific socket descriptor.
764  *
765  *   See comments with send() for more a more complete description of the
766  *   functionality.
767  *
768  * Input Parameters:
769  *   psock - An instance of the internal socket structure.
770  *   buf   - Data to send
771  *   len   - Length of data to send
772  *   flags - Send flags
773  *
774  * Returned Value:
775  *   On success, returns the number of characters sent.  On any failure, a
776  *   negated errno value is returned (See comments with send() for a list
777  *   of the appropriate errno value).
778  *
779  ****************************************************************************/
780 
781 ssize_t psock_send(FAR struct socket *psock, const void *buf, size_t len,
782                    int flags);
783 
784 /****************************************************************************
785  * Name: nx_send
786  *
787  * Description:
788  *   The nx_send() call may be used only when the socket is in a
789  *   connected state (so that the intended recipient is known).  This is an
790  *   internal OS interface.  It is functionally equivalent to send() except
791  *   that:
792  *
793  *   - It is not a cancellation point, and
794  *   - It does not modify the errno variable.
795  *
796  *   See comments with send() for more a more complete description of the
797  *   functionality.
798  *
799  * Input Parameters:
800  *   sockfd - Socket descriptor of the socket
801  *   buf    - Data to send
802  *   len    - Length of data to send
803  *   flags  - Send flags
804  *
805  * Returned Value:
806  *   On success, returns the number of characters sent.  On any failure, a
807  *   negated errno value is returned (See comments with send() for a list
808  *   of the appropriate errno value).
809  *
810  ****************************************************************************/
811 
812 ssize_t nx_send(int sockfd, FAR const void *buf, size_t len, int flags);
813 
814 /****************************************************************************
815  * Name: psock_sendto
816  *
817  * Description:
818  *   If sendto() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET)
819  *   socket, the parameters to and 'tolen' are ignored (and the error EISCONN
820  *   may be returned when they are not NULL and 0), and the error ENOTCONN is
821  *   returned when the socket was not actually connected.
822  *
823  * Input Parameters:
824  *   psock    A pointer to a NuttX-specific, internal socket structure
825  *   buf      Data to send
826  *   len      Length of data to send
827  *   flags    Send flags
828  *   to       Address of recipient
829  *   tolen    The length of the address structure
830  *
831  * Returned Value:
832  *   On success, returns the number of characters sent.  On any failure, a
833  *   negated errno value is returned.  One of:
834  *
835  *   EAGAIN or EWOULDBLOCK
836  *     The socket is marked non-blocking and the requested operation
837  *     would block.
838  *   EBADF
839  *     An invalid descriptor was specified.
840  *   ECONNRESET
841  *     Connection reset by peer.
842  *   EDESTADDRREQ
843  *     The socket is not connection-mode, and no peer address is set.
844  *   EFAULT
845  *      An invalid user space address was specified for a parameter.
846  *   EINTR
847  *      A signal occurred before any data was transmitted.
848  *   EINVAL
849  *      Invalid argument passed.
850  *   EISCONN
851  *     The connection-mode socket was connected already but a recipient
852  *     was specified. (Now either this error is returned, or the recipient
853  *     specification is ignored.)
854  *   EMSGSIZE
855  *     The socket type requires that message be sent atomically, and the
856  *     size of the message to be sent made this impossible.
857  *   ENOBUFS
858  *     The output queue for a network interface was full. This generally
859  *     indicates that the interface has stopped sending, but may be
860  *     caused by transient congestion.
861  *   ENOMEM
862  *     No memory available.
863  *   ENOTCONN
864  *     The socket is not connected, and no target has been given.
865  *   ENOTSOCK
866  *     The argument s is not a socket.
867  *   EOPNOTSUPP
868  *     Some bit in the flags argument is inappropriate for the socket
869  *     type.
870  *   EPIPE
871  *     The local end has been shut down on a connection oriented socket.
872  *     In this case the process will also receive a SIGPIPE unless
873  *     MSG_NOSIGNAL is set.
874  *
875  ****************************************************************************/
876 
877 ssize_t psock_sendto(struct socket *psock, const void *buf,
878                      size_t len, int flags, const struct sockaddr *to,
879                      socklen_t tolen);
880 
881 /****************************************************************************
882  * Name: psock_recvfrom
883  *
884  * Description:
885  *   psock_recvfrom() receives messages from a socket, and may be used to
886  *   receive data on a socket whether or not it is connection-oriented.
887  *   This is an internal OS interface.  It is functionally equivalent to
888  *   recvfrom() except that:
889  *
890  *   - It is not a cancellation point,
891  *   - It does not modify the errno variable, and
892  *   - I accepts the internal socket structure as an input rather than an
893  *     task-specific socket descriptor.
894  *
895  * Input Parameters:
896  *   psock   - A pointer to a NuttX-specific, internal socket structure
897  *   buf     - Buffer to receive data
898  *   len     - Length of buffer
899  *   flags   - Receive flags
900  *   from    - Address of source (may be NULL)
901  *   fromlen - The length of the address structure
902  *
903  * Returned Value:
904  *   On success, returns the number of characters sent.  If no data is
905  *   available to be received and the peer has performed an orderly shutdown,
906  *   recv() will return 0.  Otherwise, on any failure, a negated errno value
907  *   is returned (see comments with send() for a list of appropriate errno
908  *   values).
909  *
910  ****************************************************************************/
911 
912 ssize_t psock_recvfrom(struct socket *psock, void *buf, size_t len,
913                        int flags, struct sockaddr *from,
914                        socklen_t *fromlen);
915 
916 /* recv using the underlying socket structure */
917 
918 #define psock_recv(psock,buf,len,flags) \
919   psock_recvfrom(psock,buf,len,flags,NULL,0)
920 
921 /****************************************************************************
922  * Name: nx_recvfrom
923  *
924  * Description:
925  *   nx_recvfrom() receives messages from a socket, and may be used to
926  *   receive data on a socket whether or not it is connection-oriented.
927  *   This is an internal OS interface.  It is functionally equivalent to
928  *   recvfrom() except that:
929  *
930  *   - It is not a cancellation point, and
931  *   - It does not modify the errno variable.
932  *
933  * Input Parameters:
934  *   sockfd  - Socket descriptor of socket
935  *   buf     - Buffer to receive data
936  *   len     - Length of buffer
937  *   flags   - Receive flags
938  *   from    - Address of source (may be NULL)
939  *   fromlen - The length of the address structure
940  *
941  * Returned Value:
942  *   On success, returns the number of characters sent.  If no data is
943  *   available to be received and the peer has performed an orderly shutdown,
944  *   recv() will return 0.  Otherwise, on any failure, a negated errno value
945  *   is returned (see comments with send() for a list of appropriate errno
946  *   values).
947  *
948  ****************************************************************************/
949 
950 ssize_t nx_recvfrom(int sockfd, FAR void *buf, size_t len, int flags,
951                     FAR struct sockaddr *from, FAR socklen_t *fromlen);
952 
953 /* Internal version os recv */
954 
955 #define nx_recv(psock,buf,len,flags) nx_recvfrom(psock,buf,len,flags,NULL,0)
956 
957 /****************************************************************************
958  * Name: psock_getsockopt
959  *
960  * Description:
961  *   getsockopt() retrieve thse value for the option specified by the
962  *   'option' argument for the socket specified by the 'psock' argument. If
963  *   the size of the option value is greater than 'value_len', the value
964  *   stored in the object pointed to by the 'value' argument will be silently
965  *   truncated. Otherwise, the length pointed to by the 'value_len' argument
966  *   will be modified to indicate the actual length of the'value'.
967  *
968  *   The 'level' argument specifies the protocol level of the option. To
969  *   retrieve options at the socket level, specify the level argument as
970  *   SOL_SOCKET.
971  *
972  *   See <sys/socket.h> a complete list of values for the 'option' argument.
973  *
974  * Input Parameters:
975  *   psock     Socket structure of the socket to query
976  *   level     Protocol level to set the option
977  *   option    identifies the option to get
978  *   value     Points to the argument value
979  *   value_len The length of the argument value
980  *
981  * Returned Value:
982  *  Returns zero (OK) on success.  On failure, it returns a negated errno
983  *  value to indicate the nature of the error:
984  *
985  *  EINVAL
986  *    The specified option is invalid at the specified socket 'level' or the
987  *    socket has been shutdown.
988  *  ENOPROTOOPT
989  *    The 'option' is not supported by the protocol.
990  *  ENOTSOCK
991  *    The 'psock' argument does not refer to a socket.
992  *  ENOBUFS
993  *    Insufficient resources are available in the system to complete the
994  *    call.
995  *
996  ****************************************************************************/
997 
998 int psock_getsockopt(struct socket *psock, int level, int option,
999                      void *value, socklen_t *value_len);
1000 
1001 /****************************************************************************
1002  * Name: psock_setsockopt
1003  *
1004  * Description:
1005  *   psock_setsockopt() sets the option specified by the 'option' argument,
1006  *   at the protocol level specified by the 'level' argument, to the value
1007  *   pointed to by the 'value' argument for the socket on the 'psock'
1008  *   argument.
1009  *
1010  *   The 'level' argument specifies the protocol level of the option. To set
1011  *   options at the socket level, specify the level argument as SOL_SOCKET.
1012  *
1013  *   See <sys/socket.h> a complete list of values for the 'option' argument.
1014  *
1015  * Input Parameters:
1016  *   psock     Socket structure of socket to operate on
1017  *   level     Protocol level to set the option
1018  *   option    identifies the option to set
1019  *   value     Points to the argument value
1020  *   value_len The length of the argument value
1021  *
1022  * Returned Value:
1023  *  Returns zero (OK) on success.  On failure, it returns a negated errno
1024  *  value to indicate the nature of the error:
1025  *
1026  *  EDOM
1027  *    The send and receive timeout values are too big to fit into the
1028  *    timeout fields in the socket structure.
1029  *  EINVAL
1030  *    The specified option is invalid at the specified socket 'level' or the
1031  *    socket has been shut down.
1032  *  EISCONN
1033  *    The socket is already connected, and a specified option cannot be set
1034  *    while the socket is connected.
1035  *  ENOPROTOOPT
1036  *    The 'option' is not supported by the protocol.
1037  *  ENOTSOCK
1038  *    The 'sockfd' argument does not refer to a socket.
1039  *  ENOMEM
1040  *    There was insufficient memory available for the operation to complete.
1041  *  ENOBUFS
1042  *    Insufficient resources are available in the system to complete the
1043  *    call.
1044  *
1045  * Assumptions:
1046  *
1047  ****************************************************************************/
1048 
1049 int psock_setsockopt(struct socket *psock, int level, int option,
1050                      const void *value, socklen_t value_len);
1051 
1052 /****************************************************************************
1053  * Name: psock_getsockname
1054  *
1055  * Description:
1056  *   The psock_getsockname() function retrieves the locally-bound name of the
1057  *   the specified socket, stores this address in the sockaddr structure
1058  *   pointed to by the 'addr' argument, and stores the length of this
1059  *   address in the object pointed to by the 'addrlen' argument.
1060  *
1061  *   If the actual length of the address is greater than the length of the
1062  *   supplied sockaddr structure, the stored address will be truncated.
1063  *
1064  *   If the socket has not been bound to a local name, the value stored in
1065  *   the object pointed to by address is unspecified.
1066  *
1067  * Parameters:
1068  *   psock    Socket structure of socket to operate on
1069  *   addr     sockaddr structure to receive data [out]
1070  *   addrlen  Length of sockaddr structure [in/out]
1071  *
1072  * Returned Value:
1073  *   On success, 0 is returned, the 'addr' argument points to the address
1074  *   of the socket, and the 'addrlen' argument points to the length of the
1075  *   address. Otherwise, -1 is returned and errno is set to indicate the
1076  *   error.  Possible errno values that may be returned include:
1077  *
1078  *   EBADF      - The socket argument is not a valid file descriptor.
1079  *   ENOTSOCK   - The socket argument does not refer to a socket.
1080  *   EOPNOTSUPP - The operation is not supported for this socket's protocol.
1081  *   ENOTCONN   - The socket is not connected or otherwise has not had the
1082  *                peer pre-specified.
1083  *   EINVAL     - The socket has been shut down.
1084  *   ENOBUFS    - Insufficient resources were available in the system to
1085  *                complete the function.
1086  *
1087  ****************************************************************************/
1088 
1089 int psock_getsockname(FAR struct socket *psock, FAR struct sockaddr *addr,
1090                       FAR socklen_t *addrlen);
1091 
1092 /****************************************************************************
1093  * Name: psock_getpeername
1094  *
1095  * Description:
1096  *   The psock_getpeername() function retrieves the remote-connected name of
1097  *   the specified socket, stores this address in the sockaddr structure
1098  *   pointed to by the 'addr' argument, and stores the length of this address
1099  *   in the object pointed to by the 'addrlen' argument.
1100  *
1101  *   If the actual length of the address is greater than the length of the
1102  *   supplied sockaddr structure, the stored address will be truncated.
1103  *
1104  *   If the socket has not been bound to a local name, the value stored in
1105  *   the object pointed to by address is unspecified.
1106  *
1107  * Parameters:
1108  *   psock    Socket structure of socket to operate on
1109  *   addr     sockaddr structure to receive data [out]
1110  *   addrlen  Length of sockaddr structure [in/out]
1111  *
1112  * Returned Value:
1113  *   On success, 0 is returned, the 'addr' argument points to the address
1114  *   of the socket, and the 'addrlen' argument points to the length of the
1115  *   address. Otherwise, -1 is returned and errno is set to indicate the
1116  *   error.  Possible errno values that may be returned include:
1117  *
1118  *   EBADF      - The socket argument is not a valid file descriptor.
1119  *   ENOTSOCK   - The socket argument does not refer to a socket.
1120  *   EOPNOTSUPP - The operation is not supported for this socket's protocol.
1121  *   ENOTCONN   - The socket is not connected or otherwise has not had the
1122  *                peer pre-specified.
1123  *   EINVAL     - The socket has been shut down.
1124  *   ENOBUFS    - Insufficient resources were available in the system to
1125  *                complete the function.
1126  *
1127  ****************************************************************************/
1128 
1129 int psock_getpeername(FAR struct socket *psock, FAR struct sockaddr *addr,
1130                       FAR socklen_t *addrlen);
1131 
1132 /****************************************************************************
1133  * Name: psock_ioctl
1134  *
1135  * Description:
1136  *   Perform network device specific operations.
1137  *
1138  * Input Parameters:
1139  *   psock    A pointer to a NuttX-specific, internal socket structure
1140  *   cmd      The ioctl command
1141  *   arg      The argument of the ioctl cmd
1142  *
1143  * Returned Value:
1144  *   A non-negative value is returned on success; a negated errno value is
1145  *   returned on any failure to indicate the nature of the failure:
1146  *
1147  *   EBADF
1148  *     'psock' is not a valid, connected socket structure.
1149  *   EFAULT
1150  *     'arg' references an inaccessible memory area.
1151  *   ENOTTY
1152  *     'cmd' not valid.
1153  *   EINVAL
1154  *     'arg' is not valid.
1155  *   ENOTTY
1156  *     'sockfd' is not associated with a network device.
1157  *   ENOTTY
1158  *      The specified request does not apply to the kind of object that the
1159  *      descriptor 'sockfd' references.
1160  *
1161  ****************************************************************************/
1162 
1163 int psock_ioctl(FAR struct socket *psock, int cmd, unsigned long arg);
1164 
1165 /****************************************************************************
1166  * Name: netdev_ioctl
1167  *
1168  * Description:
1169  *   Perform network device specific operations.
1170  *
1171  * Input Parameters:
1172  *   sockfd   Socket descriptor of device
1173  *   cmd      The ioctl command
1174  *   arg      The argument of the ioctl cmd
1175  *
1176  * Returned Value:
1177  *   A non-negative value is returned on success; a negated errno value is
1178  *   returned on any failure to indicate the nature of the failure:
1179  *
1180  *   EBADF
1181  *     'sockfd' is not a valid socket descriptor.
1182  *   EFAULT
1183  *     'arg' references an inaccessible memory area.
1184  *   ENOTTY
1185  *     'cmd' not valid.
1186  *   EINVAL
1187  *     'arg' is not valid.
1188  *   ENOTTY
1189  *     'sockfd' is not associated with a network device.
1190  *   ENOTTY
1191  *      The specified request does not apply to the kind of object that the
1192  *      descriptor 'sockfd' references.
1193  *
1194  ****************************************************************************/
1195 
1196 int netdev_ioctl(int sockfd, int cmd, unsigned long arg);
1197 
1198 /****************************************************************************
1199  * Name: psock_poll
1200  *
1201  * Description:
1202  *   The standard poll() operation redirects operations on socket descriptors
1203  *   to this function.
1204  *
1205  * Input Parameters:
1206  *   psock - An instance of the internal socket structure.
1207  *   fds   - The structure describing the events to be monitored, OR NULL if
1208  *           this is a request to stop monitoring events.
1209  *   setup - true: Setup up the poll; false: Teardown the poll
1210  *
1211  * Returned Value:
1212  *  0: Success; Negated errno on failure
1213  *
1214  ****************************************************************************/
1215 
1216 struct pollfd; /* Forward reference -- see poll.h */
1217 
1218 int psock_poll(struct socket *psock, struct pollfd *fds, bool setup);
1219 
1220 /****************************************************************************
1221  * Name: net_poll
1222  *
1223  * Description:
1224  *   The standard poll() operation redirects operations on socket descriptors
1225  *   to this function.
1226  *
1227  * Input Parameters:
1228  *   fd    - The socket descriptor of interest
1229  *   fds   - The structure describing the events to be monitored, OR NULL if
1230  *           this is a request to stop monitoring events.
1231  *   setup - true: Setup up the poll; false: Teardown the poll
1232  *
1233  * Returned Value:
1234  *  0: Success; Negated errno on failure
1235  *
1236  ****************************************************************************/
1237 
1238 struct pollfd; /* Forward reference -- see poll.h */
1239 
1240 int net_poll(int sockfd, struct pollfd *fds, bool setup);
1241 
1242 /****************************************************************************
1243  * Name: psock_dupsd
1244  *
1245  * Description:
1246  *   Clone a socket descriptor to an arbitray descriptor number.  If file
1247  *   descriptors are implemented, then this is called by dup() for the case
1248  *   of socket file descriptors.  If file descriptors are not implemented,
1249  *   then this function IS dup().
1250  *
1251  * Returned Value:
1252  *   On success, returns the number of characters sent.  On any error,
1253  *   a negated errno value is returned:.
1254  *
1255  ****************************************************************************/
1256 
1257 int psock_dupsd(FAR struct socket *psock, int minsd);
1258 
1259 /****************************************************************************
1260  * Name: net_dupsd
1261  *
1262  * Description:
1263  *   Clone a socket descriptor to an arbitray descriptor number.  If file
1264  *   descriptors are implemented, then this is called by dup() for the case
1265  *   of socket file descriptors.  If file descriptors are not implemented,
1266  *   then this function IS dup().
1267  *
1268  * Returned Value:
1269  *   On success, returns the number of characters sent.  On any error,
1270  *   a negated errno value is returned:.
1271  *
1272  ****************************************************************************/
1273 
1274 int net_dupsd(int sockfd, int minsd);
1275 
1276 /****************************************************************************
1277  * Name: net_dupsd2
1278  *
1279  * Description:
1280  *   Clone a socket descriptor to an arbitray descriptor number.  If file
1281  *   descriptors are implemented, then this is called by dup2() for the case
1282  *   of socket file descriptors.  If file descriptors are not implemented,
1283  *   then this function IS dup2().
1284  *
1285  * Returned Value:
1286  *   On success, returns the number of characters sent.  On any error,
1287  *   a negated errno value is returned:.
1288  *
1289  ****************************************************************************/
1290 
1291 int net_dupsd2(int sockfd1, int sockfd2);
1292 
1293 /****************************************************************************
1294  * Name: net_fstat
1295  *
1296  * Description:
1297  *   Performs fstat operations on socket
1298  *
1299  * Input Parameters:
1300  *   sockfd - Socket descriptor of the socket to operate on
1301  *   bug    - Caller-provided location in which to return the fstat data
1302  *
1303  * Returned Value:
1304  *   Zero (OK) is returned on success; a negated errno value is returned on
1305  *   any failure to indicate the nature of the failure.
1306  *
1307  ****************************************************************************/
1308 
1309 struct stat;  /* Forward reference.  See sys/stat.h */
1310 
1311 int net_fstat(int sockfd, FAR struct stat *buf);
1312 
1313 /****************************************************************************
1314  * Name: net_clone
1315  *
1316  * Description:
1317  *   Performs the low level, common portion of net_dupsd() and net_dupsd2()
1318  *
1319  ****************************************************************************/
1320 
1321 int net_clone(struct socket *psock1, struct socket *psock2);
1322 
1323 /****************************************************************************
1324  * Name: net_sendfile
1325  *
1326  * Description:
1327  *   The send() call may be used only when the socket is in a connected state
1328  *   (so that the intended recipient is known). The only difference between
1329  *   send() and write() is the presence of flags. With zero flags parameter,
1330  *   send() is equivalent to write(). Also, send(sockfd,buf,len,flags) is
1331  *   equivalent to sendto(sockfd,buf,len,flags,NULL,0).
1332  *
1333  * Input Parameters:
1334  *   psock    An instance of the internal socket structure.
1335  *   buf      Data to send
1336  *   len      Length of data to send
1337  *   flags    Send flags
1338  *
1339  * Returned Value:
1340  *   On success, returns the number of characters sent.  On  error,
1341  *   -1 is returned, and errno is set appropriately:
1342  *
1343  *   EAGAIN or EWOULDBLOCK
1344  *     The socket is marked non-blocking and the requested operation
1345  *     would block.
1346  *   EBADF
1347  *     An invalid descriptor was specified.
1348  *   ECONNRESET
1349  *     Connection reset by peer.
1350  *   EDESTADDRREQ
1351  *     The socket is not connection-mode, and no peer address is set.
1352  *   EFAULT
1353  *      An invalid user space address was specified for a parameter.
1354  *   EINTR
1355  *      A signal occurred before any data was transmitted.
1356  *   EINVAL
1357  *      Invalid argument passed.
1358  *   EISCONN
1359  *     The connection-mode socket was connected already but a recipient
1360  *     was specified. (Now either this error is returned, or the recipient
1361  *     specification is ignored.)
1362  *   EMSGSIZE
1363  *     The socket type requires that message be sent atomically, and the
1364  *     size of the message to be sent made this impossible.
1365  *   ENOBUFS
1366  *     The output queue for a network interface was full. This generally
1367  *     indicates that the interface has stopped sending, but may be
1368  *     caused by transient congestion.
1369  *   ENOMEM
1370  *     No memory available.
1371  *   ENOTCONN
1372  *     The socket is not connected, and no target has been given.
1373  *   ENOTSOCK
1374  *     The argument s is not a socket.
1375  *   EOPNOTSUPP
1376  *     Some bit in the flags argument is inappropriate for the socket
1377  *     type.
1378  *   EPIPE
1379  *     The local end has been shut down on a connection oriented socket.
1380  *     In this case the process will also receive a SIGPIPE unless
1381  *     MSG_NOSIGNAL is set.
1382  *
1383  ****************************************************************************/
1384 
1385 #ifdef CONFIG_NET_SENDFILE
1386 struct file;
1387 ssize_t net_sendfile(int outfd, struct file *infile, off_t *offset,
1388                      size_t count);
1389 #endif
1390 
1391 /****************************************************************************
1392  * Name: psock_vfcntl
1393  *
1394  * Description:
1395  *   Performs fcntl operations on socket
1396  *
1397  * Input Parameters:
1398  *   psock - An instance of the internal socket structure.
1399  *   cmd   - The fcntl command.
1400  *   ap    - Command-specific arguments
1401  *
1402  * Returned Value:
1403  *   Zero (OK) is returned on success; a negated errno value is returned on
1404  *   any failure to indicate the nature of the failure.
1405  *
1406  ****************************************************************************/
1407 
1408 int psock_vfcntl(FAR struct socket *psock, int cmd, va_list ap);
1409 
1410 /****************************************************************************
1411  * Name: psock_fcntl
1412  *
1413  * Description:
1414  *   Similar to the standard fcntl function except that is accepts a struct
1415  *   struct socket instance instead of a file descriptor.
1416  *
1417  * Input Parameters:
1418  *   psock - An instance of the internal socket structure.
1419  *   cmd   - Identifies the operation to be performed.  Command specific
1420  *           arguments may follow.
1421  *
1422  * Returned Value:
1423  *   The nature of the return value depends on the command.  Non-negative
1424  *   values indicate success.  Failures are reported as negated errno
1425  *   values.
1426  *
1427  ****************************************************************************/
1428 
1429 int psock_fcntl(FAR struct socket *psock, int cmd, ...);
1430 
1431 /****************************************************************************
1432  * Name: net_vfcntl
1433  *
1434  * Description:
1435  *   Performs fcntl operations on socket
1436  *
1437  * Input Parameters:
1438  *   sockfd - Socket descriptor of the socket to operate on
1439  *   cmd    - The fcntl command.
1440  *   ap     - Command-specific arguments
1441  *
1442  * Returned Value:
1443  *   Zero (OK) is returned on success; a negated errno value is returned on
1444  *   any failure to indicate the nature of the failure.
1445  *
1446  ****************************************************************************/
1447 
1448 int net_vfcntl(int sockfd, int cmd, va_list ap);
1449 
1450 /****************************************************************************
1451  * Name: netdev_register
1452  *
1453  * Description:
1454  *   Register a network device driver and assign a name to it so that it can
1455  *   be found in subsequent network ioctl operations on the device.
1456  *
1457  *   A custom, device-specific interface name format string may be selected
1458  *   by putting that format string into the device structure's d_ifname[]
1459  *   array before calling netdev_register().  Otherwise, the d_ifname[] must
1460  *   be zeroed on entry.
1461  *
1462  * Input Parameters:
1463  *   dev    - The device driver structure to be registered.
1464  *   lltype - Link level protocol used by the driver (Ethernet, SLIP, TUN, ...
1465  *
1466  * Returned Value:
1467  *   0:Success; negated errno on failure
1468  *
1469  * Assumptions:
1470  *   Called during system initialization from normal user mode
1471  *
1472  ****************************************************************************/
1473 
1474 int netdev_register(struct net_driver_s *dev, enum net_lltype_e lltype);
1475 
1476 /****************************************************************************
1477  * Name: netdev_unregister
1478  *
1479  * Description:
1480  *   Unregister a network device driver.
1481  *
1482  * Input Parameters:
1483  *   dev - The device driver structure to un-register
1484  *
1485  * Returned Value:
1486  *   0:Success; negated errno on failure
1487  *
1488  * Assumptions:
1489  *   Currently only called for USB networking devices when the device is
1490  *   physically removed from the slot
1491  *
1492  ****************************************************************************/
1493 
1494 int netdev_unregister(struct net_driver_s *dev);
1495 
1496 
1497 #ifdef __cplusplus
1498 #if __cplusplus
1499 }
1500 #endif /* __cplusplus */
1501 #endif /* __cplusplus */
1502 
1503 #endif /* CONFIG_NET */
1504 #endif /* __INCLUDE_NET_NET_H */
1505