diff src/protocols/qq/qq_proxy.c @ 14021:ef8490f9e823

[gaim-migrate @ 16618] Replaced all C++-style comments with C-style ones. Cleaned up some comments and implemented a more consistent formatting scheme. committer: Tailor Script <tailor@pidgin.im>
author Mark Huetsch <markhuetsch>
date Wed, 02 Aug 2006 15:35:36 +0000
parents 16102b9c5c4a
children 16b05033f5ee
line wrap: on
line diff
--- a/src/protocols/qq/qq_proxy.c	Wed Aug 02 13:37:13 2006 +0000
+++ b/src/protocols/qq/qq_proxy.c	Wed Aug 02 15:35:36 2006 +0000
@@ -21,34 +21,29 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-// START OF FILE
-/*****************************************************************************/
-#include "debug.h"		// gaim_debug
-#include "internal.h"		// strlen, _("get_text")#include "md5.h"                    // md5 functions
-//#include "md5.h"
-#include "cipher.h"            //gfhuang
+#include "cipher.h"
+#include "debug.h"
+#include "internal.h"
 
 #ifdef _WIN32
 #define random rand
 #define srandom srand
 #endif
 
-#include "utils.h"		// qq_debug
-#include "packet_parse.h"	// MAX_PACKET_SIZE
-#include "buddy_info.h"		// qq_info_query_free
-#include "buddy_opt.h"		// qq_add_buddy_request_free
-#include "char_conv.h"		// qq_sending_im_msg_cb
-#include "group_free.h"		// qq_group_packets_free
-#include "login_logout.h"	// qq_send_packet_login
-#include "qq_proxy.h"		//
-#include "recv_core.h"		// qq_pending, qq_b4_packets_free
-#include "send_core.h"		// qq_send_cmd
-#include "sendqueue.h"		// qq_sendqueue_timeout_callback
-#include "udp_proxy_s5.h"	// qq_proxy_sock5
+#include "packet_parse.h"
+#include "buddy_info.h"
+#include "buddy_opt.h"
+#include "char_conv.h"
+#include "group_free.h"
+#include "login_logout.h"
+#include "qq_proxy.h"
+#include "recv_core.h"
+#include "send_core.h"
+#include "sendqueue.h"
+#include "udp_proxy_s5.h"
+#include "utils.h"
 
-/*****************************************************************************/
-
-/* These functions are used only in development phased
+/* These functions are used only in development phase
  *
 static void _qq_show_socket(gchar *desc, gint fd) {
 	struct sockaddr_in sin;
@@ -59,7 +54,7 @@
 }
 */
 
-void _qq_show_packet(gchar * desc, gchar * buf, gint len)
+void _qq_show_packet(gchar *desc, gchar *buf, gint len)
 {
 	char buf1[4096], buf2[10];
 	int i;
@@ -72,24 +67,13 @@
 	gaim_debug(GAIM_DEBUG_INFO, desc, buf1);
 }
 
-/*****************************************************************************/
-// QQ 2003iii uses double MD5 for the pwkey to get the session key
-static guint8 *_gen_pwkey(const gchar * pwd)
+/* QQ 2003iii uses double MD5 for the pwkey to get the session key */
+static guint8 *_gen_pwkey(const gchar *pwd)
 {
-//	md5_state_t ctx;        //gfhuang
         GaimCipher *cipher;
         GaimCipherContext *context;
 
 	gchar pwkey_tmp[QQ_KEY_LENGTH];
-/*
-	md5_init(&ctx);
-	md5_append(&ctx, pwd, strlen(pwd));
-	md5_finish(&ctx, pwkey_tmp);
-
-	md5_init(&ctx);
-	md5_append(&ctx, pwkey_tmp, QQ_KEY_LENGTH);
-	md5_finish(&ctx, pwkey_tmp);
-*/        //gfhuang
 
 	cipher = gaim_ciphers_find_cipher("md5");
 	context = gaim_cipher_context_new(cipher, NULL);
@@ -101,13 +85,10 @@
 	gaim_cipher_context_digest(context, sizeof(pwkey_tmp), pwkey_tmp, NULL);
 	gaim_cipher_context_destroy(context);
 
-
 	return g_memdup(pwkey_tmp, QQ_KEY_LENGTH);
-}				// _gen_pwkey
+}
 
