Commit 39b6efc8067de56d9955d015e4a7472877a3fe89

Authored by vibi
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 161  
162 162 /* create socket + bind */
163 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 167 slisten = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
168   - if (slisten < 0) {
  168 + if (slisten < 0) {
169 169 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
170 170 inet_strfamily(e->ai_family), strerror(errno));
171   - continue;
172   - }
  171 + continue;
  172 + }
173 173  
174 174 setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
175 175 #ifdef IPV6_V6ONLY
176 176 if (e->ai_family == PF_INET6) {
177 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 181 #endif
181 182  
... ... @@ -183,7 +184,7 @@ int inet_listen(const char *str, char *ostr, int olen,
183 184 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
184 185 if (sockets_debug)
185 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 188 goto listen;
188 189 }
189 190 try_next = to && (inet_getport(e) <= to + port_offset);
... ... @@ -207,6 +208,7 @@ listen:
207 208 if (listen(slisten,1) != 0) {
208 209 perror("listen");
209 210 closesocket(slisten);
  211 + freeaddrinfo(res);
210 212 return -1;
211 213 }
212 214 if (ostr) {
... ... @@ -278,35 +280,35 @@ int inet_connect(const char *str, int socktype)
278 280 inet_print_addrinfo(__FUNCTION__, res);
279 281  
280 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 286 fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
285   - continue;
286   - }
  287 + continue;
  288 + }
287 289 sock = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
288   - if (sock < 0) {
  290 + if (sock < 0) {
289 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 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 299 if (sockets_debug || NULL == e->ai_next)
298 300 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
299 301 inet_strfamily(e->ai_family),
300 302 e->ai_canonname, uaddr, uport, strerror(errno));
301 303 closesocket(sock);
302   - continue;
303   - }
  304 + continue;
  305 + }
304 306 if (sockets_debug)
305 307 fprintf(stderr, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__,
306 308 inet_strfamily(e->ai_family),
307 309 e->ai_canonname, uaddr, uport);
308 310 freeaddrinfo(res);
309   - return sock;
  311 + return sock;
310 312 }
311 313 freeaddrinfo(res);
312 314 return -1;
... ... @@ -322,17 +324,17 @@ int unix_listen(const char *str, char *ostr, int olen)
322 324  
323 325 sock = socket(PF_UNIX, SOCK_STREAM, 0);
324 326 if (sock < 0) {
325   - perror("socket(unix)");
326   - return -1;
  327 + perror("socket(unix)");
  328 + return -1;
327 329 }
328 330  
329 331 opts = strchr(str, ',');
330 332 if (opts) {
331 333 len = opts - str;
332   - path = malloc(len+1);
  334 + path = qemu_malloc(len+1);
333 335 snprintf(path, len+1, "%.*s", len, str);
334 336 } else
335   - path = strdup(str);
  337 + path = qemu_strdup(str);
336 338  
337 339 memset(&un, 0, sizeof(un));
338 340 un.sun_family = AF_UNIX;
... ... @@ -365,11 +367,11 @@ int unix_listen(const char *str, char *ostr, int olen)
365 367  
366 368 if (sockets_debug)
367 369 fprintf(stderr, "bind(unix:%s): OK\n", un.sun_path);
368   - free(path);
  370 + qemu_free(path);
369 371 return sock;
370 372  
371 373 err:
372   - free(path);
  374 + qemu_free(path);
373 375 closesocket(sock);
374 376 return -1;
375 377 }
... ... @@ -381,8 +383,8 @@ int unix_connect(const char *path)
381 383  
382 384 sock = socket(PF_UNIX, SOCK_STREAM, 0);
383 385 if (sock < 0) {
384   - perror("socket(unix)");
385   - return -1;
  386 + perror("socket(unix)");
  387 + return -1;
386 388 }
387 389  
388 390 memset(&un, 0, sizeof(un));
... ...