svn commit: samba r11820 - in branches/SAMBA_4_0/source/lib/socket: .

tridge at samba.org tridge at samba.org
Mon Nov 21 07:23:36 GMT 2005


Author: tridge
Date: 2005-11-21 07:23:36 +0000 (Mon, 21 Nov 2005)
New Revision: 11820

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=11820

Log:

fixed some problems with the socket socket.c code. 

 - removed the duplicate calls to socket_connect(), instead creating a
   common function socket_send_connect() used by both code paths

 - fixed some NULL ptr checks (probably was cut-and-paste bugs)

 - ensure we use the result of the name resolution

 - added a few comments

 - use 'fde' for the file description event. The variable name
  'connect_ev' immediately made me think of an event context, not a
  fde. Using common variable name conventions makes code a bit easier
  to read


Modified:
   branches/SAMBA_4_0/source/lib/socket/connect.c


Changeset:
Modified: branches/SAMBA_4_0/source/lib/socket/connect.c
===================================================================
--- branches/SAMBA_4_0/source/lib/socket/connect.c	2005-11-21 06:01:02 UTC (rev 11819)
+++ branches/SAMBA_4_0/source/lib/socket/connect.c	2005-11-21 07:23:36 UTC (rev 11820)
@@ -37,7 +37,7 @@
 	const char *server_address;
 	int server_port;
 	uint32_t flags;
