* @brief Closes a socket. * * This function closes the socket specified by the file descriptor 's'. Once closed, the socket * can no longer be used for communication. Any pending data that has not been transmitted may be lost. * * @param s The file descriptor of the socket to close. * * @return Returns '0' on success. On failure, returns '-1' and sets errno to indicate the error. * * @not
| 700 | * @see send() Sends data on a socket. |
| 701 | */ |
| 702 | int closesocket(int s) |
| 703 | { |
| 704 | int error = 0; |
| 705 | int socket = -1; |
| 706 | struct dfs_file *d; |
| 707 | |
| 708 | socket = dfs_net_getsocket(s); |
| 709 | if (socket < 0) |
| 710 | { |
| 711 | rt_set_errno(-ENOTSOCK); |
| 712 | return -1; |
| 713 | } |
| 714 | |
| 715 | d = fd_get(s); |
| 716 | if (d == RT_NULL) |
| 717 | { |
| 718 | rt_set_errno(-EBADF); |
| 719 | return -1; |
| 720 | } |
| 721 | |
| 722 | if (!d->vnode) |
| 723 | { |
| 724 | rt_set_errno(-EBADF); |
| 725 | return -1; |
| 726 | } |
| 727 | |
| 728 | if (sal_closesocket(socket) == 0) |
| 729 | { |
| 730 | error = 0; |
| 731 | } |
| 732 | else |
| 733 | { |
| 734 | rt_set_errno(-ENOTSOCK); |
| 735 | error = -1; |
| 736 | } |
| 737 | |
| 738 | /* socket has been closed, delete it from file system fd */ |
| 739 | fd_release(s); |
| 740 | |
| 741 | return error; |
| 742 | } |
| 743 | RTM_EXPORT(closesocket); |
| 744 | |
| 745 | /** |
no test coverage detected