-
-/*****************************************************************************/
-gint _qq_fill_host(struct sockaddr_in * addr, const gchar * host, guint16 port)
+gint _qq_fill_host(struct sockaddr_in *addr, const gchar *host, guint16 port)
 {
 	if (!inet_aton(host, &(addr->sin_addr))) {
 		struct hostent *hp;
@@ -117,15 +98,15 @@
 		memset(addr, 0, sizeof(struct sockaddr_in));
 		memcpy(&(addr->sin_addr.s_addr), hp->h_addr, hp->h_length);
 		addr->sin_family = hp->h_addrtype;
-	} else
+	} else {
 		addr->sin_family = AF_INET;
+	}
 
 	addr->sin_port = htons(port);
 	return 0;
-}				// _qq_fill_host
+}
 
-/*****************************************************************************/
-// set up any finalizing start-up stuff
+/* set up any finalizing start-up stuff */
 static void _qq_start_services(GaimConnection *gc)
 {
 	/* start watching for IMs about to be sent */
@@ -136,8 +117,8 @@
 			*/
 }
 
-// the callback function after socket is built
-// we setup the qq protocol related configuration here
+/* the callback function after socket is built
+ * we setup the qq protocol related configuration here */
 static void _qq_got_login(gpointer data, gint source, GaimInputCondition cond)
 {
 	qq_data *qd;
@@ -153,14 +134,14 @@
 		return;
 	}
 
-	if (source < 0) {	// socket returns -1
+	if (source < 0) {	/* socket returns -1 */
 		gaim_connection_error(gc, _("Unable to connect."));
 		return;
 	}
 
 	qd = (qq_data *) gc->proto_data;
 
-	// QQ use random seq, to minimize duplicated packets
+	/* QQ use random seq, to minimize duplicated packets */
 	srandom(time(NULL));
 	qd->send_seq = random() & 0x0000ffff;
 	qd->fd = source;
@@ -169,35 +150,33 @@
 	qd->uid = strtol(gaim_account_get_username(gaim_connection_get_account(gc)), NULL, 10);
 	qd->before_login_packets = g_queue_new();
 
-	// now generate md5 processed passwd                        
+	/* now generate md5 processed passwd */
 	passwd = gaim_account_get_password(gaim_connection_get_account(gc));
 	qd->pwkey = _gen_pwkey(passwd);
 
 	qd->sendqueue_timeout = gaim_timeout_add(QQ_SENDQUEUE_TIMEOUT, qq_sendqueue_timeout_callback, gc);
 	gc->inpa = gaim_input_add(qd->fd, GAIM_INPUT_READ, qq_input_pending, gc);
 
-	// Update the login progress status display
+	/* Update the login progress status display */
 	buf = g_strdup_printf("Login as %d", qd->uid);
 	gaim_connection_update_progress(gc, buf, 1, QQ_CONNECT_STEPS);
 	g_free(buf);
 
 	_qq_start_services(gc);
 
-//	qq_send_packet_login(gc);	// finally ready to fire
 	qq_send_packet_request_login_token(gc);
-}				// _qq_got_login
+}
 
-/*****************************************************************************/
-// clean up qq_data structure and all its components
-// always used before a redirectly connection
-static void _qq_common_clean(GaimConnection * gc)
+/* clean up qq_data structure and all its components
+ * always used before a redirectly connection */
+static void _qq_common_clean(GaimConnection *gc)
 {
 	qq_data *qd;
 
 	g_return_if_fail(gc != NULL && gc->proto_data != NULL);
 	qd = (qq_data *) gc->proto_data;
 
-	// finish  all I/O
+	/* finish  all I/O */
 	if (qd->fd >= 0 && qd->logged_in)
 		qq_send_packet_logout(gc);
 	close(qd->fd);
@@ -205,12 +184,12 @@
 	if (qd->sendqueue_timeout > 0) {
 		gaim_timeout_remove(qd->sendqueue_timeout);
 		qd->sendqueue_timeout = 0;
-	}			// qd->sendqueue_timeout
+	}
 
 	if (gc->inpa > 0) {
 		gaim_input_remove(gc->inpa);
 		gc->inpa = 0;
-	}			// gc->inpa
+	}
 
 	qq_b4_packets_free(qd);
 	qq_sendqueue_free(qd);
@@ -218,11 +197,9 @@
 	qq_group_free_all(qd);
 	qq_add_buddy_request_free(qd);
 	qq_info_query_free(qd);
-	qq_buddies_list_free(gc->account /* by gfhuang */, qd);
+	qq_buddies_list_free(gc->account, qd);
+}
 
-}				// _qq_common_clean
-
-/*****************************************************************************/
 static gint _qq_proxy_none(struct PHB *phb, struct sockaddr *addr, socklen_t addrlen)
 {
 	gint fd = -1;
@@ -233,9 +210,9 @@
 	if (fd < 0) {
 		gaim_debug(GAIM_DEBUG_ERROR, "QQ Redirect", "Unable to create socket: %s\n", strerror(errno));
 		return -1;
-	}			// if fd
+	}
 