-	struct fd_event *connect_ev;
+	struct fd_event *fde;
 };
 
 static void socket_connect_handler(struct event_context *ev,
@@ -46,6 +46,44 @@
 static void socket_connect_recv_addr(struct composite_context *ctx);
 static void socket_connect_recv_conn(struct composite_context *ctx);
 
+/*
+  call the real socket_connect() call, and setup event handler
+*/
+static void socket_send_connect(struct connect_state *state)
+{
+	struct composite_context *ctx;
+
+	ctx = talloc_zero(state, struct composite_context);
+	if (composite_nomem(ctx, state->ctx)) return;
+	ctx->state = COMPOSITE_STATE_IN_PROGRESS;
+	ctx->event_ctx = state->ctx->event_ctx;
+	ctx->async.fn = socket_connect_recv_conn;
+	ctx->async.private_data = state;
+
+	state->ctx->status = socket_connect(state->sock,
+					    state->my_address,
+					    state->my_port, 
+					    state->server_address,
+					    state->server_port,
+					    state->flags);
+	if (NT_STATUS_IS_ERR(state->ctx->status) && 
+	    !NT_STATUS_EQUAL(state->ctx->status,
+			     NT_STATUS_MORE_PROCESSING_REQUIRED)) {
+		composite_error(state->ctx, state->ctx->status);
+		return;
+	}
+
+	state->fde = event_add_fd(ctx->event_ctx, state,
+				  socket_get_fd(state->sock),
+				  EVENT_FD_WRITE, 
+				  socket_connect_handler, ctx);
+	composite_nomem(state->fde, state->ctx);
+}
+
+
+/*
+  send a socket connect, potentially doing some name resolution first
+*/
 struct composite_context *socket_connect_send(struct socket_context *sock,
 					      const char *my_address,
 					      int my_port,
@@ -57,24 +95,29 @@
 	struct composite_context *result, *ctx;
 	struct connect_state *state;
 
-	result = talloc_zero(NULL, struct composite_context);
-	if (result == NULL) goto failed;
+	result = talloc_zero(sock, struct composite_context);
+	if (result == NULL) return NULL;
 	result->state = COMPOSITE_STATE_IN_PROGRESS;
 	result->async.fn = NULL;
 	result->event_ctx = event_ctx;
 
-	state = talloc(result, struct connect_state);
-	if (state == NULL) goto failed;
+	state = talloc_zero(result, struct connect_state);
+	if (composite_nomem(state, result)) goto failed;
 	state->ctx = result;
 	result->private_data = state;
 
 	state->sock = talloc_reference(state, sock);
-	if (state->sock == NULL) goto failed;
-	state->my_address = talloc_strdup(state, my_address);
-	if (state->sock == NULL) goto failed;
+	if (composite_nomem(state->sock, result)) goto failed;
+
+	if (my_address) {
+		state->my_address = talloc_strdup(state, my_address);
+		if (composite_nomem(state->my_address, result)) goto failed;
+	}
 	state->my_port = my_port;
+
 	state->server_address = talloc_strdup(state, server_address);
-	if (state->sock == NULL) goto failed;
+	if (composite_nomem(state->server_address, result)) goto failed;
+
 	state->server_port = server_port;
 	state->flags = flags;
 
@@ -91,37 +134,13 @@
 		return result;
 	}
 
-	ctx = talloc_zero(state, struct composite_context);
-	if (ctx == NULL) goto failed;
-	ctx->state = COMPOSITE_STATE_IN_PROGRESS;
-	ctx->event_ctx = event_ctx;
-	ctx->async.fn = socket_connect_recv_conn;
-	ctx->async.private_data = state;
+	socket_send_connect(state);
 
-	state->ctx->status = socket_connect(sock, my_address, my_port, 
-					    server_address, server_port,
-					    flags);
-	if (NT_STATUS_IS_ERR(state->ctx->status) && 
-	    !NT_STATUS_EQUAL(state->ctx->status,
-			     NT_STATUS_MORE_PROCESSING_REQUIRED)) {
-		composite_trigger_error(state->ctx);
-		return result;
-	}
+	return result;
 
-	state->connect_ev = event_add_fd(state->ctx->event_ctx, state,
-					 socket_get_fd(sock), EVENT_FD_WRITE, 
-					 socket_connect_handler, ctx);
-	if (state->connect_ev == NULL) {
-		state->ctx->status = NT_STATUS_NO_MEMORY;
-		composite_trigger_error(state->ctx);
-		return result;
-	}
-
+failed:
+	composite_trigger_error(result);
 	return result;
-
- failed:
-	talloc_free(result);
-	return NULL;
 }
 
 /*
@@ -143,6 +162,9 @@
 	composite_done(ctx);
 }
 
+/*
+  recv name resolution reply then send the connect
+*/
 static void socket_connect_recv_addr(struct composite_context *ctx)
 {
 	struct connect_state *state =
@@ -152,35 +174,14 @@
 	state->ctx->status = resolve_name_recv(ctx, state, &addr);
 	if (!composite_is_ok(state->ctx)) return;
 
-	ctx = talloc_zero(state, struct composite_context);
-	if (composite_nomem(ctx, state->ctx)) return;
-	ctx->state = COMPOSITE_STATE_IN_PROGRESS;
-	ctx->event_ctx = state->ctx->event_ctx;
-	ctx->async.fn = socket_connect_recv_conn;
-	ctx->async.private_data = state;
+	state->server_address = addr;
 
-	state->ctx->status = socket_connect(state->sock,
-					    state->my_address,
-					    state->my_port, 
-					    state->server_address,
-					    state->server_port,
-					    state->flags);
-	if (NT_STATUS_IS_ERR(state->ctx->status) && 
-	    !NT_STATUS_EQUAL(state->ctx->status,
-			     NT_STATUS_MORE_PROCESSING_REQUIRED)) {
-		composite_error(state->ctx, state->ctx->status);
-		return;
-	}
-
-	state->connect_ev = event_add_fd(ctx->event_ctx, state,
-					 socket_get_fd(state->sock),
-					 EVENT_FD_WRITE, 
-					 socket_connect_handler, ctx);
-	if (composite_nomem(state->connect_ev, state->ctx)) return;
-
-	return;
+	socket_send_connect(state);
 }
 
+/*
+  called when a connect has finished. Complete the top level composite context
+*/
 static void socket_connect_recv_conn(struct composite_context *ctx)
 {
 	struct connect_state *state =
@@ -188,14 +189,13 @@
 
 	state->ctx->status = ctx->status;
 	if (!composite_is_ok(state->ctx)) return;
-
-	/* We have to free the event context here because the continuation
-	 * function might add an event for this socket directly. */
-	talloc_free(state->connect_ev);
-
 	composite_done(state->ctx);
 }
 
+
+/*
+  wait for a socket_connect_send() to finish
+*/
 NTSTATUS socket_connect_recv(struct composite_context *ctx)
 {
 	NTSTATUS status = composite_wait(ctx);
@@ -203,6 +203,10 @@
 	return status;
 }
 
+
+/*
+  like socket_connect() but takes an event context, doing a semi-async connect
+*/
 NTSTATUS socket_connect_ev(struct socket_context *sock,
 			   const char *my_address, int my_port,
 			   const char *server_address, int server_port,



More information about the samba-cvs mailing list