loudmouth/lm-connection.c
changeset 261 703c5734ec61
parent 260 bdf166b3fa9c
child 262 d372a2b61b1d
equal deleted inserted replaced
260:bdf166b3fa9c 261:703c5734ec61
   169 					      gpointer           user_data);
   169 					      gpointer           user_data);
   170 static GSource * connection_create_source       (LmConnection *connection);
   170 static GSource * connection_create_source       (LmConnection *connection);
   171 static void      connection_signal_disconnect   (LmConnection *connection,
   171 static void      connection_signal_disconnect   (LmConnection *connection,
   172 						 LmDisconnectReason reason);
   172 						 LmDisconnectReason reason);
   173 
   173 
   174 static void     connection_do_connect           (LmConnectData *connect_data);
   174 static gboolean connection_do_connect           (LmConnectData *connect_data);
   175 static guint    connection_add_watch            (LmConnection  *connection,
   175 static guint    connection_add_watch            (LmConnection  *connection,
   176 						 GIOChannel    *channel,
   176 						 GIOChannel    *channel,
   177 						 GIOCondition   condition,
   177 						 GIOCondition   condition,
   178 						 GIOFunc        func,
   178 						 GIOFunc        func,
   179 						 gpointer       user_data);
   179 						 gpointer       user_data);
   467 		
   467 		
   468 	g_free (server_from_jid);
   468 	g_free (server_from_jid);
   469 	lm_message_unref (m);
   469 	lm_message_unref (m);
   470 }
   470 }
   471 
   471 
   472 void 
   472 gboolean 
   473 _lm_connection_failed_with_error (LmConnectData *connect_data, int error) 
   473 _lm_connection_failed_with_error (LmConnectData *connect_data, int error) 
   474 {
   474 {
   475 	LmConnection *connection;
   475 	LmConnection *connection;
   476 	
   476 	
   477 	g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
   477 	g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
   510 							   cb->user_data);
   510 							   cb->user_data);
   511 			_lm_utils_free_callback (cb);
   511 			_lm_utils_free_callback (cb);
   512 		}
   512 		}
   513 	} else {
   513 	} else {
   514 		/* try to connect to the next host */
   514 		/* try to connect to the next host */
   515 		connection_do_connect (connect_data);
   515 		return connection_do_connect (connect_data);
   516 	}
   516 	}
   517 }
   517 
   518 
   518 	return FALSE;
   519 void 
   519 }
       
   520 
       
   521 gboolean
   520 _lm_connection_failed (LmConnectData *connect_data)
   522 _lm_connection_failed (LmConnectData *connect_data)
   521 {
   523 {
   522 	_lm_connection_failed_with_error (connect_data, 
   524 	return _lm_connection_failed_with_error (connect_data, 
   523 					  _lm_sock_get_last_error());
   525 						 _lm_sock_get_last_error());
   524 }
   526 }
   525 
   527 
   526 static gboolean 
   528 static gboolean 
   527 connection_connect_cb (GIOChannel   *source, 
   529 connection_connect_cb (GIOChannel   *source, 
   528 		       GIOCondition  condition,
   530 		       GIOCondition  condition,
   583 		} 
   585 		} 
   584 	} else {		
   586 	} else {		
   585 		/* for blocking sockets, G_IO_OUT means we are connected */
   587 		/* for blocking sockets, G_IO_OUT means we are connected */
   586 		g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
   588 		g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
   587 		       "Connection success.\n");
   589 		       "Connection success.\n");
   588 		
   590 
   589 		_lm_connection_succeeded (connect_data);
   591 		_lm_connection_succeeded (connect_data);
   590 	}
   592 	}
   591 
   593 
   592  	return TRUE; 
   594  	return TRUE; 
   593 }
   595 }
   611 		strcat(buf, "G_IO_OUT ");
   613 		strcat(buf, "G_IO_OUT ");
   612 
   614 
   613 	return buf;
   615 	return buf;
   614 }
   616 }
   615 
   617 
   616 static void
   618 static gboolean
   617 connection_do_connect (LmConnectData *connect_data) 
   619 connection_do_connect (LmConnectData *connect_data) 
   618 {
   620 {
   619 	LmConnection    *connection;
   621 	LmConnection    *connection;
   620 	LmSocket         fd;
   622 	LmSocket         fd;
   621 	int              res, err;
   623 	int              res, err;
   640 			   name,     sizeof (name),
   642 			   name,     sizeof (name),
   641 			   portname, sizeof (portname),
   643 			   portname, sizeof (portname),
   642 			   NI_NUMERICHOST | NI_NUMERICSERV);
   644 			   NI_NUMERICHOST | NI_NUMERICSERV);
   643 	
   645 	
   644 	if (res < 0) {
   646 	if (res < 0) {
   645 		_lm_connection_failed (connect_data);
   647 		return _lm_connection_failed (connect_data);
   646 		return;
       
   647 	}
   648 	}
   648 
   649 
   649 	g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
   650 	g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET,
   650 	       "Trying %s port %s...\n", name, portname);
   651 	       "Trying %s port %s...\n", name, portname);
   651 	
   652 	
   656 	if (!_LM_SOCK_VALID (fd)) {
   657 	if (!_LM_SOCK_VALID (fd)) {
   657 		g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, 
   658 		g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, 
   658 		       "Failed making socket, error:%d...\n",
   659 		       "Failed making socket, error:%d...\n",
   659 		       _lm_sock_get_last_error ());
   660 		       _lm_sock_get_last_error ());
   660 
   661 
   661 		_lm_connection_failed (connect_data);
   662 		return _lm_connection_failed (connect_data);
   662 
       
   663 		return;
       
   664 	}
   663 	}
   665 
   664 
   666 	/* Even though it says _unix_new(), it is supported by glib on
   665 	/* Even though it says _unix_new(), it is supported by glib on
   667 	 * win32 because glib does some cool stuff to find out if it
   666 	 * win32 because glib does some cool stuff to find out if it
   668 	 * can treat it as a FD or a windows SOCKET.
   667 	 * can treat it as a FD or a windows SOCKET.
   698 				addr->ai_addr, (int)addr->ai_addrlen);  
   697 				addr->ai_addr, (int)addr->ai_addrlen);  
   699 	if (res < 0) {
   698 	if (res < 0) {
   700 		err = _lm_sock_get_last_error ();
   699 		err = _lm_sock_get_last_error ();
   701 		if (!_lm_sock_is_blocking_error (err)) {
   700 		if (!_lm_sock_is_blocking_error (err)) {
   702 			_lm_sock_close (connect_data->fd);
   701 			_lm_sock_close (connect_data->fd);
   703 			_lm_connection_failed_with_error (connect_data, err);
   702 			return _lm_connection_failed_with_error (connect_data, err);
   704 
   703 		}
   705 			return;
   704 	}
   706 		}
   705 
   707 	}
   706 	return TRUE;
   708 }
   707 }
   709 
   708 
   710 static guint
   709 static guint
   711 connection_add_watch (LmConnection *connection,
   710 connection_add_watch (LmConnection *connection,
   712 		      GIOChannel   *channel,
   711 		      GIOChannel   *channel,
   942 	data->io_channel     = NULL;
   941 	data->io_channel     = NULL;
   943 	data->fd             = -1;
   942 	data->fd             = -1;
   944 
   943 
   945 	connection->connect_data = data;
   944 	connection->connect_data = data;
   946 
   945 
   947 	connection_do_connect (data);
   946 	if (!connection_do_connect (data)) {
       
   947 		g_set_error (error,
       
   948 			LM_ERROR,                 
       
   949 			LM_ERROR_CONNECTION_FAILED,
       
   950 			"unable to connect");
       
   951 		return FALSE;
       
   952 	}
       
   953 
   948 	return TRUE;
   954 	return TRUE;
   949 }
   955 }
   950 					
   956 					
   951 static void
   957 static void
   952 connection_do_close (LmConnection *connection)
   958 connection_do_close (LmConnection *connection)