1 /* 2 * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef WM_SOCKET_API2_0_3_H 17 #define WM_SOCKET_API2_0_3_H 18 19 #include <stdio.h> 20 #include <time.h> 21 #include "wm_type_def.h" 22 #include "wm_config.h" 23 24 /** If your port already typedef's sa_family_t, define SA_FAMILY_T_DEFINED 25 to prevent this code from redefining it. */ 26 #if !defined(sa_family_t) && !defined(SA_FAMILY_T_DEFINED) 27 typedef u8_t sa_family_t; 28 #endif 29 /** If your port already typedef's in_port_t, define IN_PORT_T_DEFINED 30 to prevent this code from redefining it. */ 31 #if !defined(in_port_t) && !defined(IN_PORT_T_DEFINED) 32 typedef u16_t in_port_t; 33 #endif 34 35 /** If your port already typedef's in_addr_t, define IN_ADDR_T_DEFINED 36 to prevent this code from redefining it. */ 37 #if !defined(in_addr_t) && !defined(IN_ADDR_T_DEFINED) 38 #if !defined(_NEWLIB_VERSION_H__) 39 typedef u32_t in_addr_t; 40 #endif 41 #endif 42 43 struct in_addr { 44 in_addr_t s_addr; 45 }; 46 47 struct in6_addr { 48 union { 49 u32_t u32_addr[4]; 50 u8_t u8_addr[16]; 51 }un; 52 #define s6_addr un.u8_addr 53 }; 54 55 /** 255.255.255.255 */ 56 #define IPADDR_NONE ((u32_t)0xffffffffUL) 57 /** 127.0.0.1 */ 58 #define IPADDR_LOOPBACK ((u32_t)0x7f000001UL) 59 /** 0.0.0.0 */ 60 #define IPADDR_ANY ((u32_t)0x00000000UL) 61 /** 255.255.255.255 */ 62 #define IPADDR_BROADCAST ((u32_t)0xffffffffUL) 63 64 /** 255.255.255.255 */ 65 #define INADDR_NONE IPADDR_NONE 66 /** 127.0.0.1 */ 67 #define INADDR_LOOPBACK IPADDR_LOOPBACK 68 /** 0.0.0.0 */ 69 #define INADDR_ANY IPADDR_ANY 70 /** 255.255.255.255 */ 71 #define INADDR_BROADCAST IPADDR_BROADCAST 72 73 #if TLS_CONFIG_IPV4 74 /** members are in network byte order */ 75 struct sockaddr_in { 76 u8_t sin_len; 77 sa_family_t sin_family; 78 in_port_t sin_port; 79 struct in_addr sin_addr; 80 #define SIN_ZERO_LEN 8 81 char sin_zero[SIN_ZERO_LEN]; 82 }; 83 #endif /* TLS_CONFIG_IPV4 */ 84 85 #if TLS_CONFIG_IPV6 86 struct sockaddr_in6 { 87 u8_t sin6_len; /* length of this structure */ 88 sa_family_t sin6_family; /* AF_INET6 */ 89 in_port_t sin6_port; /* Transport layer port # */ 90 u32_t sin6_flowinfo; /* IPv6 flow information */ 91 struct in6_addr sin6_addr; /* IPv6 address */ 92 u32_t sin6_scope_id; /* Set of interfaces for scope */ 93 }; 94 #endif /* TLS_CONFIG_IPV6 */ 95 96 struct sockaddr { 97 u8_t sa_len; 98 sa_family_t sa_family; 99 char sa_data[14]; 100 }; 101 102 struct sockaddr_storage { 103 u8_t s2_len; 104 sa_family_t ss_family; 105 char s2_data1[2]; 106 u32_t s2_data2[3]; 107 #if TLS_CONFIG_IPV6 108 u32_t s2_data3[3]; 109 #endif /* TLS_CONFIG_IPV6 */ 110 }; 111 112 struct hostent { 113 char *h_name; /* Official name of the host. */ 114 char **h_aliases; /* A pointer to an array of pointers to alternative host names, 115 terminated by a null pointer. */ 116 int h_addrtype; /* Address type. */ 117 int h_length; /* The length, in bytes, of the address. */ 118 char **h_addr_list; /* A pointer to an array of pointers to network addresses (in 119 network byte order) for the host, terminated by a null pointer. */ 120 #define h_addr h_addr_list[0] /* for backward compatibility */ 121 }; 122 123 struct sockaddr_store { 124 u8_t s2_len; 125 sa_family_t ss_family; 126 char s2_data1[2]; 127 u32_t s2_data2[3]; 128 #if TLS_CONFIG_IPV6 129 u32_t s2_data3[3]; 130 #endif /* TLS_CONFIG_IPV6 */ 131 }; 132 133 /** If your port already typedef's socklen_t, define SOCKLEN_T_DEFINED 134 to prevent this code from redefining it. */ 135 #if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED) 136 typedef u32_t socklen_t; 137 #endif 138 139 struct lwip_sock; 140 141 /** Socket protocol types (TCP/UDP/RAW) */ 142 #define SOCK_STREAM 1 143 #define SOCK_DGRAM 2 144 #define SOCK_RAW 3 145 146 /** 147 * Option flags per-socket. These must match the SOF_ flags in ip.h (checked in init.c) 148 */ 149 #define SO_REUSEADDR 0x0004 /* Allow local address reuse */ 150 #define SO_KEEPALIVE 0x0008 /* keep connections alive */ 151 #define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */ 152 153 /** 154 * Additional options, not kept in so_options. 155 */ 156 #define SO_DEBUG 0x0001 /* Unimplemented: turn on debugging info recording */ 157 #define SO_ACCEPTCONN 0x0002 /* socket has had listen() */ 158 #define SO_DONTROUTE 0x0010 /* Unimplemented: just use interface addresses */ 159 #define SO_USELOOPBACK 0x0040 /* Unimplemented: bypass hardware when possible */ 160 #define SO_LINGER 0x0080 /* linger on close if data present */ 161 #define SO_DONTLINGER ((int)(~SO_LINGER)) 162 #define SO_OOBINLINE 0x0100 /* Unimplemented: leave received OOB data in line */ 163 #define SO_REUSEPORT 0x0200 /* Unimplemented: allow local address & port reuse */ 164 #define SO_SNDBUF 0x1001 /* Unimplemented: send buffer size */ 165 #define SO_RCVBUF 0x1002 /* receive buffer size */ 166 #define SO_SNDLOWAT 0x1003 /* Unimplemented: send low-water mark */ 167 #define SO_RCVLOWAT 0x1004 /* Unimplemented: receive low-water mark */ 168 #define SO_SNDTIMEO 0x1005 /* send timeout */ 169 #define SO_RCVTIMEO 0x1006 /* receive timeout */ 170 #define SO_ERROR 0x1007 /* get error status and clear */ 171 #define SO_TYPE 0x1008 /* get socket type */ 172 #define SO_CONTIMEO 0x1009 /* Unimplemented: connect timeout */ 173 #define SO_NO_CHECK 0x100a /* don't create UDP checksum */ 174 175 /** 176 * Structure used for manipulating linger option. 177 */ 178 struct linger { 179 int l_onoff; /* option on/off */ 180 int l_linger; /* linger time in seconds */ 181 }; 182 183 /** 184 * Level number for (get/set)sockopt() to apply to socket itself. 185 */ 186 #define SOL_SOCKET 0xfff /* options for socket level */ 187 188 189 #define AF_UNSPEC 0 190 #define AF_INET 2 191 #define AF_IPX 4 192 193 #if TLS_CONFIG_IPV6 194 #define AF_INET6 10 195 #else /* TLS_CONFIG_IPV6 */ 196 #define AF_INET6 AF_UNSPEC 197 #endif /* TLS_CONFIG_IPV6 */ 198 #define PF_INET AF_INET 199 #define PF_INET6 AF_INET6 200 #define PF_UNSPEC AF_UNSPEC 201 202 #define IPPROTO_IP 0 203 #define IPPROTO_ICMP 1 204 #define IPPROTO_TCP 6 205 #define IPPROTO_UDP 17 206 #if TLS_CONFIG_IPV6 207 #define IPPROTO_IPV6 41 208 #define IPPROTO_ICMPV6 58 209 #endif /* TLS_CONFIG_IPV6 */ 210 #define IPPROTO_UDPLITE 136 211 #define IPPROTO_RAW 255 212 213 /** Flags we can use with send and recv. */ 214 #define MSG_PEEK 0x01 /* Peeks at an incoming message */ 215 #define MSG_WAITALL 0x02 /* Unimplemented: Requests that the function block until 216 the full amount of data requested can be returned */ 217 #define MSG_OOB 0x04 /* Unimplemented: Requests out-of-band data. 218 The significance and semantics of out-of-band data are protocol-specific */ 219 #define MSG_DONTWAIT 0x08 /* Nonblocking i/o for this operation only */ 220 #define MSG_MORE 0x10 /* Sender will send more */ 221 222 /** 223 * Options for level IPPROTO_IP 224 */ 225 #define IP_TOS 1 226 #define IP_TTL 2 227 228 /* 229 * Options for level IPPROTO_TCP 230 */ 231 #define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */ 232 #define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */ 233 #define TCP_KEEPIDLE 0x03 /* set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */ 234 #define TCP_KEEPINTVL 0x04 /* set pcb->keep_intvl - Use seconds for get/setsockopt */ 235 #define TCP_KEEPCNT 0x05 /* set pcb->keep_cnt - Use number of probes sent for get/setsockopt */ 236 237 #if TLS_CONFIG_IPV6 238 /** 239 * Options for level IPPROTO_IPV6 240 */ 241 #define IPV6_CHECKSUM 7 /* RFC3542: calculate and insert the ICMPv6 checksum for raw sockets. */ 242 #define IPV6_V6ONLY 27 /* RFC3493: boolean control to restrict AF_INET6 sockets to IPv6 communications only. */ 243 #endif /* TLS_CONFIG_IPV6 */ 244 245 /** 246 * Options for level IPPROTO_UDPLITE 247 */ 248 #define UDPLITE_SEND_CSCOV 0x01 /* sender checksum coverage */ 249 #define UDPLITE_RECV_CSCOV 0x02 /* minimal receiver checksum coverage */ 250 251 #if TLS_CONFIG_IGMP 252 /** 253 * Options and types for UDP multicast traffic handling 254 */ 255 #define IP_MULTICAST_TTL 5 256 #define IP_MULTICAST_IF 6 257 #define IP_MULTICAST_LOOP 7 258 #endif /* TLS_CONFIG_IGMP */ 259 260 #if 1 /* TLS_CONFIG_IGMP */ 261 /** 262 * Options and types related to multicast membership 263 */ 264 #define IP_ADD_MEMBERSHIP 3 265 #define IP_DROP_MEMBERSHIP 4 266 267 typedef struct ip_mreq { 268 struct in_addr imr_multiaddr; /* IP multicast address of group */ 269 struct in_addr imr_interface; /* local IP address of interface */ 270 }ip_mreq; 271 272 #if TLS_CONFIG_IPV6 273 #define IPV6_JOIN_GROUP 38 274 #define IPV6_LEAVE_GROUP 39 275 typedef struct ipv6_mreq { 276 struct in6_addr ipv6mr_multiaddr; /* IP multicast address of group */ 277 int ipv6mr_interface; /* index of interface */ 278 }ipv6_mreq; 279 #endif 280 #endif /* TLS_CONFIG_IGMP */ 281 282 /** 283 * @brief The Type of Service provides an indication of the abstract 284 * parameters of the quality of service desired. These parameters are 285 * to be used to guide the selection of the actual service parameters 286 * when transmitting a datagram through a particular network. Several 287 * networks offer service precedence, which somehow treats high 288 * precedence traffic as more important than other traffic (generally 289 * by accepting only traffic above a certain precedence at time of high 290 * load). The major choice is a three way tradeoff between low-delay, 291 * high-reliability, and high-throughput. 292 * The use of the Delay, Throughput, and Reliability indications may 293 * increase the cost (in some sense) of the service. In many networks 294 * better performance for one of these parameters is coupled with worse 295 * performance on another. Except for very unusual cases at most two 296 * of these three indications should be set. 297 */ 298 #define IPTOS_TOS_MASK 0x1E 299 #define IPTOS_LOWDELAY 0x10 300 #define IPTOS_THROUGHPUT 0x08 301 #define IPTOS_RELIABILITY 0x04 302 #define IPTOS_LOWCOST 0x02 303 #define IPTOS_MINCOST IPTOS_LOWCOST 304 305 /* 306 * @brief The Network Control precedence designation is intended to be used 307 * within a network only. The actual use and control of that 308 * designation is up to each network. The Internetwork Control 309 * designation is intended for use by gateway control originators only. 310 * If the actual use of these precedence designations is of concern to 311 * a particular network, it is the responsibility of that network to 312 * control the access to, and use of, those precedence designations. 313 */ 314 #define IPTOS_PREC_MASK 0xe0 315 #define IPTOS_PREC_NETCONTROL 0xe0 316 #define IPTOS_PREC_INTERNETCONTROL 0xc0 317 #define IPTOS_PREC_CRITIC_ECP 0xa0 318 #define IPTOS_PREC_FLASHOVERRIDE 0x80 319 #define IPTOS_PREC_FLASH 0x60 320 #define IPTOS_PREC_IMMEDIATE 0x40 321 #define IPTOS_PREC_PRIORITY 0x20 322 #define IPTOS_PREC_ROUTINE 0x00 323 324 /* 325 * @brief Commands for ioctlsocket(), taken from the BSD file fcntl.h. 326 * lwip_ioctl only supports FIONREAD and FIONBIO, for now 327 * 328 * Ioctl's have the command encoded in the lower word, 329 * and the size of any in or out parameters in the upper 330 * word. The high 2 bits of the upper word are used 331 * to encode the in/out status of the parameter; for now 332 * we restrict parameters to at most 128 bytes. 333 */ 334 #if !defined(FIONREAD) || !defined(FIONBIO) 335 #define IOCPARM_MASK 0x7fU /* parameters must be < 128 bytes */ 336 #define IOC_VOID 0x20000000UL /* no parameters */ 337 #define IOC_OUT 0x40000000UL /* copy out parameters */ 338 #define IOC_IN 0x80000000UL /* copy in parameters */ 339 #define IOC_INOUT (IOC_IN | IOC_OUT) /* 0x20000000 distinguishes new & old ioctl's */ 340 #endif /* !defined(FIONREAD) || !defined(FIONBIO) */ 341 342 #ifndef FIONREAD 343 #define FIONREAD _IOR('f', 127, unsigned long) /* get # bytes to read */ 344 #endif 345 #ifndef FIONBIO 346 #define FIONBIO _IOW('f', 126, unsigned long) /* set/clear non-blocking i/o */ 347 #endif 348 349 /** Socket I/O Controls: unimplemented */ 350 #ifndef SIOCSHIWAT 351 #define SIOCSHIWAT _IOW('s', 0, unsigned long) /* set high watermark */ 352 #define SIOCGHIWAT _IOR('s', 1, unsigned long) /* get high watermark */ 353 #define SIOCSLOWAT _IOW('s', 2, unsigned long) /* set low watermark */ 354 #define SIOCGLOWAT _IOR('s', 3, unsigned long) /* get low watermark */ 355 #define SIOCATMARK _IOR('s', 7, unsigned long) /* at oob mark? */ 356 #endif 357 358 /** commands for fnctl */ 359 #ifndef F_GETFL 360 #define F_GETFL 3 361 #endif 362 #ifndef F_SETFL 363 #define F_SETFL 4 364 #endif 365 366 /** File status flags and file access modes for fnctl, 367 these are bits in an int. */ 368 #ifndef O_NONBLOCK 369 #define O_NONBLOCK 1 /* nonblocking I/O */ 370 #endif 371 #ifndef O_NDELAY 372 #define O_NDELAY 1 /* same as O_NONBLOCK, for compatibility */ 373 #endif 374 375 #ifndef SHUT_RD 376 #define SHUT_RD 0 377 #define SHUT_WR 1 378 #define SHUT_RDWR 2 379 #endif 380 381 /** FD_SET used for lwip_select */ 382 #ifndef FD_SET 383 #undef FD_SETSIZE 384 385 #ifndef LWIP_SOCKET_OFFSET 386 #define LWIP_SOCKET_OFFSET 0 387 #endif 388 389 #ifndef MEMP_NUM_NETCONN 390 #define MEMP_NUM_NETCONN 8 391 #endif 392 393 /** Make FD_SETSIZE match NUM_SOCKETS in socket.c */ 394 #define FD_SETSIZE MEMP_NUM_NETCONN 395 396 typedef struct fd_set { 397 unsigned char fd_bits [(FD_SETSIZE + 7) / 8]; 398 }fd_set; 399 400 #elif LWIP_SOCKET_OFFSET 401 #error LWIP_SOCKET_OFFSET does not work with external FD_SET! 402 #endif /* FD_SET */ 403 404 /** LWIP_TIMEVAL_PRIVATE: if you want to use the struct timeval provided 405 * by your system, set this to 0 and include <sys/time.h> in cc.h */ 406 #ifndef LWIP_TIMEVAL_PRIVATE 407 #define LWIP_TIMEVAL_PRIVATE 0 408 #endif 409 410 #if LWIP_TIMEVAL_PRIVATE 411 struct timeval { 412 long tv_sec; /* seconds */ 413 long tv_usec; /* and microseconds */ 414 }; 415 #endif /* LWIP_TIMEVAL_PRIVATE */ 416 417 u32_t ipaddr_addr(const char *cp); 418 419 #ifdef htonl 420 #undef htonl 421 #endif /* htonl */ 422 #ifdef ntohl 423 #undef ntohl 424 #endif /* ntohl */ 425 #ifdef htons 426 #undef htons 427 #endif /* htons */ 428 #ifdef ntohs 429 #undef ntohs 430 #endif /* ntohs */ 431 432 #if TLS_CONFIG_IPV4 433 /** This is the aligned version of ip4_addr_t, 434 used as local variable, on the stack, etc. */ 435 struct ip4_addr { 436 u32_t addr; 437 }; 438 439 /** ip4_addr_t uses a struct for convenience only, so that the same defines can 440 * operate both on ip4_addr_t as well as on ip4_addr_p_t. */ 441 typedef struct ip4_addr ip4_addr_t; 442 443 #define IP4ADDR_STRLEN_MAX 16 444 445 u32_t ipaddr_addr(const char *cp); 446 int ip4addr_aton(const char *cp, ip4_addr_t *addr); 447 /** returns ptr to static buffer; not reentrant! */ 448 char *ip4addr_ntoa(const ip4_addr_t *addr); 449 char *ip4addr_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen); 450 #endif 451 452 /** @ingroup ipaddr 453 * IP address types for use in ip_addr_t.type member. 454 * @see tcp_new_ip_type(), udp_new_ip_type(), raw_new_ip_type(). 455 */ 456 enum lwip_ip_addr_type { 457 /** IPv4 */ 458 IPADDR_TYPE_V4 = 0U, 459 /** IPv6 */ 460 IPADDR_TYPE_V6 = 6U, 461 /** IPv4+IPv6 ("dual-stack") */ 462 IPADDR_TYPE_ANY = 46U 463 }; 464 465 #if TLS_CONFIG_IPV4&&TLS_CONFIG_IPV6 466 /** 467 * @ingroup ipaddr 468 * A union struct for both IP version's addresses. 469 * ATTENTION: watch out for its size when adding IPv6 address scope! 470 */ 471 typedef struct ip_addr { 472 union { 473 ip6_addr_t ip6; 474 ip4_addr_t ip4; 475 }u_addr; 476 /** @ref lwip_ip_addr_type */ 477 u8_t type; 478 }ip_addr_t; 479 480 int ipaddr_aton(const char *cp, ip_addr_t *addr); 481 482 /** @ingroup ipaddr */ 483 #define IPADDR_STRLEN_MAX IP6ADDR_STRLEN_MAX 484 485 #else 486 #if TLS_CONFIG_IPV4 487 typedef ip4_addr_t ip_addr_t; 488 489 #define IPADDR_STRLEN_MAX IP4ADDR_STRLEN_MAX 490 491 #else 492 typedef ip6_addr_t ip_addr_t; 493 494 #define IPADDR_STRLEN_MAX IP6ADDR_STRLEN_MAX 495 496 #endif 497 #endif 498 499 #endif 500