Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / src / wps / http_client.c
index 9b53b80..cdf3a51 100644 (file)
@@ -2,14 +2,8 @@
  * http_client - HTTP client
  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
  */
 
 #include "includes.h"
@@ -91,15 +85,16 @@ static void http_client_tx_ready(int sock, void *eloop_ctx, void *sock_ctx)
 {
        struct http_client *c = eloop_ctx;
        int res;
+       size_t send_len;
 
+       send_len = wpabuf_len(c->req) - c->req_pos;
        wpa_printf(MSG_DEBUG, "HTTP: Send client request to %s:%d (%lu of %lu "
                   "bytes remaining)",
                   inet_ntoa(c->dst.sin_addr), ntohs(c->dst.sin_port),
                   (unsigned long) wpabuf_len(c->req),
-                  (unsigned long) wpabuf_len(c->req) - c->req_pos);
+                  (unsigned long) send_len);
 
-       res = send(c->sd, wpabuf_head(c->req) + c->req_pos,
-                  wpabuf_len(c->req) - c->req_pos, 0);
+       res = send(c->sd, wpabuf_head_u8(c->req) + c->req_pos, send_len, 0);
        if (res < 0) {
                wpa_printf(MSG_DEBUG, "HTTP: Failed to send buffer: %s",
                           strerror(errno));
@@ -108,12 +103,11 @@ static void http_client_tx_ready(int sock, void *eloop_ctx, void *sock_ctx)
                return;
        }
 
-       if ((size_t) res < wpabuf_len(c->req) - c->req_pos) {
+       if ((size_t) res < send_len) {
                wpa_printf(MSG_DEBUG, "HTTP: Sent %d of %lu bytes; %lu bytes "
                           "remaining",
                           res, (unsigned long) wpabuf_len(c->req),
-                          (unsigned long) wpabuf_len(c->req) - c->req_pos -
-                          res);
+                          (unsigned long) send_len - res);
                c->req_pos += res;
                return;
        }
@@ -152,24 +146,20 @@ struct http_client * http_client_addr(struct sockaddr_in *dst,
        c->cb_ctx = cb_ctx;
 
        c->sd = socket(AF_INET, SOCK_STREAM, 0);
-       if (c->sd < 0) {
-               http_client_free(c);
-               return NULL;
-       }
+       if (c->sd < 0)
+               goto fail;
 
        if (fcntl(c->sd, F_SETFL, O_NONBLOCK) != 0) {
                wpa_printf(MSG_DEBUG, "HTTP: fnctl(O_NONBLOCK) failed: %s",
                           strerror(errno));
-               http_client_free(c);
-               return NULL;
+               goto fail;
        }
 
        if (connect(c->sd, (struct sockaddr *) dst, sizeof(*dst))) {
                if (errno != EINPROGRESS) {
                        wpa_printf(MSG_DEBUG, "HTTP: Failed to connect: %s",
                                   strerror(errno));
-                       http_client_free(c);
-                       return NULL;
+                       goto fail;
                }
 
                /*
@@ -179,20 +169,18 @@ struct http_client * http_client_addr(struct sockaddr_in *dst,
        }
 
        if (eloop_register_sock(c->sd, EVENT_TYPE_WRITE, http_client_tx_ready,
-                               c, NULL)) {
-               http_client_free(c);
-               return NULL;
-       }
-
-       if (eloop_register_timeout(HTTP_CLIENT_TIMEOUT_SEC, 0,
-                                  http_client_timeout, c, NULL)) {
-               http_client_free(c);
-               return NULL;
-       }
+                               c, NULL) ||
+           eloop_register_timeout(HTTP_CLIENT_TIMEOUT_SEC, 0,
+                                  http_client_timeout, c, NULL))
+               goto fail;
 
        c->req = req;
 
        return c;
+
+fail:
+       http_client_free(c);
+       return NULL;
 }