* @brief Create and connect a new UNIX STREAM socket. * * Creates and connects a new STREAM socket with the socket given in `path`. * * @retval >0 Success; return value is a socket file descriptor * @retval <0 Error. */
| 110 | * @retval <0 Error. |
| 111 | */ |
| 112 | int create_unix_stream_socket(const char* path, int flags) { |
| 113 | struct sockaddr_un saddr; |
| 114 | int sfd; |
| 115 | |
| 116 | if (path == NULL) return -1; |
| 117 | |
| 118 | if (-1 == check_error(sfd = socket(AF_UNIX, SOCK_STREAM | flags, 0))) |
| 119 | return -1; |
| 120 | |
| 121 | memset(&saddr, 0, sizeof(struct sockaddr_un)); |
| 122 | |
| 123 | if (strlen(path) > (sizeof(saddr.sun_path) - 1)) { |
| 124 | #ifdef VERBOSE |
| 125 | debug_write( |
| 126 | "create_unix_stream_socket: UNIX socket destination path too " |
| 127 | "long\n"); |
| 128 | #endif |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | saddr.sun_family = AF_UNIX; |
| 133 | strncpy(saddr.sun_path, path, sizeof(saddr.sun_path) - 1); |
| 134 | |
| 135 | if (-1 == check_error( |
| 136 | connect(sfd, (struct sockaddr*)&saddr, |
| 137 | sizeof(saddr.sun_family) + strlen(saddr.sun_path)))) { |
| 138 | close(sfd); |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | return sfd; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @brief Create a UNIX DGRAM socket |
no test coverage detected