14192
|
1 /**
|
|
2 * @file dcc_send.c Functions used in sending files with DCC SEND
|
|
3 *
|
|
4 * gaim
|
|
5 *
|
|
6 * Copyright (C) 2004, Timothy T Ringenbach <omarvo@hotmail.com>
|
|
7 * Copyright (C) 2003, Robbert Haarman <gaim@inglorion.net>
|
|
8 *
|
|
9 * This program is free software; you can redistribute it and/or modify
|
|
10 * it under the terms of the GNU General Public License as published by
|
|
11 * the Free Software Foundation; either version 2 of the License, or
|
|
12 * (at your option) any later version.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
22 */
|
|
23
|
|
24 #include "internal.h"
|
|
25 #include "irc.h"
|
|
26 #include "debug.h"
|
|
27 #include "ft.h"
|
|
28 #include "notify.h"
|
|
29 #include "network.h"
|
|
30
|
|
31 /***************************************************************************
|
|
32 * Functions related to receiving files via DCC SEND
|
|
33 ***************************************************************************/
|
|
34
|
|
35 struct irc_xfer_rx_data {
|
|
36 gchar *ip;
|
|
37 };
|
|
38
|
|
39 static void irc_dccsend_recv_destroy(GaimXfer *xfer)
|
|
40 {
|
|
41 struct irc_xfer_rx_data *xd = xfer->data;
|
|
42
|
|
43 if (xd->ip != NULL)
|
|
44 g_free(xd->ip);
|
|
45
|
|
46 g_free(xd);
|
|
47 }
|
|
48
|
|
49 /*
|
|
50 * This function is called whenever data is received.
|
|
51 * It sends the acknowledgement (in the form of a total byte count as an
|
|
52 * unsigned 4 byte integer in network byte order)
|
|
53 */
|
|
54 static void irc_dccsend_recv_ack(GaimXfer *xfer, const guchar *data, size_t size) {
|
|
55 unsigned long l;
|
|
56
|
|
57 l = htonl(xfer->bytes_sent);
|
|
58 write(xfer->fd, &l, sizeof(l));
|
|
59 }
|
|
60
|
|
61 static void irc_dccsend_recv_init(GaimXfer *xfer) {
|
|
62 struct irc_xfer_rx_data *xd = xfer->data;
|
|
63
|
|
64 gaim_xfer_start(xfer, -1, xd->ip, xfer->remote_port);
|
|
65 g_free(xd->ip);
|
|
66 xd->ip = NULL;
|
|
67 }
|
|
68
|
|
69 /* This function makes the necessary arrangements for receiving files */
|
|
70 void irc_dccsend_recv(struct irc_conn *irc, const char *from, const char *msg) {
|
|
71 GaimXfer *xfer;
|
|
72 struct irc_xfer_rx_data *xd;
|
|
73 gchar **token;
|
|
74 struct in_addr addr;
|
|
75 GString *filename;
|
|
76 int i = 0;
|
|
77 guint32 nip;
|
|
78
|
|
79 token = g_strsplit(msg, " ", 0);
|
|
80 if (!token[0] || !token[1] || !token[2]) {
|
|
81 g_strfreev(token);
|
|
82 return;
|
|
83 }
|
|
84
|
|
85 filename = g_string_new("");
|
|
86 if (token[0][0] == '"') {
|
|
87 if (!strchr(&(token[0][1]), '"')) {
|
|
88 g_string_append(filename, &(token[0][1]));
|
|
89 for (i = 1; token[i]; i++)
|
|
90 if (!strchr(token[i], '"')) {
|
|
91 g_string_append_printf(filename, " %s", token[i]);
|
|
92 } else {
|
|
93 g_string_append_len(filename, token[i], strlen(token[i]) - 1);
|
|
94 break;
|
|
95 }
|
|
96 } else {
|
|
97 g_string_append_len(filename, &(token[0][1]), strlen(&(token[0][1])) - 1);
|
|
98 }
|
|
99 } else {
|
|
100 g_string_append(filename, token[0]);
|
|
101 }
|
|
102
|
|
103 if (!token[i] || !token[i+1] || !token[i+2]) {
|
|
104 g_strfreev(token);
|
|
105 g_string_free(filename, TRUE);
|
|
106 return;
|
|
107 }
|
|
108 i++;
|
|
109
|
|
110 xfer = gaim_xfer_new(irc->account, GAIM_XFER_RECEIVE, from);
|
|
111 xd = g_new0(struct irc_xfer_rx_data, 1);
|
|
112 xfer->data = xd;
|
|
113
|
|
114 gaim_xfer_set_filename(xfer, filename->str);
|
|
115 xfer->remote_port = atoi(token[i+1]);
|
|
116
|
|
117 nip = strtoul(token[i], NULL, 10);
|
|
118 if (nip) {
|
|
119 addr.s_addr = htonl(nip);
|
|
120 xd->ip = g_strdup(inet_ntoa(addr));
|
|
121 } else {
|
|
122 xd->ip = g_strdup(token[i]);
|
|
123 }
|
|
124 gaim_debug(GAIM_DEBUG_INFO, "irc", "Receiving file from %s\n",
|
|
125 xd->ip);
|
|
126 gaim_xfer_set_size(xfer, token[i+2] ? atoi(token[i+2]) : 0);
|
|
127
|
|
128 gaim_xfer_set_init_fnc(xfer, irc_dccsend_recv_init);
|
|
129 gaim_xfer_set_ack_fnc(xfer, irc_dccsend_recv_ack);
|
|
130
|
|
131 gaim_xfer_set_end_fnc(xfer, irc_dccsend_recv_destroy);
|
|
132 gaim_xfer_set_request_denied_fnc(xfer, irc_dccsend_recv_destroy);
|
|
133 gaim_xfer_set_cancel_send_fnc(xfer, irc_dccsend_recv_destroy);
|
|
134
|
|
135 gaim_xfer_request(xfer);
|
|
136 g_strfreev(token);
|
|
137 g_string_free(filename, TRUE);
|
|
138 }
|
|
139
|
|
140 /*******************************************************************
|
|
141 * Functions related to sending files via DCC SEND
|
|
142 *******************************************************************/
|
|
143
|
|
144 struct irc_xfer_send_data {
|
14267
|
145 GaimNetworkListenData *listen_data;
|
14192
|
146 gint inpa;
|
|
147 int fd;
|
|
148 guchar *rxqueue;
|
|
149 guint rxlen;
|
|
150 };
|
|
151
|
|
152 static void irc_dccsend_send_destroy(GaimXfer *xfer)
|
|
153 {
|
|
154 struct irc_xfer_send_data *xd = xfer->data;
|
|
155
|
|
156 if (xd == NULL)
|
|
157 return;
|
|
158
|
14267
|
159 if (xd->listen_data != NULL)
|
|
160 gaim_network_listen_cancel(xd->listen_data);
|
14192
|
161 if (xd->inpa > 0)
|
|
162 gaim_input_remove(xd->inpa);
|
|
163 if (xd->fd != -1)
|
|
164 close(xd->fd);
|
|
165
|
|
166 if (xd->rxqueue)
|
|
167 g_free(xd->rxqueue);
|
|
168
|
|
169 g_free(xd);
|
|
170 }
|
|
171
|
|
172 /* just in case you were wondering, this is why DCC is gay */
|
|
173 static void irc_dccsend_send_read(gpointer data, int source, GaimInputCondition cond)
|
|
174 {
|
|
175 GaimXfer *xfer = data;
|
|
176 struct irc_xfer_send_data *xd = xfer->data;
|
|
177 char *buffer[16];
|
|
178 int len;
|
|
179
|
|
180 len = read(source, buffer, sizeof(buffer));
|
|
181
|
|
182 if (len < 0 && errno == EAGAIN)
|
|
183 return;
|
|
184 else if (len <= 0) {
|
|
185 /* XXX: Shouldn't this be canceling the transfer? */
|
|
186 gaim_input_remove(xd->inpa);
|
|
187 xd->inpa = 0;
|
|
188 return;
|
|
189 }
|
|
190
|
|
191 xd->rxqueue = g_realloc(xd->rxqueue, len + xd->rxlen);
|
|
192 memcpy(xd->rxqueue + xd->rxlen, buffer, len);
|
|
193 xd->rxlen += len;
|
|
194
|
|
195 while (1) {
|
|
196 size_t acked;
|
|
197
|
|
198 if (xd->rxlen < 4)
|
|
199 break;
|
|
200
|
|
201 acked = ntohl(*((gint32 *)xd->rxqueue));
|
|
202
|
|
203 xd->rxlen -= 4;
|
|
204 if (xd->rxlen) {
|
|
205 unsigned char *tmp = g_memdup(xd->rxqueue + 4, xd->rxlen);
|
|
206 g_free(xd->rxqueue);
|
|
207 xd->rxqueue = tmp;
|
|
208 } else {
|
|
209 g_free(xd->rxqueue);
|
|
210 xd->rxqueue = NULL;
|
|
211 }
|
|
212
|
|
213 if (acked >= gaim_xfer_get_size(xfer)) {
|
|
214 gaim_input_remove(xd->inpa);
|
|
215 xd->inpa = 0;
|
|
216 gaim_xfer_set_completed(xfer, TRUE);
|
|
217 gaim_xfer_end(xfer);
|
|
218 return;
|
|
219 }
|
|
220 }
|
|
221 }
|
|
222
|
|
223 static gssize irc_dccsend_send_write(const guchar *buffer, size_t size, GaimXfer *xfer)
|
|
224 {
|
|
225 gssize s;
|
|
226 int ret;
|
|
227
|
|
228 s = MIN(gaim_xfer_get_bytes_remaining(xfer), size);
|
|
229 if (!s)
|
|
230 return 0;
|
|
231
|
|
232 ret = write(xfer->fd, buffer, s);
|
|
233
|
|
234 if (ret < 0 && errno == EAGAIN)
|
|
235 ret = 0;
|
|
236
|
|
237 return ret;
|
|
238 }
|
|
239
|
|
240 static void irc_dccsend_send_connected(gpointer data, int source, GaimInputCondition cond) {
|
|
241 GaimXfer *xfer = (GaimXfer *) data;
|
|
242 struct irc_xfer_send_data *xd = xfer->data;
|
|
243 int conn;
|
|
244
|
|
245 conn = accept(xd->fd, NULL, 0);
|
|
246 if (conn == -1) {
|
|
247 /* Accepting the connection failed. This could just be related
|
|
248 * to the nonblocking nature of the listening socket, so we'll
|
|
249 * just try again next time */
|
|
250 /* Let's print an error message anyway */
|
|
251 gaim_debug_warning("irc", "accept: %s\n", strerror(errno));
|
|
252 return;
|
|
253 }
|
|
254
|
|
255 gaim_input_remove(xfer->watcher);
|
|
256 xfer->watcher = 0;
|
|
257 close(xd->fd);
|
|
258 xd->fd = -1;
|
|
259
|
|
260 xd->inpa = gaim_input_add(conn, GAIM_INPUT_READ, irc_dccsend_send_read, xfer);
|
|
261 /* Start the transfer */
|
|
262 gaim_xfer_start(xfer, conn, NULL, 0);
|
|
263 }
|
|
264
|
|
265 static void
|
|
266 irc_dccsend_network_listen_cb(int sock, gpointer data)
|
|
267 {
|
|
268 GaimXfer *xfer = data;
|
|
269 struct irc_xfer_send_data *xd;
|
|
270 GaimConnection *gc;
|
|
271 struct irc_conn *irc;
|
|
272 const char *arg[2];
|
|
273 char *tmp;
|
|
274 struct in_addr addr;
|
|
275 unsigned short int port;
|
|
276
|
14267
|
277 xd = xfer->data;
|
|
278 xd->listen_data = NULL;
|
|
279
|
14192
|
280 if (gaim_xfer_get_status(xfer) == GAIM_XFER_STATUS_CANCEL_LOCAL
|
|
281 || gaim_xfer_get_status(xfer) == GAIM_XFER_STATUS_CANCEL_REMOTE) {
|
|
282 gaim_xfer_unref(xfer);
|
|
283 return;
|
|
284 }
|
|
285
|
|
286 xd = xfer->data;
|
|
287 gc = gaim_account_get_connection(gaim_xfer_get_account(xfer));
|
|
288 irc = gc->proto_data;
|
|
289
|
|
290 gaim_xfer_unref(xfer);
|
|
291
|
|
292 if (sock < 0) {
|
|
293 gaim_notify_error(gc, NULL, _("File Transfer Failed"),
|
|
294 _("Gaim could not open a listening port."));
|
|
295 gaim_xfer_cancel_local(xfer);
|
|
296 return;
|
|
297 }
|
|
298
|
|
299 xd->fd = sock;
|
|
300
|
|
301 port = gaim_network_get_port_from_fd(sock);
|
|
302 gaim_debug_misc("irc", "port is %hu\n", port);
|
|
303 /* Monitor the listening socket */
|
|
304 xfer->watcher = gaim_input_add(sock, GAIM_INPUT_READ,
|
|
305 irc_dccsend_send_connected, xfer);
|
|
306
|
|
307 /* Send the intended recipient the DCC request */
|
|
308 arg[0] = xfer->who;
|
|
309 inet_aton(gaim_network_get_my_ip(irc->fd), &addr);
|
|
310 arg[1] = tmp = g_strdup_printf("\001DCC SEND \"%s\" %u %hu %" G_GSIZE_FORMAT "\001",
|
|
311 xfer->filename, ntohl(addr.s_addr),
|
|
312 port, xfer->size);
|
|
313
|
|
314 irc_cmd_privmsg(gc->proto_data, "msg", NULL, arg);
|
|
315 g_free(tmp);
|
|
316 }
|
|
317
|
|
318 /*
|
|
319 * This function is called after the user has selected a file to send.
|
|
320 */
|
|
321 static void irc_dccsend_send_init(GaimXfer *xfer) {
|
|
322 GaimConnection *gc = gaim_account_get_connection(gaim_xfer_get_account(xfer));
|
14267
|
323 struct irc_xfer_send_data *xd = xfer->data;
|
14192
|
324
|
|
325 xfer->filename = g_path_get_basename(xfer->local_filename);
|
|
326
|
|
327 gaim_xfer_ref(xfer);
|
|
328
|
|
329 /* Create a listening socket */
|
14267
|
330 xd->listen_data = gaim_network_listen_range(0, 0, SOCK_STREAM,
|
|
331 irc_dccsend_network_listen_cb, xfer);
|
|
332 if (xd->listen_data == NULL) {
|
14192
|
333 gaim_xfer_unref(xfer);
|
|
334 gaim_notify_error(gc, NULL, _("File Transfer Failed"),
|
|
335 _("Gaim could not open a listening port."));
|
|
336 gaim_xfer_cancel_local(xfer);
|
|
337 }
|
|
338
|
|
339 }
|
|
340
|
|
341 GaimXfer *irc_dccsend_new_xfer(GaimConnection *gc, const char *who) {
|
|
342 GaimXfer *xfer;
|
|
343 struct irc_xfer_send_data *xd;
|
|
344
|
|
345 /* Build the file transfer handle */
|
|
346 xfer = gaim_xfer_new(gaim_connection_get_account(gc), GAIM_XFER_SEND, who);
|
|
347
|
|
348 xd = g_new0(struct irc_xfer_send_data, 1);
|
|
349 xd->fd = -1;
|
|
350 xfer->data = xd;
|
|
351
|
|
352 /* Setup our I/O op functions */
|
|
353 gaim_xfer_set_init_fnc(xfer, irc_dccsend_send_init);
|
|
354 gaim_xfer_set_write_fnc(xfer, irc_dccsend_send_write);
|
|
355 gaim_xfer_set_end_fnc(xfer, irc_dccsend_send_destroy);
|
|
356 gaim_xfer_set_request_denied_fnc(xfer, irc_dccsend_send_destroy);
|
|
357 gaim_xfer_set_cancel_send_fnc(xfer, irc_dccsend_send_destroy);
|
|
358
|
|
359 return xfer;
|
|
360 }
|
|
361
|
|
362 /**
|
|
363 * Gaim calls this function when the user selects Send File from the
|
|
364 * buddy menu
|
|
365 * It sets up the GaimXfer struct and tells Gaim to go ahead
|
|
366 */
|
|
367 void irc_dccsend_send_file(GaimConnection *gc, const char *who, const char *file) {
|
|
368 GaimXfer *xfer = irc_dccsend_new_xfer(gc, who);
|
|
369
|
|
370 /* Perform the request */
|
|
371 if (file)
|
|
372 gaim_xfer_request_accepted(xfer, file);
|
|
373 else
|
|
374 gaim_xfer_request(xfer);
|
|
375 }
|