Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers socket.h Source File

socket.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008-2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 /**
00018  *
00019  * \file socket.h
00020  * \brief Socket API definitions.
00021  *
00022  *  Socket API functions and relevant structures.
00023  *
00024  *
00025  */
00026 
00027 
00028 #ifndef _NS_SOCKET_H
00029 #define _NS_SOCKET_H
00030 
00031 #include "Core/include/ns_error_types.h"
00032 #include "Core/include/ns_buffer.h"
00033 #include "Core/include/sockbuf.h"
00034 
00035 #ifndef SOCKET_RX_LIMIT
00036 #define SOCKET_RX_LIMIT 3
00037 #endif
00038 
00039 #ifndef SOCKETS_MAX
00040 #define SOCKETS_MAX 16
00041 #endif
00042 
00043 #ifndef SOCKET_DEFAULT_STREAM_RCVBUF
00044 #define SOCKET_DEFAULT_STREAM_RCVBUF 2048
00045 #endif
00046 
00047 #ifndef SOCKET_DEFAULT_STREAM_SNDBUF
00048 #define SOCKET_DEFAULT_STREAM_SNDBUF 2048
00049 #endif
00050 
00051 #ifndef SOCKET_DEFAULT_STREAM_SNDLOWAT
00052 #define SOCKET_DEFAULT_STREAM_SNDLOWAT 512
00053 #endif
00054 
00055 typedef enum socket_family_e {
00056     SOCKET_FAMILY_NONE,
00057     SOCKET_FAMILY_IPV6,
00058 } socket_family_t;
00059 
00060 // This is always true because we currently have only 1 type
00061 // If we support more types in future it becomes "((socket)->family == SOCKET_FAMILY_IPV6)"
00062 #define socket_is_ipv6(socket) true
00063 
00064 typedef enum socket_type_e {
00065     SOCKET_TYPE_DGRAM,
00066     SOCKET_TYPE_STREAM,
00067     SOCKET_TYPE_RAW
00068 } socket_type_t;
00069 
00070 #define SOCKET_FLAG_CLOSED          128     /* Has been closed by application */
00071 #define SOCKET_FLAG_PENDING         64      /* Is waiting for accept on a listening socket */
00072 #define SOCKET_FLAG_CANT_RECV_MORE  32
00073 #define SOCKET_FLAG_SHUT_WR         16
00074 #define SOCKET_FLAG_CONNECTED       8       /* Connection completed (and possibly since broken) */
00075 #define SOCKET_LISTEN_STATE         4
00076 #define SOCKET_BUFFER_CB            2
00077 #define SOCKET_FLAG_CONNECTING      1       /* Connection in progress */
00078 
00079 #ifndef SOCKET_IPV6_TCLASS_DEFAULT
00080 #define SOCKET_IPV6_TCLASS_DEFAULT  0   /** Default traffic class for a new socket */
00081 #endif
00082 
00083 #ifndef SOCKET_LISTEN_BACKLOG_MAX
00084 #define SOCKET_LISTEN_BACKLOG_MAX 10
00085 #endif
00086 
00087 typedef enum {
00088     ARM_SOCKET_INIT = 0,
00089     ARM_SOCKET_EVENT_CB = 1,
00090     ARM_SOCKET_DATA_CB = 2,  // Original data event - one buffer stored in event
00091     ARM_SOCKET_DATA_QUEUED_CB = 3, // New data event - no buffer in event, buffer in queue
00092     ARM_SOCKET_TCP_TIMER_CB ,
00093 } arm_socket_event_id_e;
00094 
00095 typedef struct socket_buffer_callback_t {
00096     uint8_t event_type;
00097     int8_t socket_id;
00098     int8_t interface_id;
00099     buffer_t *buf;
00100     void *session_ptr;
00101 } socket_buffer_callback_t;
00102 
00103 typedef enum {
00104     SOCKET_SRC_ADDRESS_PRIMARY = 0,
00105     SOCKET_SRC_ADDRESS_SECONDARY = 1,
00106 } socket_src_address_type;
00107 
00108 struct inet_pcb_s;
00109 struct ns_msghdr;
00110 struct socket;
00111 typedef NS_LIST_HEAD_INCOMPLETE(struct socket) socket_queue_list_t;
00112 
00113 struct socket_pending {
00114     ns_list_link_t link ;        /*!< Link as member of connection queue */
00115     struct socket *listen_head; /*!< Pointer to listening socket we're queued against */
00116 };
00117 
00118 struct socket_live {
00119     socket_queue_list_t queue;  /*!< Incoming connection queue */
00120     void (*fptr)(void *);
00121 };
00122 
00123 /** Socket structure */
00124 typedef struct socket {
00125     /* This listen queue must be first, due to need to use incomplete list head */
00126     union {
00127         struct socket_pending pending; /* !< Info if PENDING flag set (pending connection) */
00128         struct socket_live live;      /* !< Info if PENDING flag clear (normal socket) */
00129     } u;
00130     int8_t    id ;                     /*!< socket id */
00131     uint8_t   flags ;                  /*!< Socket option flags */
00132     int8_t    tasklet ;                /*!< Receiver tasklet */
00133     uint8_t   refcount;
00134     socket_family_t family;
00135     socket_type_t type;
00136     int8_t    default_interface_id;
00137     int8_t    broadcast_pan;
00138     uint8_t   listen_backlog ;       /*!< Limit if pending connection queue */
00139     struct inet_pcb_s *inet_pcb ;    /*!< shortcut to Internet control block */
00140     sockbuf_t   rcvq;
00141     sockbuf_t   sndq;
00142     ns_list_link_t link ;            /*!< link */
00143 } socket_t;
00144 
00145 NS_STATIC_ASSERT(offsetof(socket_t, u.pending.link) == 0, "Listen queue link must be first (NS_LIST_HEAD_INCOMPLETE)")
00146 
00147 typedef struct inet_group {
00148     uint8_t group_addr[16];
00149     int8_t interface_id;
00150     ns_list_link_t link;
00151 } inet_group_t;
00152 
00153 /** Internet protocol control block */
00154 typedef struct inet_pcb_s {
00155     socket_t    *socket;
00156 #ifndef NO_TCP
00157     void        *session ;           /*!< Session for transport protocol - only used by TCP at present */
00158 #endif
00159     uint8_t     local_address [16];  /*!< Local address */
00160     uint8_t     remote_address [16];    /*!< Destination address */
00161     uint16_t    local_port ;         /*!< Local port */
00162     uint16_t    remote_port ;           /*!< Destination port */
00163     int16_t     unicast_hop_limit ;  /*!< May be -1, which will give per-interface default */
00164     uint8_t     multicast_hop_limit;
00165     int8_t      multicast_if;
00166     uint32_t    addr_preferences;
00167     uint8_t     protocol ;           /*!< IP type IPV6_NH_TCP, IPV6_NH_UDP, IPV6_NH_ICMPV6... */
00168     int8_t      link_layer_security;
00169 #ifndef NO_IPV6_PMTUD
00170     int8_t      use_min_mtu ;        /*!< RFC 3542 socket option */
00171 #endif
00172 #ifndef NO_IP_FRAGMENT_TX
00173     int8_t      dontfrag;
00174 #endif
00175     uint8_t     tclass;
00176     bool        multicast_loop:1;
00177     bool        recvpktinfo:1;
00178     bool        recvhoplimit:1;
00179     bool        recvtclass:1;
00180     int_least24_t flow_label;
00181     NS_LIST_HEAD(inet_group_t, link) mc_groups;
00182 } inet_pcb_t;
00183 
00184 typedef NS_LIST_HEAD(socket_t, link) socket_list_t;
00185 extern socket_list_t socket_list;
00186 
00187 extern const uint8_t ns_in6addr_any[16];
00188 
00189 extern void socket_init(void);
00190 extern int8_t socket_event_handler_id_get(void);
00191 extern bool socket_data_queued_event_push(socket_t *socket);
00192 extern void socket_event_push(uint8_t sock_event, socket_t *socket, int8_t interface_id, void *session_ptr, uint16_t length);
00193 extern error_t socket_create(socket_family_t family, socket_type_t type, uint8_t protocol , int8_t *sid, uint16_t port, void (*passed_fptr)(void *), bool buffer_type);
00194 extern socket_t *socket_new_incoming_connection(socket_t *listen_socket);
00195 void socket_connection_abandoned(socket_t *socket, int8_t interface_id, uint8_t reason);
00196 void socket_connection_complete(socket_t *socket, int8_t interface_id);
00197 extern void socket_leave_pending_state(socket_t *socket, void (*fptr)(void *));
00198 void socket_cant_recv_more(socket_t *socket, int8_t interface_id);
00199 extern int8_t socket_id_assign_and_attach(socket_t *socket);
00200 extern void socket_id_detach(int8_t sid);
00201 extern buffer_t *socket_buffer_read(socket_t *socket);
00202 extern socket_t *socket_lookup(socket_family_t family, uint8_t protocol, const sockaddr_t *local_addr, const sockaddr_t *remote_addr);
00203 extern socket_t *socket_lookup_ipv6(uint8_t protocol, const sockaddr_t *local_addr, const sockaddr_t *remote_addr, bool allow_wildcards);
00204 extern error_t socket_port_validate(uint16_t port, uint8_t protocol);
00205 extern error_t socket_up(buffer_t *buf);
00206 extern bool socket_message_validate_iov(const struct ns_msghdr *msg, uint16_t *length_out);
00207 extern int16_t socket_buffer_sendmsg(int8_t sid, buffer_t *buf, const struct ns_msghdr *msg, int flags);
00208 extern socket_t *socket_pointer_get(int8_t socket);
00209 extern void socket_inet_pcb_set_buffer_hop_limit(const inet_pcb_t *socket, buffer_t *buf, const int16_t *msg_hoplimit);
00210 extern bool socket_validate_listen_backlog(const socket_t *socket_ptr);
00211 extern void socket_list_print(route_print_fn_t *print_fn, char sep);
00212 extern socket_t *socket_reference(socket_t *);
00213 extern socket_t *socket_dereference(socket_t *);
00214 
00215 extern void socket_release(socket_t *socket);
00216 
00217 extern void socket_tx_buffer_event_and_free(buffer_t *buf, uint8_t status);
00218 extern buffer_t *socket_tx_buffer_event(buffer_t *buf, uint8_t status);
00219 
00220 extern inet_pcb_t *socket_inet_pcb_allocate(void);
00221 extern inet_pcb_t *socket_inet_pcb_clone(const inet_pcb_t *orig);
00222 
00223 /**
00224  * Free reserved inet_pcb
00225  */
00226 extern inet_pcb_t *socket_inet_pcb_free(inet_pcb_t *inet_pcb);
00227 
00228 extern int8_t socket_inet_pcb_join_group(inet_pcb_t *inet_pcb, int8_t interface_id, const uint8_t group[__static 16]);
00229 extern int8_t socket_inet_pcb_leave_group(inet_pcb_t *inet_pcb, int8_t interface_id, const uint8_t group[__static 16]);
00230 
00231 /**
00232  * Determine interface based on socket_id and address
00233  */
00234 extern struct protocol_interface_info_entry *socket_interface_determine(const socket_t *socket, buffer_t *buf);
00235 
00236 extern uint16_t socket_generate_random_port(uint8_t protocol);
00237 
00238 #endif /*_NS_SOCKET_H*/