Commit 39b6efc8067de56d9955d015e4a7472877a3fe89
Committed by
Anthony Liguori
1 parent
4e12cd94
Fix in file qemu-sockets.c
1) Changed usage of malloc,free,strdup to qemu_malloc,qemu_free,qemu_strdup 2) Some coding style fixes (based on CODING_STYLE document) 3) Free struct addrinfo *res after failure of listen Signed-off-by: vibi <vibi_sreenivasan@cms.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing
1 changed file
with
32 additions
and
30 deletions
qemu-sockets.c
| @@ -161,21 +161,22 @@ int inet_listen(const char *str, char *ostr, int olen, | @@ -161,21 +161,22 @@ int inet_listen(const char *str, char *ostr, int olen, | ||
| 161 | 161 | ||
| 162 | /* create socket + bind */ | 162 | /* create socket + bind */ |
| 163 | for (e = res; e != NULL; e = e->ai_next) { | 163 | for (e = res; e != NULL; e = e->ai_next) { |
| 164 | - getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, | ||
| 165 | - uaddr,INET6_ADDRSTRLEN,uport,32, | ||
| 166 | - NI_NUMERICHOST | NI_NUMERICSERV); | 164 | + getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, |
| 165 | + uaddr,INET6_ADDRSTRLEN,uport,32, | ||
| 166 | + NI_NUMERICHOST | NI_NUMERICSERV); | ||
| 167 | slisten = socket(e->ai_family, e->ai_socktype, e->ai_protocol); | 167 | slisten = socket(e->ai_family, e->ai_socktype, e->ai_protocol); |
| 168 | - if (slisten < 0) { | 168 | + if (slisten < 0) { |
| 169 | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, | 169 | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, |
| 170 | inet_strfamily(e->ai_family), strerror(errno)); | 170 | inet_strfamily(e->ai_family), strerror(errno)); |
| 171 | - continue; | ||
| 172 | - } | 171 | + continue; |
| 172 | + } | ||
| 173 | 173 | ||
| 174 | setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); | 174 | setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); |
| 175 | #ifdef IPV6_V6ONLY | 175 | #ifdef IPV6_V6ONLY |
| 176 | if (e->ai_family == PF_INET6) { | 176 | if (e->ai_family == PF_INET6) { |
| 177 | /* listen on both ipv4 and ipv6 */ | 177 | /* listen on both ipv4 and ipv6 */ |
| 178 | - setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,sizeof(off)); | 178 | + setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off, |
| 179 | + sizeof(off)); | ||
| 179 | } | 180 | } |
| 180 | #endif | 181 | #endif |
| 181 | 182 | ||
| @@ -183,7 +184,7 @@ int inet_listen(const char *str, char *ostr, int olen, | @@ -183,7 +184,7 @@ int inet_listen(const char *str, char *ostr, int olen, | ||
| 183 | if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) { | 184 | if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) { |
| 184 | if (sockets_debug) | 185 | if (sockets_debug) |
| 185 | fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__, | 186 | fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__, |
| 186 | - inet_strfamily(e->ai_family), uaddr, inet_getport(e)); | 187 | + inet_strfamily(e->ai_family), uaddr, inet_getport(e)); |
| 187 | goto listen; | 188 | goto listen; |
| 188 | } | 189 | } |
| 189 | try_next = to && (inet_getport(e) <= to + port_offset); | 190 | try_next = to && (inet_getport(e) <= to + port_offset); |
| @@ -207,6 +208,7 @@ listen: | @@ -207,6 +208,7 @@ listen: | ||
| 207 | if (listen(slisten,1) != 0) { | 208 | if (listen(slisten,1) != 0) { |
| 208 | perror("listen"); | 209 | perror("listen"); |
| 209 | closesocket(slisten); | 210 | closesocket(slisten); |
| 211 | + freeaddrinfo(res); | ||
| 210 | return -1; | 212 | return -1; |
| 211 | } | 213 | } |
| 212 | if (ostr) { | 214 | if (ostr) { |
| @@ -278,35 +280,35 @@ int inet_connect(const char *str, int socktype) | @@ -278,35 +280,35 @@ int inet_connect(const char *str, int socktype) | ||
| 278 | inet_print_addrinfo(__FUNCTION__, res); | 280 | inet_print_addrinfo(__FUNCTION__, res); |
| 279 | 281 | ||
| 280 | for (e = res; e != NULL; e = e->ai_next) { | 282 | for (e = res; e != NULL; e = e->ai_next) { |
| 281 | - if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, | ||
| 282 | - uaddr,INET6_ADDRSTRLEN,uport,32, | ||
| 283 | - NI_NUMERICHOST | NI_NUMERICSERV) != 0) { | 283 | + if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, |
| 284 | + uaddr,INET6_ADDRSTRLEN,uport,32, | ||
| 285 | + NI_NUMERICHOST | NI_NUMERICSERV) != 0) { | ||
| 284 | fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__); | 286 | fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__); |
| 285 | - continue; | ||
| 286 | - } | 287 | + continue; |
| 288 | + } | ||
| 287 | sock = socket(e->ai_family, e->ai_socktype, e->ai_protocol); | 289 | sock = socket(e->ai_family, e->ai_socktype, e->ai_protocol); |
| 288 | - if (sock < 0) { | 290 | + if (sock < 0) { |
| 289 | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, | 291 | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, |
| 290 | - inet_strfamily(e->ai_family), strerror(errno)); | ||
| 291 | - continue; | ||
| 292 | - } | 292 | + inet_strfamily(e->ai_family), strerror(errno)); |
| 293 | + continue; | ||
| 294 | + } | ||
| 293 | setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); | 295 | setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); |
| 294 | 296 | ||
| 295 | - /* connect to peer */ | ||
| 296 | - if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) { | 297 | + /* connect to peer */ |
| 298 | + if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) { | ||
| 297 | if (sockets_debug || NULL == e->ai_next) | 299 | if (sockets_debug || NULL == e->ai_next) |
| 298 | fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__, | 300 | fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__, |
| 299 | inet_strfamily(e->ai_family), | 301 | inet_strfamily(e->ai_family), |
| 300 | e->ai_canonname, uaddr, uport, strerror(errno)); | 302 | e->ai_canonname, uaddr, uport, strerror(errno)); |
| 301 | closesocket(sock); | 303 | closesocket(sock); |
| 302 | - continue; | ||
| 303 | - } | 304 | + continue; |
| 305 | + } | ||
| 304 | if (sockets_debug) | 306 | if (sockets_debug) |
| 305 | fprintf(stderr, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__, | 307 | fprintf(stderr, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__, |
| 306 | inet_strfamily(e->ai_family), | 308 | inet_strfamily(e->ai_family), |
| 307 | e->ai_canonname, uaddr, uport); | 309 | e->ai_canonname, uaddr, uport); |
| 308 | freeaddrinfo(res); | 310 | freeaddrinfo(res); |
| 309 | - return sock; | 311 | + return sock; |
| 310 | } | 312 | } |
| 311 | freeaddrinfo(res); | 313 | freeaddrinfo(res); |
| 312 | return -1; | 314 | return -1; |
| @@ -322,17 +324,17 @@ int unix_listen(const char *str, char *ostr, int olen) | @@ -322,17 +324,17 @@ int unix_listen(const char *str, char *ostr, int olen) | ||
| 322 | 324 | ||
| 323 | sock = socket(PF_UNIX, SOCK_STREAM, 0); | 325 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 324 | if (sock < 0) { | 326 | if (sock < 0) { |
| 325 | - perror("socket(unix)"); | ||
| 326 | - return -1; | 327 | + perror("socket(unix)"); |
| 328 | + return -1; | ||
| 327 | } | 329 | } |
| 328 | 330 | ||
| 329 | opts = strchr(str, ','); | 331 | opts = strchr(str, ','); |
| 330 | if (opts) { | 332 | if (opts) { |
| 331 | len = opts - str; | 333 | len = opts - str; |
| 332 | - path = malloc(len+1); | 334 | + path = qemu_malloc(len+1); |
| 333 | snprintf(path, len+1, "%.*s", len, str); | 335 | snprintf(path, len+1, "%.*s", len, str); |
| 334 | } else | 336 | } else |
| 335 | - path = strdup(str); | 337 | + path = qemu_strdup(str); |
| 336 | 338 | ||
| 337 | memset(&un, 0, sizeof(un)); | 339 | memset(&un, 0, sizeof(un)); |
| 338 | un.sun_family = AF_UNIX; | 340 | un.sun_family = AF_UNIX; |
| @@ -365,11 +367,11 @@ int unix_listen(const char *str, char *ostr, int olen) | @@ -365,11 +367,11 @@ int unix_listen(const char *str, char *ostr, int olen) | ||
| 365 | 367 | ||
| 366 | if (sockets_debug) | 368 | if (sockets_debug) |
| 367 | fprintf(stderr, "bind(unix:%s): OK\n", un.sun_path); | 369 | fprintf(stderr, "bind(unix:%s): OK\n", un.sun_path); |
| 368 | - free(path); | 370 | + qemu_free(path); |
| 369 | return sock; | 371 | return sock; |
| 370 | 372 | ||
| 371 | err: | 373 | err: |
| 372 | - free(path); | 374 | + qemu_free(path); |
| 373 | closesocket(sock); | 375 | closesocket(sock); |
| 374 | return -1; | 376 | return -1; |
| 375 | } | 377 | } |
| @@ -381,8 +383,8 @@ int unix_connect(const char *path) | @@ -381,8 +383,8 @@ int unix_connect(const char *path) | ||
| 381 | 383 | ||
| 382 | sock = socket(PF_UNIX, SOCK_STREAM, 0); | 384 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 383 | if (sock < 0) { | 385 | if (sock < 0) { |
| 384 | - perror("socket(unix)"); | ||
| 385 | - return -1; | 386 | + perror("socket(unix)"); |
| 387 | + return -1; | ||
| 386 | } | 388 | } |
| 387 | 389 | ||
| 388 | memset(&un, 0, sizeof(un)); | 390 | memset(&un, 0, sizeof(un)); |