-	// we use non-blocking mode to speed up connection
+	/* we use non-blocking mode to speed up connection */
 	fcntl(fd, F_SETFL, O_NONBLOCK);
 
 	/* From Unix-socket-FAQ: http://www.faqs.org/faqs/unix-faq/socket/
@@ -258,27 +235,26 @@
 		 *    A signal interrupted the call. 
 		 *    The connection is established asynchronously.
 		 */
-		if ((errno == EINPROGRESS) || (errno == EINTR))
+		if ((errno == EINPROGRESS) || (errno == EINTR)) {
 			gaim_debug(GAIM_DEBUG_WARNING, "QQ", "Connect in asynchronous mode.\n");
-		else {
+		} else {
 			gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Faiil connection: %d\n", strerror(errno));
 			close(fd);
 			return -1;
-		}		// if errno
-	} else {		// connect returns 0
+		}		/* if errno */
+	} else {		/* connect returns 0 */
 		gaim_debug(GAIM_DEBUG_INFO, "QQ", "Connected.\n");
 		fcntl(fd, F_SETFL, 0);
 		phb->func(phb->data, fd, GAIM_INPUT_READ);
-	}			// if connect
+	}
 
 	return fd;
-}				// _qq_proxy_none
+}
 
-/*****************************************************************************/
-// returns the socket handler, or -1 if there is any error
-static gint _qq_udp_proxy_connect(GaimAccount * account,
-			   const gchar * server,
-			   guint16 port, void callback(gpointer, gint, GaimInputCondition), GaimConnection * gc)
+/* returns the socket handler, or -1 if there is any error */
+static gint _qq_udp_proxy_connect(GaimAccount *account,
+			   const gchar *server,
+			   guint16 port, void callback(gpointer, gint, GaimInputCondition), GaimConnection *gc)
 {
 	struct sockaddr_in sin;
 	struct PHB *phb;
@@ -302,12 +278,12 @@
 		gaim_debug(GAIM_DEBUG_ERROR, "QQ",
 			   "gethostbyname(\"%s\", %d) failed: %s\n", server, port, hstrerror(h_errno));
 		return -1;
-	}			// if _qq_fill_host
+	}
 
 	if (info == NULL) {
 		qd->proxy_type = GAIM_PROXY_NONE;
 		return _qq_proxy_none(phb, (struct sockaddr *) &sin, sizeof(sin));
-	}			// if info
+	}
 
 	qd->proxy_type = info->type;
 	gaim_debug(GAIM_DEBUG_INFO, "QQ", "Choosing proxy type %d\n", info->type);
@@ -316,26 +292,25 @@
 	case GAIM_PROXY_NONE:
 		return _qq_proxy_none(phb, (struct sockaddr *) &sin, sizeof(sin));
 	case GAIM_PROXY_SOCKS5:
-		// as the destination is always QQ server during the session, 
-		// we can set dest_sin here, instead of _qq_s5_canread_again
+		/* as the destination is always QQ server during the session, 
+		 * we can set dest_sin here, instead of _qq_s5_canread_again */
 		_qq_fill_host(&qd->dest_sin, phb->host, phb->port);
 		_qq_fill_host(&sin, phb->gpi->host, phb->gpi->port);
 		return qq_proxy_socks5(phb, (struct sockaddr *) &sin, sizeof(sin));
 	default:
 		return _qq_proxy_none(phb, (struct sockaddr *) &sin, sizeof(sin));
-	}			// switch
+	}
 
 	return -1;
 }
 
-/*****************************************************************************/
-// QQ connection via UDP/TCP. 
-// I use GAIM proxy function to provide TCP proxy support,
-// and qq_udp_proxy.c to add UDP proxy support (thanks henry)
-// return the socket handle, -1 means fail
-static gint _proxy_connect_full
-    (GaimAccount * account, const gchar * host, guint16 port, GaimInputFunction func, gpointer data, gboolean use_tcp) {
-
+/* QQ connection via UDP/TCP. 
+ * I use GAIM proxy function to provide TCP proxy support,
+ * and qq_udp_proxy.c to add UDP proxy support (thanks henry)
+ *  return the socket handle, -1 means fail */
+static gint _proxy_connect_full (GaimAccount *account, const gchar *host, guint16 port, 
+		GaimInputFunction func, gpointer data, gboolean use_tcp)
+{
 	GaimConnection *gc;
 	qq_data *qd;
 
@@ -344,17 +319,15 @@
 	qd->server_ip = g_strdup(host);
 	qd->server_port = port;
 
-	return use_tcp ? gaim_proxy_connect(account, host, port, func, data) :	// TCP mode
-	    _qq_udp_proxy_connect(account, host, port, func, data);	// UDP mode
-
-}				// _gaim_proxy_connect_full
+	return use_tcp ? gaim_proxy_connect(account, host, port, func, data) :	/* TCP mode */
+	    _qq_udp_proxy_connect(account, host, port, func, data);	/* UDP mode */
+}
 
