Blame SOURCES/0001-Simplify-sendto_kdc.c.patch

4be148
From 42b3c2ed11c1e62c1691f868a6796983f93c3beb Mon Sep 17 00:00:00 2001
4be148
From: Greg Hudson <ghudson@mit.edu>
4be148
Date: Wed, 9 Apr 2014 13:19:03 -0400
4be148
Subject: [PATCH 01/13] Simplify sendto_kdc.c
4be148
4be148
* Get rid of the "x" member of conn_state, which used to be a union
4be148
  but hasn't been since r14742.
4be148
* Define a structure type for the "out" member of conn_state.
4be148
* Rename incoming_krb5_message to incoming_message for brevity.
4be148
* Make the "pos" member of incoming_message an offset instead of a
4be148
  pointer, simplifying several present and future computations.
4be148
* Use "in" and "out" aliases to the conn_state in and out members
4be148
  where it improves brevity.
4be148
* Rename set_conn_state_msg_length to set_transport_message and give
4be148
  it a descriptive comment.
4be148
* Call set_transport_message from start_connection only, instead of
4be148
  once in add_connection and perhaps again in start_connection.  To
4be148
  make this possible, pass the original message argument to maybe_send
4be148
  and start_connection.
4be148
* Use make_data and empty_data helpers where appropriate.
4be148
---
4be148
 src/lib/krb5/os/sendto_kdc.c | 159 +++++++++++++++++++++----------------------
4be148
 1 file changed, 79 insertions(+), 80 deletions(-)
