comparison src/lib/network.c @ 5985:8ae4b8d78a25 HEAD

More union usage to avoid casting.
author Timo Sirainen <tss@iki.fi>
date Fri, 13 Jul 2007 05:52:37 +0300
parents 74a6130211c2
children 6a64e64fa3a3
comparison
equal deleted inserted replaced
5984:74a6130211c2 5985:8ae4b8d78a25
184 return fd; 184 return fd;
185 } 185 }
186 186
187 int net_connect_unix(const char *path) 187 int net_connect_unix(const char *path)
188 { 188 {
189 struct sockaddr_un sa; 189 union {
190 struct sockaddr sa;
191 struct sockaddr_un un;
192 } sa;
190 int fd, ret; 193 int fd, ret;
191 194
192 memset(&sa, 0, sizeof(sa)); 195 memset(&sa, 0, sizeof(sa));
193 sa.sun_family = AF_UNIX; 196 sa.un.sun_family = AF_UNIX;
194 if (strocpy(sa.sun_path, path, sizeof(sa.sun_path)) < 0) { 197 if (strocpy(sa.un.sun_path, path, sizeof(sa.un.sun_path)) < 0) {
195 /* too long path */ 198 /* too long path */
196 errno = EINVAL; 199 errno = EINVAL;
197 return -1; 200 return -1;
198 } 201 }
199 202
205 } 208 }
206 209
207 net_set_nonblock(fd, TRUE); 210 net_set_nonblock(fd, TRUE);
208 211
209 /* connect */ 212 /* connect */
210 ret = connect(fd, (void *)&sa, sizeof(sa)); 213 ret = connect(fd, &sa.sa, sizeof(sa));
211 if (ret < 0 && errno != EINPROGRESS) { 214 if (ret < 0 && errno != EINPROGRESS) {
212 close_keep_errno(fd); 215 close_keep_errno(fd);
213 return -1; 216 return -1;
214 } 217 }
215 218
321 return -1; 324 return -1;
322 } 325 }
323 326
324 int net_listen_unix(const char *path, int backlog) 327 int net_listen_unix(const char *path, int backlog)
325 { 328 {
326 struct sockaddr_un sa; 329 union {
330 struct sockaddr sa;
331 struct sockaddr_un un;
332 } sa;
327 int fd; 333 int fd;
328 334
329 memset(&sa, 0, sizeof(sa)); 335 memset(&sa, 0, sizeof(sa));
330 sa.sun_family = AF_UNIX; 336 sa.un.sun_family = AF_UNIX;
331 if (strocpy(sa.sun_path, path, sizeof(sa.sun_path)) < 0) { 337 if (strocpy(sa.un.sun_path, path, sizeof(sa.un.sun_path)) < 0) {
332 /* too long path */ 338 /* too long path */
333 errno = EINVAL; 339 errno = EINVAL;
334 return -1; 340 return -1;
335 } 341 }
336 342
340 i_error("socket() failed: %m"); 346 i_error("socket() failed: %m");
341 return -1; 347 return -1;
342 } 348 }
343 349
344 /* bind */ 350 /* bind */
345 if (bind(fd, (void *)&sa, sizeof(sa)) < 0) { 351 if (bind(fd, &sa.sa, sizeof(sa)) < 0) {
346 if (errno != EADDRINUSE) 352 if (errno != EADDRINUSE)
347 i_error("bind(%s) failed: %m", path); 353 i_error("bind(%s) failed: %m", path);
348 } else { 354 } else {
349 /* start listening */ 355 /* start listening */
350 if (listen(fd, backlog) == 0) 356 if (listen(fd, backlog) == 0)