comparison libpurple/protocols/oscar/flap_connection.c @ 25016:ff5e2356683f

propagate from branch 'im.pidgin.pidgin' (head 07f8d9c59020c1f69cc21b71f8252f13efd6a6e0) to branch 'im.pidgin.cpw.darkrain42.oscar-ssl' (head 8a336a3d256ee3435232fad10bd222d3bab817ac)
author Paul Aurich <paul@darkrain42.org>
date Wed, 14 Jan 2009 06:29:47 +0000
parents c6772d61af1f
children f6ef3a9534db
comparison
equal deleted inserted replaced
24960:df25f88ff22a 25016:ff5e2356683f
360 if (conn->type == SNAC_FAMILY_LOCATE) 360 if (conn->type == SNAC_FAMILY_LOCATE)
361 flap_connection_send_close(od, conn); 361 flap_connection_send_close(od, conn);
362 362
363 close(conn->fd); 363 close(conn->fd);
364 conn->fd = -1; 364 conn->fd = -1;
365 }
366
367 if (conn->gsc != NULL)
368 {
369 if (conn->type == SNAC_FAMILY_LOCATE)
370 flap_connection_send_close(od, conn);
371
372 purple_ssl_close(conn->gsc);
373 conn->gsc = NULL;
365 } 374 }
366 375
367 if (conn->watcher_incoming != 0) 376 if (conn->watcher_incoming != 0)
368 { 377 {
369 purple_input_remove(conn->watcher_incoming); 378 purple_input_remove(conn->watcher_incoming);
465 474
466 flap_connection_close(od, conn); 475 flap_connection_close(od, conn);
467 476
468 g_free(conn->error_message); 477 g_free(conn->error_message);
469 g_free(conn->cookie); 478 g_free(conn->cookie);
479 g_free(conn->ssl_cert_cn);
470 480
471 /* 481 /*
472 * Free conn->internal, if necessary 482 * Free conn->internal, if necessary
473 */ 483 */
474 if (conn->type == SNAC_FAMILY_CHAT) 484 if (conn->type == SNAC_FAMILY_CHAT)
842 /** 852 /**
843 * Read in all available data on the socket for a given connection. 853 * Read in all available data on the socket for a given connection.
844 * All complete FLAPs handled immedate after they're received. 854 * All complete FLAPs handled immedate after they're received.
845 * Incomplete FLAP data is stored locally and appended to the next 855 * Incomplete FLAP data is stored locally and appended to the next
846 * time this callback is triggered. 856 * time this callback is triggered.
847 */ 857 *
848 void 858 * This is called by flap_connection_recv_cb and
849 flap_connection_recv_cb(gpointer data, gint source, PurpleInputCondition cond) 859 * flap_connection_recv_cb_ssl for unencrypted/encrypted connections.
850 { 860 */
851 FlapConnection *conn; 861 static void
862 flap_connection_recv(FlapConnection *conn)
863 {
864 gpointer buf;
865 gsize buflen;
852 gssize read; 866 gssize read;
853
854 conn = data;
855 867
856 /* Read data until we run out of data and break out of the loop */ 868 /* Read data until we run out of data and break out of the loop */
857 while (TRUE) 869 while (TRUE)
858 { 870 {
859 /* Start reading a new FLAP */ 871 /* Start reading a new FLAP */
860 if (conn->buffer_incoming.data.data == NULL) 872 if (conn->buffer_incoming.data.data == NULL)
861 { 873 {
874 buf = conn->header + conn->header_received;
875 buflen = 6 - conn->header_received;
876
862 /* Read the first 6 bytes (the FLAP header) */ 877 /* Read the first 6 bytes (the FLAP header) */
863 read = recv(conn->fd, conn->header + conn->header_received, 878 if (conn->gsc)
864 6 - conn->header_received, 0); 879 read = purple_ssl_read(conn->gsc, buf, buflen);
880 else
881 read = recv(conn->fd, buf, buflen, 0);
865 882
866 /* Check if the FLAP server closed the connection */ 883 /* Check if the FLAP server closed the connection */
867 if (read == 0) 884 if (read == 0)
868 { 885 {
869 flap_connection_schedule_destroy(conn, 886 flap_connection_schedule_destroy(conn,
916 conn->buffer_incoming.data.len = aimutil_get16(&conn->header[4]); 933 conn->buffer_incoming.data.len = aimutil_get16(&conn->header[4]);
917 conn->buffer_incoming.data.data = g_new(guint8, conn->buffer_incoming.data.len); 934 conn->buffer_incoming.data.data = g_new(guint8, conn->buffer_incoming.data.len);
918 conn->buffer_incoming.data.offset = 0; 935 conn->buffer_incoming.data.offset = 0;
919 } 936 }
920 937
921 if (conn->buffer_incoming.data.len - conn->buffer_incoming.data.offset) 938 buflen = conn->buffer_incoming.data.len - conn->buffer_incoming.data.offset;
939 if (buflen)
922 { 940 {
941 buf = &conn->buffer_incoming.data.data[conn->buffer_incoming.data.offset];
923 /* Read data into the temporary FlapFrame until it is complete */ 942 /* Read data into the temporary FlapFrame until it is complete */
924 read = recv(conn->fd, 943 if (conn->gsc)
925 &conn->buffer_incoming.data.data[conn->buffer_incoming.data.offset], 944 read = purple_ssl_read(conn->gsc, buf, buflen);
926 conn->buffer_incoming.data.len - conn->buffer_incoming.data.offset, 945 else
927 0); 946 read = recv(conn->fd, buf, buflen, 0);
928 947
929 /* Check if the FLAP server closed the connection */ 948 /* Check if the FLAP server closed the connection */
930 if (read == 0) 949 if (read == 0)
931 { 950 {
932 flap_connection_schedule_destroy(conn, 951 flap_connection_schedule_destroy(conn,
962 981
963 conn->header_received = 0; 982 conn->header_received = 0;
964 } 983 }
965 } 984 }
966 985
986 void
987 flap_connection_recv_cb(gpointer data, gint source, PurpleInputCondition cond)
988 {
989 FlapConnection *conn = data;
990
991 flap_connection_recv(conn);
992 }
993
994 void
995 flap_connection_recv_cb_ssl(gpointer data, PurpleSslConnection *gsc, PurpleInputCondition cond)
996 {
997 FlapConnection *conn = data;
998
999 flap_connection_recv(conn);
1000 }
1001
967 static void 1002 static void
968 send_cb(gpointer data, gint source, PurpleInputCondition cond) 1003 send_cb(gpointer data, gint source, PurpleInputCondition cond)
969 { 1004 {
970 FlapConnection *conn; 1005 FlapConnection *conn;
971 int writelen, ret; 1006 int writelen, ret;
978 purple_input_remove(conn->watcher_outgoing); 1013 purple_input_remove(conn->watcher_outgoing);
979 conn->watcher_outgoing = 0; 1014 conn->watcher_outgoing = 0;
980 return; 1015 return;
981 } 1016 }
982 1017
983 ret = send(conn->fd, conn->buffer_outgoing->outptr, writelen, 0); 1018 if (conn->gsc)
1019 ret = purple_ssl_write(conn->gsc, conn->buffer_outgoing->outptr,
1020 writelen);
1021 else
1022 ret = send(conn->fd, conn->buffer_outgoing->outptr, writelen, 0);
984 if (ret <= 0) 1023 if (ret <= 0)
985 { 1024 {
986 if (ret < 0 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) 1025 if (ret < 0 && ((errno == EAGAIN) || (errno == EWOULDBLOCK)))
987 /* No worries */ 1026 /* No worries */
988 return; 1027 return;
989 1028
990 /* Error! */ 1029 /* Error! */
991 purple_input_remove(conn->watcher_outgoing); 1030 purple_input_remove(conn->watcher_outgoing);
992 conn->watcher_outgoing = 0; 1031 conn->watcher_outgoing = 0;
993 close(conn->fd); 1032 if (conn->gsc) {
994 conn->fd = -1; 1033 purple_ssl_close(conn->gsc);
1034 conn->gsc = NULL;
1035 } else {
1036 close(conn->fd);
1037 conn->fd = -1;
1038 }
995 flap_connection_schedule_destroy(conn, 1039 flap_connection_schedule_destroy(conn,
996 OSCAR_DISCONNECT_LOST_CONNECTION, g_strerror(errno)); 1040 OSCAR_DISCONNECT_LOST_CONNECTION, g_strerror(errno));
997 return; 1041 return;
998 } 1042 }
999 1043
1015 1059
1016 /* Add everything to our outgoing buffer */ 1060 /* Add everything to our outgoing buffer */
1017 purple_circ_buffer_append(conn->buffer_outgoing, bs->data, count); 1061 purple_circ_buffer_append(conn->buffer_outgoing, bs->data, count);
1018 1062
1019 /* If we haven't already started writing stuff, then start the cycle */ 1063 /* If we haven't already started writing stuff, then start the cycle */
1020 if ((conn->watcher_outgoing == 0) && (conn->fd >= 0)) 1064 if (conn->watcher_outgoing == 0)
1021 { 1065 {
1022 conn->watcher_outgoing = purple_input_add(conn->fd, 1066 if (conn->gsc) {
1023 PURPLE_INPUT_WRITE, send_cb, conn); 1067 conn->watcher_outgoing = purple_input_add(conn->gsc->fd,
1024 send_cb(conn, conn->fd, 0); 1068 PURPLE_INPUT_WRITE, send_cb, conn);
1069 send_cb(conn, 0, 0);
1070 } else if (conn->fd >= 0) {
1071 conn->watcher_outgoing = purple_input_add(conn->fd,
1072 PURPLE_INPUT_WRITE, send_cb, conn);
1073 send_cb(conn, 0, 0);
1074 }
1025 } 1075 }
1026 } 1076 }
1027 1077
1028 static void 1078 static void
1029 sendframe_flap(FlapConnection *conn, FlapFrame *frame) 1079 sendframe_flap(FlapConnection *conn, FlapFrame *frame)