[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

PATCH: Fix style of ssh_socket_pollcallback


Hi folks,

The style (mostly the indentation) of ssh_socket_pollcallback makes it
very hard to read.  The attached patch changes the indentation style
of this function to a consistent 4 spaces per indent (as I believe
Andreas indicated is the official style), adds spacing around function
parameters, and adds braces in places with a single-line if body.
Overall, this patch should be a logical no-op.

Thanks,
- Alan
From db349fdfd575542adb105701182753e708a9d370 Mon Sep 17 00:00:00 2001
From: Alan Dunn <amdunn@xxxxxxxxx>
Date: Fri, 21 Mar 2014 09:42:08 -0500
Subject: [PATCH] socket: Fix style of ssh_socket_pollcallback


Signed-off-by: Alan Dunn <amdunn@xxxxxxxxx>
---
 src/socket.c |  219 +++++++++++++++++++++++++++++-----------------------------
 1 file changed, 111 insertions(+), 108 deletions(-)

diff --git a/src/socket.c b/src/socket.c
index ab47b96..498da77 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -204,7 +204,7 @@ void ssh_socket_set_callbacks(ssh_socket s, ssh_socket_callbacks callbacks){
 }
 
 /**
- * @brief 							SSH poll callback. This callback will be used when an event
+ * @brief               SSH poll callback. This callback will be used when an event
  *                      caught on the socket.
  *
  * @param p             Poll object this callback belongs to.
@@ -216,128 +216,131 @@ void ssh_socket_set_callbacks(ssh_socket s, ssh_socket_callbacks callbacks){
  * @return              0 on success, < 0 when the poll object has been removed
  *                      from its poll context.
  */
-int ssh_socket_pollcallback(struct ssh_poll_handle_struct *p, socket_t fd, int revents, void *v_s){
-	ssh_socket s=(ssh_socket )v_s;
-	char buffer[MAX_BUF_SIZE];
-	int r;
-	int err=0;
-	socklen_t errlen=sizeof(err);
-	/* Do not do anything if this socket was already closed */
-	if(!ssh_socket_is_open(s)){
-	  return -1;
-	}
-	if(revents & POLLERR || revents & POLLHUP){
-		/* Check if we are in a connecting state */
-		if(s->state==SSH_SOCKET_CONNECTING){
-			s->state=SSH_SOCKET_ERROR;
-			r = getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&err, &errlen);
+int ssh_socket_pollcallback(struct ssh_poll_handle_struct *p, socket_t fd,
+                            int revents, void *v_s) {
+    ssh_socket s = (ssh_socket)v_s;
+    char buffer[MAX_BUF_SIZE];
+    int r;
+    int err = 0;
+    socklen_t errlen = sizeof(err);
+    /* Do not do anything if this socket was already closed */
+    if (!ssh_socket_is_open(s)) {
+        return -1;
+    }
+    if (revents & POLLERR || revents & POLLHUP) {
+        /* Check if we are in a connecting state */
+        if (s->state == SSH_SOCKET_CONNECTING) {
+            s->state = SSH_SOCKET_ERROR;
+            r = getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&err, &errlen);
             if (r < 0) {
                 err = errno;
             }
-			s->last_errno=err;
-			ssh_socket_close(s);
-			if(s->callbacks && s->callbacks->connected)
-				s->callbacks->connected(SSH_SOCKET_CONNECTED_ERROR,err,
-						s->callbacks->userdata);
-			return -1;
-		}
-		/* Then we are in a more standard kind of error */
-		/* force a read to get an explanation */
-		revents |= POLLIN;
-	}
-	if((revents & POLLIN) && s->state == SSH_SOCKET_CONNECTED){
-		s->read_wontblock=1;
-		r=ssh_socket_unbuffered_read(s,buffer,sizeof(buffer));
-		if(r<0){
-		if(p != NULL) {
-			ssh_poll_remove_events(p, POLLIN);
-		}
-			if(s->callbacks && s->callbacks->exception){
-				s->callbacks->exception(
-						SSH_SOCKET_EXCEPTION_ERROR,
-						s->last_errno,s->callbacks->userdata);
-				/* p may have been freed, so don't use it
-				* anymore in this function */
-				p = NULL;
-				return -2;
-			}
-		}
-		if(r==0){
-			if(p != NULL) {
-				ssh_poll_remove_events(p, POLLIN);
-			}
-			if(p != NULL) {
-				ssh_poll_remove_events(p, POLLIN);
-			}
-			if(s->callbacks && s->callbacks->exception){
-				s->callbacks->exception(
-						SSH_SOCKET_EXCEPTION_EOF,
-						0,s->callbacks->userdata);
-				/* p may have been freed, so don't use it
-				* anymore in this function */
-				p = NULL;
-				return -2;
-			}
-		}
-		if(r>0){
+            s->last_errno = err;
+            ssh_socket_close(s);
+            if (s->callbacks && s->callbacks->connected) {
+                s->callbacks->connected(SSH_SOCKET_CONNECTED_ERROR, err,
+                                        s->callbacks->userdata);
+            }
+            return -1;
+        }
+        /* Then we are in a more standard kind of error */
+        /* force a read to get an explanation */
+        revents |= POLLIN;
+    }
+    if ((revents & POLLIN) && s->state == SSH_SOCKET_CONNECTED) {
+        s->read_wontblock = 1;
+        r = ssh_socket_unbuffered_read(s, buffer, sizeof(buffer));
+        if (r < 0) {
+            if (p != NULL) {
+                ssh_poll_remove_events(p, POLLIN);
+            }
+            if (s->callbacks && s->callbacks->exception) {
+                s->callbacks->exception(SSH_SOCKET_EXCEPTION_ERROR,
+                                        s->last_errno, s->callbacks->userdata);
+                /* p may have been freed, so don't use it
+                 * anymore in this function */
+                p = NULL;
+                return -2;
+            }
+        }
+        if (r == 0) {
+            if (p != NULL) {
+                ssh_poll_remove_events(p, POLLIN);
+            }
+            if (p != NULL) {
+                ssh_poll_remove_events(p, POLLIN);
+            }
+            if (s->callbacks && s->callbacks->exception) {
+                s->callbacks->exception(SSH_SOCKET_EXCEPTION_EOF,
+                                        0, s->callbacks->userdata);
+                /* p may have been freed, so don't use it
+                 * anymore in this function */
+                p = NULL;
+                return -2;
+            }
+        }
+        if (r > 0) {
             if (s->session->socket_counter != NULL) {
                 s->session->socket_counter->in_bytes += r;
             }
-			/* Bufferize the data and then call the callback */
-            r = ssh_buffer_add_data(s->in_buffer,buffer,r);
+            /* Bufferize the data and then call the callback */
+            r = ssh_buffer_add_data(s->in_buffer, buffer, r);
             if (r < 0) {
                 return -1;
             }
-			if(s->callbacks && s->callbacks->data){
-				do {
-					r= s->callbacks->data(buffer_get_rest(s->in_buffer),
-							buffer_get_rest_len(s->in_buffer),
-							s->callbacks->userdata);
-					buffer_pass_bytes(s->in_buffer,r);
-				} while ((r > 0) && (s->state == SSH_SOCKET_CONNECTED));
-				/* p may have been freed, so don't use it
-				* anymore in this function */
-				p = NULL;
-			}
-		}
-	}
+            if (s->callbacks && s->callbacks->data) {
+                do {
+                    r = s->callbacks->data(buffer_get_rest(s->in_buffer),
+                                           buffer_get_rest_len(s->in_buffer),
+                                           s->callbacks->userdata);
+                    buffer_pass_bytes(s->in_buffer, r);
+                } while ((r > 0) && (s->state == SSH_SOCKET_CONNECTED));
+                /* p may have been freed, so don't use it
+                 * anymore in this function */
+                p = NULL;
+            }
+        }
+    }
 #ifdef _WIN32
-	if(revents & POLLOUT || revents & POLLWRNORM){
+    if (revents & POLLOUT || revents & POLLWRNORM) {
 #else
-	if(revents & POLLOUT){
+    if (revents & POLLOUT) {
 #endif
-		/* First, POLLOUT is a sign we may be connected */
-		if(s->state == SSH_SOCKET_CONNECTING){
-			SSH_LOG(SSH_LOG_PACKET,"Received POLLOUT in connecting state");
-			s->state = SSH_SOCKET_CONNECTED;
-			if (p != NULL) {
-				ssh_poll_set_events(p,POLLOUT | POLLIN);
-			}
-			r = ssh_socket_set_blocking(ssh_socket_get_fd_in(s));
-			if (r < 0) {
-				return -1;
-			}
-			if(s->callbacks && s->callbacks->connected)
-				s->callbacks->connected(SSH_SOCKET_CONNECTED_OK,0,s->callbacks->userdata);
-			return 0;
-		}
-		/* So, we can write data */
-		s->write_wontblock=1;
-        if(p != NULL) {
+        /* First, POLLOUT is a sign we may be connected */
+        if (s->state == SSH_SOCKET_CONNECTING) {
+            SSH_LOG(SSH_LOG_PACKET, "Received POLLOUT in connecting state");
+            s->state = SSH_SOCKET_CONNECTED;
+            if (p != NULL) {
+                ssh_poll_set_events(p, POLLOUT | POLLIN);
+            }
+            r = ssh_socket_set_blocking(ssh_socket_get_fd_in(s));
+            if (r < 0) {
+                return -1;
+            }
+            if (s->callbacks && s->callbacks->connected) {
+                s->callbacks->connected(SSH_SOCKET_CONNECTED_OK, 0,
+                                        s->callbacks->userdata);
+            }
+            return 0;
+        }
+        /* So, we can write data */
+        s->write_wontblock=1;
+        if (p != NULL) {
             ssh_poll_remove_events(p, POLLOUT);
         }
 
-		/* If buffered data is pending, write it */
-		if(buffer_get_rest_len(s->out_buffer) > 0){
-		  ssh_socket_nonblocking_flush(s);
-		} else if(s->callbacks && s->callbacks->controlflow){
-			/* Otherwise advertise the upper level that write can be done */
-			s->callbacks->controlflow(SSH_SOCKET_FLOW_WRITEWONTBLOCK,s->callbacks->userdata);
-		}
-			/* TODO: Find a way to put back POLLOUT when buffering occurs */
-	}
-	/* Return -1 if one of the poll handlers disappeared */
-	return (s->poll_in == NULL || s->poll_out == NULL) ? -1 : 0;
+        /* If buffered data is pending, write it */
+        if (buffer_get_rest_len(s->out_buffer) > 0) {
+            ssh_socket_nonblocking_flush(s);
+        } else if (s->callbacks && s->callbacks->controlflow) {
+            /* Otherwise advertise the upper level that write can be done */
+            s->callbacks->controlflow(SSH_SOCKET_FLOW_WRITEWONTBLOCK,
+                                      s->callbacks->userdata);
+        }
+        /* TODO: Find a way to put back POLLOUT when buffering occurs */
+    }
+    /* Return -1 if one of the poll handlers disappeared */
+    return (s->poll_in == NULL || s->poll_out == NULL) ? -1 : 0;
 }
 
 /** @internal
-- 
1.7.9.5


Archive administrator: postmaster@lists.cynapses.org