summaryrefslogtreecommitdiffstats
path: root/sock_stuff.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2009-04-25 06:24:05 +0200
committerGravatar Nikias Bassen2009-04-25 06:24:05 +0200
commitce4b528e203a67cbc3c8c2950b237b8fd1a41bed (patch)
treea349e15739f787bcb4c227f89004fce87f1e8383 /sock_stuff.c
parent25273957cbfa16dc908c4a56f48f2c847d5e7ab2 (diff)
downloadusbmuxd-ce4b528e203a67cbc3c8c2950b237b8fd1a41bed.tar.gz
usbmuxd-ce4b528e203a67cbc3c8c2950b237b8fd1a41bed.tar.bz2
indent -kr -ut
Diffstat (limited to 'sock_stuff.c')
-rw-r--r--sock_stuff.c470
1 files changed, 241 insertions, 229 deletions
diff --git a/sock_stuff.c b/sock_stuff.c
index 43fdf0e..b51d6ba 100644
--- a/sock_stuff.c
+++ b/sock_stuff.c
@@ -17,270 +17,282 @@ static int verbose = 0;
17 17
18void sock_stuff_set_verbose(int level) 18void sock_stuff_set_verbose(int level)
19{ 19{
20 verbose = level; 20 verbose = level;
21} 21}
22 22
23int create_unix_socket (const char *filename) 23int create_unix_socket(const char *filename)
24{ 24{
25 struct sockaddr_un name; 25 struct sockaddr_un name;
26 int sock; 26 int sock;
27 size_t size; 27 size_t size;
28 28
29 // remove if still present 29 // remove if still present
30 unlink(filename); 30 unlink(filename);
31 31
32 /* Create the socket. */ 32 /* Create the socket. */
33 sock = socket (PF_LOCAL, SOCK_STREAM, 0); 33 sock = socket(PF_LOCAL, SOCK_STREAM, 0);
34 if (sock < 0) { 34 if (sock < 0) {
35 perror ("socket"); 35 perror("socket");
36 return -1; 36 return -1;
37 } 37 }
38 38
39 /* Bind a name to the socket. */ 39 /* Bind a name to the socket. */
40 name.sun_family = AF_LOCAL; 40 name.sun_family = AF_LOCAL;
41 strncpy (name.sun_path, filename, sizeof (name.sun_path)); 41 strncpy(name.sun_path, filename, sizeof(name.sun_path));
42 name.sun_path[sizeof (name.sun_path) - 1] = '\0'; 42 name.sun_path[sizeof(name.sun_path) - 1] = '\0';
43 43
44 /* The size of the address is 44 /* The size of the address is
45 the offset of the start of the filename, 45 the offset of the start of the filename,
46 plus its length, 46 plus its length,
47 plus one for the terminating null byte. 47 plus one for the terminating null byte.
48 Alternatively you can just do: 48 Alternatively you can just do:
49 size = SUN_LEN (&name); 49 size = SUN_LEN (&name);
50 */ 50 */
51 size = (offsetof (struct sockaddr_un, sun_path) 51 size = (offsetof(struct sockaddr_un, sun_path)
52 + strlen (name.sun_path) + 1); 52 + strlen(name.sun_path) + 1);
53 53
54 if (bind (sock, (struct sockaddr *) &name, size) < 0) { 54 if (bind(sock, (struct sockaddr *) &name, size) < 0) {
55 perror("bind"); 55 perror("bind");
56 close(sock); 56 close(sock);
57 return -1; 57 return -1;
58 } 58 }
59 59
60 if (listen(sock, 10) < 0) { 60 if (listen(sock, 10) < 0) {
61 perror("listen"); 61 perror("listen");
62 close(sock); 62 close(sock);
63 return -1; 63 return -1;
64 } 64 }
65 65
66 return sock; 66 return sock;
67} 67}
68 68
69int connect_unix_socket(const char *filename) 69int connect_unix_socket(const char *filename)
70{ 70{
71 struct sockaddr_un name; 71 struct sockaddr_un name;
72 int sfd = -1; 72 int sfd = -1;
73 size_t size; 73 size_t size;
74 struct stat fst; 74 struct stat fst;
75 75
76 // check if socket file exists... 76 // check if socket file exists...
77 if (stat(filename, &fst) != 0) { 77 if (stat(filename, &fst) != 0) {
78 if (verbose >= 2) fprintf(stderr, "%s: stat '%s': %s\n", __func__, filename, strerror(errno)); 78 if (verbose >= 2)
79 return -1; 79 fprintf(stderr, "%s: stat '%s': %s\n", __func__, filename,
80 } 80 strerror(errno));
81 81 return -1;
82 // ... and if it is a unix domain socket 82 }
83 if (!S_ISSOCK(fst.st_mode)) { 83 // ... and if it is a unix domain socket
84 if (verbose >= 2) fprintf(stderr, "%s: File '%s' is not a socket!\n", __func__, filename); 84 if (!S_ISSOCK(fst.st_mode)) {
85 return -1; 85 if (verbose >= 2)
86 } 86 fprintf(stderr, "%s: File '%s' is not a socket!\n", __func__,
87 87 filename);
88 // make a new socket 88 return -1;
89 if ((sfd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) { 89 }
90 if (verbose >= 2) fprintf(stderr, "%s: socket: %s\n", __func__, strerror(errno)); 90 // make a new socket
91 return -1; 91 if ((sfd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
92 } 92 if (verbose >= 2)
93 93 fprintf(stderr, "%s: socket: %s\n", __func__, strerror(errno));
94 // and connect to 'filename' 94 return -1;
95 name.sun_family = AF_LOCAL; 95 }
96 strncpy(name.sun_path, filename, sizeof(name.sun_path)); 96 // and connect to 'filename'
97 name.sun_path[sizeof(name.sun_path) - 1] = 0; 97 name.sun_family = AF_LOCAL;
98 98 strncpy(name.sun_path, filename, sizeof(name.sun_path));
99 size = (offsetof (struct sockaddr_un, sun_path) 99 name.sun_path[sizeof(name.sun_path) - 1] = 0;
100 + strlen (name.sun_path) + 1); 100
101 101 size = (offsetof(struct sockaddr_un, sun_path)
102 if (connect(sfd, (struct sockaddr*)&name, size) < 0) { 102 + strlen(name.sun_path) + 1);
103 close(sfd); 103
104 if (verbose >= 2) fprintf(stderr, "%s: connect: %s\n", __func__, strerror(errno)); 104 if (connect(sfd, (struct sockaddr *) &name, size) < 0) {
105 return -1; 105 close(sfd);
106 } 106 if (verbose >= 2)
107 107 fprintf(stderr, "%s: connect: %s\n", __func__,
108 return sfd; 108 strerror(errno));
109 return -1;
110 }
111
112 return sfd;
109} 113}
110 114
111int create_socket(uint16_t port) 115int create_socket(uint16_t port)
112{ 116{
113 int sfd = -1; 117 int sfd = -1;
114 int yes = 1; 118 int yes = 1;
115 struct sockaddr_in saddr; 119 struct sockaddr_in saddr;
116 120
117 if ( 0 > ( sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) ) ) { 121 if (0 > (sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))) {
118 perror("socket()"); 122 perror("socket()");
119 return -1; 123 return -1;
120 } 124 }
121 125
122 if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { 126 if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
123 perror("setsockopt()"); 127 perror("setsockopt()");
124 close(sfd); 128 close(sfd);
125 return -1; 129 return -1;
126 } 130 }
127 131
128 memset((void *)&saddr, 0, sizeof(saddr)); 132 memset((void *) &saddr, 0, sizeof(saddr));
129 saddr.sin_family = AF_INET; 133 saddr.sin_family = AF_INET;
130 saddr.sin_addr.s_addr = htonl(INADDR_ANY); 134 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
131 saddr.sin_port = htons(port); 135 saddr.sin_port = htons(port);
132 136
133 if(0 > bind(sfd, (struct sockaddr *)&saddr , sizeof(saddr))) { 137 if (0 > bind(sfd, (struct sockaddr *) &saddr, sizeof(saddr))) {
134 perror("bind()"); 138 perror("bind()");
135 close(sfd); 139 close(sfd);
136 return -1; 140 return -1;
137 } 141 }
138 142
139 if (listen(sfd, 1) == -1) { 143 if (listen(sfd, 1) == -1) {
140 perror("listen()"); 144 perror("listen()");
141 close(sfd); 145 close(sfd);
142 return -1; 146 return -1;
143 } 147 }
144 148
145 return sfd; 149 return sfd;
146} 150}
147 151
148int connect_socket(const char *addr, uint16_t port) 152int connect_socket(const char *addr, uint16_t port)
149{ 153{
150 int sfd = -1; 154 int sfd = -1;
151 int yes = 1; 155 int yes = 1;
152 struct hostent *hp; 156 struct hostent *hp;
153 struct sockaddr_in saddr; 157 struct sockaddr_in saddr;
154 158
155 if (!addr) { 159 if (!addr) {
156 errno = EINVAL; 160 errno = EINVAL;
157 return -1; 161 return -1;
158 } 162 }
159 163
160 if ((hp = gethostbyname(addr)) == NULL) { 164 if ((hp = gethostbyname(addr)) == NULL) {
161 if (verbose >= 2) fprintf(stderr, "%s: unknown host '%s'\n", __func__, addr); 165 if (verbose >= 2)
162 return -1; 166 fprintf(stderr, "%s: unknown host '%s'\n", __func__, addr);
163 } 167 return -1;
164 168 }
165 if (!hp->h_addr) { 169
166 if (verbose >= 2) fprintf(stderr, "%s: gethostbyname returned NULL address!\n", __func__); 170 if (!hp->h_addr) {
167 return -1; 171 if (verbose >= 2)
168 } 172 fprintf(stderr, "%s: gethostbyname returned NULL address!\n",
169 173 __func__);
170 if ( 0 > ( sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) ) ) { 174 return -1;
171 perror("socket()"); 175 }
172 return -1; 176
173 } 177 if (0 > (sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))) {
174 178 perror("socket()");
175 if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { 179 return -1;
176 perror("setsockopt()"); 180 }
177 close(sfd); 181
178 return -1; 182 if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
179 } 183 perror("setsockopt()");
180 184 close(sfd);
181 memset((void *)&saddr, 0, sizeof(saddr)); 185 return -1;
182 saddr.sin_family = AF_INET; 186 }
183 saddr.sin_addr.s_addr = *(uint32_t*)hp->h_addr; 187
184 saddr.sin_port = htons(port); 188 memset((void *) &saddr, 0, sizeof(saddr));
185 189 saddr.sin_family = AF_INET;
186 if (connect(sfd, (struct sockaddr*)&saddr, sizeof(saddr)) < 0) { 190 saddr.sin_addr.s_addr = *(uint32_t *) hp->h_addr;
187 perror("connect"); 191 saddr.sin_port = htons(port);
188 close(sfd); 192
189 return -2; 193 if (connect(sfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) {
190 } 194 perror("connect");
191 195 close(sfd);
192 return sfd; 196 return -2;
197 }
198
199 return sfd;
193} 200}
194 201
195int check_fd(int fd, fd_mode fdm, unsigned int timeout) 202int check_fd(int fd, fd_mode fdm, unsigned int timeout)
196{ 203{
197 fd_set fds; 204 fd_set fds;
198 int sret; 205 int sret;
199 int eagain; 206 int eagain;
200 struct timeval to; 207 struct timeval to;
201 208
202 if (fd <= 0) { 209 if (fd <= 0) {
203 if (verbose >= 2) fprintf(stderr, "ERROR: invalid fd in check_fd %d\n", fd); 210 if (verbose >= 2)
204 return -1; 211 fprintf(stderr, "ERROR: invalid fd in check_fd %d\n", fd);
205 }
206
207 FD_ZERO(&fds);
208 FD_SET(fd, &fds);
209
210 to.tv_sec = (time_t)(timeout/1000);
211 to.tv_usec = (time_t)((timeout-(to.tv_sec*1000))*1000);
212
213 sret = -1;
214
215 do {
216 eagain = 0;
217 switch(fdm) {
218 case FD_READ:
219 sret = select(fd+1,&fds,NULL,NULL,&to);
220 break;
221 case FD_WRITE:
222 sret = select(fd+1,NULL,&fds,NULL,&to);
223 break;
224 case FD_EXCEPT:
225 sret = select(fd+1,NULL,NULL,&fds,&to);
226 break;
227 default:
228 return -1; 212 return -1;
229 } 213 }
230
231 if (sret < 0) {
232 switch(errno) {
233 case EINTR:
234 // interrupt signal in select
235 if (verbose >= 2) fprintf(stderr, "%s: EINTR\n", __func__);
236 eagain = 1;
237 break;
238 case EAGAIN:
239 if (verbose >= 2) fprintf(stderr, "%s: EAGAIN\n", __func__);
240 break;
241 default:
242 if (verbose >= 2) fprintf(stderr, "%s: select failed: %s\n", __func__, strerror(errno));
243 return -1;
244 }
245 }
246 } while (eagain);
247 214
248 return sret; 215 FD_ZERO(&fds);
216 FD_SET(fd, &fds);
217
218 to.tv_sec = (time_t) (timeout / 1000);
219 to.tv_usec = (time_t) ((timeout - (to.tv_sec * 1000)) * 1000);
220
221 sret = -1;
222
223 do {
224 eagain = 0;
225 switch (fdm) {
226 case FD_READ:
227 sret = select(fd + 1, &fds, NULL, NULL, &to);
228 break;
229 case FD_WRITE:
230 sret = select(fd + 1, NULL, &fds, NULL, &to);
231 break;
232 case FD_EXCEPT:
233 sret = select(fd + 1, NULL, NULL, &fds, &to);
234 break;
235 default:
236 return -1;
237 }
238
239 if (sret < 0) {
240 switch (errno) {
241 case EINTR:
242 // interrupt signal in select
243 if (verbose >= 2)
244 fprintf(stderr, "%s: EINTR\n", __func__);
245 eagain = 1;
246 break;
247 case EAGAIN:
248 if (verbose >= 2)
249 fprintf(stderr, "%s: EAGAIN\n", __func__);
250 break;
251 default:
252 if (verbose >= 2)
253 fprintf(stderr, "%s: select failed: %s\n", __func__,
254 strerror(errno));
255 return -1;
256 }
257 }
258 } while (eagain);
259
260 return sret;
249} 261}
250 262
251int recv_buf(int fd, void *data, size_t length) 263int recv_buf(int fd, void *data, size_t length)
252{ 264{
253 return recv_buf_timeout(fd, data, length, 0, RECV_TIMEOUT); 265 return recv_buf_timeout(fd, data, length, 0, RECV_TIMEOUT);
254} 266}
255 267
256int peek_buf(int fd, void *data, size_t length) 268int peek_buf(int fd, void *data, size_t length)
257{ 269{
258 return recv_buf_timeout(fd, data, length, MSG_PEEK, RECV_TIMEOUT); 270 return recv_buf_timeout(fd, data, length, MSG_PEEK, RECV_TIMEOUT);
259} 271}
260 272
261int recv_buf_timeout(int fd, void *data, size_t length, int flags, unsigned int timeout) 273int recv_buf_timeout(int fd, void *data, size_t length, int flags,
274 unsigned int timeout)
262{ 275{
263 int res; 276 int res;
264 int result; 277 int result;
265 278
266 // check if data is available 279 // check if data is available
267 res = check_fd(fd, FD_READ, timeout); 280 res = check_fd(fd, FD_READ, timeout);
268 if (res <= 0) { 281 if (res <= 0) {
269 return res; 282 return res;
270 } 283 }
271 284 // if we get here, there _is_ data available
272 // if we get here, there _is_ data available 285 result = recv(fd, data, length, flags);
273 result = recv(fd, data, length, flags); 286 if (res > 0 && result == 0) {
274 if (res > 0 && result == 0) { 287 // but this is an error condition
275 // but this is an error condition 288 if (verbose >= 3)
276 if (verbose >= 3) fprintf(stderr, "%s: fd=%d recv returned 0\n", __func__, fd); 289 fprintf(stderr, "%s: fd=%d recv returned 0\n", __func__, fd);
277 return -1; 290 return -1;
278 } 291 }
279 return result; 292 return result;
280} 293}
281 294
282int send_buf(int fd, void *data, size_t length) 295int send_buf(int fd, void *data, size_t length)
283{ 296{
284 return send(fd, data, length, 0); 297 return send(fd, data, length, 0);
285} 298}
286