1 /* 2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited 3 */ 4 5 #ifndef _AOS_SOCKET_H_ 6 #define _AOS_SOCKET_H_ 7 8 #include <sys/socket.h> 9 #include "netdb.h" 10 11 /** 12 * @brief get socket errno. 13 * 14 * @param[in] NULL 15 * 16 * @return socket errno 17 */ 18 int aos_socket_errno(void); 19 20 /** 21 * @brief creates an endpoint for communication and returns a descriptor. 22 * 23 * @param[in] domain 24 * @param[in] type 25 * @param[in] protocol 26 * 27 * @return On success, a file descriptor for the new socket is returned. 28 * On error, -1 is returned, and errno is set appropriately 29 */ 30 int aos_socket_open(int domain, int type, int protocol); 31 32 /** 33 * @brief transmit data to socket. 34 * 35 * @param[in] sockfd 36 * @param[in] data 37 * @param[in] size 38 * @param[in] flags 39 * 40 * @return On success, these calls return the number of bytes sent. 41 * On error, -1 is returned, and errno is set appropriately. 42 */ 43 int aos_socket_send(int sockfd, const void *data, size_t size, int flags); 44 45 /** 46 * @brief receive data from a socket. 47 * 48 * @param[in] sockfd 49 * @param[in] mem 50 * @param[in] len 51 * @param[in] flags 52 * 53 * @return the number of bytes received, or -1 if an error occurred. 54 */ 55 int aos_socket_recv(int sockfd, void *mem, size_t len, int flags); 56 57 /** 58 * @brief write data to socket. 59 * 60 * @param[in] sockfd 61 * @param[in] data 62 * @param[in] size 63 * 64 * @return On success, these calls return the number of bytes sent. 65 * On error, -1 is returned, and errno is set appropriately. 66 */ 67 int aos_socket_write(int sockfd, const void *data, size_t size); 68 69 /** 70 * @brief read data from a socket. 71 * 72 * @param[in] sockfd 73 * @param[in] data 74 * @param[in] len 75 * 76 * @return the number of bytes received, or -1 if an error occurred. 77 */ 78 int aos_socket_read(int sockfd, void *data, size_t len); 79 80 /** 81 * @brief transmit data to socket. 82 * 83 * @param[in] sockfd 84 * @param[in] data 85 * @param[in] size 86 * @param[in] flags 87 * @param[in] to 88 * @param[in] tolen 89 * 90 * @return On success, these calls return the number of bytes sent. 91 * On error, -1 is returned, and errno is set appropriately. 92 */ 93 int aos_socket_sendto(int sockfd, const void *data, size_t size, int flags, const struct sockaddr *to, socklen_t tolen); 94 95 /** 96 * @brief receive data from a socket. 97 * 98 * @param[in] sockfd 99 * @param[in] data 100 * @param[in] len 101 * @param[in] flags 102 * @param[in] from 103 * @param[in] fromlenq 104 * 105 * @return the number of bytes received, or -1 if an error occurred. 106 */ 107 int aos_socket_recvfrom(int socket, void *data, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen); 108 109 /** 110 * @brief manipulate options for the socket referred to by the file descriptor sockfd. 111 * 112 * @param[in] sockfd 113 * @param[in] level 114 * @param[in] optname 115 * @param[in] optval 116 * @param[in] optlen 117 * 118 * @return On success, zero is returned. 119 * On error, -1 is returned, and errno is set appropriately. 120 */ 121 int aos_socket_setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen); 122 123 /** 124 * @brief manipulate options for the socket referred to by the file descriptor sockfd. 125 * 126 * @param[in] sockfd 127 * @param[in] level 128 * @param[in] optname 129 * @param[in] optval 130 * @param[in] optlen 131 * 132 * @return On success, zero is returned. 133 * On error, -1 is returned, and errno is set appropriately. 134 */ 135 int aos_socket_getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); 136 137 /** 138 * @brief connect the socket referred to by the file descriptor sockfd to the address specified by addr. 139 * 140 * @param[in] sockfd 141 * @param[in] addr 142 * @param[in] addrlen 143 * 144 * @return On success, zero is returned. 145 * On error, -1 is returned, and errno is set appropriately. 146 */ 147 int aos_socket_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); 148 149 /** 150 * @brief assigns the address specified by addr to the socket referred to by the file descriptor sockfd. 151 * 152 * @param[in] sockfd 153 * @param[in] addr 154 * @param[in] addrlen 155 * 156 * @return On success, zero is returned. 157 * On error, -1 is returned, and errno is set appropriately. 158 */ 159 int aos_socket_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); 160 161 /** 162 * @brief marks the socket referred to by sockfd as a passive socket. 163 * 164 * @param[in] sockfd 165 * @param[in] backlog 166 * 167 * @return On success, zero is returned. 168 * On error, -1 is returned, and errno is set appropriately. 169 */ 170 int aos_socket_listen(int sockfd, int backlog); 171 172 /** 173 * @brief accept the socket referred to by sockfd. 174 * 175 * @param[in] sockfd 176 * @param[in] addr 177 * @param[in] addrlen 178 * 179 * @return On success, zero is returned. 180 * On error, -1 is returned, and errno is set appropriately. 181 */ 182 int aos_socket_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); 183 184 /** 185 * @brief Monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operatio. 186 * 187 * @param[in] maxfdp1 188 * @param[in] readset 189 * @param[in] writeset 190 * @param[in] exceptset 191 * @param[in] timeout 192 * 193 * @return On success, zero is returned. 194 * On error, -1 is returned, and errno is set appropriately. 195 */ 196 int aos_socket_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout); 197 198 /** 199 * @brief Close the socket referred to by sockfd. 200 * 201 * @param[in] sockfd 202 * 203 * @return On success, zero is returned. 204 * On error, -1 is returned, and errno is set appropriately. 205 */ 206 int aos_socket_close(int sockfd); 207 208 /** 209 * @brief Shutdown the socket referred to by sockfd. 210 * 211 * @param[in] sockfd 212 * @param[in] how 213 * 214 * @return On success, zero is returned. 215 * On error, -1 is returned, and errno is set appropriately. 216 */ 217 int aos_socket_shutdown(int socket, int how); 218 219 #endif /* _AOS_SOCKET_H_ */ 220