4be148
4be148
diff --git a/src/lib/krb5/os/sendto_kdc.c b/src/lib/krb5/os/sendto_kdc.c
4be148
index 67e2a60..5f781d3 100644
4be148
--- a/src/lib/krb5/os/sendto_kdc.c
4be148
+++ b/src/lib/krb5/os/sendto_kdc.c
4be148
@@ -76,30 +76,30 @@ static const char *const state_strings[] = {
4be148
 
4be148
 /* connection states */
4be148
 enum conn_states { INITIALIZING, CONNECTING, WRITING, READING, FAILED };
4be148
-struct incoming_krb5_message {
4be148
+struct incoming_message {
4be148
     size_t bufsizebytes_read;
4be148
     size_t bufsize;
4be148
+    size_t pos;
4be148
     char *buf;
4be148
-    char *pos;
4be148
     unsigned char bufsizebytes[4];
4be148
     size_t n_left;
4be148
 };
4be148
 
4be148
+struct outgoing_message {
4be148
+    sg_buf sgbuf[2];
4be148
+    sg_buf *sgp;
4be148
+    int sg_count;
4be148
+    unsigned char msg_len_buf[4];
4be148
+};
4be148
+
4be148
 struct conn_state {
4be148
     SOCKET fd;
4be148
     enum conn_states state;
4be148
     int (*service)(krb5_context context, struct conn_state *,
4be148
                    struct select_state *, int);
4be148
     struct remote_address addr;
4be148
-    struct {
4be148
-        struct {
4be148
-            sg_buf sgbuf[2];
4be148
-            sg_buf *sgp;
4be148
-            int sg_count;
4be148
-            unsigned char msg_len_buf[4];
4be148
-        } out;
4be148
-        struct incoming_krb5_message in;
4be148
-    } x;
4be148
+    struct incoming_message in;
4be148
+    struct outgoing_message out;
4be148
     krb5_data callback_buffer;
4be148
     size_t server_index;
4be148
     struct conn_state *next;
4be148
@@ -461,30 +461,31 @@ static int service_tcp_fd(krb5_context context, struct conn_state *conn,
4be148
 static int service_udp_fd(krb5_context context, struct conn_state *conn,
4be148
                           struct select_state *selstate, int ssflags);
4be148
 
4be148
+/* Set up the actual message we will send across the underlying transport to
4be148
+ * communicate the payload message, using one or both of state->out.sgbuf. */
4be148
 static void
4be148
-set_conn_state_msg_length (struct conn_state *state, const krb5_data *message)
4be148
+set_transport_message(struct conn_state *state, const krb5_data *message)
4be148
 {
4be148
-    if (!message || message->length == 0)
4be148
+    struct outgoing_message *out = &state->out;
4be148
+
4be148
+    if (message == NULL || message->length == 0)
4be148
         return;
4be148
 
4be148
     if (state->addr.type == SOCK_STREAM) {
4be148
-        store_32_be(message->length, state->x.out.msg_len_buf);
4be148
-        SG_SET(&state->x.out.sgbuf[0], state->x.out.msg_len_buf, 4);
4be148
-        SG_SET(&state->x.out.sgbuf[1], message->data, message->length);
4be148
-        state->x.out.sg_count = 2;
4be148
-
4be148
+        store_32_be(message->length, out->msg_len_buf);
4be148
+        SG_SET(&out->sgbuf[0], out->msg_len_buf, 4);
4be148
+        SG_SET(&out->sgbuf[1], message->data, message->length);
4be148
+        out->sg_count = 2;
4be148
     } else {
4be148
-
4be148
-        SG_SET(&state->x.out.sgbuf[0], message->data, message->length);
4be148
-        SG_SET(&state->x.out.sgbuf[1], 0, 0);
4be148
-        state->x.out.sg_count = 1;
4be148
-
4be148
+        SG_SET(&out->sgbuf[0], message->data, message->length);
4be148
+        SG_SET(&out->sgbuf[1], NULL, 0);
4be148
+        out->sg_count = 1;
4be148
     }
4be148
 }
4be148
 
4be148
 static krb5_error_code
4be148
 add_connection(struct conn_state **conns, struct addrinfo *ai,
4be148
-               size_t server_index, const krb5_data *message, char **udpbufp)
4be148
+               size_t server_index, char **udpbufp)
4be148
 {
4be148
     struct conn_state *state, **tailptr;
4be148
 
4be148
@@ -492,28 +493,26 @@ add_connection(struct conn_state **conns, struct addrinfo *ai,
4be148
     if (state == NULL)
4be148
         return ENOMEM;
4be148
     state->state = INITIALIZING;
4be148
-    state->x.out.sgp = state->x.out.sgbuf;
4be148
+    state->out.sgp = state->out.sgbuf;
4be148
     state->addr.type = ai->ai_socktype;
4be148
     state->addr.family = ai->ai_family;
4be148
     state->addr.len = ai->ai_addrlen;
4be148
     memcpy(&state->addr.saddr, ai->ai_addr, ai->ai_addrlen);
4be148
     state->fd = INVALID_SOCKET;
4be148
     state->server_index = server_index;
4be148
-    SG_SET(&state->x.out.sgbuf[1], 0, 0);
4be148
+    SG_SET(&state->out.sgbuf[1], NULL, 0);
4be148
     if (ai->ai_socktype == SOCK_STREAM) {
4be148
         state->service = service_tcp_fd;
4be148
-        set_conn_state_msg_length (state, message);
4be148
     } else {
4be148
         state->service = service_udp_fd;
4be148
-        set_conn_state_msg_length (state, message);
4be148
 
4be148
         if (*udpbufp == NULL) {
4be148
             *udpbufp = malloc(MAX_DGRAM_SIZE);
4be148
             if (*udpbufp == 0)
4be148
                 return ENOMEM;
4be148
         }
4be148
-        state->x.in.buf = *udpbufp;
4be148
-        state->x.in.bufsize = MAX_DGRAM_SIZE;
4be148
+        state->in.buf = *udpbufp;
4be148
+        state->in.bufsize = MAX_DGRAM_SIZE;
4be148
     }
4be148
 
4be148
     /* Chain the new state onto the tail of the list. */
4be148
@@ -597,7 +596,7 @@ resolve_server(krb5_context context, const struct serverlist *servers,
4be148
         ai.ai_family = entry->family;
4be148
         ai.ai_addrlen = entry->addrlen;
4be148
         ai.ai_addr = (struct sockaddr *)&entry->addr;
4be148
-        return add_connection(conns, &ai, ind, message, udpbufp);
4be148
+        return add_connection(conns, &ai, ind, udpbufp);
4be148
     }
4be148
 
4be148
     memset(&hint, 0, sizeof(hint));
4be148
@@ -617,12 +616,12 @@ resolve_server(krb5_context context, const struct serverlist *servers,
4be148
     /* Add each address with the preferred socktype. */
4be148
     retval = 0;
4be148
     for (a = addrs; a != 0 && retval == 0; a = a->ai_next)
4be148
-        retval = add_connection(conns, a, ind, message, udpbufp);
4be148
+        retval = add_connection(conns, a, ind, udpbufp);
4be148
     if (retval == 0 && entry->socktype == 0 && socktype2 != 0) {
4be148
         /* Add each address again with the non-preferred socktype. */
4be148
         for (a = addrs; a != 0 && retval == 0; a = a->ai_next) {
4be148
             a->ai_socktype = socktype2;
4be148
-            retval = add_connection(conns, a, ind, message, udpbufp);
4be148
+            retval = add_connection(conns, a, ind, udpbufp);
4be148
         }
4be148
     }
4be148
     freeaddrinfo(addrs);
4be148
@@ -631,7 +630,7 @@ resolve_server(krb5_context context, const struct serverlist *servers,
4be148
 
4be148
 static int
4be148
 start_connection(krb5_context context, struct conn_state *state,
4be148
-                 struct select_state *selstate,
4be148
+                 const krb5_data *message, struct select_state *selstate,
4be148
                  struct sendto_callback_info *callback_info)
4be148
 {
4be148
     int fd, e;
4be148
@@ -689,13 +688,14 @@ start_connection(krb5_context context, struct conn_state *state,
4be148
             return -3;
4be148
         }
4be148
 
4be148
-        set_conn_state_msg_length(state, &state->callback_buffer);
4be148
+        message = &state->callback_buffer;
4be148
     }
4be148
+    set_transport_message(state, message);
4be148
 
4be148
     if (state->addr.type == SOCK_DGRAM) {
4be148
         /* Send it now.  */
4be148
         ssize_t ret;
4be148
-        sg_buf *sg = &state->x.out.sgbuf[0];
4be148
+        sg_buf *sg = &state->out.sgbuf[0];
4be148
 
4be148
         TRACE_SENDTO_KDC_UDP_SEND_INITIAL(context, &state->addr);
4be148
         ret = send(state->fd, SG_BUF(sg), SG_LEN(sg), 0);
4be148
@@ -731,14 +731,16 @@ start_connection(krb5_context context, struct conn_state *state,
4be148
    next connection.  */
4be148
 static int
4be148
 maybe_send(krb5_context context, struct conn_state *conn,
4be148
-           struct select_state *selstate,
4be148
+           const krb5_data *message, struct select_state *selstate,
4be148
            struct sendto_callback_info *callback_info)
4be148
 {
4be148
     sg_buf *sg;
4be148
     ssize_t ret;
4be148
 
4be148
-    if (conn->state == INITIALIZING)
4be148
-        return start_connection(context, conn, selstate, callback_info);
4be148
+    if (conn->state == INITIALIZING) {
4be148
+        return start_connection(context, conn, message, selstate,
4be148
+                                callback_info);
4be148
+    }
4be148
 
4be148
     /* Did we already shut down this channel?  */
4be148
     if (conn->state == FAILED) {
4be148
@@ -752,7 +754,7 @@ maybe_send(krb5_context context, struct conn_state *conn,
4be148
     }
4be148
 
4be148
     /* UDP - retransmit after a previous attempt timed out. */
4be148
-    sg = &conn->x.out.sgbuf[0];
4be148
+    sg = &conn->out.sgbuf[0];
4be148
     TRACE_SENDTO_KDC_UDP_SEND_RETRY(context, &conn->addr);
4be148
     ret = send(conn->fd, SG_BUF(sg), SG_LEN(sg), 0);
4be148
     if (ret < 0 || (size_t) ret != SG_LEN(sg)) {
4be148
@@ -803,6 +805,8 @@ service_tcp_fd(krb5_context context, struct conn_state *conn,
4be148
     int e = 0;
4be148
     ssize_t nwritten, nread;
4be148
     SOCKET_WRITEV_TEMP tmp;
4be148
+    struct incoming_message *in = &conn->in;
4be148
+    struct outgoing_message *out = &conn->out;
4be148
 
4be148
     /* Check for a socket exception. */
4be148
     if (ssflags & SSF_EXCEPTION)
4be148
@@ -825,68 +829,68 @@ service_tcp_fd(krb5_context context, struct conn_state *conn,
4be148
         /* Fall through. */
4be148
     case WRITING:
4be148
         TRACE_SENDTO_KDC_TCP_SEND(context, &conn->addr);
4be148
-        nwritten = SOCKET_WRITEV(conn->fd, conn->x.out.sgp,
4be148
-                                 conn->x.out.sg_count, tmp);
4be148
+        nwritten = SOCKET_WRITEV(conn->fd, out->sgp, out->sg_count, tmp);
4be148
         if (nwritten < 0) {
4be148
             TRACE_SENDTO_KDC_TCP_ERROR_SEND(context, &conn->addr,
4be148
                                             SOCKET_ERRNO);
4be148
             goto kill_conn;
4be148
         }
4be148
         while (nwritten) {
4be148
-            sg_buf *sgp = conn->x.out.sgp;
4be148
+            sg_buf *sgp = out->sgp;
4be148
             if ((size_t) nwritten < SG_LEN(sgp)) {
4be148
                 SG_ADVANCE(sgp, (size_t) nwritten);
4be148
                 nwritten = 0;
4be148
             } else {
4be148
                 nwritten -= SG_LEN(sgp);
4be148
-                conn->x.out.sgp++;
4be148
-                conn->x.out.sg_count--;
4be148
+                out->sgp++;
4be148
+                out->sg_count--;
4be148
             }
4be148
         }
4be148
-        if (conn->x.out.sg_count == 0) {
4be148
+        if (out->sg_count == 0) {
4be148
             /* Done writing, switch to reading. */
4be148
             cm_read(selstate, conn->fd);
4be148
             conn->state = READING;
4be148
-            conn->x.in.bufsizebytes_read = 0;
4be148
-            conn->x.in.bufsize = 0;
4be148
-            conn->x.in.buf = 0;
4be148
-            conn->x.in.pos = 0;
4be148
-            conn->x.in.n_left = 0;
4be148
+            in->bufsizebytes_read = 0;
4be148
+            in->bufsize = 0;
4be148
+            in->pos = 0;
4be148
+            in->buf = NULL;
4be148
+            in->n_left = 0;
4be148
         }
4be148
         return 0;
4be148
 
4be148
     case READING:
4be148
-        if (conn->x.in.bufsizebytes_read == 4) {
4be148
+        if (in->bufsizebytes_read == 4) {
4be148
             /* Reading data.  */
4be148
-            nread = SOCKET_READ(conn->fd, conn->x.in.pos, conn->x.in.n_left);
4be148
+            nread = SOCKET_READ(conn->fd, &in->buf[in->pos], in->n_left);
4be148
             if (nread <= 0) {
4be148
                 e = nread ? SOCKET_ERRNO : ECONNRESET;
4be148
                 TRACE_SENDTO_KDC_TCP_ERROR_RECV(context, &conn->addr, e);
4be148
                 goto kill_conn;
4be148
             }
4be148
-            conn->x.in.n_left -= nread;
4be148
-            conn->x.in.pos += nread;
4be148
-            if (conn->x.in.n_left <= 0)
4be148
+            in->n_left -= nread;
4be148
+            in->pos += nread;
4be148
+            if (in->n_left <= 0)
4be148
                 return 1;
4be148
         } else {
4be148
             /* Reading length.  */
4be148
             nread = SOCKET_READ(conn->fd,
4be148
-                                conn->x.in.bufsizebytes + conn->x.in.bufsizebytes_read,
4be148
-                                4 - conn->x.in.bufsizebytes_read);
4be148
+                                in->bufsizebytes + in->bufsizebytes_read,
4be148
+                                4 - in->bufsizebytes_read);
4be148
             if (nread <= 0) {
4be148
                 e = nread ? SOCKET_ERRNO : ECONNRESET;
4be148
                 TRACE_SENDTO_KDC_TCP_ERROR_RECV_LEN(context, &conn->addr, e);
4be148
                 goto kill_conn;
4be148
             }
4be148
-            conn->x.in.bufsizebytes_read += nread;
4be148
-            if (conn->x.in.bufsizebytes_read == 4) {
4be148
-                unsigned long len = load_32_be (conn->x.in.bufsizebytes);
4be148
+            in->bufsizebytes_read += nread;
4be148
+            if (in->bufsizebytes_read == 4) {
4be148
+                unsigned long len = load_32_be(in->bufsizebytes);
4be148
                 /* Arbitrary 1M cap.  */
4be148
                 if (len > 1 * 1024 * 1024)
4be148
                     goto kill_conn;
4be148
-                conn->x.in.bufsize = conn->x.in.n_left = len;
4be148
-                conn->x.in.buf = conn->x.in.pos = malloc(len);
4be148
-                if (conn->x.in.buf == 0)
4be148
+                in->bufsize = in->n_left = len;
4be148
+                in->pos = 0;
4be148
+                in->buf = malloc(len);
4be148
+                if (in->buf == NULL)
4be148
                     goto kill_conn;
4be148
             }
4be148
         }
4be148
@@ -915,13 +919,13 @@ service_udp_fd(krb5_context context, struct conn_state *conn,
4be148
     if (conn->state != READING)
4be148
         abort();
4be148
 
4be148
-    nread = recv(conn->fd, conn->x.in.buf, conn->x.in.bufsize, 0);
4be148
+    nread = recv(conn->fd, conn->in.buf, conn->in.bufsize, 0);
4be148
     if (nread < 0) {
4be148
         TRACE_SENDTO_KDC_UDP_ERROR_RECV(context, &conn->addr, SOCKET_ERRNO);
4be148
         kill_conn(conn, selstate);
4be148
         return 0;
4be148
     }
4be148
-    conn->x.in.pos = conn->x.in.buf + nread;
4be148
+    conn->in.pos = nread;
4be148
     return 1;
4be148
 }
4be148
 
4be148
@@ -986,10 +990,7 @@ service_fds(krb5_context context, struct select_state *selstate,
4be148
                 int stop = 1;
4be148
 
4be148
                 if (msg_handler != NULL) {
4be148
-                    krb5_data reply;
4be148
-
4be148
-                    reply.data = state->x.in.buf;
4be148
-                    reply.length = state->x.in.pos - state->x.in.buf;
4be148
+                    krb5_data reply = make_data(state->in.buf, state->in.pos);
4be148
 
4be148
                     stop = (msg_handler(context, &reply, msg_handler_data) != 0);
4be148
                 }
4be148
@@ -1051,8 +1052,7 @@ k5_sendto(krb5_context context, const krb5_data *message,
4be148
     char *udpbuf = NULL;
4be148
     krb5_boolean done = FALSE;
4be148
 
4be148
-    reply->data = 0;
4be148
-    reply->length = 0;
4be148
+    *reply = empty_data();
4be148
 
4be148
     /* One for use here, listing all our fds in use, and one for
4be148
      * temporary use in service_fds, for the fds of interest.  */
4be148
@@ -1077,7 +1077,7 @@ k5_sendto(krb5_context context, const krb5_data *message,
4be148
             /* Contact each new connection whose socktype matches socktype1. */
4be148
             if (state->addr.type != socktype1)
4be148
                 continue;
4be148
-            if (maybe_send(context, state, sel_state, callback_info))
4be148
+            if (maybe_send(context, state, message, sel_state, callback_info))
4be148
                 continue;
4be148
             done = service_fds(context, sel_state, 1000, conns, seltemp,
4be148
                                msg_handler, msg_handler_data, &winner);
4be148
@@ -1089,7 +1089,7 @@ k5_sendto(krb5_context context, const krb5_data *message,
4be148
     for (state = conns; state != NULL && !done; state = state->next) {
4be148
         if (state->addr.type != socktype2)
4be148
             continue;
4be148
-        if (maybe_send(context, state, sel_state, callback_info))
4be148
+        if (maybe_send(context, state, message, sel_state, callback_info))
4be148
             continue;
4be148
         done = service_fds(context, sel_state, 1000, conns, seltemp,
4be148
                            msg_handler, msg_handler_data, &winner);
4be148
@@ -1105,7 +1105,7 @@ k5_sendto(krb5_context context, const krb5_data *message,
4be148
     delay = 4000;
4be148
     for (pass = 1; pass < MAX_PASS && !done; pass++) {
4be148
         for (state = conns; state != NULL && !done; state = state->next) {
4be148
-            if (maybe_send(context, state, sel_state, callback_info))
4be148
+            if (maybe_send(context, state, message, sel_state, callback_info))
4be148
                 continue;
4be148
             done = service_fds(context, sel_state, 1000, conns, seltemp,
4be148
                                msg_handler, msg_handler_data, &winner);
4be148
@@ -1127,10 +1127,9 @@ k5_sendto(krb5_context context, const krb5_data *message,
4be148
         goto cleanup;
4be148
     }
4be148
     /* Success!  */
4be148
-    reply->data = winner->x.in.buf;
4be148
-    reply->length = winner->x.in.pos - winner->x.in.buf;
4be148
+    *reply = make_data(winner->in.buf, winner->in.pos);
4be148
     retval = 0;
4be148
-    winner->x.in.buf = NULL;
4be148
+    winner->in.buf = NULL;
4be148
     if (server_used != NULL)
4be148
         *server_used = winner->server_index;
4be148
     if (remoteaddr != NULL && remoteaddrlen != 0 && *remoteaddrlen > 0)
4be148
@@ -1142,8 +1141,8 @@ cleanup:
4be148
         next = state->next;
4be148
         if (state->fd != INVALID_SOCKET)
4be148
             closesocket(state->fd);
4be148
-        if (state->state == READING && state->x.in.buf != udpbuf)
4be148
-            free(state->x.in.buf);
4be148
+        if (state->state == READING && state->in.buf != udpbuf)
4be148
+            free(state->in.buf);
4be148
         if (callback_info) {
4be148
             callback_info->pfn_cleanup(callback_info->data,
4be148
                                        &state->callback_buffer);
4be148
-- 
4be148
2.1.0
4be148