Skip to content
Browse files

Clean up: Moves a block of code to a static function.

  • Loading branch information...
1 parent 2af8ab9 commit 80ce616c3dadbc1ebe38a4ee31dab516064eafd9 @alobbs alobbs committed Nov 20, 2011
Showing with 100 additions and 83 deletions.
  1. +100 −83 cherokee/source.c
View
183 cherokee/source.c
@@ -70,118 +70,135 @@ cherokee_source_mrproper (cherokee_source_t *src)
}
-ret_t
-cherokee_source_connect (cherokee_source_t *src, cherokee_socket_t *sock)
+static ret_t
+source_set_address (cherokee_source_t *src,
+ cherokee_socket_t *sock)
{
ret_t ret;
+ cherokee_boolean_t tested_all;
cherokee_resolv_cache_t *resolv;
+ const struct addrinfo *addr;
+ const struct addrinfo *addr_info = NULL;
- /* Short path: it's already connecting
+ /* Query the resolv cache
*/
- if (sock->socket >= 0) {
- return cherokee_socket_connect (sock);
+ ret = cherokee_resolv_cache_get_default (&resolv);
+ if (unlikely (ret!=ret_ok)) {
+ return ret;
}
- /* Create the new socket and set the target IP info
+ ret = cherokee_resolv_cache_get_addrinfo (resolv, &src->host, &addr_info);
+ if ((ret != ret_ok) || (addr_info == NULL)) {
+ return ret_error;
+ }
+
+ /* Current address
*/
- if (! cherokee_buffer_is_empty (&src->unix_socket)) {
+ if (src->addr_current) {
+ tested_all = false;
+ addr = src->addr_current;
+ } else {
+ tested_all = true;
+ addr = addr_info;
+ }
- /* Create the socket descriptor
- */
- ret = cherokee_socket_create_fd (sock, AF_UNIX);
- if (unlikely (ret != ret_ok)) {
- return ret;
- }
+ /* Create the fd for the address family
+ *
+ * Iterates through the different addresses of the
+ * host and stores a pointer to the first one with
+ * a supported family.
+ */
+ while (addr != NULL) {
+ ret = cherokee_socket_create_fd (sock, addr->ai_family);
- ret = cherokee_socket_gethostbyname (sock, &src->unix_socket);
- if (unlikely (ret != ret_ok)) {
- return ret;
- }
- } else {
- cherokee_boolean_t tested_all;
- const struct addrinfo *addr;
- const struct addrinfo *addr_info = NULL;
+#ifdef TRACE_ENABLED
+ if (cherokee_trace_is_tracing()) {
+ ret_t ret2;
+ char ip[46];
- /* Query the resolv cache
- */
- ret = cherokee_resolv_cache_get_default (&resolv);
- if (unlikely (ret!=ret_ok)) {
- return ret;
+ ret2 = cherokee_ntop (addr->ai_family, addr->ai_addr, ip, sizeof(ip));
+ if (ret2 == ret_ok) {
+ TRACE (ENTRIES, "Connecting to %s, ret=%d\n", ip, ret);
+ }
}
+#endif
- ret = cherokee_resolv_cache_get_addrinfo (resolv, &src->host, &addr_info);
- if ((ret != ret_ok) || (addr_info == NULL)) {
- return ret_error;
+ if (ret == ret_ok) {
+ src->addr_current = addr;
+ break;
}
- /* Current address
- */
- if (src->addr_current) {
- tested_all = false;
- addr = src->addr_current;
- } else {
+ addr = addr->ai_next;
+ if (addr == NULL) {
+ if (tested_all) {
+ return ret_error;
+ }
+
tested_all = true;
+ src->addr_current = NULL;
addr = addr_info;
+ continue;
}
- /* Create the fd for the address family
- *
- * Iterates through the different addresses of the
- * host and stores a pointer to the first one with
- * a supported family.
- */
- while (addr != NULL) {
- ret = cherokee_socket_create_fd (sock, addr->ai_family);
+ cherokee_socket_close(sock);
+ }
-#ifdef TRACE_ENABLED
- if (cherokee_trace_is_tracing()) {
- ret_t ret2;
- char ip[46];
-
- ret2 = cherokee_ntop (addr->ai_family, addr->ai_addr, ip, sizeof(ip));
- if (ret2 == ret_ok) {
- TRACE (ENTRIES, "Connecting to %s, ret=%d\n", ip, ret);
- }
- }
-#endif
+ /* Update the new socket with the address info
+ */
+ switch (src->addr_current->ai_family) {
+ case AF_INET:
+ SOCKET_ADDR_IPv4(sock)->sin_port = htons(src->port);
+ break;
+ case AF_INET6:
+ SOCKET_ADDR_IPv6(sock)->sin6_port = htons(src->port);
+ break;
+ default:
+ SHOULDNT_HAPPEN;
+ return ret_error;
+ }
- if (ret == ret_ok) {
- src->addr_current = addr;
- break;
- }
+ ret = cherokee_socket_update_from_addrinfo (sock, src->addr_current, 0);
+ if (unlikely (ret != ret_ok)) {
+ return ret_error;
+ }
- addr = addr->ai_next;
- if (addr == NULL) {
- if (tested_all) {
- return ret_error;
- }
+ return ret_ok;
+}
- tested_all = true;
- src->addr_current = NULL;
- addr = addr_info;
- continue;
- }
- cherokee_socket_close(sock);
- }
+ret_t
+cherokee_source_connect (cherokee_source_t *src, cherokee_socket_t *sock)
+{
+ ret_t ret;
- /* Update the new socket with the address info
+ /* Short path: it's already connecting
+ */
+ if (sock->socket >= 0) {
+ return cherokee_socket_connect (sock);
+ }
+
+ /* Create the new socket and set the target IP info
+ */
+ if (! cherokee_buffer_is_empty (&src->unix_socket)) {
+
+ /* Create the socket descriptor
*/
- switch (src->addr_current->ai_family) {
- case AF_INET:
- SOCKET_ADDR_IPv4(sock)->sin_port = htons(src->port);
- break;
- case AF_INET6:
- SOCKET_ADDR_IPv6(sock)->sin6_port = htons(src->port);
- break;
- default:
- SHOULDNT_HAPPEN;
- return ret_error;
+ ret = cherokee_socket_create_fd (sock, AF_UNIX);
+ if (unlikely (ret != ret_ok)) {
+ return ret;
}
- ret = cherokee_socket_update_from_addrinfo (sock, src->addr_current, 0);
+ ret = cherokee_socket_gethostbyname (sock, &src->unix_socket);
if (unlikely (ret != ret_ok)) {
- return ret_error;
+ return ret;
+ }
+ } else {
+
+ /* Set next IP address
+ */
+ ret = source_set_address (src, sock);
+ if (ret != ret_ok) {
+ return ret;
}
}

0 comments on commit 80ce616

Please sign in to comment.
Something went wrong with that request. Please try again.