diff options
Diffstat (limited to 'libusbmuxd/sock_stuff.c')
| -rw-r--r-- | libusbmuxd/sock_stuff.c | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/libusbmuxd/sock_stuff.c b/libusbmuxd/sock_stuff.c index 302b53a..edc738e 100644 --- a/libusbmuxd/sock_stuff.c +++ b/libusbmuxd/sock_stuff.c | |||
| @@ -85,13 +85,13 @@ int create_unix_socket(const char *filename) | |||
| 85 | 85 | ||
| 86 | if (bind(sock, (struct sockaddr *) &name, size) < 0) { | 86 | if (bind(sock, (struct sockaddr *) &name, size) < 0) { |
| 87 | perror("bind"); | 87 | perror("bind"); |
| 88 | close(sock); | 88 | close_socket(sock); |
| 89 | return -1; | 89 | return -1; |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | if (listen(sock, 10) < 0) { | 92 | if (listen(sock, 10) < 0) { |
| 93 | perror("listen"); | 93 | perror("listen"); |
| 94 | close(sock); | 94 | close_socket(sock); |
| 95 | return -1; | 95 | return -1; |
| 96 | } | 96 | } |
| 97 | 97 | ||
| @@ -134,7 +134,7 @@ int connect_unix_socket(const char *filename) | |||
| 134 | + strlen(name.sun_path) + 1); | 134 | + strlen(name.sun_path) + 1); |
| 135 | 135 | ||
| 136 | if (connect(sfd, (struct sockaddr *) &name, size) < 0) { | 136 | if (connect(sfd, (struct sockaddr *) &name, size) < 0) { |
| 137 | close(sfd); | 137 | close_socket(sfd); |
| 138 | if (verbose >= 2) | 138 | if (verbose >= 2) |
| 139 | fprintf(stderr, "%s: connect: %s\n", __func__, | 139 | fprintf(stderr, "%s: connect: %s\n", __func__, |
| 140 | strerror(errno)); | 140 | strerror(errno)); |
| @@ -168,7 +168,7 @@ int create_socket(uint16_t port) | |||
| 168 | 168 | ||
| 169 | if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int)) == -1) { | 169 | if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int)) == -1) { |
| 170 | perror("setsockopt()"); | 170 | perror("setsockopt()"); |
| 171 | close(sfd); | 171 | close_socket(sfd); |
| 172 | return -1; | 172 | return -1; |
| 173 | } | 173 | } |
| 174 | 174 | ||
| @@ -179,13 +179,13 @@ int create_socket(uint16_t port) | |||
| 179 | 179 | ||
| 180 | if (0 > bind(sfd, (struct sockaddr *) &saddr, sizeof(saddr))) { | 180 | if (0 > bind(sfd, (struct sockaddr *) &saddr, sizeof(saddr))) { |
| 181 | perror("bind()"); | 181 | perror("bind()"); |
| 182 | close(sfd); | 182 | close_socket(sfd); |
| 183 | return -1; | 183 | return -1; |
| 184 | } | 184 | } |
| 185 | 185 | ||
| 186 | if (listen(sfd, 1) == -1) { | 186 | if (listen(sfd, 1) == -1) { |
| 187 | perror("listen()"); | 187 | perror("listen()"); |
| 188 | close(sfd); | 188 | close_socket(sfd); |
| 189 | return -1; | 189 | return -1; |
| 190 | } | 190 | } |
| 191 | 191 | ||
| @@ -235,7 +235,7 @@ int connect_socket(const char *addr, uint16_t port) | |||
| 235 | 235 | ||
| 236 | if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int)) == -1) { | 236 | if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(int)) == -1) { |
| 237 | perror("setsockopt()"); | 237 | perror("setsockopt()"); |
| 238 | close(sfd); | 238 | close_socket(sfd); |
| 239 | return -1; | 239 | return -1; |
| 240 | } | 240 | } |
| 241 | 241 | ||
| @@ -246,7 +246,7 @@ int connect_socket(const char *addr, uint16_t port) | |||
| 246 | 246 | ||
| 247 | if (connect(sfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { | 247 | if (connect(sfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { |
| 248 | perror("connect"); | 248 | perror("connect"); |
| 249 | close(sfd); | 249 | close_socket(sfd); |
| 250 | return -2; | 250 | return -2; |
| 251 | } | 251 | } |
| 252 | 252 | ||
| @@ -321,6 +321,14 @@ int check_fd(int fd, fd_mode fdm, unsigned int timeout) | |||
| 321 | return sret; | 321 | return sret; |
| 322 | } | 322 | } |
| 323 | 323 | ||
| 324 | int close_socket(int fd) { | ||
| 325 | #ifdef WIN32 | ||
| 326 | return closesocket(fd); | ||
| 327 | #else | ||
| 328 | return close(fd); | ||
| 329 | #endif | ||
| 330 | } | ||
| 331 | |||
| 324 | int recv_buf(int fd, void *data, size_t length) | 332 | int recv_buf(int fd, void *data, size_t length) |
| 325 | { | 333 | { |
| 326 | return recv_buf_timeout(fd, data, length, 0, RECV_TIMEOUT); | 334 | return recv_buf_timeout(fd, data, length, 0, RECV_TIMEOUT); |