-/*****************************************************************************/
-// establish a generic QQ connection 
-// TCP/UDP, and direct/redirected
-// return the socket handler, or -1 if there is any error
-gint qq_connect(GaimAccount * account, const gchar * host, guint16 port, gboolean use_tcp, gboolean is_redirect) {
-
+/* establish a generic QQ connection 
+ * TCP/UDP, and direct/redirected
+ * return the socket handler, or -1 if there is any error */
+gint qq_connect(GaimAccount *account, const gchar *host, guint16 port, gboolean use_tcp, gboolean is_redirect)
+{
 	GaimConnection *gc;
 
 	g_return_val_if_fail(host != NULL, -1);
@@ -367,11 +340,10 @@
 		_qq_common_clean(gc);
 
 	return _proxy_connect_full(account, host, port, _qq_got_login, gc, use_tcp);
-}				// qq_connect
+}
 
-/*****************************************************************************/
-// clean up the given QQ connection and free all resources
-void qq_disconnect(GaimConnection * gc)
+/* clean up the given QQ connection and free all resources */
+void qq_disconnect(GaimConnection *gc)
 {
 	qq_data *qd;
 
@@ -387,38 +359,37 @@
 	g_free(qd);
 
 	gc->proto_data = NULL;
-}				// qq_disconnect
+}
 
-/*****************************************************************************/
-// send packet with proxy support
-gint qq_proxy_write(qq_data * qd, guint8 * data, gint len)
+/* send packet with proxy support */
+gint qq_proxy_write(qq_data *qd, guint8 *data, gint len)
 {
 	guint8 *buf;
 	gint ret;
 
 	g_return_val_if_fail(qd != NULL && qd->fd >= 0 && data != NULL && len > 0, -1);
 
-	// TCP sock5 may be processed twice
-	// so we need to check qd->use_tcp as well
-	if ((!qd->use_tcp) && qd->proxy_type == GAIM_PROXY_SOCKS5) {	// UDP sock5
+	/* TCP sock5 may be processed twice
+	 * so we need to check qd->use_tcp as well */
+	if ((!qd->use_tcp) && qd->proxy_type == GAIM_PROXY_SOCKS5) {	/* UDP sock5 */
 		buf = g_newa(guint8, len + 10);
 		buf[0] = 0x00;
-		buf[1] = 0x00;	//reserved
-		buf[2] = 0x00;	//frag
-		buf[3] = 0x01;	//type
+		buf[1] = 0x00;	/* reserved */
+		buf[2] = 0x00;	/* frag */
+		buf[3] = 0x01;	/* type */
 		g_memmove(buf + 4, &(qd->dest_sin.sin_addr.s_addr), 4);
 		g_memmove(buf + 8, &(qd->dest_sin.sin_port), 2);
 		g_memmove(buf + 10, data, len);
 		ret = send(qd->fd, buf, len + 10, 0);
-	} else
+	} else {
 		ret = send(qd->fd, data, len, 0);
+	}
 
 	return ret;
 }
 
-/*****************************************************************************/
-// read packet input with proxy support
-gint qq_proxy_read(qq_data * qd, guint8 * data, gint len)
+/* read packet input with proxy support */
+gint qq_proxy_read(qq_data *qd, guint8 *data, gint len)
 {
 	guint8 *buf;
 	gint bytes;
@@ -431,16 +402,14 @@
 	if (bytes < 0)
 		return -1;
 
-	if ((!qd->use_tcp) && qd->proxy_type == GAIM_PROXY_SOCKS5) {	// UDP sock5
+	if ((!qd->use_tcp) && qd->proxy_type == GAIM_PROXY_SOCKS5) {	/* UDP sock5 */
 		if (bytes < 10)
 			return -1;
 		bytes -= 10;
-		g_memmove(data, buf + 10, bytes);	//cut off the header
-	} else
+		g_memmove(data, buf + 10, bytes);	/* cut off the header */
+	} else {
 		g_memmove(data, buf, bytes);
+	}
 
 	return bytes;
-}				// qq_proxy_read
-
-/*****************************************************************************/
-// END of FILE
+}