loudmouth/lm-socket.c
changeset 344 11473296fea5
parent 343 35a19649d773
child 346 ca353cc81e9a
equal deleted inserted replaced
343:35a19649d773 344:11473296fea5
   338 		}
   338 		}
   339 
   339 
   340 		_lm_sock_shutdown (socket->fd);
   340 		_lm_sock_shutdown (socket->fd);
   341 		_lm_sock_close (socket->fd);
   341 		_lm_sock_close (socket->fd);
   342 
   342 
   343 		if (!delayed)
   343 		if (!delayed && socket->connect_func) {
   344 			(socket->connect_func) (socket, FALSE, socket->user_data);
   344 			(socket->connect_func) (socket, FALSE, socket->user_data);
   345 
   345 		}
       
   346 		
   346 		return FALSE;
   347 		return FALSE;
   347 	}
   348 	}
   348 
   349 
   349 #ifdef HAVE_GNUTLS
   350 #ifdef HAVE_GNUTLS
   350 	_lm_sock_set_blocking (socket->fd, FALSE); 
   351 	_lm_sock_set_blocking (socket->fd, FALSE); 
   378 	}
   379 	}
   379 
   380 
   380 	/* Need some way to report error/success */
   381 	/* Need some way to report error/success */
   381 	if (socket->cancel_open) {
   382 	if (socket->cancel_open) {
   382 		lm_verbose ("Cancelling connection...\n");
   383 		lm_verbose ("Cancelling connection...\n");
   383 		(socket->connect_func) (socket, FALSE, socket->user_data);
   384 		if (socket->connect_func) {
       
   385 			(socket->connect_func) (socket, FALSE, socket->user_data);
       
   386 		}
   384 		return;
   387 		return;
   385 	}
   388 	}
   386 	
   389 	
   387 	socket->fd = connect_data->fd;
   390 	socket->fd = connect_data->fd;
   388 	socket->io_channel = connect_data->io_channel;
   391 	socket->io_channel = connect_data->io_channel;
   391 	socket->connect_data = NULL;
   394 	socket->connect_data = NULL;
   392 	g_free (connect_data);
   395 	g_free (connect_data);
   393 
   396 
   394 	/* old-style ssl should be started immediately */
   397 	/* old-style ssl should be started immediately */
   395 	if (socket->ssl && (lm_ssl_get_use_starttls (socket->ssl) == FALSE)) {
   398 	if (socket->ssl && (lm_ssl_get_use_starttls (socket->ssl) == FALSE)) {
   396 		if (!_lm_socket_ssl_init (socket, FALSE))
   399 		if (!_lm_socket_ssl_init (socket, FALSE)) {
   397 			return;
   400 			return;
       
   401 		}
   398 	}
   402 	}
   399 
   403 
   400 	socket->watch_in = 
   404 	socket->watch_in = 
   401 		lm_misc_add_io_watch (socket->context,
   405 		lm_misc_add_io_watch (socket->context,
   402 				      socket->io_channel,
   406 				      socket->io_channel,
   422 				      G_IO_HUP,
   426 				      G_IO_HUP,
   423 				      (GIOFunc) socket_hup_event,
   427 				      (GIOFunc) socket_hup_event,
   424 				      socket);
   428 				      socket);
   425 #endif
   429 #endif
   426 
   430 
   427 	(socket->connect_func) (socket, TRUE, socket->user_data);
   431 	if (socket->connect_func) {
       
   432 		(socket->connect_func) (socket, TRUE, socket->user_data);
       
   433 	}
   428 }
   434 }
   429 
   435 
   430 gboolean 
   436 gboolean 
   431 _lm_socket_failed_with_error (LmConnectData *connect_data, int error) 
   437 _lm_socket_failed_with_error (LmConnectData *connect_data, int error) 
   432 {
   438 {
   449 		g_io_channel_unref (connect_data->io_channel);
   455 		g_io_channel_unref (connect_data->io_channel);
   450 		/* FIXME: need to check for last unref and close the socket */
   456 		/* FIXME: need to check for last unref and close the socket */
   451 	}
   457 	}
   452 	
   458 	
   453 	if (connect_data->current_addr == NULL) {
   459 	if (connect_data->current_addr == NULL) {
   454 		(socket->connect_func) (socket, FALSE, socket->user_data);
   460 		if (socket->connect_func) {
       
   461 			(socket->connect_func) (socket, FALSE, socket->user_data);
       
   462 		}
   455 		
   463 		
   456 		 /* if the user callback called connection_close(), this is already freed */
   464 		 /* if the user callback called connection_close(), this is already freed */
   457 		if (socket->connect_data != NULL) {
   465 		if (socket->connect_data != NULL) {
   458 			freeaddrinfo (connect_data->resolved_addrs);
   466 			freeaddrinfo (connect_data->resolved_addrs);
   459 			socket->connect_data = NULL;
   467 			socket->connect_data = NULL;
   964 			     socket->resolv_query,
   972 			     socket->resolv_query,
   965 			     (gpointer) PHASE_2);
   973 			     (gpointer) PHASE_2);
   966 #else
   974 #else
   967 	err = getaddrinfo (remote_addr, NULL, &req, &ans);
   975 	err = getaddrinfo (remote_addr, NULL, &req, &ans);
   968 	_lm_socket_create_phase2 (socket, (err) ? NULL : ans);
   976 	_lm_socket_create_phase2 (socket, (err) ? NULL : ans);
   969 	if (err != 0)
   977 	if (err != 0) {
   970 		return;
   978 		return;
       
   979 	}
   971 #endif
   980 #endif
   972 }
   981 }
   973 
   982 
   974 static void
   983 static void
   975 _lm_socket_create_phase2 (LmSocket *socket, struct addrinfo *ans)
   984 _lm_socket_create_phase2 (LmSocket *socket, struct addrinfo *ans)
   976 {
   985 {
   977 	if (ans == NULL) {
   986 	if (ans == NULL) {
   978 		lm_verbose ("error while resolving, bailing out\n");
   987 		lm_verbose ("error while resolving, bailing out\n");
   979 		(socket->connect_func) (socket, FALSE, socket->user_data);
   988 		if (socket->connect_func) {
       
   989 			(socket->connect_func) (socket, FALSE, socket->user_data);
       
   990 		}
   980 		g_free (socket->connect_data);
   991 		g_free (socket->connect_data);
   981 		socket->connect_data = NULL;
   992 		socket->connect_data = NULL;
   982 		return;
   993 		return;
   983 	}
   994 	}
   984 
   995 
  1027 	socket->cancel_open = FALSE;
  1038 	socket->cancel_open = FALSE;
  1028 	socket->ssl = ssl;
  1039 	socket->ssl = ssl;
  1029 	socket->ssl_started = FALSE;
  1040 	socket->ssl_started = FALSE;
  1030 	socket->proxy = NULL;
  1041 	socket->proxy = NULL;
  1031 	socket->blocking = blocking;
  1042 	socket->blocking = blocking;
  1032 	socket->data_func = data_func;
       
  1033 	socket->closed_func = closed_func;
       
  1034 	socket->connect_func = connect_func;
       
  1035 	socket->user_data = user_data;
       
  1036 
  1043 
  1037 	if (context) {
  1044 	if (context) {
  1038 		socket->context = g_main_context_ref (context);
  1045 		socket->context = g_main_context_ref (context);
  1039 	}
  1046 	}
  1040 
  1047 
  1064 	} else {
  1071 	} else {
  1065 		lm_verbose ("SRV lookup disabled for %s\n", socket->server);
  1072 		lm_verbose ("SRV lookup disabled for %s\n", socket->server);
  1066 		_lm_socket_create_phase1 (socket, NULL, 0);
  1073 		_lm_socket_create_phase1 (socket, NULL, 0);
  1067 	}
  1074 	}
  1068 
  1075 
       
  1076 	if (socket->connect_data == NULL) {
       
  1077 		/* Open failed synchronously, probably a DNS lookup problem */
       
  1078 		lm_socket_unref(socket);
       
  1079 		
       
  1080 		g_set_error (error,
       
  1081 			     LM_ERROR,                 
       
  1082 			     LM_ERROR_CONNECTION_FAILED,   
       
  1083 			     "Failed to resolve server");
       
  1084 		
       
  1085 		return NULL;
       
  1086 	}
       
  1087 		
       
  1088 
       
  1089 	/* If the connection fails synchronously, we don't want to call the
       
  1090 	 * connect_func to indicate an error, we return an error indication
       
  1091 	 * instead. So, we delay saving the functions until after we know
       
  1092 	 * we are going to return success.
       
  1093 	 */
       
  1094 	socket->data_func = data_func;
       
  1095 	socket->closed_func = closed_func;
       
  1096 	socket->connect_func = connect_func;
       
  1097 	socket->user_data = user_data;
       
  1098 	
  1069 	return socket;
  1099 	return socket;
  1070 }
  1100 }
  1071 
  1101 
  1072 void
  1102 void
  1073 lm_socket_flush (LmSocket *socket)
  1103 lm_socket_flush (LmSocket *socket)