loudmouth/lm-connection.c
changeset 245 df2655dbe873
parent 244 8a05b79bad24
child 247 a519eff901e6
equal deleted inserted replaced
244:8a05b79bad24 245:df2655dbe873
    53 	/* Parameters */
    53 	/* Parameters */
    54 	GMainContext *context;
    54 	GMainContext *context;
    55 	gchar        *server;
    55 	gchar        *server;
    56 	gchar        *jid;
    56 	gchar        *jid;
    57 	guint         port;
    57 	guint         port;
       
    58 	gboolean      use_srv;
    58 
    59 
    59 	LmSocket     *socket;
    60 	LmSocket     *socket;
    60 	LmSSL        *ssl;
    61 	LmSSL        *ssl;
    61 	LmProxy      *proxy;
    62 	LmProxy      *proxy;
    62 	LmParser     *parser;
    63 	LmParser     *parser;
   149 static void      connection_message_queue_cb     (LmMessageQueue  *queue,
   150 static void      connection_message_queue_cb     (LmMessageQueue  *queue,
   150 						  LmConnection    *connection);
   151 						  LmConnection    *connection);
   151 static void      connection_incoming_data        (LmSocket        *socket, 
   152 static void      connection_incoming_data        (LmSocket        *socket, 
   152 						  const gchar     *buf,
   153 						  const gchar     *buf,
   153 						  LmConnection    *connection);
   154 						  LmConnection    *connection);
       
   155 static gboolean  connection_get_server_from_jid  (const gchar     *jid,
       
   156 						  gchar          **server);
   154 
   157 
   155 static void
   158 static void
   156 connection_free (LmConnection *connection)
   159 connection_free (LmConnection *connection)
   157 {
   160 {
   158 	int        i;
   161 	int        i;
   392 /* Returns directly */
   395 /* Returns directly */
   393 /* Setups all data needed to start the connection attempts */
   396 /* Setups all data needed to start the connection attempts */
   394 static gboolean
   397 static gboolean
   395 connection_do_open (LmConnection *connection, GError **error) 
   398 connection_do_open (LmConnection *connection, GError **error) 
   396 {
   399 {
       
   400 	gchar *connect_server;
       
   401 
   397 	if (lm_connection_is_open (connection)) {
   402 	if (lm_connection_is_open (connection)) {
   398 		g_set_error (error,
   403 		g_set_error (error,
   399 			     LM_ERROR,
   404 			     LM_ERROR,
   400 			     LM_ERROR_CONNECTION_NOT_OPEN,
   405 			     LM_ERROR_CONNECTION_NOT_OPEN,
   401 			     "Connection is already open, call lm_connection_close() first");
   406 			     "Connection is already open, call lm_connection_close() first");
   402 		return FALSE;
   407 		return FALSE;
   403 	}
   408 	}
   404 
   409 
   405 	if (!connection->server) {
   410 	if (!connection->server) {
   406 		g_set_error (error,
   411 		if (!connection->use_srv) {
   407 			     LM_ERROR,
   412 			g_set_error (error,
   408 			     LM_ERROR_CONNECTION_FAILED,
   413 				     LM_ERROR,
   409 			     "You need to set the server hostname in the call to lm_connection_new()");
   414 				     LM_ERROR_CONNECTION_FAILED,
   410 		return FALSE;
   415 				     "You need to set the server hostname in the call to lm_connection_new()");
       
   416 			return FALSE;
       
   417 		} else {
       
   418 			if (!connection_get_server_from_jid (connection->jid,
       
   419 							     &connect_server)) {
       
   420 				g_set_error (error,
       
   421 					     LM_ERROR,
       
   422 					     LM_ERROR_CONNECTION_FAILED,
       
   423 					     "You need to either set server hostname or jid");
       
   424 				return FALSE;
       
   425 			}
       
   426 		}
       
   427 	} else {
       
   428 		connect_server = g_strdup (connection->server);
   411 	}
   429 	}
   412 
   430 
   413 	lm_message_queue_attach (connection->queue, connection->context);
   431 	lm_message_queue_attach (connection->queue, connection->context);
   414 	
   432 	
   415 	lm_verbose ("Connecting to: %s:%d\n", 
   433 	lm_verbose ("Connecting to: %s:%d\n", 
   421 	connection->socket = lm_socket_create (connection->context,
   439 	connection->socket = lm_socket_create (connection->context,
   422 					       (IncomingDataFunc) connection_incoming_data,
   440 					       (IncomingDataFunc) connection_incoming_data,
   423 					       connection,
   441 					       connection,
   424 					       connection,
   442 					       connection,
   425 					       connection->blocking,
   443 					       connection->blocking,
   426 					       connection->server,
   444 					       connect_server,
   427 					       connection->port,
   445 					       connection->port,
   428 					       FALSE,
   446 					       connection->use_srv,
   429 					       connection->ssl,
   447 					       connection->ssl,
   430 					       connection->proxy,
   448 					       connection->proxy,
   431 					       error);
   449 					       error);
       
   450 	g_free (connect_server);
       
   451 
   432 	if (!connection->socket) {
   452 	if (!connection->socket) {
   433 		return FALSE;
   453 		return FALSE;
   434 	}
   454 	}
   435 
   455 
   436 	return TRUE;
   456 	return TRUE;
   727 			  LmConnection *connection)
   747 			  LmConnection *connection)
   728 {
   748 {
   729 	lm_parser_parse (connection->parser, buf);
   749 	lm_parser_parse (connection->parser, buf);
   730 }
   750 }
   731 
   751 
       
   752 static gboolean
       
   753 connection_get_server_from_jid (const gchar     *jid,
       
   754 				gchar          **server)
       
   755 {
       
   756 	gchar *ch;
       
   757 	gchar *ch_end;
       
   758 
       
   759 	if (jid != NULL && (ch = strchr (jid, '@')) != NULL) {
       
   760 		ch_end = strchr(ch + 1, '/');
       
   761 		if (ch_end != NULL) {
       
   762 			*server = g_strndup (ch + 1, ch_end - ch - 1);
       
   763 		} else {
       
   764 			*server = g_strdup (ch + 1);
       
   765 		}
       
   766 
       
   767 		return TRUE;
       
   768 	} 
       
   769 	
       
   770 	return FALSE;
       
   771 }
       
   772 
   732 void 
   773 void 
   733 _lm_connection_socket_result (LmConnection *connection, gboolean result)
   774 _lm_connection_socket_result (LmConnection *connection, gboolean result)
   734 {
   775 {
   735 	LmMessage    *m;
   776 	LmMessage    *m;
   736 	gchar        *server_from_jid;
   777 	gchar        *server_from_jid;
   940 	_lm_sock_library_init ();
   981 	_lm_sock_library_init ();
   941 
   982 
   942 	connection = g_new0 (LmConnection, 1);
   983 	connection = g_new0 (LmConnection, 1);
   943 
   984 
   944 	if (server) {
   985 	if (server) {
   945 		connection->server = _lm_utils_hostname_to_punycode (server);
   986 		connection->server  = _lm_utils_hostname_to_punycode (server);
       
   987 		connection->use_srv = FALSE;
   946 	} else {
   988 	} else {
   947 		connection->server = NULL;
   989 		connection->server  = NULL;
       
   990 		connection->use_srv = TRUE;
   948 	}
   991 	}
   949 
   992 
   950 	connection->context           = NULL;
   993 	connection->context           = NULL;
   951 	connection->port              = LM_CONNECTION_DEFAULT_PORT;
   994 	connection->port              = LM_CONNECTION_DEFAULT_PORT;
   952 	connection->jid               = NULL;
   995 	connection->jid               = NULL;
  1147 connection_sasl_auth_finished (LmSASL *sasl,
  1190 connection_sasl_auth_finished (LmSASL *sasl,
  1148 			       LmConnection *connection,
  1191 			       LmConnection *connection,
  1149 			       gboolean success,
  1192 			       gboolean success,
  1150 			       const gchar *reason)
  1193 			       const gchar *reason)
  1151 {
  1194 {
  1152 	gchar *server_from_jid;
  1195 	gchar     *server_from_jid;
  1153 	gchar *ch;
       
  1154 	LmMessage *m;
  1196 	LmMessage *m;
  1155 
  1197 
  1156 	if (!success) {
  1198 	if (!success) {
  1157 		lm_verbose ("SASL authentication failed, closing connection\n");
  1199 		lm_verbose ("SASL authentication failed, closing connection\n");
  1158 		connection_call_auth_cb (connection, FALSE);
  1200 		connection_call_auth_cb (connection, FALSE);
  1159 		return;
  1201 		return;
  1160 	}
  1202 	}
  1161 
  1203 
  1162 	if (connection->jid != NULL && (ch = strchr (connection->jid, '@')) != NULL) {
  1204 	if (!connection_get_server_from_jid (connection->jid, &server_from_jid)) {
  1163 		server_from_jid = ch + 1;
  1205 		server_from_jid = g_strdup (connection->server);
  1164 	} else {
       
  1165 		server_from_jid = connection->server;
       
  1166 	}
  1206 	}
  1167 
  1207 
  1168 	m = lm_message_new (server_from_jid, LM_MESSAGE_TYPE_STREAM);
  1208 	m = lm_message_new (server_from_jid, LM_MESSAGE_TYPE_STREAM);
  1169 	lm_message_node_set_attributes (m->node,
  1209 	lm_message_node_set_attributes (m->node,
  1170 					"xmlns:stream", 
  1210 					"xmlns:stream", 
  1171 					"http://etherx.jabber.org/streams",
  1211 					"http://etherx.jabber.org/streams",
  1172 					"xmlns", "jabber:client",
  1212 					"xmlns", "jabber:client",
  1173 					"version", "1.0",
  1213 					"version", "1.0",
  1174 					NULL);
  1214 					NULL);
       
  1215 
       
  1216 	g_free (server_from_jid);
  1175 
  1217 
  1176 	lm_verbose ("Reopening XMPP 1.0 stream...");
  1218 	lm_verbose ("Reopening XMPP 1.0 stream...");
  1177 
  1219 
  1178 	if (!lm_connection_send (connection, m, NULL)) {
  1220 	if (!lm_connection_send (connection, m, NULL)) {
  1179 		lm_verbose ("Failed to send stream information\n");
  1221 		lm_verbose ("Failed to send stream information\n");
  1449 		return;
  1491 		return;
  1450 	}
  1492 	}
  1451 	
  1493 	
  1452 	g_free (connection->server);
  1494 	g_free (connection->server);
  1453 	connection->server = _lm_utils_hostname_to_punycode (server);
  1495 	connection->server = _lm_utils_hostname_to_punycode (server);
       
  1496 	connection->use_srv = FALSE;
  1454 }
  1497 }
  1455 
  1498 
  1456 /**
  1499 /**
  1457  * lm_connection_get_jid:
  1500  * lm_connection_get_jid:
  1458  * @connection: an #LmConnection
  1501  * @connection: an #LmConnection