MCPcopy Create free account
hub / github.com/dermesser/libsocket / create_unix_stream_socket

Function create_unix_stream_socket

C/unix/libunixsocket.c:112–143  ·  view source on GitHub ↗

* @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. */

Source from the content-addressed store, hash-verified

110 * @retval <0 Error.
111 */
112int 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

Callers 2

connectMethod · 0.85
mainFunction · 0.85

Calls 2

socketClass · 0.85
check_errorFunction · 0.70

Tested by

no test coverage detected