annotate plugins/crazychat/cc_network.c @ 13871:2be9dfa9569b

[gaim-migrate @ 16341] Fixed a bug that prevented libqq from being loaded as a plugin. committer: Tailor Script <tailor@pidgin.im>
author Mark Huetsch <markhuetsch>
date Mon, 26 Jun 2006 03:44:39 +0000
parents 17142948653e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11232
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
1 #include <assert.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
2 #include <errno.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
3 #include <string.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
4 #include <gtk/gtk.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
5 #include "conversation.h"
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
6 #include "network.h"
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
7 #include <sys/socket.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
8 #include <sys/types.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
9 #include <netinet/in.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
10 #include <sys/time.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
11 #include <unistd.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
12 #include <fcntl.h>
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
13 #include "cc_network.h"
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
14 #include "cc_interface.h"
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
15 #include "util.h"
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
16
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
17 /* --- begin constant definitions --- */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
18
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
19 #define NETWORK_TIMEOUT_DELAY 40 /* in ms */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
20 #define MAX_ACCEPT_CHECKS 1000
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
21
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
22 /* --- begin type declarations --- */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
23
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
24 struct accept_args {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
25 GaimAccount *account;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
26 struct crazychat *cc;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
27 char *name;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
28 guint32 peer_ip;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
29 guint16 peer_port;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
30 };
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
31
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
32 struct sock_accept_args {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
33 GaimAccount *account;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
34 struct cc_session *session;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
35 };
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
36
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
37 /* --- begin function prototypes --- */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
38
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
39 /**
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
40 * Creates a server socket and sends a response to the peer.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
41 * @param account the gaim account sending the ready msg
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
42 * @param session the peer CrazyChat session
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
43 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
44 static void cc_net_send_ready(GaimAccount *account, struct cc_session *session);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
45
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
46 /**
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
47 * Handles responses from the CrazyChat session invite dialog box.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
48 * @param dialog the dialog box
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
49 * @param response the dialog box button response
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
50 * @param args account, crazychat global data, peer name
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
51 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
52 static void invite_handler(GtkDialog *dialog, gint response,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
53 struct accept_args *args);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
54
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
55 /**
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
56 * Periodically checks the server socket for peer's connection. Gives up
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
57 * after a set number of checks.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
58 * @param args peer session and account
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
59 * @return TRUE to continue checking, FALSE to stop
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
60 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
61 static gboolean accept_cb(struct sock_accept_args *args);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
62
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
63 /**
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
64 * Initialize CrazyChat network session. Sets up the UDP socket and port.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
65 * @param account the account the session is part of
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
66 * @param session the CrazyChat network session
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
67 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
68 static void init_cc_net_session(GaimAccount *account,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
69 struct cc_session *session);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
70
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
71 /**
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
72 * Handles checking the network for new feature data and sending out the
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
73 * latest features.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
74 * @param session the session we're checking for network traffic
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
75 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
76 static gboolean network_cb(struct cc_session *session);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
77
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
78 /**
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
79 * Generates random bytes in the user specified byte buffer.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
80 * @param buf the byte buffer
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
81 * @param len length of the byte buffer
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
82 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
83 static void generate_randomness(uint8_t buf[], unsigned int len);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
84
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
85 /**
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
86 * Sends data over a socket.
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
87 * @param s socket file descriptor
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
88 * @param buf data buffer
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
89 * @param len data buffer length
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
90 * @return number of bytes sent or -1 if an error occurred
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
91 */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
92 static int __send(int s, char *buf, int len);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
93
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
94 /* --- begin function definitions --- */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
95
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
96 void cc_net_send_invite(struct crazychat *cc, char *name, GaimAccount *account)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
97 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
98 struct cc_session *session;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
99 GaimConversation *conv;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
100 GaimConvIm *im;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
101 char buf[BUFSIZ];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
102
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
103 session = cc_find_session(cc, name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
104 if (session) return; /* already have a session with this guy */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
105 session = cc_add_session(cc, name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
106 session->state = INVITE;
11338
17142948653e [gaim-migrate @ 13551]
Mark Doliner <mark@kingant.net>
parents: 11272
diff changeset
107 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_ANY, name, account);
11232
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
108 if (!conv) {
11338
17142948653e [gaim-migrate @ 13551]
Mark Doliner <mark@kingant.net>
parents: 11272
diff changeset
109 conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, account, name);
11232
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
110 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
111 im = gaim_conversation_get_im_data(conv);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
112 snprintf(buf, BUFSIZ, "%s%s!%d", CRAZYCHAT_INVITE_CODE,
11272
80ee726755d4 [gaim-migrate @ 13455]
Charlie Stockman <chuckleberry>
parents: 11232
diff changeset
113 gaim_network_get_my_ip(-1), cc->tcp_port);
11232
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
114 Debug("Sent invite to %s for port: %d\n", name, cc->tcp_port);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
115 gaim_conv_im_send(im, buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
116 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
117
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
118 void cc_net_recv_invite(GaimAccount *account, struct crazychat *cc, char *name,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
119 const char *peer_ip, const char *peer_port)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
120 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
121 struct cc_session *session;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
122 GaimConversation *conv;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
123 GaimConvWindow *convwin;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
124 char buf[BUFSIZ];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
125 struct accept_args *args;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
126
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
127 assert(cc);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
128 assert(name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
129 Debug("Received a CrazyChat session invite from %s on port %s!\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
130 name, peer_port);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
131 session = cc_find_session(cc, name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
132 if (!session) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
133 Debug("Creating a CrazyChat session invite dialog box!\n");
11338
17142948653e [gaim-migrate @ 13551]
Mark Doliner <mark@kingant.net>
parents: 11272
diff changeset
134 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_ANY, name, account);
11232
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
135 if (conv) convwin = gaim_conversation_get_window(conv);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
136 else convwin = NULL;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
137 /* pop gtk window asking if want to accept */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
138 GtkWidget *dialog =
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
139 gtk_dialog_new_with_buttons("CrazyChat Session Invite",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
140 GTK_WINDOW(convwin),
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
141 GTK_DIALOG_MODAL |
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
142 GTK_DIALOG_DESTROY_WITH_PARENT,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
143 GTK_STOCK_OK,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
144 GTK_RESPONSE_ACCEPT,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
145 GTK_STOCK_CANCEL,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
146 GTK_RESPONSE_REJECT,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
147 NULL);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
148 snprintf(buf, BUFSIZ, "Would you like to CRaZYchAT with %s?", name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
149 GtkWidget *label = gtk_label_new(buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
150 gtk_container_add(GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
151 label);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
152 args = (struct accept_args*)malloc(sizeof(*args));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
153 args->account = account;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
154 args->cc = cc;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
155 args->name = strdup(name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
156 assert(inet_aton(peer_ip, (struct in_addr*)&args->peer_ip));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
157 args->peer_port = atoi(peer_port);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
158
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
159 g_signal_connect(GTK_OBJECT(dialog), "response",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
160 G_CALLBACK(invite_handler), args);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
161
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
162 gtk_widget_show_all(dialog);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
163 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
164 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
165
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
166 void cc_net_recv_accept(GaimAccount *account, struct crazychat *cc, char *name,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
167 const char *peer_ip)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
168 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
169 struct cc_session *session;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
170 struct in_addr peer_addr;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
171
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
172 assert(cc);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
173 assert(name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
174 Debug("Received a CrazyChat session accept!\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
175 session = cc_find_session(cc, name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
176 if (session && session->state == INVITE) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
177 session->state = ACCEPTED;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
178 assert(inet_aton(peer_ip, &peer_addr));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
179 session->peer_ip = peer_addr.s_addr;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
180 cc_net_send_ready(account, session);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
181 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
182 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
183
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
184 static void cc_net_send_ready(GaimAccount *account, struct cc_session *session)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
185 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
186 struct sock_accept_args *args;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
187
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
188 assert(session);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
189 Debug("Initializing the server socket and sending ready message\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
190 /* create the server socket */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
191 session->tcp_sock = socket(AF_INET, SOCK_STREAM, 0);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
192 assert(session->tcp_sock != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
193 int reuse = 1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
194 assert(setsockopt(session->tcp_sock, SOL_SOCKET, SO_REUSEADDR,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
195 &reuse, sizeof(int)) != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
196 struct sockaddr_in my_addr;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
197 my_addr.sin_family = AF_INET;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
198 my_addr.sin_port = htons(session->cc->tcp_port);
11272
80ee726755d4 [gaim-migrate @ 13455]
Charlie Stockman <chuckleberry>
parents: 11232
diff changeset
199 assert(inet_aton(gaim_network_get_my_ip(-1),
11232
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
200 &my_addr.sin_addr));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
201 memset(&my_addr.sin_zero, 0, sizeof(my_addr.sin_zero));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
202 assert(bind(session->tcp_sock, (struct sockaddr*)&my_addr,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
203 sizeof(my_addr)) != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
204 Debug("Listening on port: %d\n", my_addr.sin_port);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
205 assert(listen(session->tcp_sock, 1) != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
206
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
207 /* socket created, send the ready message */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
208 GaimConversation *conv;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
209 GaimConvIm *im;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
210
11338
17142948653e [gaim-migrate @ 13551]
Mark Doliner <mark@kingant.net>
parents: 11272
diff changeset
211 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_ANY, session->name, account);
11232
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
212 if (!conv) {
11338
17142948653e [gaim-migrate @ 13551]
Mark Doliner <mark@kingant.net>
parents: 11272
diff changeset
213 conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, account,
11232
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
214 session->name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
215 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
216 im = gaim_conversation_get_im_data(conv);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
217 gaim_conv_im_send(im, CRAZYCHAT_READY_CODE);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
218
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
219 /* register timer callback for checking socket connection */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
220 args = (struct sock_accept_args*)malloc(sizeof(*args));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
221 args->session = session;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
222 args->account = account;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
223 session->udp_sock = MAX_ACCEPT_CHECKS;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
224 session->timer_id = g_timeout_add(NETWORK_TIMEOUT_DELAY,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
225 (GSourceFunc)accept_cb, args);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
226 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
227
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
228 void cc_net_recv_ready(GaimAccount *account, struct crazychat *cc, char *name)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
229 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
230 struct cc_session *session;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
231 struct sockaddr_in server_addr, my_addr;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
232 int sock;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
233
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
234 assert(cc);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
235 assert(name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
236 Debug("Received a CrazyChat session ready!\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
237 session = cc_find_session(cc, name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
238 if (session && session->state == ACCEPTED) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
239 /* connect to peer */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
240 session->tcp_sock = socket(AF_INET, SOCK_STREAM, 0);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
241 assert(session->tcp_sock != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
242 server_addr.sin_family = AF_INET;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
243 server_addr.sin_port = session->peer_port;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
244 server_addr.sin_addr.s_addr = session->peer_ip;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
245 memset(&(server_addr.sin_zero), 0,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
246 sizeof(server_addr.sin_zero));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
247 assert(connect(session->tcp_sock,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
248 (struct sockaddr*)&server_addr,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
249 sizeof(server_addr)) != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
250 Debug("Connecting to peer on port %d\n", session->peer_port);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
251
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
252 /* now set state */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
253 session->state = CONNECTED;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
254 init_cc_net_session(account, session);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
255 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
256 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
257
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
258 static void invite_handler(GtkDialog *dialog, gint response, struct accept_args *args)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
259 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
260 struct cc_session *session;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
261 char buf[BUFSIZ];
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
262 GaimConversation *conv;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
263 GaimConvIm *im;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
264
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
265 if (response == GTK_RESPONSE_ACCEPT) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
266 assert(args);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
267 session = cc_find_session(args->cc, args->name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
268 assert(!session);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
269 session = cc_add_session(args->cc, args->name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
270 session->state = ACCEPTED;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
271 session->peer_ip = args->peer_ip;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
272 session->peer_port = args->peer_port;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
273 snprintf(buf, BUFSIZ, "%s%s", CRAZYCHAT_ACCEPT_CODE,
11272
80ee726755d4 [gaim-migrate @ 13455]
Charlie Stockman <chuckleberry>
parents: 11232
diff changeset
274 gaim_network_get_my_ip(-1));
11338
17142948653e [gaim-migrate @ 13551]
Mark Doliner <mark@kingant.net>
parents: 11272
diff changeset
275 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_ANY, args->name,
11232
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
276 args->account);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
277 if (!conv) {
11338
17142948653e [gaim-migrate @ 13551]
Mark Doliner <mark@kingant.net>
parents: 11272
diff changeset
278 conv = gaim_conversation_new(GAIM_CONV_TYPE_IM,
11232
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
279 args->account, args->name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
280 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
281 im = gaim_conversation_get_im_data(conv);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
282 gaim_conv_im_send(im, buf);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
283 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
284 free(args->name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
285 free(args);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
286 gtk_widget_destroy(GTK_WIDGET(dialog));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
287 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
288
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
289 static gboolean accept_cb(struct sock_accept_args *args)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
290 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
291 fd_set fds;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
292 struct timeval zero;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
293 int ret;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
294 GaimAccount *account;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
295 struct cc_session *session;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
296
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
297 assert(args);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
298 account = args->account;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
299 session = args->session;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
300 assert(account);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
301 assert(session);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
302
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
303 /* set select to check on our tcp socket */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
304 FD_ZERO(&fds);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
305 FD_SET(session->tcp_sock, &fds);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
306 memset(&zero, 0, sizeof(zero));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
307
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
308 /* check socket */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
309 ret = select(session->tcp_sock+1,&fds, NULL, NULL, &zero);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
310 assert(ret != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
311
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
312 if (ret) { /* got something to check */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
313 Debug("Checking pending connection\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
314 int sock;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
315 struct sockaddr_in client_addr;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
316 socklen_t sin_size;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
317
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
318 sin_size = sizeof(client_addr);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
319 sock = accept(session->tcp_sock,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
320 (struct sockaddr*)&client_addr, &sin_size);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
321 assert(sock != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
322
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
323 /* check if it's a match */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
324 if (client_addr.sin_addr.s_addr == session->peer_ip) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
325 /* cool, we're set */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
326 Debug("Accepted tcp connect from %s\n", session->name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
327 close(session->tcp_sock);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
328 session->tcp_sock = sock;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
329 session->state = CONNECTED;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
330 session->timer_id = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
331 init_cc_net_session(account, session);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
332 Debug("Will start sending to port %d\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
333 session->peer_port);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
334 free(args);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
335 return FALSE;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
336 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
337 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
338
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
339 session->udp_sock--;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
340
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
341 if (!session->udp_sock) { /* timed out */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
342 /* remove session from session list */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
343 cc_remove_session(session->cc, session);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
344 free(args);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
345 return FALSE;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
346 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
347
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
348 return TRUE;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
349 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
350
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
351 static void init_cc_net_session(GaimAccount *account,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
352 struct cc_session *session)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
353 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
354 struct sockaddr_in my_addr;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
355 struct sockaddr_in peer_addr;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
356 int reuse;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
357
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
358 /* send/obtain the udp port information */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
359
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
360 assert(__send(session->tcp_sock, (char*)&session->cc->udp_port,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
361 sizeof(session->cc->udp_port)) ==
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
362 sizeof(session->cc->udp_port));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
363 assert(recv(session->tcp_sock, (char*)&session->peer_port,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
364 sizeof(session->peer_port), 0) ==
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
365 sizeof(session->peer_port));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
366
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
367 Debug("Established a CrazyChat session with %s!\n", session->name);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
368
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
369 /* connect the udp sockets */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
370
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
371 session->udp_sock = socket(AF_INET, SOCK_DGRAM, 0);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
372
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
373 assert(!setsockopt(session->udp_sock, SOL_SOCKET, SO_REUSEADDR,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
374 &reuse, sizeof(reuse)));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
375
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
376 my_addr.sin_family = AF_INET;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
377 my_addr.sin_port = htons(session->cc->udp_port);
11272
80ee726755d4 [gaim-migrate @ 13455]
Charlie Stockman <chuckleberry>
parents: 11232
diff changeset
378 assert(inet_aton(gaim_network_get_my_ip(-1),
11232
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
379 &my_addr.sin_addr));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
380 memset(my_addr.sin_zero, 0, sizeof(my_addr.sin_zero));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
381 assert(!bind(session->udp_sock, (struct sockaddr*)&my_addr,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
382 sizeof(my_addr)));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
383 session->peer.sin_family = AF_INET;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
384 session->peer.sin_port = htons(session->peer_port);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
385 session->peer.sin_addr.s_addr = session->peer_ip;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
386 memset(&session->peer.sin_zero, 0, sizeof(session->peer.sin_zero));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
387
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
388 Debug("Bound udp sock to port %d, connecting to port %d\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
389 session->cc->udp_port, session->peer_port);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
390
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
391 memset(&session->features, 0, sizeof(session->features));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
392
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
393 session->output = init_output(&session->features, session);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
394
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
395 session->filter = Filter_Initialize();
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
396
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
397 /* initialize timer callback */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
398 session->timer_id = g_timeout_add(NETWORK_TIMEOUT_DELAY,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
399 (GSourceFunc)network_cb, session);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
400
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
401 /* initialize input subsystem if not initialized */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
402 if (!session->cc->features_state) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
403 session->cc->input_data = init_input(session->cc);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
404 session->cc->features_state = 1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
405 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
406 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
407
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
408 static gboolean network_cb(struct cc_session *session)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
409 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
410 fd_set fds;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
411 struct timeval zero;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
412 int ret;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
413 int command;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
414 struct cc_features *features;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
415
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
416 assert(session);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
417
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
418 Debug("Checking for data\n");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
419
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
420 /* set select to check on our tcp socket */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
421 FD_ZERO(&fds);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
422 FD_SET(session->tcp_sock, &fds);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
423 memset(&zero, 0, sizeof(zero));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
424
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
425 /* check tcp socket */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
426 ret = select(session->tcp_sock+1, &fds, NULL, NULL, &zero);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
427 assert(ret != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
428
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
429 while (ret) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
430 ret = recv(session->tcp_sock, &command, sizeof(command), 0);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
431 assert(ret != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
432 if (!ret) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
433 /* tcp connection closed, destroy connection */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
434 gtk_widget_destroy(session->output->widget);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
435 return FALSE;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
436 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
437 assert(ret == sizeof(command));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
438
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
439 FD_ZERO(&fds);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
440 FD_SET(session->tcp_sock, &fds);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
441 ret = select(session->tcp_sock+1, &fds, NULL, NULL, &zero);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
442 assert(ret != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
443 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
444
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
445 /* set select to check on our udp socket */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
446 FD_ZERO(&fds);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
447 FD_SET(session->udp_sock, &fds);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
448 memset(&zero, 0, sizeof(zero));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
449
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
450 /* check udp socket */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
451 ret = select(session->udp_sock+1, &fds, NULL, NULL, &zero);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
452 assert(ret != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
453
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
454 features = &session->features;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
455
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
456 while (ret) { /* have data, let's copy it for output */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
457 struct sockaddr_in from;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
458 int fromlen;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
459 ret = recvfrom(session->udp_sock, &session->features,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
460 sizeof(session->features),
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
461 0, (struct sockaddr*)&from, &fromlen);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
462 Debug("Received %d bytes from port %d\n", ret,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
463 ntohs(from.sin_port));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
464 filter(features, session->filter);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
465 Debug("\thead size: %d\n", features->head_size);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
466 Debug("\topen: left(%s), right(%s), mouth(%s)\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
467 features->left_eye_open ? "yes" : "no",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
468 features->right_eye_open ? "yes" : "no",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
469 features->mouth_open ? "yes" : "no");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
470 Debug("\thead rotation: x(%d), y(%d), z(%d)\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
471 features->head_x_rot, features->head_y_rot,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
472 features->head_z_rot);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
473 Debug("\tx(%d), y(%d)\n", features->x, features->y);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
474 if (ret == -1) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
475 perror("wtf:");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
476 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
477 assert(ret != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
478
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
479 FD_ZERO(&fds);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
480 FD_SET(session->udp_sock, &fds);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
481 ret = select(session->udp_sock+1, &fds, NULL, NULL, &zero);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
482 assert(ret != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
483 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
484
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
485 #ifdef _DISABLE_QT_
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
486 struct cc_features bogus;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
487 features = &bogus;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
488 generate_randomness((uint8_t*)features, sizeof(*features));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
489 #else
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
490 features = &session->cc->input_data->face;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
491 #endif
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
492 assert(sendto(session->udp_sock, (char*)features,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
493 sizeof(*features), 0, (struct sockaddr*)&session->peer,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
494 sizeof(session->peer)) == sizeof(*features));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
495 Debug("Sent %d bytes\n", sizeof(*features));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
496 Debug("\thead size: %d\n", features->head_size);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
497 Debug("\topen: left(%s), right(%s), mouth(%s)\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
498 features->left_eye_open ? "yes" : "no",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
499 features->right_eye_open ? "yes" : "no",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
500 features->mouth_open ? "yes" : "no");
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
501 Debug("\thead rotation: x(%d), y(%d), z(%d)\n",
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
502 features->head_x_rot, features->head_y_rot,
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
503 features->head_z_rot);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
504 Debug("\tx(%d), y(%d)\n", features->x, features->y);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
505
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
506 /* clear easter egg */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
507 features->mode = 0;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
508
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
509 return TRUE;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
510 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
511
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
512 static void generate_randomness(uint8_t buf[], unsigned int len)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
513 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
514 int fd;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
515
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
516 fd = open("/dev/random", O_RDONLY);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
517 assert(fd != -1);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
518
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
519 assert(read(fd, buf, len) == len);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
520 close(fd);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
521 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
522
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
523 static int __send(int s, char *buf, int len)
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
524 {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
525 int total = 0; /* how many bytes we've sent */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
526 int bytesleft = len; /* how many we have left to send */
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
527 int n;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
528
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
529 while (total < len) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
530 n = send(s, buf + total, bytesleft, 0);
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
531 if (n == -1) {
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
532 Debug("ERROR: %s\n", strerror(errno));
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
533 return -1;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
534 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
535 total += n;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
536 bytesleft -= n;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
537 }
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
538
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
539 return total;
8bcd4d4ccef6 [gaim-migrate @ 13372]
Charlie Stockman <chuckleberry>
parents:
diff changeset
540 }