/
srvc_ft.c
671 lines (462 loc) · 14.9 KB
/
srvc_ft.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*
Meanwhile - Unofficial Lotus Sametime Community Client Library
Copyright (C) 2004 Christopher (siege) O'Brien
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <glib.h>
#include "mw_channel.h"
#include "mw_common.h"
#include "mw_debug.h"
#include "mw_error.h"
#include "mw_message.h"
#include "mw_service.h"
#include "mw_session.h"
#include "mw_srvc_ft.h"
#include "mw_util.h"
#define PROTOCOL_TYPE 0x00000000
#define PROTOCOL_VER 0x00000001
/** send-on-channel type: FT transfer data */
#define msg_TRANSFER 0x0001
/** ack received transfer data */
#define msg_RECEIVED 0x0002
struct mwServiceFileTransfer {
struct mwService service;
struct mwFileTransferHandler *handler;
GList *transfers;
};
struct mwFileTransfer {
struct mwServiceFileTransfer *service;
struct mwChannel *channel;
struct mwIdBlock who;
enum mwFileTransferState state;
char *filename;
char *message;
guint32 size;
guint32 remaining;
struct mw_datum client_data;
};
/** momentarily places a mwLoginInfo into a mwIdBlock */
static void login_into_id(struct mwIdBlock *to, struct mwLoginInfo *from) {
to->user = from->user_id;
to->community = from->community;
}
static const char *ft_state_str(enum mwFileTransferState state) {
switch(state) {
case mwFileTransfer_NEW:
return "new";
case mwFileTransfer_PENDING:
return "pending";
case mwFileTransfer_OPEN:
return "open";
case mwFileTransfer_CANCEL_LOCAL:
return "cancelled locally";
case mwFileTransfer_CANCEL_REMOTE:
return "cancelled remotely";
case mwFileTransfer_DONE:
return "done";
case mwFileTransfer_ERROR:
return "error";
case mwFileTransfer_UNKNOWN:
default:
return "UNKNOWN";
}
}
static void ft_state(struct mwFileTransfer *ft,
enum mwFileTransferState state) {
g_return_if_fail(ft != NULL);
if(ft->state == state) return;
g_info("setting ft (%s, %s) state: %s",
NSTR(ft->who.user), NSTR(ft->who.community),
ft_state_str(state));
ft->state = state;
}
static void recv_channelCreate(struct mwServiceFileTransfer *srvc,
struct mwChannel *chan,
struct mwMsgChannelCreate *msg) {
struct mwFileTransferHandler *handler;
struct mwGetBuffer *b;
char *fnm, *txt;
guint32 size, junk;
gboolean b_err;
g_return_if_fail(srvc->handler != NULL);
handler = srvc->handler;
b = mwGetBuffer_wrap(&msg->addtl);
guint32_get(b, &junk); /* unknown */
mwString_get(b, &fnm); /* offered filename */
mwString_get(b, &txt); /* offering message */
guint32_get(b, &size); /* size of offered file */
guint32_get(b, &junk); /* unknown */
/* and we just skip an unknown guint16 at the end */
b_err = mwGetBuffer_error(b);
mwGetBuffer_free(b);
if(b_err) {
g_warning("bad/malformed addtl in File Transfer service");
mwChannel_destroy(chan, ERR_FAILURE, NULL);
} else {
struct mwIdBlock idb;
struct mwFileTransfer *ft;
login_into_id(&idb, mwChannel_getUser(chan));
ft = mwFileTransfer_new(srvc, &idb, txt, fnm, size);
ft->channel = chan;
ft_state(ft, mwFileTransfer_PENDING);
mwChannel_setServiceData(chan, ft, NULL);
if(handler->ft_offered)
handler->ft_offered(ft);
}
g_free(fnm);
g_free(txt);
}
static void recv_channelAccept(struct mwServiceFileTransfer *srvc,
struct mwChannel *chan,
struct mwMsgChannelAccept *msg) {
// `msg` unused
(void)msg;
struct mwFileTransferHandler *handler;
struct mwFileTransfer *ft;
g_return_if_fail(srvc->handler != NULL);
handler = srvc->handler;
ft = mwChannel_getServiceData(chan);
g_return_if_fail(ft != NULL);
ft_state(ft, mwFileTransfer_OPEN);
if(handler->ft_opened)
handler->ft_opened(ft);
}
static void recv_channelDestroy(struct mwServiceFileTransfer *srvc,
struct mwChannel *chan,
struct mwMsgChannelDestroy *msg) {
struct mwFileTransferHandler *handler;
struct mwFileTransfer *ft;
guint32 code;
code = msg->reason;
g_return_if_fail(srvc->handler != NULL);
handler = srvc->handler;
ft = mwChannel_getServiceData(chan);
g_return_if_fail(ft != NULL);
ft->channel = NULL;
if(! mwFileTransfer_isDone(ft))
ft_state(ft, mwFileTransfer_CANCEL_REMOTE);
mwFileTransfer_close(ft, code);
}
static void recv_TRANSFER(struct mwFileTransfer *ft,
struct mwOpaque *data) {
struct mwServiceFileTransfer *srvc;
struct mwFileTransferHandler *handler;
srvc = ft->service;
handler = srvc->handler;
g_return_if_fail(mwFileTransfer_isOpen(ft));
if(data->len > ft->remaining) {
/* @todo handle error */
} else {
ft->remaining -= data->len;
if(! ft->remaining)
ft_state(ft, mwFileTransfer_DONE);
if(handler->ft_recv)
handler->ft_recv(ft, data);
}
}
static void recv_RECEIVED(struct mwFileTransfer *ft,
struct mwOpaque *data) {
// `data` unused
(void)data;
struct mwServiceFileTransfer *srvc;
struct mwFileTransferHandler *handler;
srvc = ft->service;
handler = srvc->handler;
if(! ft->remaining)
ft_state(ft, mwFileTransfer_DONE);
if(handler->ft_ack)
handler->ft_ack(ft);
if(! ft->remaining)
mwFileTransfer_close(ft, mwFileTransfer_SUCCESS);
}
static void recv(struct mwService *srvc, struct mwChannel *chan,
guint16 type, struct mwOpaque *data) {
// `srvc` unused
(void)srvc;
struct mwFileTransfer *ft;
ft = mwChannel_getServiceData(chan);
g_return_if_fail(ft != NULL);
switch(type) {
case msg_TRANSFER:
recv_TRANSFER(ft, data);
break;
case msg_RECEIVED:
recv_RECEIVED(ft, data);
break;
default:
mw_mailme_opaque(data, "unknown message in ft service: 0x%04x", type);
}
}
static void clear(struct mwServiceFileTransfer *srvc) {
struct mwFileTransferHandler *h;
h = srvc->handler;
if(h && h->clear)
h->clear(srvc);
srvc->handler = NULL;
}
static const char *name(struct mwService *srvc) {
// `srvc` unused
(void)srvc;
return "File Transfer";
}
static const char *desc(struct mwService *srvc) {
// `srvc` unused
(void)srvc;
return "Provides file transfer capabilities through the community server";
}
static void start(struct mwService *srvc) {
mwService_started(srvc);
}
static void stop(struct mwServiceFileTransfer *srvc) {
while(srvc->transfers) {
mwFileTransfer_free(srvc->transfers->data);
}
mwService_stopped(MW_SERVICE(srvc));
}
struct mwServiceFileTransfer *
mwServiceFileTransfer_new(struct mwSession *session,
struct mwFileTransferHandler *handler) {
struct mwServiceFileTransfer *srvc_ft;
struct mwService *srvc;
g_return_val_if_fail(session != NULL, NULL);
g_return_val_if_fail(handler != NULL, NULL);
srvc_ft = g_new0(struct mwServiceFileTransfer, 1);
srvc = MW_SERVICE(srvc_ft);
mwService_init(srvc, session, mwService_FILE_TRANSFER);
srvc->recv_create = (mwService_funcRecvCreate) recv_channelCreate;
srvc->recv_accept = (mwService_funcRecvAccept) recv_channelAccept;
srvc->recv_destroy = (mwService_funcRecvDestroy) recv_channelDestroy;
srvc->recv = recv;
srvc->clear = (mwService_funcClear) clear;
srvc->get_name = name;
srvc->get_desc = desc;
srvc->start = start;
srvc->stop = (mwService_funcStop) stop;
srvc_ft->handler = handler;
return srvc_ft;
}
struct mwFileTransferHandler *
mwServiceFileTransfer_getHandler(struct mwServiceFileTransfer *srvc) {
g_return_val_if_fail(srvc != NULL, NULL);
return srvc->handler;
}
const GList *
mwServiceFileTransfer_getTransfers(struct mwServiceFileTransfer *srvc) {
g_return_val_if_fail(srvc != NULL, NULL);
return srvc->transfers;
}
struct mwFileTransfer *
mwFileTransfer_new(struct mwServiceFileTransfer *srvc,
const struct mwIdBlock *who, const char *msg,
const char *filename, guint32 filesize) {
struct mwFileTransfer *ft;
g_return_val_if_fail(srvc != NULL, NULL);
g_return_val_if_fail(who != NULL, NULL);
ft = g_new0(struct mwFileTransfer, 1);
ft->service = srvc;
mwIdBlock_clone(&ft->who, who);
ft->filename = g_strdup(filename);
ft->message = g_strdup(msg);
ft->size = ft->remaining = filesize;
ft_state(ft, mwFileTransfer_NEW);
/* stick a reference in the service */
srvc->transfers = g_list_prepend(srvc->transfers, ft);
return ft;
}
struct mwServiceFileTransfer *
mwFileTransfer_getService(struct mwFileTransfer *ft) {
g_return_val_if_fail(ft != NULL, NULL);
return ft->service;
}
enum mwFileTransferState
mwFileTransfer_getState(struct mwFileTransfer *ft) {
g_return_val_if_fail(ft != NULL, mwFileTransfer_UNKNOWN);
return ft->state;
}
const struct mwIdBlock *
mwFileTransfer_getUser(struct mwFileTransfer *ft) {
g_return_val_if_fail(ft != NULL, NULL);
return &ft->who;
}
const char *
mwFileTransfer_getMessage(struct mwFileTransfer *ft) {
g_return_val_if_fail(ft != NULL, NULL);
return ft->message;
}
const char *
mwFileTransfer_getFileName(struct mwFileTransfer *ft) {
g_return_val_if_fail(ft != NULL, NULL);
return ft->filename;
}
guint32 mwFileTransfer_getFileSize(struct mwFileTransfer *ft) {
g_return_val_if_fail(ft != NULL, 0);
return ft->size;
}
guint32 mwFileTransfer_getRemaining(struct mwFileTransfer *ft) {
g_return_val_if_fail(ft != NULL, 0);
return ft->remaining;
}
int mwFileTransfer_accept(struct mwFileTransfer *ft) {
struct mwServiceFileTransfer *srvc;
struct mwFileTransferHandler *handler;
int ret;
g_return_val_if_fail(ft != NULL, -1);
g_return_val_if_fail(ft->channel != NULL, -1);
g_return_val_if_fail(mwFileTransfer_isPending(ft), -1);
g_return_val_if_fail(mwChannel_isIncoming(ft->channel), -1);
g_return_val_if_fail(mwChannel_isState(ft->channel, mwChannel_WAIT), -1);
g_return_val_if_fail(ft->service != NULL, -1);
srvc = ft->service;
g_return_val_if_fail(srvc->handler != NULL, -1);
handler = srvc->handler;
ret = mwChannel_accept(ft->channel);
if(ret) {
mwFileTransfer_close(ft, ERR_FAILURE);
} else {
ft_state(ft, mwFileTransfer_OPEN);
if(handler->ft_opened)
handler->ft_opened(ft);
}
return ret;
}
static void ft_create_chan(struct mwFileTransfer *ft) {
struct mwSession *s;
struct mwChannelSet *cs;
struct mwChannel *chan;
struct mwLoginInfo *login;
struct mwPutBuffer *b;
/* we only should be calling this if there isn't a channel already
associated with the conversation */
g_return_if_fail(ft != NULL);
g_return_if_fail(mwFileTransfer_isNew(ft));
g_return_if_fail(ft->channel == NULL);
s = mwService_getSession(MW_SERVICE(ft->service));
cs = mwSession_getChannels(s);
chan = mwChannel_newOutgoing(cs);
mwChannel_setService(chan, MW_SERVICE(ft->service));
mwChannel_setProtoType(chan, PROTOCOL_TYPE);
mwChannel_setProtoVer(chan, PROTOCOL_VER);
/* offer all known ciphers */
mwChannel_populateSupportedCipherInstances(chan);
/* set the target */
login = mwChannel_getUser(chan);
login->user_id = g_strdup(ft->who.user);
login->community = g_strdup(ft->who.community);
/* compose the addtl create */
b = mwPutBuffer_new();
guint32_put(b, 0x00);
mwString_put(b, ft->filename);
mwString_put(b, ft->message);
guint32_put(b, ft->size);
guint32_put(b, 0x00);
guint16_put(b, 0x00);
mwPutBuffer_finalize(mwChannel_getAddtlCreate(chan), b);
ft->channel = mwChannel_create(chan)? NULL: chan;
if(ft->channel) {
mwChannel_setServiceData(ft->channel, ft, NULL);
}
}
int mwFileTransfer_offer(struct mwFileTransfer *ft) {
struct mwServiceFileTransfer *srvc;
struct mwFileTransferHandler *handler;
g_return_val_if_fail(ft != NULL, -1);
g_return_val_if_fail(ft->channel == NULL, -1);
g_return_val_if_fail(mwFileTransfer_isNew(ft), -1);
g_return_val_if_fail(ft->service != NULL, -1);
srvc = ft->service;
g_return_val_if_fail(srvc->handler != NULL, -1);
handler = srvc->handler;
ft_create_chan(ft);
if(ft->channel) {
ft_state(ft, mwFileTransfer_PENDING);
} else {
ft_state(ft, mwFileTransfer_ERROR);
mwFileTransfer_close(ft, ERR_FAILURE);
}
return 0;
}
int mwFileTransfer_close(struct mwFileTransfer *ft, guint32 code) {
struct mwServiceFileTransfer *srvc;
struct mwFileTransferHandler *handler;
int ret = 0;
g_return_val_if_fail(ft != NULL, -1);
if(mwFileTransfer_isOpen(ft))
ft_state(ft, mwFileTransfer_CANCEL_LOCAL);
if(ft->channel) {
ret = mwChannel_destroy(ft->channel, code, NULL);
ft->channel = NULL;
}
srvc = ft->service;
g_return_val_if_fail(srvc != NULL, ret);
handler = srvc->handler;
g_return_val_if_fail(handler != NULL, ret);
if(handler->ft_closed)
handler->ft_closed(ft, code);
return ret;
}
void mwFileTransfer_free(struct mwFileTransfer *ft) {
struct mwServiceFileTransfer *srvc;
if(! ft) return;
srvc = ft->service;
if(srvc)
srvc->transfers = g_list_remove(srvc->transfers, ft);
if(ft->channel) {
mwChannel_destroy(ft->channel, mwFileTransfer_SUCCESS, NULL);
ft->channel = NULL;
}
mwFileTransfer_removeClientData(ft);
mwIdBlock_clear(&ft->who);
g_free(ft->filename);
g_free(ft->message);
g_free(ft);
}
int mwFileTransfer_send(struct mwFileTransfer *ft,
struct mwOpaque *data) {
struct mwChannel *chan;
int ret;
g_return_val_if_fail(ft != NULL, -1);
g_return_val_if_fail(mwFileTransfer_isOpen(ft), -1);
g_return_val_if_fail(ft->channel != NULL, -1);
chan = ft->channel;
g_return_val_if_fail(mwChannel_isOutgoing(chan), -1);
if(data->len > ft->remaining) {
/* @todo handle error */
return -1;
}
ret = mwChannel_send(chan, msg_TRANSFER, data);
if(! ret) ft->remaining -= data->len;
/* we're not done until we receive an ACK for the last piece of
outgoing data */
return ret;
}
int mwFileTransfer_ack(struct mwFileTransfer *ft) {
struct mwChannel *chan;
g_return_val_if_fail(ft != NULL, -1);
chan = ft->channel;
g_return_val_if_fail(chan != NULL, -1);
g_return_val_if_fail(mwChannel_isIncoming(chan), -1);
return mwChannel_sendEncrypted(chan, msg_RECEIVED, NULL, FALSE);
}
void mwFileTransfer_setClientData(struct mwFileTransfer *ft,
gpointer data, GDestroyNotify clean) {
g_return_if_fail(ft != NULL);
mw_datum_set(&ft->client_data, data, clean);
}
gpointer mwFileTransfer_getClientData(struct mwFileTransfer *ft) {
g_return_val_if_fail(ft != NULL, NULL);
return mw_datum_get(&ft->client_data);
}
void mwFileTransfer_removeClientData(struct mwFileTransfer *ft) {
g_return_if_fail(ft != NULL);
mw_datum_clear(&ft->client_data);
}