* @brief Creates a new socket for communication. * * This system call creates a new socket endpoint for communication and returns a file descriptor * that can be used for subsequent socket operations, such as binding, connecting, or data transfer. * * @param[in] domain Specifies the protocol family to be used for the socket. Common values include: * - `AF_INET`: IPv4 I
| 6196 | * @see sys_bind(), sys_connect(), sys_accept(), sys_close() |
| 6197 | */ |
| 6198 | sysret_t sys_socket(int domain, int type, int protocol) |
| 6199 | { |
| 6200 | int fd = -1; |
| 6201 | int nonblock = 0; |
| 6202 | /* not support SOCK_CLOEXEC type */ |
| 6203 | if (type & SOCK_CLOEXEC) |
| 6204 | { |
| 6205 | type &= ~SOCK_CLOEXEC; |
| 6206 | } |
| 6207 | if (type & SOCK_NONBLOCK) |
| 6208 | { |
| 6209 | nonblock = 1; |
| 6210 | type &= ~SOCK_NONBLOCK; |
| 6211 | } |
| 6212 | |
| 6213 | fd = socket(domain, type, protocol); |
| 6214 | if (fd < 0) |
| 6215 | { |
| 6216 | goto out; |
| 6217 | } |
| 6218 | if (nonblock) |
| 6219 | { |
| 6220 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 6221 | } |
| 6222 | |
| 6223 | out: |
| 6224 | return (fd < 0 ? GET_ERRNO() : fd); |
| 6225 | } |
| 6226 | |
| 6227 | /** |
| 6228 | * @brief Creates a pair of connected sockets. |