1 /** 2 * @file hi_net_api.h 3 * 4 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /** 19 * @defgroup hi_net Lwip API 20 */ 21 /** 22 * @defgroup hi_net_basic Lwip Basic Interface 23 * @ingroup hi_net 24 */ 25 26 #ifndef __HI_NET_API_H__ 27 #define __HI_NET_API_H__ 28 29 #include <stdint.h> 30 #include <stddef.h> 31 #include <errno.h> 32 33 #ifdef __cplusplus 34 extern "C" 35 { 36 #endif 37 38 typedef unsigned char u8_t; 39 typedef unsigned short u16_t; 40 typedef unsigned int u32_t; 41 typedef int err_t; 42 43 #ifndef LWIP_TIMEVAL_PRIVATE 44 #define LWIP_TIMEVAL_PRIVATE 0 45 #endif 46 47 /** 48 * @ingroup hi_net_basic 49 * 50 * max length of INET ADDRESS.CNcomment:INET最大的字符串长度。CNend 51 */ 52 #define INET_ADDRSTRLEN 16 53 54 /** 55 * @ingroup hi_net_basic 56 * 57 * max num of FD.CNcomment:最大的描述符个数。CNend 58 */ 59 #define FD_SETSIZE_MAX 1024 60 61 #define EPERM 1 /* Operation not permitted */ 62 #define ENOENT 2 /* No such file or directory */ 63 #define ESRCH 3 /* No such process */ 64 #define EINTR 4 /* Interrupted system call */ 65 #define EIO 5 /* I/O error */ 66 #define ENXIO 6 /* No such device or address */ 67 #define E2BIG 7 /* Arg list too long */ 68 #define ENOEXEC 8 /* Exec format error */ 69 #define EBADF 9 /* Bad file number */ 70 #define ECHILD 10 /* No child processes */ 71 #define EAGAIN 11 /* Try again */ 72 #define ENOMEM 12 /* Out of memory */ 73 #define EACCES 13 /* Permission denied */ 74 #define EFAULT 14 /* Bad address */ 75 #define ENOTBLK 15 /* Block device required */ 76 #define EBUSY 16 /* Device or resource busy */ 77 #define EEXIST 17 /* File exists */ 78 #define EXDEV 18 /* Cross-device link */ 79 #define ENODEV 19 /* No such device */ 80 #define ENOTDIR 20 /* Not a directory */ 81 #define EISDIR 21 /* Is a directory */ 82 #define ENFILE 23 /* File table overflow */ 83 #define EMFILE 24 /* Too many open files */ 84 #define ENOTTY 25 /* Not a typewriter */ 85 #define ETXTBSY 26 /* Text file busy */ 86 #define EFBIG 27 /* File too large */ 87 #define ENOSPC 28 /* No space left on device */ 88 #define ESPIPE 29 /* Illegal seek */ 89 #define EROFS 30 /* Read-only file system */ 90 #define EMLINK 31 /* Too many links */ 91 #define EPIPE 32 /* Broken pipe */ 92 #define EDOM 33 /* Math argument out of domain of func */ 93 #define EDEADLK 35 /* Resource deadlock would occur */ 94 #define ENAMETOOLONG 36 /* File name too long */ 95 #define ENOLCK 37 /* No record locks available */ 96 #define ENOSYS 38 /* Function not implemented */ 97 #define ENOTEMPTY 39 /* Directory not empty */ 98 #define ELOOP 40 /* Too many symbolic links encountered */ 99 #define EWOULDBLOCK EAGAIN /* Operation would block */ 100 #define ENOMSG 42 /* No message of desired type */ 101 #define EIDRM 43 /* Identifier removed */ 102 #define ECHRNG 44 /* Channel number out of range */ 103 #define EL2NSYNC 45 /* Level 2 not synchronized */ 104 #define EL3HLT 46 /* Level 3 halted */ 105 #define EL3RST 47 /* Level 3 reset */ 106 #define ELNRNG 48 /* Link number out of range */ 107 #define EUNATCH 49 /* Protocol driver not attached */ 108 #define ENOCSI 50 /* No CSI structure available */ 109 #define EL2HLT 51 /* Level 2 halted */ 110 #define EBADE 52 /* Invalid exchange */ 111 #define EBADR 53 /* Invalid request descriptor */ 112 #define EXFULL 54 /* Exchange full */ 113 #define ENOANO 55 /* No anode */ 114 #define EBADRQC 56 /* Invalid request code */ 115 #define EBADSLT 57 /* Invalid slot */ 116 #define EDEADLOCK EDEADLK 117 #define EBFONT 59 /* Bad font file format */ 118 #define ENOSTR 60 /* Device not a stream */ 119 #define ENODATA 61 /* No data available */ 120 #define ETIME 62 /* Timer expired */ 121 #define ENOSR 63 /* Out of streams resources */ 122 #define ENONET 64 /* Machine is not on the network */ 123 #define ENOPKG 65 /* Package not installed */ 124 #define EREMOTE 66 /* Object is remote */ 125 #define ENOLINK 67 /* Link has been severed */ 126 #define EADV 68 /* Advertise error */ 127 #define ESRMNT 69 /* Srmount error */ 128 #define ECOMM 70 /* Communication error on send */ 129 #define EPROTO 71 /* Protocol error */ 130 #define EMULTIHOP 72 /* Multihop attempted */ 131 #define EDOTDOT 73 /* RFS specific error */ 132 #define EBADMSG 74 /* Not a data message */ 133 #define EOVERFLOW 75 /* Value too large for defined data type */ 134 #define ENOTUNIQ 76 /* Name not unique on network */ 135 #define EBADFD 77 /* File descriptor in bad state */ 136 #define EREMCHG 78 /* Remote address changed */ 137 #define ELIBACC 79 /* Can not access a needed shared library */ 138 #define ELIBBAD 80 /* Accessing a corrupted shared library */ 139 #define ELIBSCN 81 /* .lib section in a.out corrupted */ 140 #define ELIBMAX 82 /* Attempting to link in too many shared libraries */ 141 #define ELIBEXEC 83 /* Cannot exec a shared library directly */ 142 #define EILSEQ 84 /* Illegal byte sequence */ 143 #define ERESTART 85 /* Interrupted system call should be restarted */ 144 #define ESTRPIPE 86 /* Streams pipe error */ 145 #define EUSERS 87 /* Too many users */ 146 #define ENOTSOCK 88 /* Socket operation on non-socket */ 147 #define EDESTADDRREQ 89 /* Destination address required */ 148 #define EMSGSIZE 90 /* Message too long */ 149 #define EPROTOTYPE 91 /* Protocol wrong type for socket */ 150 #define ENOPROTOOPT 92 /* Protocol not available */ 151 #define EPROTONOSUPPORT 93 /* Protocol not supported */ 152 #define ESOCKTNOSUPPORT 94 /* Socket type not supported */ 153 #define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */ 154 #define EPFNOSUPPORT 96 /* Protocol family not supported */ 155 #define EAFNOSUPPORT 97 /* Address family not supported by protocol */ 156 #define EADDRINUSE 98 /* Address already in use */ 157 #define EADDRNOTAVAIL 99 /* Cannot assign requested address */ 158 #define ENETDOWN 100 /* Network is down */ 159 #define ENETUNREACH 101 /* Network is unreachable */ 160 #define ENETRESET 102 /* Network dropped connection because of reset */ 161 #define ECONNABORTED 103 /* Software caused connection abort */ 162 #define ECONNRESET 104 /* Connection reset by peer */ 163 #define ENOBUFS 105 /* No buffer space available */ 164 #define EISCONN 106 /* Transport endpoint is already connected */ 165 #define ENOTCONN 107 /* Transport endpoint is not connected */ 166 #define ESHUTDOWN 108 /* Cannot send after transport endpoint shutdown */ 167 #define ETOOMANYREFS 109 /* Too many references: cannot splice */ 168 #define ETIMEDOUT 110 /* Connection timed out */ 169 #define ECONNREFUSED 111 /* Connection refused */ 170 #define EHOSTDOWN 112 /* Host is down */ 171 #define EHOSTUNREACH 113 /* No route to host */ 172 #define EALREADY 114 /* Operation already in progress */ 173 #define EINPROGRESS 115 /* Operation now in progress */ 174 #define ESTALE 116 /* Stale NFS file handle */ 175 #define EUCLEAN 117 /* Structure needs cleaning */ 176 #define ENOTNAM 118 /* Not a XENIX named type file */ 177 #define ENAVAIL 119 /* No XENIX semaphores available */ 178 #define EISNAM 120 /* Is a named type file */ 179 #define EREMOTEIO 121 /* Remote I/O error */ 180 #define EDQUOT 122 /* Quota exceeded */ 181 #define ENOMEDIUM 123 /* No medium found */ 182 #define EMEDIUMTYPE 124 /* Wrong medium type */ 183 184 #define SOCK_STREAM 1 185 #define SOCK_DGRAM 2 186 #define SOCK_RAW 3 187 188 #define AF_UNSPEC 0 189 #define AF_INET 2 190 #define AF_INET6 AF_UNSPEC 191 #define PF_INET AF_INET 192 #define PF_INET6 AF_INET6 193 #define PF_UNSPEC AF_UNSPEC 194 195 #define IPPROTO_IP 0 196 #define IPPROTO_TCP 6 197 #define IPPROTO_UDP 17 198 #define IPPROTO_UDPLITE 136 199 200 #define MSG_PEEK 0x02 201 #define MSG_WAITALL 0x100 202 #define MSG_OOB 0x01 203 #define MSG_DONTWAIT 0x40 204 #define MSG_MORE 0x8000 205 206 #define SOL_SOCKET 1 207 #define SO_REUSEADDR 2 208 #define SO_TYPE 3 209 #define SO_ERROR 4 210 #define SO_BROADCAST 6 211 #define SO_SNDBUF 7 212 #define SO_RCVBUF 8 213 #define SO_KEEPALIVE 9 214 #define SO_NO_CHECK 11 215 #define SO_SNDLOWAT 19 216 #define SO_RCVLOWAT 18 217 #define SO_SNDTIMEO 67 218 #define SO_RCVTIMEO 66 219 #define SO_CONTIMEO 0x1009 220 221 222 #ifndef O_NONBLOCK 223 #define O_NONBLOCK 00004000 224 #endif 225 226 #ifndef O_NDELAY 227 #define O_NDELAY O_NONBLOCK 228 #endif 229 230 #ifndef F_GETFL 231 #define F_GETFL 3 232 #endif 233 #ifndef F_SETFL 234 #define F_SETFL 4 235 #endif 236 237 #define IP_MULTICAST_TTL 5 238 #define IP_MULTICAST_IF 6 239 #define IP_MULTICAST_LOOP 7 240 #define IP_ADD_MEMBERSHIP 35 241 #define IP_DROP_MEMBERSHIP 36 242 243 #define IPADDR_NONE ((u32_t)0xffffffffUL) 244 #define IPADDR_ANY ((u32_t)0x00000000UL) 245 #define INADDR_ANY IPADDR_ANY 246 247 #define ip4_addr1(ipaddr) (((u8_t*)(ipaddr))[0]) 248 #define ip4_addr2(ipaddr) (((u8_t*)(ipaddr))[1]) 249 #define ip4_addr3(ipaddr) (((u8_t*)(ipaddr))[2]) 250 #define ip4_addr4(ipaddr) (((u8_t*)(ipaddr))[3]) 251 252 #define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr)) 253 #define ip4_addr2_16(ipaddr) ((u16_t)ip4_addr2(ipaddr)) 254 #define ip4_addr3_16(ipaddr) ((u16_t)ip4_addr3(ipaddr)) 255 #define ip4_addr4_16(ipaddr) ((u16_t)ip4_addr4(ipaddr)) 256 257 /** 258 * @ingroup hi_net_basic 259 * 260 * host Host byte order converted to network byte order.CNcomment:主机序转为网络序。CNend 261 */ 262 #define LWIP_PLATFORM_HTONS(_n) ((u16_t)((((_n) & 0xff) << 8) | (((_n) >> 8) & 0xff))) 263 264 /** 265 * @ingroup hi_net_basic 266 * 267 * network bytehost order converted to Host byte order.CNcomment:网络序转为主机序。CNend 268 */ 269 #define LWIP_PLATFORM_HTONL(_n) ((u32_t)((((_n) & 0xff) << 24) | (((_n) & 0xff00) << 8) | \ 270 (((_n) >> 8) & 0xff00) | (((_n) >> 24) & 0xff))) 271 272 /** 273 * @ingroup hi_net_basic 274 * 275 * set ip addr for four address.CNcomment:根据ip四段地址设置相应的值。CNend 276 */ 277 #define IP4_ADDR(ipaddr, a, b, c, d) \ 278 (ipaddr)->addr = ((u32_t)((d) & 0xff) << 24) | \ 279 ((u32_t)((c) & 0xff) << 16) | \ 280 ((u32_t)((b) & 0xff) << 8) | \ 281 (u32_t)((a) & 0xff) 282 283 /***************************************************************************** 284 * 8、LWIP协议栈bsd socket htons/ntohs/ntohl转换 285 *****************************************************************************/ 286 #define htons(x) lwip_htons(x) 287 #define ntohs(x) lwip_ntohs(x) 288 #define htonl(x) lwip_htonl(x) 289 #define ntohl(x) lwip_ntohl(x) 290 291 #define lwip_htons(x) LWIP_PLATFORM_HTONS(x) 292 #define lwip_ntohs(x) LWIP_PLATFORM_HTONS(x) 293 #define lwip_htonl(x) LWIP_PLATFORM_HTONL(x) 294 #define lwip_ntohl(x) LWIP_PLATFORM_HTONL(x) 295 296 /** 297 * @ingroup hi_net_basic 298 * @brief Point to decimal.CNcomment:ip地址点分制转为十进制CNend 299 * 300 * @par Description: 301 * Point to decimal.CNcomment:ip地址点分制转为十进制CNend 302 * 303 * @attention cp cannot be empty. CNcomment:传入字符串不能为空CNend 304 * @param the ip addr in string CNcomment:字符串ip地址CNend 305 * @retval #IPADDR Execute successfully. 306 * @retval #IPADDR_NONE Execute failed. 307 * @par NA: 308 * NA 309 * @see NULL 310 311 */ 312 u32_t ipaddr_addr(const char *cp); 313 #define inet_addr(cp) ipaddr_addr(cp) 314 315 #if !defined(in_addr_t) && !defined(IN_ADDR_T_DEFINED) 316 #ifndef _IN_ADDR_T_DECLARED 317 typedef u32_t in_addr_t; 318 #define _IN_ADDR_T_DECLARED 319 #endif 320 #endif 321 322 #if !defined(sa_family_t) && !defined(SA_FAMILY_T_DEFINED) 323 typedef u16_t sa_family_t; 324 #endif 325 326 #if !defined(in_port_t) && !defined(IN_PORT_T_DEFINED) 327 typedef u16_t in_port_t; 328 #endif 329 330 #if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED) 331 typedef u32_t socklen_t; 332 #endif 333 334 /** 335 * @ingroup hi_net_basic 336 * 337 * ip address in decimal.CNcomment:十进制ip地址CNend 338 * 339 */ 340 struct ip_addr { 341 u32_t addr; 342 }; 343 344 /** 345 * @ingroup hi_net_basic 346 * 347 * Package structure ip_addr_t to in_addr.CNcomment:封装结构体in_addr_t到in_addrCNend 348 * 349 */ 350 struct in_addr { 351 in_addr_t s_addr; 352 }; 353 354 /** 355 * @ingroup hi_net_basic 356 * 357 * Address form of socket in Internet Environment.CNcomment:internet环境下套接字的地址形式CNend 358 * 359 */ 360 struct sockaddr_in { 361 sa_family_t sin_family; 362 in_port_t sin_port; 363 struct in_addr sin_addr; 364 #define SIN_ZERO_LEN 8 365 char sin_zero[SIN_ZERO_LEN]; 366 }; 367 368 /** 369 * @ingroup hi_net_basic 370 * 371 * General socket address.CNcomment:通用的套接字地址CNend 372 * 373 */ 374 struct sockaddr { 375 sa_family_t sa_family; 376 #define SA_DATA_LEN 14 377 char sa_data[SA_DATA_LEN]; 378 }; 379 380 /** 381 * @ingroup hi_net_basic 382 * 383 * Multicast address and interface.CNcomment:多播地址和接口CNend 384 * 385 */ 386 typedef struct ip_mreq { 387 struct in_addr imr_multiaddr; 388 struct in_addr imr_interface; 389 } ip_mreq; 390 391 /** 392 * @ingroup hi_net_basic 393 * 394 * IPv4 address.CNcomment:ipv4的地址CNend 395 * 396 */ 397 struct ip4_addr { 398 u32_t addr; 399 }; 400 typedef struct ip4_addr ip4_addr_t; 401 typedef ip4_addr_t ip_addr_t; 402 403 /** 404 * @ingroup hi_net_basic 405 * @brief Network byte order IP address converted to dotted decimal format. 406 * CNcomment:网络字节序的IP地址转化为点分十进制格式CNend 407 * 408 * @par Description: 409 * Address translation.CNcomment:地址转换CNend 410 * 411 * @attention NULL 412 * @param cp [IN] Type #ip4_addr_t *, Network byte order IP address.CNcomment:点分十进制格式的IP地址CNend 413 * @param addr [IN] Type #const char *, length of device name.CNcomment:点分十进制ip地址CNend 414 * 415 * @retval #1 Excute successfully 416 * @retval #0 Excute failure 417 * @par NULL 418 * 419 * @see NULL 420 421 */ 422 int ip4addr_aton(const char *cp, ip4_addr_t *addr); 423 #define inet_aton(cp, addr) ip4addr_aton(cp, (ip4_addr_t*)addr) 424 425 /** 426 * @ingroup hi_net_basic 427 * @brief Dotted decimal format IP address converted to Network byte order. 428 * CNcomment:点分十进制的IP地址转化为网络字节序格式CNend 429 * 430 * @par Description: 431 * Address translation.CNcomment:地址转换CNend 432 * 433 * @attention NULL 434 * @param addr [IN] Type #ip4_addr_t *, addr ip address in network order to convert.CNcomment:网络字节序ip地址CNend 435 * 436 * @retval #Not NULL Excute successfully 437 * @retval #NULL Excute failure 438 * @par NULL 439 * 440 * @see NULL 441 442 */ 443 char *ip4addr_ntoa(const ip4_addr_t *addr); 444 #define inet_ntoa(addr) ip4addr_ntoa((ip4_addr_t*)&(addr)) 445 446 447 typedef void (*dns_found_callback)(const char *name, ip_addr_t *ipaddr, void *callback_arg); 448 err_t dns_gethostbyname(const char *hostname, ip_addr_t *addr, 449 dns_found_callback found, void *callback_arg); 450 451 /** 452 * @ingroup hi_net_basic 453 * 454 * Domain name and network address structure.CNcomment:域名和网络地址结构体CNend 455 * 456 */ 457 struct hostent { 458 char *h_name; /**< Indicates the official name of the host. */ 459 char **h_aliases; /**< Indicates a pointer to an array of pointers to alternative host names, 460 terminated by a null pointer. */ 461 int h_addrtype; /**< Indicates the address type. */ 462 int h_length; /**< Indicates the length, in bytes, of the address. */ 463 char **h_addr_list; /**< Indicates a pointer to an array of pointers to network addresses (in 464 network byte order) for the host, terminated by a null pointer. */ 465 #define h_addr h_addr_list[0] /* for backward compatibility */ 466 }; 467 468 /** 469 * @ingroup hi_net_basic 470 * @brief Get IP address according to domain name.CNcomment:根据域名获取IP地址CNend 471 * 472 * @par Description: 473 * The IP address is obtained by using the domain name in string format, 474 * and the address information is loaded into the host domain name structure 475 * CNcomment:利用字符串格式的域名获得IP地址,并且将地址信息装入hostent域名结构体CNend 476 * 477 * @attention NULL 478 * @param name [IN] Type #const char * the hostname to resolve.CNcomment:解析的域名CNend 479 * 480 * @retval #hostent Execute successfully. 481 * @retval #NULL Execute failed. 482 * @par Dependency: 483 * #NULL 484 * @see NULL 485 */ 486 struct hostent *gethostbyname(const char *name); 487 488 /** 489 * @ingroup hi_net_basic 490 * 491 * Network interface structure.CNcomment:网络接口结构体CNend 492 * 493 */ 494 struct netif { 495 #define NETIF_DATA_LEN 8 496 unsigned char data[NETIF_DATA_LEN]; 497 }; 498 499 /** 500 * @ingroup hi_net_basic 501 * @brief Get the corresponding interface pointer according to the interface name. 502 * CNcomment:根据接口名字获取相应接口指针CNend 503 * 504 * @par Description: 505 * Get the corresponding interface pointer according to the interface name 506 * CNcomment:根据接口名字获取相应接口指针CNend 507 * 508 * @attention NULL 509 * @param name [IN] Type #const char * the interface name to find.CNcomment:要找的接口名字CNend 510 * 511 * @retval #struct netif * Execute successfully. 512 * @retval #NULL Execute failed. 513 * @par Dependency: 514 * #NULL 515 * @see NULL 516 */ 517 struct netif *netif_find(const char *name); 518 519 /** 520 * @ingroup hi_net_basic 521 * @brief Start DHCP client according to interface.CNcomment:根据接口启动dhcp客户端CNend 522 * 523 * @par Description: 524 * Start DHCP client according to interface 525 * CNcomment:根据接口启动dhcp客户端CNend 526 * 527 * @attention NULL 528 * @param netif [IN] Type #struct netif * Interface address.CNcomment:接口地址CNend 529 * 530 * @retval #ERR_OK Execute successfully. 531 * @retval #OTHERS Execute failed. 532 * @par Dependency: 533 * #NULL 534 * @see NULL 535 */ 536 err_t netifapi_dhcp_start(struct netif *netif); 537 538 /** 539 * @ingroup hi_net_basic 540 * @brief Start DHCP server according to interface.CNcomment:根据接口启动dhcp服务端CNend 541 * 542 * @par Description: 543 * Start DHCP server according to interface 544 * CNcomment:根据接口启动dhcp服务端CNend 545 * 546 * @attention NULL 547 * @param netif [IN] Type #struct netif * Interface address.CNcomment:接口地址CNend 548 * @param start_ip [IN] Type #char * Assigned client start address.CNcomment:分配的客户端起始地址CNend 549 * @param ip_num [IN] Type #u16_t Total number of clients assigned.CNcomment:分配的客户端总数目CNend 550 * @retval #ERR_OK Execute successfully. 551 * @retval #OTHERS Execute failed. 552 * @par Dependency: 553 * #NULL 554 * @see NULL 555 556 */ 557 err_t netifapi_dhcps_start(struct netif *netif, char *start_ip, u16_t ip_num); 558 559 /** 560 * @ingroup hi_net_basic 561 * @brief This API is used to set the the vendor class identifier information 562 * of the netif, which is using in DHCP Message. 563 * CNcomment:这个API用来设置dhcp的hostname消息CNend 564 * 565 * @par Description: 566 * The hostname string lenght should be less than NETIF_HOSTNAME_MAX_LEN, 567 * otherwise the hostname will truncate to (NETIF_HOSTNAME_MAX_LEN-1). 568 * CNcomment:hostname的长度要小于NETIF_HOSTNAME_MAX_LEN,否则会被设置为(NETIF_HOSTNAME_MAX_LEN-1)CNend 569 * 570 * @attention NULL 571 * @param netif [IN] Type #struct netif * Interface address.CNcomment:接口地址CNend 572 * @param hostname [IN] Type #char * hostname The new hostname to use.CNcomment:传入使用的名字CNend 573 * @param namelen [IN] Type #u8_t The hostname string length.CNcomment:名称长度CNend 574 * @retval #ERR_OK Execute successfully. 575 * @retval #ERR_ARG: On passing invalid arguments. 576 * @par Dependency: 577 * #NULL 578 * @see NULL 579 */ 580 err_t netifapi_set_hostname(struct netif *netif, char *hostname, u8_t namelen); 581 582 583 /** 584 * @ingroup hi_net_basic 585 * @brief This API is used to set the vendor class identifier information, which is used in DHCP message. 586 * CNcomment:设置dhcp消息的vci信息CNend 587 * 588 * @par Description: 589 * Length of vendor class identifier information string ,should be not more than DHCP_VCI_MAX_LEN(default 32), 590 * otherwise it will return with ERR_ARG. vci_len is the real length of vendor class identifier information string. 591 * CNcomment:vci消息长度不超过32个字节,否则会返回ERR_ARG,vci_len是厂商分类信息的真实长度CNend 592 * 593 * @attention NULL 594 * @param vci [IN] Type #char * The new vendor class identifier information to use.CNcomment:厂商设备信息数据CNend 595 * @param vci_len [IN] Type #u8_t The length of vendor class identifier information string.CNcomment:上述数据的长度CNend 596 * @retval #ERR_OK On success 597 * @retval #ERR_ARG On passing invalid arguments 598 * @retval #ERR_VAL On failure 599 * @par Dependency: 600 * #NULL 601 * @see NULL 602 */ 603 err_t netifapi_set_vci(char *vci, u8_t vci_len); 604 605 /** 606 * @ingroup hi_net_basic 607 * @brief allocate a socket.CNcomment:分配套接字CNend 608 * 609 * @par Description: 610 * It creates an endpoint for communication and returns a file descriptor 611 * CNcomment:为通信创建一个端点并返回一个文件描述符CNend 612 * 613 * @attention NULL 614 * @param domain [IN] Type #int Specifies a protocol family.CNcomment:指定协议族CNend 615 * @param type [IN] Type #int Specifies the socket type.CNcomment:指定协议类型CNend 616 * @param protocol [IN] Type #int Specifies the protocol to be used with the socket. 617 * CNcomment:指定要与套接字一起使用的协议CNend 618 * @retval #>0 Execute successfully. 619 * @retval #-1 Execute failed. 620 * @par Dependency: 621 * #NULL 622 * @see NULL 623 */ 624 int socket(int domain, int type, int protocol); 625 626 /** 627 * @ingroup hi_net_basic 628 * @brief bind a socket.CNcomment:绑定套接字CNend 629 * 630 * @par Description: 631 * It creates an endpoint for communication and returns a file descriptor 632 * CNcomment:为通信创建一个端点并返回一个文件描述符CNend 633 * 634 * @attention NULL 635 * @param s [IN] Type #int Specifies the file descriptor of the socket to be bound.CNcomment:要绑定的描述符CNend 636 * @param name [IN] Type #struct sockaddr * Points to a sockaddr structure containing the address 637 * to be bound to the socket. CNcomment:指向包含要绑定到套接字的地址的sockaddr结构CNend 638 * @param namelen [IN] Type #socklen_t Specifies the length of the sockaddr structure pointed to by the address argument. 639 * CNcomment:指定address参数指向的sockaddr结构的长度CNend 640 * @retval #0 Execute successfully. 641 * @retval #-1 Execute failed. 642 * @par Dependency: 643 * #NULL 644 * @see NULL 645 */ 646 int bind(int s, const struct sockaddr *name, socklen_t namelen); 647 648 /** 649 * @ingroup hi_net_basic 650 * @brief Get socket optional fields.CNcomment:获取套接字可选字段CNend 651 * 652 * @par Description: 653 * Get socket optional fields 654 * CNcomment:获取套接字可选字段CNend 655 * 656 * @attention NULL 657 * @param socket [IN] Type #int Specifies a socket file descriptor.CNcomment:指定的文件描述符CNend 658 * @param level [IN] Type #int Specifies the protocol level at which the option resides. 659 * CNcomment:指定选项所在的协议级别CNend 660 * @param option_name [IN] Type #int Specifies a single option to set.CNcomment:指定选项的名字CNend 661 * @param option_value [OUT] Type #void * Indicates the pointer to the option value.CNcomment:指示指向选项值的指针CNend 662 * @param option_len [IN] Type #socklen_t * Specifies the size of option value.CNcomment:指定选项的值CNend 663 * @retval #0 Execute successfully. 664 * @retval #-1 Execute failed. 665 * @par Dependency: 666 * #NULL 667 * @see NULL 668 */ 669 int getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen); 670 671 /** 672 * @ingroup hi_net_basic 673 * @brief Set socket optional fields.CNcomment:设置套接字可选字段CNend 674 * 675 * @par Description: 676 * Set socket optional fields 677 * CNcomment:设置套接字可选字段CNend 678 * 679 * @attention NULL 680 * @param socket [IN] Type #int Specifies a socket file descriptor.CNcomment:指定的文件描述符CNend 681 * @param level [IN] Type #int Specifies the protocol level at which the option resides. 682 * CNcomment:指定选项所在的协议级别CNend 683 * @param option_name [IN] Type #int Specifies a single option to set.CNcomment:指定选项的名字CNend 684 * @param option_value [OUT] Type #void * Indicates the pointer to the option value.CNcomment:指示指向选项值的指针CNend 685 * @param option_len [IN] Type #socklen_t Specifies the size of option value.CNcomment:指定选项的值CNend 686 * @retval #0 Execute successfully. 687 * @retval #-1 Execute failed. 688 * @par Dependency: 689 * #NULL 690 * @see NULL 691 */ 692 int setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen); 693 694 /** 695 * @ingroup hi_net_basic 696 * @brief Accepts a new connection on a socket.CNcomment:套接字上接收一个连接CNend 697 * 698 * @par Description: 699 * Accepts a new connection on a socket 700 * CNcomment:套接字上接收一个连接CNend 701 * 702 * @attention NULL 703 * @param socket [IN] Type #int Specifies a socket that was created with socket(),has been bound to an address 704 * with bind(),and has issued a successful call to listen(). 705 * CNcomment:指定用socket()创建的套接字,该套接字已bind()绑定,并已成功调用listen()CNend 706 * @param address [OUT] Type #struct sockaddr * Indicates either a null pointer, or a pointer to a sockaddr structure 707 * where the address of the connecting socket shall be returned. 708 * CNcomment:指示空指针或指向sockaddr结构的指针,其中应返回连接套接字的地址CNend 709 * @param address_len [IN,OUT] Type #socklen_t * Indicates either a null pointer,if address is a null pointer, 710 * or a pointer to a socklen_t object which on input 711 * specifies the length of the supplied sockaddr structure,and on output specifies 712 * the length of the stored address. 713 * CNcomment:如果地址是空指针,则指示空指针,或者指示指向socklen_t对象的指针, 714 * 该对象在输入时指定提供的sockaddr结构的长度,在输出时指定存储地址的长度.CNend 715 * @retval #>0 Execute successfully. 716 * @retval #-1 Execute failed. 717 * @par Dependency: 718 * #NULL 719 * @see NULL 720 */ 721 int accept(int s, struct sockaddr *addr, socklen_t *addrlen); 722 723 /** 724 * @ingroup hi_net_basic 725 * @brief Connection to peer IP address.CNcomment:连接对端IP地址CNend 726 * 727 * @par Description: 728 * attempt to make a connection on a connection-mode socket or to set or 729 * reset the peer address of a connectionless-mode socket 730 * CNcomment:尝试在连接模式套接字上建立连接,或设置或重置无连接模式套接字的对等地址CNend 731 * 732 * @attention NULL 733 * @param s [IN] Type #int Specifies a socket file descriptor.CNcomment:指定套接字CNend 734 * @param name [IN] Type #struct sockaddr * Specifies a pointer to the sockaddr structure 735 * which identifies the connection. CNcomment:指定指向sockaddr结构的指针,该结构标识连接CNend 736 * @param namelen [IN] Type # socklen_t Specifies the size of name structure. 737 * CNcomment:指定名字结构体的长度CNend 738 * @retval #0 Execute successfully. 739 * @retval #-1 Execute failed. 740 * @par Dependency: 741 * #NULL 742 * @see NULL 743 */ 744 int connect(int s, const struct sockaddr *name, socklen_t namelen); 745 746 /** 747 * @ingroup hi_net_basic 748 * @brief Recieve a message from connected socket.CNcomment:从已经连接的套接字接收消息CNend 749 * 750 * @par Description: 751 * Recieve a message from connected socket. 752 * CNcomment:从已经连接的套接字接收消息CNend 753 * 754 * @attention NULL 755 * @param socket [IN] Type #int Specifies the socket file descriptor.CNcomment:指定套接字CNend 756 * @param buffer [OUT] Type #void * Points to a buffer where the message should be stored. 757 * CNcomment:接收存储的缓存CNend 758 * @param length [IN] Type #size_t Specifies the length in bytes of the buffer pointed to by the buffer argument. 759 * CNcomment:每次接收的长度CNend 760 * @param flags [IN] Type #int Specifies the type of message reception.CNcomment:指定套接字的标志位CNend 761 * @retval #>0 Execute successfully. 762 * @retval #-1 Execute failed. 763 * @par Dependency: 764 * #NULL 765 * @see NULL 766 */ 767 int recv(int s, void *mem, size_t len, int flags); 768 769 /** 770 * @ingroup hi_net_basic 771 * @brief Recieve a message from connected socket.CNcomment:从已经连接的套接字接收消息CNend 772 * 773 * @par Description: 774 * Recieve a message from connected socket. 775 * CNcomment:从已经连接的套接字接收消息CNend 776 * 777 * @attention NULL 778 * @param socket [IN] Type #int Specifies the socket file descriptor.CNcomment:指定套接字CNend 779 * @param buffer [OUT] Type #void * Points to a buffer where the message should be stored. 780 * CNcomment:接收存储的缓存CNend 781 * @param length [IN] Type #size_t Specifies the length in bytes of the buffer pointed to 782 * by the buffer argument.CNcomment:每次接收的长度CNend 783 * @param flags [IN] Type #int Specifies the type of message reception.CNcomment:指定套接字的标志位CNend 784 * @param flags [IN] Type #struct sockaddr * A null pointer, or points to a sockaddr structure in which 785 * the sending address is to be stored. 786 * CNcomment:空指针,或指向要存储发送地址的sockaddr结构CNend 787 * @param flags [IN] Type #socklen_t * Either a null pointer, if address is a null pointer, or a pointer 788 * to a socklen_t objectwhich on input specifies the length of the supplied sockaddr structure, 789 * and on output specifies the length of the stored address. 790 * CNcomment:如果地址是空指针,则为空指针,或者指向socklen_t对象的指针, 791 * 该对象在输入时指定提供的sockaddr结构的长度,在输出时指定存储地址的长度CNend 792 * @retval #>0 Execute successfully. 793 * @retval #-1 Execute failed. 794 * @par Dependency: 795 * #NULL 796 * @see NULL 797 */ 798 int recvfrom(int s, void *mem, size_t len, int flags, 799 struct sockaddr *from, socklen_t *fromlen); 800 801 /** 802 * @ingroup hi_net_basic 803 * @brief Initiates transmission of a message from the specified socket to its peer.CNcomment:传输指定长度消息到对端CNend 804 * 805 * @par Description: 806 * Initiates transmission of a message from the specified socket to its peer 807 * CNcomment:启动从指定套接字到其对等端的消息传输。CNend 808 * 809 * @attention NULL 810 * @param socket [IN] Type #int Specifies the socket file descriptor.CNcomment:指定套接字CNend 811 * @param buffer [IN] Type #void * Specifies a buffer containing the message to send. 812 * CNcomment:指定要发送的缓存CNend 813 * @param length [IN] Type #size_t Specifies the length of the message to send.CNcomment:指定消息长度CNend 814 * @param flags [IN] Type #int Specifies the type of message reception.CNcomment:指定套接字的标志位CNend 815 * @retval #>0 Execute successfully. 816 * @retval #-1 Execute failed. 817 * @par Dependency: 818 * #NULL 819 * @see NULL 820 */ 821 int send(int s, const void *dataptr, size_t size, int flags); 822 823 /** 824 * @ingroup hi_net_basic 825 * @brief send messages from a connection-oriented and connectionless sockets. 826 * CNcomment:从面向连接和无连接的套接字发送消息CNend 827 * 828 * @par Description: 829 * If the socket is in the connectionless mode, the message is sent to the address specified by the 'to' parameter. 830 * 831 * CNcomment:如果套接字处于无连接模式,则将消息发送到“to”参数指定的地址 832 * 如果套接字处于连接模式,则忽略“to”参数中的目标地址CNend 833 * @attention NULL 834 * @param socket [IN] Type #int Specifies the socket file descriptor.CNcomment:指定套接字CNend 835 * @param buffer [IN] Type #void * Specifies a buffer containing the message to send. 836 * CNcomment:指定要发送的缓存CNend 837 * @param length [IN] Type #size_t Specifies the length of the message to send.CNcomment:指定消息长度CNend 838 * @param flags [IN] Type #int Specifies the type of message reception.CNcomment:指定套接字的标志位CNend 839 * @param flags [IN] Type #struct sockaddr * Specifies a pointer to the sockaddr structure 840 * that contains the destination address. 841 * CNcomment:指定指向包含目标地址的sockaddr结构的指针CNend 842 * @param flags [IN] Type #socklen_t * Specifies the size of the 'to' structure. 843 * CNcomment:指定“to”结构的大小CNend 844 * @retval #>0 Execute successfully. 845 * @retval #-1 Execute failed. 846 * @par Dependency: 847 * #NULL 848 * @see NULL 849 */ 850 int sendto(int s, const void *dataptr, size_t size, int flags, 851 const struct sockaddr *to, socklen_t tolen); 852 853 /** 854 * @ingroup hi_net_basic 855 * @brief Allows a program to monitor multiple file descriptors.CNcomment:允许程序监视多个文件描述符CNend 856 * 857 * @par Description: 858 * select() can monitor only file descriptors numbers that are less than FD_SETSIZE. 859 * select() uses a timeout that is a struct timeval (with seconds and microseconds). 860 * CNcomment:select()只能监视小于FD_SETSIZE的文件描述符编号. 861 * select()使用的超时值是struct timeval(秒和微秒)CNend 862 * @attention NULL 863 * @param nfds [IN] Type #int Specifies a range of file descriptors.CNcomment:允许程序监视多个文件描述符CNend 864 * @param readfds [IN] Type #fd_set * Specifies a pointer to struct fd_set, and specifies the descriptor to 865 * check for being ready to read. 866 * CNcomment:指定struct fd_set的指针,并要检查是否准备好读取的描述符CNend 867 * @param writefds [IN] Type #fd_set * Specifies a pointer to struct fd_set, and specifies the descriptor 868 * to check for being ready to write.CNcomment: 869 * 指定指向struct fd_set的指针,并指定要检查是否准备好写入的描述符CNend 870 * @param exceptfds [IN] Type #fd_set * Specifies a pointer to struct fd_set, and specifies the descriptor 871 * to check for pending error conditions.CNcomment: 872 * 指定指向struct fd_set的指针,并指定要检查挂起错误条件的描述符CNend 873 * @param timeout [IN] Type #struct timeval * Specifies a pointer to struct timeval, for timeout application. 874 * CNcomment:为超时应用程序指定指向struct timeval的指针CNend 875 * @retval #>0 Execute successfully. 876 * @retval #-1 Execute failed. 877 * @par Dependency: 878 * #NULL 879 * @see NULL 880 881 */ 882 #if LWIP_TIMEVAL_PRIVATE 883 int select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, 884 struct timeval *timeout); 885 #endif 886 /** 887 * @ingroup hi_net_basic 888 * @brief Initiates transmission of a message from the specified socket to its peer.CNcomment:传输指定长度消息到对端CNend 889 * 890 * @par Description: 891 * Initiates transmission of a message from the specified socket to its peer 892 * CNcomment:启动从指定套接字到其对等端的消息传输。CNend 893 * 894 * @attention NULL 895 * @param s [IN] Type #int Indicates the socket file descriptor.CNcomment:指定文件描述符CNend 896 * @param cmd [IN] Type #int Indicates a command to select an operation[F_GETFL, F_SETFL]. 897 * CNcomment:指示选择操作的命令[F_GETFL, F_SETFL]CNend 898 * @param val [IN] Type #int Indicates an additional flag, to set non-blocking. 899 * CNcomment:指示一个附加标志,以设置非阻塞CNend 900 * @retval #0 Execute successfully. 901 * @retval #-1 & Others Execute failed. 902 * @par Dependency: 903 * #NULL 904 * @see NULL 905 */ 906 int fcntl(int s, int cmd, int val); 907 908 /** 909 * @ingroup hi_net_basic 910 * @brief Close the socket.CNcomment:关闭套接字CNend 911 * 912 * @par Description: 913 * If O_NONBLOCK is not set and if there is data on the module's write queue, 914 * close() waits for an unspecified time for any output to drain before dismantling the STREAM. 915 * If the O_NONBLOCK flag is set,close() does not wait for output to drain, and dismantles the STREAM immediately. 916 * CNcomment:如果未设置O_NONBLOCK,并且模块的写入队列中有数据,close()等待未指定的时间,等待任何输出排出,然后再拆卸流. 917 * 如果设置了O_NONBLOCK标志,close()不会等待输出耗尽,并立即取消流CNend 918 * 919 * @attention NULL 920 * @param s [IN] Type #int Indicates the socket file descriptor.CNcomment:指定文件描述符CNend 921 * @retval #0 Execute successfully. 922 * @retval #-1 & Others Execute failed. 923 * @par Dependency: 924 * #NULL 925 * @see NULL 926 */ 927 int closesocket(int s); 928 #define close(s) closesocket(s) 929 930 #ifdef __cplusplus 931 } 932 #endif 933 934 #endif 935