Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 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 included_vcom_glibc_socket_h |
| 17 | #define included_vcom_glibc_socket_h |
| 18 | |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <sys/select.h> |
| 22 | #include <arpa/inet.h> |
| 23 | #include <fcntl.h> |
| 24 | |
| 25 | #include <sys/epoll.h> |
shrinivasan ganapathy | 1d35963 | 2017-10-15 15:46:09 -0700 | [diff] [blame] | 26 | |
| 27 | #include <signal.h> |
Keith Burns (alagalah) | b327c2b | 2017-10-09 08:52:59 -0700 | [diff] [blame] | 28 | #include <poll.h> |
| 29 | |
| 30 | /* |
| 31 | * |
| 32 | * Generic glibc fd api |
| 33 | * |
| 34 | */ |
| 35 | /* |
| 36 | * glibc APIs from <unistd.h> |
| 37 | */ |
| 38 | |
| 39 | /* Close the file descriptor FD. |
| 40 | |
| 41 | This function is a cancellation point and therefore not marked with |
| 42 | __THROW. */ |
| 43 | extern int close (int __fd); |
| 44 | |
| 45 | /* Read NBYTES into BUF from FD. Return the |
| 46 | number read, -1 for errors or 0 for EOF. |
| 47 | |
| 48 | This function is a cancellation point and therefore not marked with |
| 49 | __THROW. */ |
| 50 | extern ssize_t __wur read (int __fd, void *__buf, size_t __nbytes); |
| 51 | |
| 52 | /* Write N bytes of BUF to FD. Return the number written, or -1. |
| 53 | |
| 54 | This function is a cancellation point and therefore not marked with |
| 55 | __THROW. */ |
| 56 | extern ssize_t __wur write (int __fd, const void *__buf, size_t __n); |
| 57 | |
| 58 | |
| 59 | /* |
| 60 | * glibc APIs from <fcntl.h> |
| 61 | */ |
| 62 | |
| 63 | /* Do the file control operation described by CMD on FD. |
| 64 | The remaining arguments are interpreted depending on CMD. |
| 65 | |
| 66 | This function is a cancellation point and therefore not marked with |
| 67 | __THROW. */ |
| 68 | extern int fcntl (int __fd, int __cmd, ...); |
| 69 | |
| 70 | |
| 71 | /* |
| 72 | * glibc APIs from <sys/select.h> |
| 73 | */ |
| 74 | |
| 75 | /* Check the first NFDS descriptors each in READFDS (if not NULL) for read |
| 76 | readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS |
| 77 | (if not NULL) for exceptional conditions. If TIMEOUT is not NULL, time out |
| 78 | after waiting the interval specified therein. Returns the number of ready |
| 79 | descriptors, or -1 for errors. |
| 80 | |
| 81 | |
| 82 | This function is a cancellation point and therefore not marked with |
| 83 | __THROW. */ |
| 84 | extern int |
| 85 | select (int __nfds, fd_set * __restrict __readfds, |
| 86 | fd_set * __restrict __writefds, |
| 87 | fd_set * __restrict __exceptfds, |
| 88 | struct timeval *__restrict __timeout); |
| 89 | |
| 90 | #ifdef __USE_XOPEN2K |
| 91 | /* Same as above only that the TIMEOUT value is given with higher |
| 92 | resolution and a sigmask which is been set temporarily. This version |
| 93 | should be used. |
| 94 | |
| 95 | This function is a cancellation point and therefore not marked with |
| 96 | __THROW. */ |
| 97 | extern int |
| 98 | pselect (int __nfds, fd_set * __restrict __readfds, |
| 99 | fd_set * __restrict __writefds, |
| 100 | fd_set * __restrict __exceptfds, |
| 101 | const struct timespec *__restrict __timeout, |
| 102 | const __sigset_t * __restrict __sigmask); |
| 103 | #endif |
| 104 | |
| 105 | |
| 106 | /* |
| 107 | * |
| 108 | * Socket specific glibc api |
| 109 | * |
| 110 | */ |
| 111 | |
| 112 | /* |
| 113 | * glibc APIs from <sys/socket.h> |
| 114 | */ |
| 115 | |
| 116 | /* Create a new socket of type TYPE in domain DOMAIN, using |
| 117 | protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically. |
| 118 | Returns a file descriptor for the new socket, or -1 for errors. */ |
| 119 | extern int __THROW socket (int __domain, int __type, int __protocol); |
| 120 | |
| 121 | /* Create two new sockets, of type TYPE in domain DOMAIN and using |
| 122 | protocol PROTOCOL, which are connected to each other, and put file |
| 123 | descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero, |
| 124 | one will be chosen automatically. Returns 0 on success, -1 for errors. */ |
| 125 | extern int __THROW |
| 126 | socketpair (int __domain, int __type, int __protocol, int __fds[2]); |
| 127 | |
| 128 | /* Give the socket FD the local address ADDR (which is LEN bytes long). */ |
| 129 | extern int __THROW |
| 130 | bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len); |
| 131 | |
| 132 | /* Put the local address of FD into *ADDR and its length in *LEN. */ |
| 133 | extern int __THROW |
| 134 | getsockname (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __len); |
| 135 | |
| 136 | /* Open a connection on socket FD to peer at ADDR (which LEN bytes long). |
| 137 | For connectionless socket types, just set the default address to send to |
| 138 | and the only address from which to accept transmissions. |
| 139 | Return 0 on success, -1 for errors. |
| 140 | |
| 141 | This function is a cancellation point and therefore not marked with |
| 142 | __THROW. */ |
| 143 | extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len); |
| 144 | |
| 145 | /* Put the address of the peer connected to socket FD into *ADDR |
| 146 | (which is *LEN bytes long), and its actual length into *LEN. */ |
| 147 | extern int __THROW |
| 148 | getpeername (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __len); |
| 149 | |
| 150 | /* Send N bytes of BUF to socket FD. Returns the number sent or -1. |
| 151 | |
| 152 | This function is a cancellation point and therefore not marked with |
| 153 | __THROW. */ |
| 154 | extern ssize_t send (int __fd, const void *__buf, size_t __n, int __flags); |
| 155 | |
| 156 | /* Read N bytes into BUF from socket FD. |
| 157 | Returns the number read or -1 for errors. |
| 158 | |
| 159 | This function is a cancellation point and therefore not marked with |
| 160 | __THROW. */ |
| 161 | extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags); |
| 162 | |
| 163 | /* Send N bytes of BUF on socket FD to peer at address ADDR (which is |
| 164 | ADDR_LEN bytes long). Returns the number sent, or -1 for errors. |
| 165 | |
| 166 | This function is a cancellation point and therefore not marked with |
| 167 | __THROW. */ |
| 168 | extern ssize_t |
| 169 | sendto (int __fd, const void *__buf, size_t __n, |
| 170 | int __flags, __CONST_SOCKADDR_ARG __addr, socklen_t __addr_len); |
| 171 | |
| 172 | /* Read N bytes into BUF through socket FD. |
| 173 | If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of |
| 174 | the sender, and store the actual size of the address in *ADDR_LEN. |
| 175 | Returns the number of bytes read or -1 for errors. |
| 176 | |
| 177 | This function is a cancellation point and therefore not marked with |
| 178 | __THROW. */ |
| 179 | extern ssize_t |
| 180 | recvfrom (int __fd, void *__restrict __buf, |
| 181 | size_t __n, int __flags, |
| 182 | __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len); |
| 183 | |
| 184 | /* Send a message described MESSAGE on socket FD. |
| 185 | Returns the number of bytes sent, or -1 for errors. |
| 186 | |
| 187 | This function is a cancellation point and therefore not marked with |
| 188 | __THROW. */ |
| 189 | extern ssize_t |
| 190 | sendmsg (int __fd, const struct msghdr *__message, int __flags); |
| 191 | |
| 192 | #ifdef __USE_GNU |
| 193 | /* Send a VLEN messages as described by VMESSAGES to socket FD. |
| 194 | Returns the number of datagrams successfully written or -1 for errors. |
| 195 | |
| 196 | This function is a cancellation point and therefore not marked with |
| 197 | __THROW. */ |
| 198 | extern int |
| 199 | sendmmsg (int __fd, struct mmsghdr *__vmessages, |
| 200 | unsigned int __vlen, int __flags); |
| 201 | #endif |
| 202 | |
| 203 | /* Receive a message as described by MESSAGE from socket FD. |
| 204 | Returns the number of bytes read or -1 for errors. |
| 205 | |
| 206 | This function is a cancellation point and therefore not marked with |
| 207 | __THROW. */ |
| 208 | extern ssize_t recvmsg (int __fd, struct msghdr *__message, int __flags); |
| 209 | |
| 210 | #ifdef __USE_GNU |
| 211 | /* Receive up to VLEN messages as described by VMESSAGES from socket FD. |
| 212 | Returns the number of messages received or -1 for errors. |
| 213 | |
| 214 | This function is a cancellation point and therefore not marked with |
| 215 | __THROW. */ |
| 216 | extern int |
| 217 | recvmmsg (int __fd, struct mmsghdr *__vmessages, |
| 218 | unsigned int __vlen, int __flags, struct timespec *__tmo); |
| 219 | #endif |
| 220 | |
| 221 | |
| 222 | /* Put the current value for socket FD's option OPTNAME at protocol level LEVEL |
| 223 | into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the value's |
| 224 | actual length. Returns 0 on success, -1 for errors. */ |
| 225 | extern int __THROW |
| 226 | getsockopt (int __fd, int __level, int __optname, |
| 227 | void *__restrict __optval, socklen_t * __restrict __optlen); |
| 228 | |
| 229 | /* Set socket FD's option OPTNAME at protocol level LEVEL |
| 230 | to *OPTVAL (which is OPTLEN bytes long). |
| 231 | Returns 0 on success, -1 for errors. */ |
| 232 | extern int __THROW |
| 233 | setsockopt (int __fd, int __level, int __optname, |
| 234 | const void *__optval, socklen_t __optlen); |
| 235 | |
| 236 | /* Prepare to accept connections on socket FD. |
| 237 | N connection requests will be queued before further requests are refused. |
| 238 | Returns 0 on success, -1 for errors. */ |
| 239 | extern int __THROW listen (int __fd, int __n); |
| 240 | |
| 241 | /* Await a connection on socket FD. |
| 242 | When a connection arrives, open a new socket to communicate with it, |
| 243 | set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting |
| 244 | peer and *ADDR_LEN to the address's actual length, and return the |
| 245 | new socket's descriptor, or -1 for errors. |
| 246 | |
| 247 | This function is a cancellation point and therefore not marked with |
| 248 | __THROW. */ |
| 249 | extern int |
| 250 | accept (int __fd, __SOCKADDR_ARG __addr, socklen_t * __restrict __addr_len); |
| 251 | |
| 252 | #ifdef __USE_GNU |
| 253 | /* Similar to 'accept' but takes an additional parameter to specify flags. |
| 254 | |
| 255 | This function is a cancellation point and therefore not marked with |
| 256 | __THROW. */ |
| 257 | /* TBD: implemented later */ |
| 258 | extern int |
| 259 | accept4 (int __fd, __SOCKADDR_ARG __addr, |
| 260 | socklen_t * __restrict __addr_len, int __flags); |
| 261 | #endif |
| 262 | |
| 263 | /* Shut down all or part of the connection open on socket FD. |
| 264 | HOW determines what to shut down: |
| 265 | SHUT_RD = No more receptions; |
| 266 | SHUT_WR = No more transmissions; |
| 267 | SHUT_RDWR = No more receptions or transmissions. |
| 268 | Returns 0 on success, -1 for errors. */ |
| 269 | extern int __THROW shutdown (int __fd, int __how); |
| 270 | |
| 271 | |
| 272 | /* |
| 273 | * glibc APIs from <sys/epoll.h> |
| 274 | */ |
| 275 | |
| 276 | /* Creates an epoll instance. Returns an fd for the new instance. |
| 277 | The "size" parameter is a hint specifying the number of file |
| 278 | descriptors to be associated with the new instance. The fd |
| 279 | returned by epoll_create() should be closed with close(). */ |
| 280 | extern int __THROW epoll_create (int __size); |
| 281 | |
| 282 | /* Same as epoll_create but with an FLAGS parameter. The unused SIZE |
| 283 | parameter has been dropped. */ |
| 284 | extern int __THROW epoll_create1 (int __flags); |
| 285 | |
| 286 | /* Manipulate an epoll instance "epfd". Returns 0 in case of success, |
| 287 | -1 in case of error ( the "errno" variable will contain the |
| 288 | specific error code ) The "op" parameter is one of the EPOLL_CTL_* |
| 289 | constants defined above. The "fd" parameter is the target of the |
| 290 | operation. The "event" parameter describes which events the caller |
| 291 | is interested in and any associated user data. */ |
| 292 | extern int __THROW |
| 293 | epoll_ctl (int __epfd, int __op, int __fd, struct epoll_event *__event); |
| 294 | |
| 295 | #define EP_INT_MAX ((int)(~0U>>1)) |
| 296 | #define EP_MAX_EVENTS (EP_INT_MAX / sizeof(struct epoll_event)) |
| 297 | |
| 298 | /* Wait for events on an epoll instance "epfd". Returns the number of |
| 299 | triggered events returned in "events" buffer. Or -1 in case of |
| 300 | error with the "errno" variable set to the specific error code. The |
| 301 | "events" parameter is a buffer that will contain triggered |
| 302 | events. The "maxevents" is the maximum number of events to be |
| 303 | returned ( usually size of "events" ). The "timeout" parameter |
| 304 | specifies the maximum wait time in milliseconds (-1 == infinite). |
| 305 | |
| 306 | This function is a cancellation point and therefore not marked with |
| 307 | __THROW. */ |
| 308 | extern int |
| 309 | epoll_wait (int __epfd, struct epoll_event *__events, |
| 310 | int __maxevents, int __timeout); |
| 311 | |
| 312 | /* Same as epoll_wait, but the thread's signal mask is temporarily |
| 313 | and atomically replaced with the one provided as parameter. |
| 314 | |
| 315 | This function is a cancellation point and therefore not marked with |
| 316 | __THROW. */ |
| 317 | extern int |
| 318 | epoll_pwait (int __epfd, struct epoll_event *__events, |
| 319 | int __maxevents, int __timeout, const __sigset_t * __ss); |
| 320 | |
| 321 | /* Poll the file descriptors described by the NFDS structures starting at |
| 322 | FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for |
| 323 | an event to occur; if TIMEOUT is -1, block until an event occurs. |
| 324 | Returns the number of file descriptors with events, zero if timed out, |
| 325 | or -1 for errors. |
| 326 | |
| 327 | This function is a cancellation point and therefore not marked with |
| 328 | __THROW. */ |
| 329 | extern int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout); |
| 330 | |
| 331 | #ifdef __USE_GNU |
| 332 | /* Like poll, but before waiting the threads signal mask is replaced |
| 333 | with that specified in the fourth parameter. For better usability, |
| 334 | the timeout value is specified using a TIMESPEC object. |
| 335 | |
| 336 | This function is a cancellation point and therefore not marked with |
| 337 | __THROW. */ |
| 338 | extern int ppoll (struct pollfd *__fds, nfds_t __nfds, |
| 339 | const struct timespec *__timeout, const __sigset_t * __ss); |
| 340 | #endif |
| 341 | |
| 342 | |
| 343 | #endif /* included_vcom_glibc_socket_h */ |
| 344 | |
| 345 | /* |
| 346 | * fd.io coding-style-patch-verification: ON |
| 347 | * |
| 348 | * Local Variables: |
| 349 | * eval: (c-set-style "gnu") |
| 350 | * End: |
| 351 | */ |