5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2007 Pekka Riikonen
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; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 * Created: Fri Jul 25 18:52:14 1997
26 /************************** Types and definitions ***************************/
28 /* Per scheduler (which usually means per thread) data. We put per scheduler
29 data here for accessing without locking. SILC Schedule dictates that
30 tasks are dispatched in one thread, hence the per scheduler context. */
32 SilcSchedule schedule; /* The scheduler */
33 SilcPacketEngine engine; /* Packet engine */
34 SilcDList inbufs; /* Data inbut buffer list */
35 SilcUInt32 stream_count; /* Number of streams using this */
36 } *SilcPacketEngineContext;
39 struct SilcPacketEngineStruct {
40 SilcMutex lock; /* Engine lock */
41 SilcRng rng; /* RNG for engine */
42 SilcHashTable contexts; /* Per scheduler contexts */
43 SilcPacketCallbacks *callbacks; /* Packet callbacks */
44 void *callback_context; /* Context for callbacks */
45 SilcList streams; /* All streams in engine */
46 SilcList packet_pool; /* Free list for received packets */
47 SilcHashTable udp_remote; /* UDP remote streams, or NULL */
48 unsigned int local_is_router : 1;
51 /* Packet processor context */
52 typedef struct SilcPacketProcessStruct {
53 SilcPacketType *types; /* Packets to process */
54 SilcPacketCallbacks *callbacks; /* Callbacks or NULL */
55 void *callback_context;
56 SilcInt32 priority; /* Priority */
59 /* UDP remote stream tuple */
61 char *remote_ip; /* Remote IP address */
62 SilcUInt16 remote_port; /* Remote port */
63 } *SilcPacketRemoteUDP;
66 struct SilcPacketStreamStruct {
67 struct SilcPacketStreamStruct *next;
68 SilcPacketEngineContext sc; /* Per scheduler context */
69 SilcStream stream; /* Underlaying stream */
70 SilcMutex lock; /* Packet stream lock */
71 SilcDList process; /* Packet processors, or NULL */
72 SilcPacketRemoteUDP remote_udp; /* UDP remote stream tuple, or NULL */
73 void *stream_context; /* Stream context */
74 SilcBufferStruct outbuf; /* Out buffer */
75 SilcBuffer inbuf; /* Inbuf from inbuf list or NULL */
76 SilcCipher send_key[2]; /* Sending key */
77 SilcHmac send_hmac[2]; /* Sending HMAC */
78 SilcCipher receive_key[2]; /* Receiving key */
79 SilcHmac receive_hmac[2]; /* Receiving HMAC */
80 unsigned char *src_id; /* Source ID */
81 unsigned char *dst_id; /* Destination ID */
82 SilcUInt32 send_psn; /* Sending sequence */
83 SilcUInt32 receive_psn; /* Receiving sequence */
84 SilcAtomic8 refcnt; /* Reference counter */
85 SilcUInt8 sid; /* Security ID, set if IV included */
86 unsigned int src_id_len : 6;
87 unsigned int src_id_type : 2;
88 unsigned int dst_id_len : 6;
89 unsigned int dst_id_type : 2;
90 unsigned int is_router : 1; /* Set if router stream */
91 unsigned int destroyed : 1; /* Set if destroyed */
92 unsigned int iv_included : 1; /* Set if IV included */
93 unsigned int udp : 1; /* UDP remote stream */
96 /* Initial size of stream buffers */
97 #define SILC_PACKET_DEFAULT_SIZE 1024
99 /* Header length without source and destination ID's. */
100 #define SILC_PACKET_HEADER_LEN 10
102 /* Minimum length of SILC Packet Header. */
103 #define SILC_PACKET_MIN_HEADER_LEN 16
104 #define SILC_PACKET_MIN_HEADER_LEN_IV 32 + 1
106 /* Maximum padding length */
107 #define SILC_PACKET_MAX_PADLEN 128
109 /* Default padding length */
110 #define SILC_PACKET_DEFAULT_PADLEN 16
112 /* Minimum packet length */
113 #define SILC_PACKET_MIN_LEN (SILC_PACKET_HEADER_LEN + 1)
115 /* Returns true length of the packet. */
116 #define SILC_PACKET_LENGTH(__packetdata, __ret_truelen, __ret_paddedlen) \
118 SILC_GET16_MSB((__ret_truelen), (__packetdata)); \
119 (__ret_paddedlen) = (__ret_truelen) + (SilcUInt8)(__packetdata)[4]; \
122 /* Calculates the data length with given header length. This macro
123 can be used to check whether the data_len with header_len exceeds
124 SILC_PACKET_MAX_LEN. If it does, this returns the new data_len
125 so that the SILC_PACKET_MAX_LEN is not exceeded. If the data_len
126 plus header_len fits SILC_PACKET_MAX_LEN the returned data length
127 is the data_len given as argument. */
128 #define SILC_PACKET_DATALEN(data_len, header_len) \
129 ((data_len + header_len) > SILC_PACKET_MAX_LEN ? \
130 data_len - ((data_len + header_len) - SILC_PACKET_MAX_LEN) : data_len)
132 /* Calculates the length of the padding in the packet. */
133 #define SILC_PACKET_PADLEN(__packetlen, __blocklen, __padlen) \
135 __padlen = (SILC_PACKET_DEFAULT_PADLEN - (__packetlen) % \
136 ((__blocklen) ? (__blocklen) : SILC_PACKET_DEFAULT_PADLEN)); \
138 __padlen += ((__blocklen) ? (__blocklen) : SILC_PACKET_DEFAULT_PADLEN); \
141 /* Returns the length of the padding up to the maximum length, which
143 #define SILC_PACKET_PADLEN_MAX(__packetlen, __blocklen, __padlen) \
145 __padlen = (SILC_PACKET_MAX_PADLEN - (__packetlen) % \
146 ((__blocklen) ? (__blocklen) : SILC_PACKET_DEFAULT_PADLEN)); \
150 #define SILC_PACKET_CALLBACK_EOS(s) \
152 (s)->sc->engine->callbacks->eos((s)->sc->engine, s, \
153 (s)->sc->engine->callback_context, \
154 (s)->stream_context); \
158 #define SILC_PACKET_CALLBACK_ERROR(s, err) \
160 (s)->sc->engine->callbacks->error((s)->sc->engine, s, err, \
161 (s)->sc->engine->callback_context, \
162 (s)->stream_context); \
165 static SilcBool silc_packet_dispatch(SilcPacket packet);
166 static void silc_packet_read_process(SilcPacketStream stream);
167 static inline SilcBool silc_packet_send_raw(SilcPacketStream stream,
169 SilcPacketFlags flags,
170 SilcIdType src_id_type,
171 unsigned char *src_id,
172 SilcUInt32 src_id_len,
173 SilcIdType dst_id_type,
174 unsigned char *dst_id,
175 SilcUInt32 dst_id_len,
176 const unsigned char *data,
181 /************************ Static utility functions **************************/
183 /* Injects packet to new stream created with silc_packet_stream_add_remote. */
185 SILC_TASK_CALLBACK(silc_packet_stream_inject_packet)
187 SilcPacket packet = context;
188 SilcPacketStream stream = packet->stream;
190 SILC_LOG_DEBUG(("Injecting packet %p to stream %p", packet, packet->stream));
192 silc_mutex_lock(stream->lock);
193 if (!stream->destroyed)
194 silc_packet_dispatch(packet);
195 silc_mutex_unlock(stream->lock);
196 silc_packet_stream_unref(stream);
199 /* Write data to the stream. Must be called with ps->lock locked. Unlocks
200 the lock inside this function, unless no_unlock is TRUE. Unlocks always
201 in case it returns FALSE. */
203 static inline SilcBool silc_packet_stream_write(SilcPacketStream ps,
211 stream = ((SilcPacketStream)ps->stream)->stream;
215 if (ps->udp && silc_socket_stream_is_udp(stream, &connected)) {
217 /* Connectionless UDP stream */
218 while (silc_buffer_len(&ps->outbuf) > 0) {
219 i = silc_net_udp_send(stream, ps->remote_udp->remote_ip,
220 ps->remote_udp->remote_port,
221 ps->outbuf.data, silc_buffer_len(&ps->outbuf));
222 if (silc_unlikely(i == -2)) {
224 silc_buffer_reset(&ps->outbuf);
225 SILC_PACKET_CALLBACK_ERROR(ps, SILC_PACKET_ERR_WRITE);
229 if (silc_unlikely(i == -1)) {
230 /* Cannot write now, write later. */
232 silc_mutex_unlock(ps->lock);
237 silc_buffer_pull(&ps->outbuf, i);
240 silc_buffer_reset(&ps->outbuf);
242 silc_mutex_unlock(ps->lock);
248 /* Write the data to the stream */
249 while (silc_buffer_len(&ps->outbuf) > 0) {
250 i = silc_stream_write(stream, ps->outbuf.data,
251 silc_buffer_len(&ps->outbuf));
252 if (silc_unlikely(i == 0)) {
254 silc_buffer_reset(&ps->outbuf);
255 silc_mutex_unlock(ps->lock);
256 SILC_PACKET_CALLBACK_EOS(ps);
260 if (silc_unlikely(i == -2)) {
262 silc_buffer_reset(&ps->outbuf);
263 silc_mutex_unlock(ps->lock);
264 SILC_PACKET_CALLBACK_ERROR(ps, SILC_PACKET_ERR_WRITE);
268 if (silc_unlikely(i == -1)) {
269 /* Cannot write now, write later. */
271 silc_mutex_unlock(ps->lock);
276 silc_buffer_pull(&ps->outbuf, i);
279 silc_buffer_reset(&ps->outbuf);
281 silc_mutex_unlock(ps->lock);
286 /* Reads data from stream. Must be called with ps->lock locked. If this
287 returns FALSE the lock has been unlocked. If this returns packet stream
288 to `ret_ps' its lock has been acquired and `ps' lock has been unlocked.
289 It is returned if the stream is UDP and remote UDP stream exists for
290 the sender of the packet. */
292 static inline SilcBool silc_packet_stream_read(SilcPacketStream ps,
293 SilcPacketStream *ret_ps)
295 SilcStream stream = ps->stream;
300 /* Get inbuf. If there is already some data for this stream in the buffer
301 we already have it. Otherwise get the current one from list, it will
305 silc_dlist_start(ps->sc->inbufs);
306 inbuf = silc_dlist_get(ps->sc->inbufs);
308 /* Allocate new data input buffer */
309 inbuf = silc_buffer_alloc(SILC_PACKET_DEFAULT_SIZE * 31);
311 silc_mutex_unlock(ps->lock);
314 silc_buffer_reset(inbuf);
315 silc_dlist_add(ps->sc->inbufs, inbuf);
319 /* Make sure there is enough room to read */
320 if (SILC_PACKET_DEFAULT_SIZE * 2 > silc_buffer_taillen(inbuf))
321 silc_buffer_realloc(inbuf, silc_buffer_truelen(inbuf) +
322 (SILC_PACKET_DEFAULT_SIZE * 2));
324 if (silc_socket_stream_is_udp(stream, &connected)) {
326 /* Connectionless UDP stream, read one UDP packet */
327 char remote_ip[64], tuple[64];
329 SilcPacketStream remote;
331 ret = silc_net_udp_receive(stream, remote_ip, sizeof(remote_ip),
332 &remote_port, inbuf->tail,
333 silc_buffer_taillen(inbuf));
335 if (silc_unlikely(ret < 0)) {
336 silc_mutex_unlock(ps->lock);
338 /* Cannot read now, do it later. */
339 silc_buffer_pull(inbuf, silc_buffer_len(inbuf));
344 silc_buffer_reset(inbuf);
345 SILC_PACKET_CALLBACK_ERROR(ps, SILC_PACKET_ERR_READ);
349 /* See if remote packet stream exist for this sender */
350 silc_snprintf(tuple, sizeof(tuple), "%d%s", remote_port, remote_ip);
351 silc_mutex_lock(ps->sc->engine->lock);
352 if (silc_hash_table_find(ps->sc->engine->udp_remote, tuple, NULL,
354 silc_mutex_unlock(ps->sc->engine->lock);
355 SILC_LOG_DEBUG(("UDP packet from %s:%d for stream %p", remote_ip,
356 remote_port, remote));
357 silc_mutex_unlock(ps->lock);
358 silc_mutex_lock(remote->lock);
362 silc_mutex_unlock(ps->sc->engine->lock);
365 if (!ps->remote_udp) {
366 ps->remote_udp = silc_calloc(1, sizeof(*ps->remote_udp));
367 if (silc_unlikely(!ps->remote_udp)) {
368 silc_mutex_unlock(ps->lock);
369 SILC_PACKET_CALLBACK_ERROR(ps, SILC_PACKET_ERR_NO_MEMORY);
374 /* Save sender IP and port */
375 silc_free(ps->remote_udp->remote_ip);
376 ps->remote_udp->remote_ip = strdup(remote_ip);
377 ps->remote_udp->remote_port = remote_port;
379 silc_buffer_pull_tail(inbuf, ret);
384 /* Read data from the stream */
385 ret = silc_stream_read(stream, inbuf->tail, silc_buffer_taillen(inbuf));
386 if (silc_unlikely(ret <= 0)) {
387 silc_mutex_unlock(ps->lock);
390 silc_buffer_reset(inbuf);
391 SILC_PACKET_CALLBACK_EOS(ps);
396 /* Cannot read now, do it later. */
397 silc_buffer_pull(inbuf, silc_buffer_len(inbuf));
402 silc_buffer_reset(inbuf);
403 SILC_PACKET_CALLBACK_ERROR(ps, SILC_PACKET_ERR_READ);
407 silc_buffer_pull_tail(inbuf, ret);
411 /* Our stream IO notifier callback. */
413 static void silc_packet_stream_io(SilcStream stream, SilcStreamStatus status,
416 SilcPacketStream remote = NULL, ps = context;
418 silc_mutex_lock(ps->lock);
420 if (silc_unlikely(ps->destroyed)) {
421 silc_mutex_unlock(ps->lock);
426 case SILC_STREAM_CAN_READ:
427 /* Reading is locked also with stream->lock because we may be reading
428 at the same time other thread is writing to same underlaying stream. */
429 SILC_LOG_DEBUG(("Reading data from stream %p, ps %p", ps->stream, ps));
431 /* Read data from stream */
432 if (!silc_packet_stream_read(ps, &remote))
435 /* Now process the data */
436 silc_packet_stream_ref(ps);
438 silc_packet_read_process(ps);
439 silc_mutex_unlock(ps->lock);
441 silc_packet_read_process(remote);
442 silc_mutex_unlock(remote->lock);
444 silc_packet_stream_unref(ps);
447 case SILC_STREAM_CAN_WRITE:
448 SILC_LOG_DEBUG(("Writing pending data to stream %p, ps %p",
451 if (silc_unlikely(!silc_buffer_headlen(&ps->outbuf))) {
452 silc_mutex_unlock(ps->lock);
456 /* Write pending data to stream */
457 silc_packet_stream_write(ps, FALSE);
461 silc_mutex_unlock(ps->lock);
466 /* Allocate packet */
468 static SilcPacket silc_packet_alloc(SilcPacketEngine engine)
472 SILC_LOG_DEBUG(("Packet pool count %d",
473 silc_list_count(engine->packet_pool)));
475 silc_mutex_lock(engine->lock);
477 /* Get packet from freelist or allocate new one. */
478 packet = silc_list_get(engine->packet_pool);
482 silc_mutex_unlock(engine->lock);
484 packet = silc_calloc(1, sizeof(*packet));
485 if (silc_unlikely(!packet))
488 SILC_LOG_DEBUG(("Allocating new packet %p", packet));
490 tmp = silc_malloc(SILC_PACKET_DEFAULT_SIZE);
491 if (silc_unlikely(!tmp)) {
495 silc_buffer_set(&packet->buffer, tmp, SILC_PACKET_DEFAULT_SIZE);
496 silc_buffer_reset(&packet->buffer);
501 SILC_LOG_DEBUG(("Get packet %p", packet));
503 /* Delete from freelist */
504 silc_list_del(engine->packet_pool, packet);
506 silc_mutex_unlock(engine->lock);
511 /* UDP remote stream hash table destructor */
513 static void silc_packet_engine_hash_destr(void *key, void *context,
519 /* Per scheduler context hash table destructor */
521 static void silc_packet_engine_context_destr(void *key, void *context,
524 SilcPacketEngineContext sc = context;
527 silc_dlist_start(sc->inbufs);
528 while ((buffer = silc_dlist_get(sc->inbufs))) {
529 silc_buffer_clear(buffer);
530 silc_buffer_free(buffer);
531 silc_dlist_del(sc->inbufs, buffer);
534 silc_dlist_uninit(sc->inbufs);
539 /******************************** Packet API ********************************/
541 /* Allocate new packet engine */
544 silc_packet_engine_start(SilcRng rng, SilcBool router,
545 SilcPacketCallbacks *callbacks,
546 void *callback_context)
548 SilcPacketEngine engine;
553 SILC_LOG_DEBUG(("Starting new packet engine"));
557 if (!callbacks->packet_receive || !callbacks->eos || !callbacks->error)
560 engine = silc_calloc(1, sizeof(*engine));
564 engine->contexts = silc_hash_table_alloc(0, silc_hash_ptr, NULL, NULL, NULL,
565 silc_packet_engine_context_destr,
567 if (!engine->contexts) {
573 engine->local_is_router = router;
574 engine->callbacks = callbacks;
575 engine->callback_context = callback_context;
576 silc_list_init(engine->streams, struct SilcPacketStreamStruct, next);
577 silc_mutex_alloc(&engine->lock);
579 /* Allocate packet free list */
580 silc_list_init(engine->packet_pool, struct SilcPacketStruct, next);
581 for (i = 0; i < 5; i++) {
582 packet = silc_calloc(1, sizeof(*packet));
584 silc_packet_engine_stop(engine);
588 tmp = silc_malloc(SILC_PACKET_DEFAULT_SIZE);
590 silc_packet_engine_stop(engine);
593 silc_buffer_set(&packet->buffer, tmp, SILC_PACKET_DEFAULT_SIZE);
594 silc_buffer_reset(&packet->buffer);
596 silc_list_add(engine->packet_pool, packet);
598 silc_list_start(engine->packet_pool);
603 /* Stop packet engine */
605 void silc_packet_engine_stop(SilcPacketEngine engine)
609 SILC_LOG_DEBUG(("Stopping packet engine"));
614 /* Free packet free list */
615 silc_list_start(engine->packet_pool);
616 while ((packet = silc_list_get(engine->packet_pool))) {
617 silc_buffer_purge(&packet->buffer);
621 silc_hash_table_free(engine->contexts);
622 silc_mutex_free(engine->lock);
626 /* Create new packet stream */
628 SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
629 SilcSchedule schedule,
636 SILC_LOG_DEBUG(("Creating new packet stream"));
638 if (!engine || !stream)
641 ps = silc_calloc(1, sizeof(*ps));
646 silc_atomic_init8(&ps->refcnt, 1);
647 silc_mutex_alloc(&ps->lock);
649 /* Allocate out buffer */
650 tmp = silc_malloc(SILC_PACKET_DEFAULT_SIZE);
652 silc_packet_stream_destroy(ps);
655 silc_buffer_set(&ps->outbuf, tmp, SILC_PACKET_DEFAULT_SIZE);
656 silc_buffer_reset(&ps->outbuf);
658 /* Initialize packet procesors list */
659 ps->process = silc_dlist_init();
661 silc_packet_stream_destroy(ps);
665 silc_mutex_lock(engine->lock);
667 /* Add per scheduler context */
668 if (!silc_hash_table_find(engine->contexts, schedule, NULL,
670 ps->sc = silc_calloc(1, sizeof(*ps->sc));
672 silc_packet_stream_destroy(ps);
673 silc_mutex_unlock(engine->lock);
676 ps->sc->engine = engine;
677 ps->sc->schedule = schedule;
679 /* Allocate data input buffer */
680 inbuf = silc_buffer_alloc(SILC_PACKET_DEFAULT_SIZE * 31);
684 silc_packet_stream_destroy(ps);
685 silc_mutex_unlock(engine->lock);
688 silc_buffer_reset(inbuf);
690 ps->sc->inbufs = silc_dlist_init();
691 if (!ps->sc->inbufs) {
692 silc_buffer_free(inbuf);
695 silc_packet_stream_destroy(ps);
696 silc_mutex_unlock(engine->lock);
699 silc_dlist_add(ps->sc->inbufs, inbuf);
701 /* Add to per scheduler context hash table */
702 if (!silc_hash_table_add(engine->contexts, schedule, ps->sc)) {
703 silc_buffer_free(inbuf);
704 silc_dlist_del(ps->sc->inbufs, inbuf);
707 silc_packet_stream_destroy(ps);
708 silc_mutex_unlock(engine->lock);
712 ps->sc->stream_count++;
714 /* Add the packet stream to engine */
715 silc_list_add(engine->streams, ps);
717 /* If this is UDP stream, allocate UDP remote stream hash table */
718 if (!engine->udp_remote && silc_socket_stream_is_udp(stream, NULL))
719 engine->udp_remote = silc_hash_table_alloc(0, silc_hash_string, NULL,
720 silc_hash_string_compare, NULL,
721 silc_packet_engine_hash_destr,
724 silc_mutex_unlock(engine->lock);
726 /* Set IO notifier callback. This schedules this stream for I/O. */
727 if (!silc_stream_set_notifier(ps->stream, schedule,
728 silc_packet_stream_io, ps)) {
729 SILC_LOG_DEBUG(("Cannot set stream notifier for packet stream"));
730 silc_packet_stream_destroy(ps);
737 /* Add new remote packet stream for UDP packet streams */
739 SilcPacketStream silc_packet_stream_add_remote(SilcPacketStream stream,
740 const char *remote_ip,
741 SilcUInt16 remote_port,
744 SilcPacketEngine engine = stream->sc->engine;
749 SILC_LOG_DEBUG(("Adding UDP remote %s:%d to packet stream %p",
750 remote_ip, remote_port, stream));
752 if (!stream || !remote_ip || !remote_port)
755 if (!silc_socket_stream_is_udp(stream->stream, NULL)) {
756 SILC_LOG_ERROR(("Stream is not UDP stream, cannot add remote IP"));
760 ps = silc_calloc(1, sizeof(*ps));
765 silc_atomic_init8(&ps->refcnt, 1);
766 silc_mutex_alloc(&ps->lock);
768 /* Set the UDP packet stream as underlaying stream */
769 silc_packet_stream_ref(stream);
770 ps->stream = (SilcStream)stream;
773 /* Allocate out buffer */
774 tmp = silc_malloc(SILC_PACKET_DEFAULT_SIZE);
776 silc_packet_stream_destroy(ps);
779 silc_buffer_set(&ps->outbuf, tmp, SILC_PACKET_DEFAULT_SIZE);
780 silc_buffer_reset(&ps->outbuf);
782 /* Initialize packet procesors list */
783 ps->process = silc_dlist_init();
785 silc_packet_stream_destroy(ps);
789 /* Add to engine with this IP and port pair */
790 tuple = silc_format("%d%s", remote_port, remote_ip);
791 silc_mutex_lock(engine->lock);
792 if (!tuple || !silc_hash_table_add(engine->udp_remote, tuple, ps)) {
793 silc_mutex_unlock(engine->lock);
794 silc_packet_stream_destroy(ps);
797 silc_mutex_unlock(engine->lock);
799 /* Save remote IP and port pair */
800 ps->remote_udp = silc_calloc(1, sizeof(*ps->remote_udp));
801 if (!ps->remote_udp) {
802 silc_packet_stream_destroy(ps);
805 ps->remote_udp->remote_port = remote_port;
806 ps->remote_udp->remote_ip = strdup(remote_ip);
807 if (!ps->remote_udp->remote_ip) {
808 silc_packet_stream_destroy(ps);
813 /* Inject packet to the new stream */
815 silc_packet_stream_ref(ps);
816 silc_schedule_task_add_timeout(silc_stream_get_schedule(stream->stream),
817 silc_packet_stream_inject_packet, packet,
824 /* Destroy packet stream */
826 void silc_packet_stream_destroy(SilcPacketStream stream)
828 SilcPacketEngine engine;
833 if (silc_atomic_sub_int8(&stream->refcnt, 1) > 0) {
834 stream->destroyed = TRUE;
836 /* Close the underlaying stream */
837 if (!stream->udp && stream->stream)
838 silc_stream_close(stream->stream);
842 SILC_LOG_DEBUG(("Destroying packet stream %p", stream));
845 /* Delete from engine */
846 engine = stream->sc->engine;
847 silc_mutex_lock(engine->lock);
848 silc_list_del(engine->streams, stream);
850 /* Remove per scheduler context, if it is not used anymore */
852 stream->sc->stream_count--;
853 if (!stream->sc->stream_count)
854 silc_hash_table_del(engine->contexts, stream->sc->schedule);
856 silc_mutex_unlock(engine->lock);
858 /* Destroy the underlaying stream */
860 silc_stream_destroy(stream->stream);
862 /* Delete from UDP remote hash table */
864 engine = stream->sc->engine;
865 silc_snprintf(tuple, sizeof(tuple), "%d%s", stream->remote_udp->remote_port,
866 stream->remote_udp->remote_ip);
867 silc_mutex_lock(engine->lock);
868 silc_hash_table_del(engine->udp_remote, tuple);
869 silc_mutex_unlock(engine->lock);
871 silc_free(stream->remote_udp->remote_ip);
872 silc_free(stream->remote_udp);
874 /* Unreference the underlaying packet stream */
875 silc_packet_stream_unref((SilcPacketStream)stream->stream);
878 /* Clear and free buffers */
879 silc_buffer_clear(&stream->outbuf);
880 silc_buffer_purge(&stream->outbuf);
882 if (stream->process) {
884 silc_dlist_start(stream->process);
885 while ((p = silc_dlist_get(stream->process))) {
888 silc_dlist_del(stream->process, p);
890 silc_dlist_uninit(stream->process);
893 /* Destroy ciphers and HMACs */
894 if (stream->send_key[0])
895 silc_cipher_free(stream->send_key[0]);
896 if (stream->receive_key[0])
897 silc_cipher_free(stream->receive_key[0]);
898 if (stream->send_hmac[0])
899 silc_hmac_free(stream->send_hmac[0]);
900 if (stream->receive_hmac[0])
901 silc_hmac_free(stream->receive_hmac[0]);
902 if (stream->send_key[1])
903 silc_cipher_free(stream->send_key[1]);
904 if (stream->receive_key[1])
905 silc_cipher_free(stream->receive_key[1]);
906 if (stream->send_hmac[1])
907 silc_hmac_free(stream->send_hmac[1]);
908 if (stream->receive_hmac[1])
909 silc_hmac_free(stream->receive_hmac[1]);
912 silc_free(stream->src_id);
913 silc_free(stream->dst_id);
915 silc_atomic_uninit8(&stream->refcnt);
916 silc_mutex_free(stream->lock);
920 /* Marks as router stream */
922 void silc_packet_stream_set_router(SilcPacketStream stream)
924 stream->is_router = TRUE;
927 /* Mark to include IV in ciphertext */
929 void silc_packet_stream_set_iv_included(SilcPacketStream stream)
931 stream->iv_included = TRUE;
934 /* Links `callbacks' to `stream' for specified packet types */
936 static SilcBool silc_packet_stream_link_va(SilcPacketStream stream,
937 SilcPacketCallbacks *callbacks,
938 void *callback_context,
939 int priority, va_list ap)
941 SilcPacketProcess p, e;
942 SilcInt32 packet_type;
945 SILC_LOG_DEBUG(("Linking callbacks %p to stream %p", callbacks, stream));
949 if (!callbacks->packet_receive)
952 p = silc_calloc(1, sizeof(*p));
956 p->priority = priority;
957 p->callbacks = callbacks;
958 p->callback_context = callback_context;
960 silc_mutex_lock(stream->lock);
962 if (!stream->process) {
963 stream->process = silc_dlist_init();
964 if (!stream->process) {
965 silc_mutex_unlock(stream->lock);
970 /* According to priority set the procesor to correct position. First
971 entry has the highest priority */
972 silc_dlist_start(stream->process);
973 while ((e = silc_dlist_get(stream->process)) != SILC_LIST_END) {
974 if (p->priority > e->priority) {
975 silc_dlist_insert(stream->process, p);
980 silc_dlist_add(stream->process, p);
982 /* Get packet types to process */
985 packet_type = va_arg(ap, SilcInt32);
987 if (packet_type == SILC_PACKET_ANY)
990 if (packet_type == -1)
993 p->types = silc_realloc(p->types, sizeof(*p->types) * (i + 1));
995 silc_mutex_unlock(stream->lock);
999 p->types[i - 1] = (SilcPacketType)packet_type;
1003 p->types[i - 1] = 0;
1005 silc_mutex_unlock(stream->lock);
1007 silc_packet_stream_ref(stream);
1012 /* Links `callbacks' to `stream' for specified packet types */
1014 SilcBool silc_packet_stream_link(SilcPacketStream stream,
1015 SilcPacketCallbacks *callbacks,
1016 void *callback_context,
1022 va_start(ap, priority);
1023 ret = silc_packet_stream_link_va(stream, callbacks, callback_context,
1030 /* Unlinks `callbacks' from `stream'. */
1032 void silc_packet_stream_unlink(SilcPacketStream stream,
1033 SilcPacketCallbacks *callbacks,
1034 void *callback_context)
1036 SilcPacketProcess p;
1038 SILC_LOG_DEBUG(("Unlinking callbacks %p from stream %p",
1039 callbacks, stream));
1041 silc_mutex_lock(stream->lock);
1043 silc_dlist_start(stream->process);
1044 while ((p = silc_dlist_get(stream->process)) != SILC_LIST_END)
1045 if (p->callbacks == callbacks &&
1046 p->callback_context == callback_context) {
1047 silc_dlist_del(stream->process, p);
1048 silc_free(p->types);
1053 if (!silc_dlist_count(stream->process)) {
1054 silc_dlist_uninit(stream->process);
1055 stream->process = NULL;
1058 silc_mutex_unlock(stream->lock);
1060 silc_packet_stream_unref(stream);
1063 /* Returns TRUE if stream is UDP stream */
1065 SilcBool silc_packet_stream_is_udp(SilcPacketStream stream)
1067 return stream->udp || silc_socket_stream_is_udp(stream->stream, NULL);
1070 /* Return packet sender IP and port for UDP packet stream */
1072 SilcBool silc_packet_get_sender(SilcPacket packet,
1073 const char **sender_ip,
1074 SilcUInt16 *sender_port)
1076 if (!packet->stream->remote_udp)
1079 *sender_ip = packet->stream->remote_udp->remote_ip;
1080 *sender_port = packet->stream->remote_udp->remote_port;
1085 /* Reference packet stream */
1087 void silc_packet_stream_ref(SilcPacketStream stream)
1089 silc_atomic_add_int8(&stream->refcnt, 1);
1090 SILC_LOG_DEBUG(("Stream %p, refcnt %d->%d", stream,
1091 silc_atomic_get_int8(&stream->refcnt) - 1,
1092 silc_atomic_get_int8(&stream->refcnt)));
1095 /* Unreference packet stream */
1097 void silc_packet_stream_unref(SilcPacketStream stream)
1099 SILC_LOG_DEBUG(("Stream %p, refcnt %d->%d", stream,
1100 silc_atomic_get_int8(&stream->refcnt),
1101 silc_atomic_get_int8(&stream->refcnt) - 1));
1102 if (silc_atomic_sub_int8(&stream->refcnt, 1) > 0)
1104 silc_atomic_add_int8(&stream->refcnt, 1);
1105 silc_packet_stream_destroy(stream);
1110 SilcPacketEngine silc_packet_get_engine(SilcPacketStream stream)
1112 return stream->sc->engine;
1115 /* Set application context for packet stream */
1117 void silc_packet_set_context(SilcPacketStream stream, void *stream_context)
1119 silc_mutex_lock(stream->lock);
1120 stream->stream_context = stream_context;
1121 silc_mutex_unlock(stream->lock);
1124 /* Return application context from packet stream */
1126 void *silc_packet_get_context(SilcPacketStream stream)
1129 silc_mutex_lock(stream->lock);
1130 context = stream->stream_context;
1131 silc_mutex_unlock(stream->lock);
1135 /* Change underlaying stream */
1137 void silc_packet_stream_set_stream(SilcPacketStream ps,
1141 silc_stream_set_notifier(ps->stream, ps->sc->schedule, NULL, NULL);
1142 ps->stream = stream;
1143 silc_stream_set_notifier(ps->stream, ps->sc->schedule, silc_packet_stream_io,
1147 /* Return underlaying stream */
1149 SilcStream silc_packet_stream_get_stream(SilcPacketStream stream)
1151 return stream->stream;
1156 SilcBool silc_packet_set_keys(SilcPacketStream stream, SilcCipher send_key,
1157 SilcCipher receive_key, SilcHmac send_hmac,
1158 SilcHmac receive_hmac, SilcBool rekey)
1160 SILC_LOG_DEBUG(("Setting new keys to packet stream %p", stream));
1162 /* If doing rekey, send REKEY_DONE packet */
1164 /* This will take stream lock. */
1165 if (!silc_packet_send_raw(stream, SILC_PACKET_REKEY_DONE, 0,
1166 stream->src_id_type, stream->src_id,
1167 stream->src_id_len, stream->dst_id_type,
1168 stream->dst_id, stream->dst_id_len,
1169 NULL, 0, stream->send_key[0],
1170 stream->send_hmac[0]))
1173 /* Write the packet to the stream */
1174 if (!silc_packet_stream_write(stream, TRUE))
1177 silc_mutex_lock(stream->lock);
1180 /* In case IV Included is set, save the old keys */
1181 if (stream->iv_included) {
1182 if (stream->send_key[1] && send_key) {
1183 silc_cipher_free(stream->send_key[1]);
1184 stream->send_key[1] = stream->send_key[0];
1186 if (stream->receive_key[1] && receive_key) {
1187 silc_cipher_free(stream->receive_key[1]);
1188 stream->receive_key[1] = stream->receive_key[0];
1190 if (stream->send_hmac[1] && send_hmac) {
1191 silc_hmac_free(stream->send_hmac[1]);
1192 stream->send_hmac[1] = stream->send_hmac[0];
1194 if (stream->receive_hmac[1] && receive_hmac) {
1195 silc_hmac_free(stream->receive_hmac[1]);
1196 stream->receive_hmac[1] = stream->receive_hmac[0];
1199 if (stream->send_key[0] && send_key)
1200 silc_cipher_free(stream->send_key[0]);
1201 if (stream->receive_key[0] && receive_key)
1202 silc_cipher_free(stream->receive_key[0]);
1203 if (stream->send_hmac[0] && send_hmac)
1204 silc_hmac_free(stream->send_hmac[0]);
1205 if (stream->receive_hmac[0] && receive_hmac)
1206 silc_hmac_free(stream->receive_hmac[0]);
1211 stream->send_key[0] = send_key;
1213 stream->receive_key[0] = receive_key;
1215 stream->send_hmac[0] = send_hmac;
1217 stream->receive_hmac[0] = receive_hmac;
1219 silc_mutex_unlock(stream->lock);
1223 /* Return current ciphers from packet stream */
1225 SilcBool silc_packet_get_keys(SilcPacketStream stream,
1226 SilcCipher *send_key,
1227 SilcCipher *receive_key,
1228 SilcHmac *send_hmac,
1229 SilcHmac *receive_hmac)
1231 if (!stream->send_key[0] && !stream->receive_key[0] &&
1232 !stream->send_hmac[0] && !stream->receive_hmac[0])
1235 silc_mutex_lock(stream->lock);
1238 *send_key = stream->send_key[0];
1240 *receive_key = stream->receive_key[0];
1242 *send_hmac = stream->send_hmac[0];
1244 *receive_hmac = stream->receive_hmac[0];
1246 silc_mutex_unlock(stream->lock);
1251 /* Set SILC IDs to packet stream */
1253 SilcBool silc_packet_set_ids(SilcPacketStream stream,
1254 SilcIdType src_id_type, const void *src_id,
1255 SilcIdType dst_id_type, const void *dst_id)
1258 unsigned char tmp[32];
1260 if (!src_id && !dst_id)
1263 SILC_LOG_DEBUG(("Setting new IDs to packet stream"));
1265 silc_mutex_lock(stream->lock);
1268 silc_free(stream->src_id);
1269 if (!silc_id_id2str(src_id, src_id_type, tmp, sizeof(tmp), &len)) {
1270 silc_mutex_unlock(stream->lock);
1273 stream->src_id = silc_memdup(tmp, len);
1274 if (!stream->src_id) {
1275 silc_mutex_unlock(stream->lock);
1278 stream->src_id_type = src_id_type;
1279 stream->src_id_len = len;
1283 silc_free(stream->dst_id);
1284 if (!silc_id_id2str(dst_id, dst_id_type, tmp, sizeof(tmp), &len)) {
1285 silc_mutex_unlock(stream->lock);
1288 stream->dst_id = silc_memdup(tmp, len);
1289 if (!stream->dst_id) {
1290 silc_mutex_unlock(stream->lock);
1293 stream->dst_id_type = dst_id_type;
1294 stream->dst_id_len = len;
1297 silc_mutex_unlock(stream->lock);
1302 /* Adds Security ID (SID) */
1304 SilcBool silc_packet_set_sid(SilcPacketStream stream, SilcUInt8 sid)
1306 if (!stream->iv_included)
1309 SILC_LOG_DEBUG(("Set packet stream %p SID to %d", stream, sid));
1317 void silc_packet_free(SilcPacket packet)
1319 SilcPacketStream stream = packet->stream;
1321 SILC_LOG_DEBUG(("Freeing packet %p", packet));
1323 /* Check for double free */
1324 SILC_ASSERT(packet->stream != NULL);
1326 packet->stream = NULL;
1327 packet->src_id = packet->dst_id = NULL;
1328 silc_buffer_reset(&packet->buffer);
1330 silc_mutex_lock(stream->sc->engine->lock);
1332 /* Put the packet back to freelist */
1333 silc_list_add(stream->sc->engine->packet_pool, packet);
1334 if (silc_list_count(stream->sc->engine->packet_pool) == 1)
1335 silc_list_start(stream->sc->engine->packet_pool);
1337 silc_mutex_unlock(stream->sc->engine->lock);
1340 /****************************** Packet Sending ******************************/
1342 /* Prepare outgoing data buffer for packet sending. Returns the
1343 pointer to that buffer into the `packet'. */
1345 static inline SilcBool silc_packet_send_prepare(SilcPacketStream stream,
1350 unsigned char *oldptr;
1351 unsigned int mac_len = hmac ? silc_hmac_len(hmac) : 0;
1355 /* Allocate more space if needed */
1356 if (silc_unlikely(silc_buffer_taillen(&stream->outbuf) < totlen)) {
1357 if (!silc_buffer_realloc(&stream->outbuf,
1358 silc_buffer_truelen(&stream->outbuf) + totlen))
1362 /* Pull data area for the new packet, and return pointer to the start of
1363 the data area and save the pointer in to the `packet'. MAC is pulled
1364 later after it's computed. */
1365 oldptr = silc_buffer_pull_tail(&stream->outbuf, totlen);
1366 silc_buffer_set(packet, oldptr, totlen);
1367 silc_buffer_push_tail(packet, mac_len);
1372 /* Increments counter when encrypting in counter mode. */
1374 static inline void silc_packet_send_ctr_increment(SilcPacketStream stream,
1376 unsigned char *ret_iv)
1378 unsigned char *iv = silc_cipher_get_iv(cipher);
1379 SilcUInt32 pc1, pc2;
1381 /* Increment 64-bit packet counter */
1382 SILC_GET32_MSB(pc1, iv + 4);
1383 SILC_GET32_MSB(pc2, iv + 8);
1386 SILC_PUT32_MSB(pc1, iv + 4);
1387 SILC_PUT32_MSB(pc2, iv + 8);
1389 /* Reset block counter */
1390 memset(iv + 12, 0, 4);
1392 /* If IV Included flag, return the 64-bit IV for inclusion in packet */
1393 if (stream->iv_included) {
1395 ret_iv[0] = silc_rng_get_byte_fast(stream->sc->engine->rng);
1396 ret_iv[1] = ret_iv[0] + iv[4];
1397 ret_iv[2] = ret_iv[0] ^ ret_iv[1];
1398 ret_iv[3] = ret_iv[0] + ret_iv[2];
1399 SILC_PUT32_MSB(pc2, ret_iv + 4);
1400 SILC_LOG_HEXDUMP(("IV"), ret_iv, 8);
1402 /* Set new nonce to counter block */
1403 memcpy(iv + 4, ret_iv, 4);
1406 SILC_LOG_HEXDUMP(("Counter Block"), iv, 16);
1409 /* Internal routine to assemble outgoing packet. Assembles and encryptes
1410 the packet. The silc_packet_stream_write needs to be called to send it
1411 after this returns TRUE. */
1413 static inline SilcBool silc_packet_send_raw(SilcPacketStream stream,
1414 SilcPacketType type,
1415 SilcPacketFlags flags,
1416 SilcIdType src_id_type,
1417 unsigned char *src_id,
1418 SilcUInt32 src_id_len,
1419 SilcIdType dst_id_type,
1420 unsigned char *dst_id,
1421 SilcUInt32 dst_id_len,
1422 const unsigned char *data,
1423 SilcUInt32 data_len,
1427 unsigned char tmppad[SILC_PACKET_MAX_PADLEN], iv[33], psn[4];
1428 int block_len = (cipher ? silc_cipher_get_block_len(cipher) : 0);
1429 int i, enclen, truelen, padlen = 0, ivlen = 0, psnlen = 0;
1431 SilcBufferStruct packet;
1433 SILC_LOG_DEBUG(("Sending packet %s (%d) flags %d, src %d dst %d, "
1434 "data len %d", silc_get_packet_name(type), stream->send_psn,
1435 flags, src_id_type, dst_id_type, data_len));
1437 /* Get the true length of the packet. This is saved as payload length
1438 into the packet header. This does not include the length of the
1440 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
1441 src_id_len + dst_id_len));
1442 enclen = truelen = (data_len + SILC_PACKET_HEADER_LEN +
1443 src_id_len + dst_id_len);
1445 /* If using CTR mode, increment the counter */
1446 ctr = (cipher && silc_cipher_get_mode(cipher) == SILC_CIPHER_MODE_CTR);
1448 silc_packet_send_ctr_increment(stream, cipher, iv + 1);
1450 /* If IV is included, the SID, IV and sequence number is added to packet */
1451 if (stream->iv_included && cipher) {
1452 psnlen = sizeof(psn);
1454 iv[0] = stream->sid;
1457 /* If IV is included, the SID, IV and sequence number is added to packet */
1458 if (stream->iv_included && cipher) {
1459 psnlen = sizeof(psn);
1460 ivlen = block_len + 1;
1461 iv[0] = stream->sid;
1462 memcpy(iv + 1, silc_cipher_get_iv(cipher), block_len);
1466 /* We automatically figure out the packet structure from the packet
1467 type and flags, and calculate correct length. Private messages with
1468 private keys and channel messages are special packets as their
1469 payload is encrypted already. */
1470 if ((type == SILC_PACKET_PRIVATE_MESSAGE &&
1471 flags & SILC_PACKET_FLAG_PRIVMSG_KEY) ||
1472 type == SILC_PACKET_CHANNEL_MESSAGE) {
1474 /* Padding is calculated from header + IDs */
1476 SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN + src_id_len + dst_id_len +
1477 psnlen), block_len, padlen);
1479 /* Length to encrypt, header + IDs + padding. */
1480 enclen = (SILC_PACKET_HEADER_LEN + src_id_len + dst_id_len +
1484 /* Padding is calculated from true length of the packet */
1485 if (flags & SILC_PACKET_FLAG_LONG_PAD)
1486 SILC_PACKET_PADLEN_MAX(truelen + psnlen, block_len, padlen);
1488 SILC_PACKET_PADLEN(truelen + psnlen, block_len, padlen);
1490 enclen += padlen + psnlen;
1493 /* Remove implementation specific flags */
1494 flags &= ~(SILC_PACKET_FLAG_LONG_PAD);
1496 /* Get random padding */
1497 for (i = 0; i < padlen; i++) tmppad[i] =
1498 silc_rng_get_byte_fast(stream->sc->engine->rng);
1500 silc_mutex_lock(stream->lock);
1502 /* Get packet pointer from the outgoing buffer */
1503 if (silc_unlikely(!silc_packet_send_prepare(stream, truelen + padlen + ivlen
1504 + psnlen, hmac, &packet))) {
1505 silc_mutex_unlock(stream->lock);
1509 SILC_PUT32_MSB(stream->send_psn, psn);
1511 /* Create the packet. This creates the SILC header, adds padding, and
1512 the actual packet data. */
1513 i = silc_buffer_format(&packet,
1514 SILC_STR_DATA(iv, ivlen),
1515 SILC_STR_DATA(psn, psnlen),
1516 SILC_STR_UI_SHORT(truelen),
1517 SILC_STR_UI_CHAR(flags),
1518 SILC_STR_UI_CHAR(type),
1519 SILC_STR_UI_CHAR(padlen),
1520 SILC_STR_UI_CHAR(0),
1521 SILC_STR_UI_CHAR(src_id_len),
1522 SILC_STR_UI_CHAR(dst_id_len),
1523 SILC_STR_UI_CHAR(src_id_type),
1524 SILC_STR_DATA(src_id, src_id_len),
1525 SILC_STR_UI_CHAR(dst_id_type),
1526 SILC_STR_DATA(dst_id, dst_id_len),
1527 SILC_STR_DATA(tmppad, padlen),
1528 SILC_STR_DATA(data, data_len),
1530 if (silc_unlikely(i < 0)) {
1531 silc_mutex_unlock(stream->lock);
1535 SILC_LOG_HEXDUMP(("Assembled packet, len %d", silc_buffer_len(&packet)),
1536 silc_buffer_data(&packet), silc_buffer_len(&packet));
1538 /* Encrypt the packet */
1539 if (silc_likely(cipher)) {
1540 SILC_LOG_DEBUG(("Encrypting packet"));
1541 silc_cipher_set_iv(cipher, NULL);
1542 if (silc_unlikely(!silc_cipher_encrypt(cipher, packet.data + ivlen,
1543 packet.data + ivlen, enclen,
1545 SILC_LOG_ERROR(("Packet encryption failed"));
1546 silc_mutex_unlock(stream->lock);
1552 if (silc_likely(hmac)) {
1555 /* MAC is computed from the entire encrypted packet data, and put
1556 to the end of the packet. */
1557 silc_hmac_init(hmac);
1558 silc_hmac_update(hmac, psn, sizeof(psn));
1559 silc_hmac_update(hmac, packet.data, silc_buffer_len(&packet));
1560 silc_hmac_final(hmac, packet.tail, &mac_len);
1561 silc_buffer_pull_tail(&packet, mac_len);
1568 /* Sends a packet */
1570 SilcBool silc_packet_send(SilcPacketStream stream,
1571 SilcPacketType type, SilcPacketFlags flags,
1572 const unsigned char *data, SilcUInt32 data_len)
1576 ret = silc_packet_send_raw(stream, type, flags,
1577 stream->src_id_type,
1580 stream->dst_id_type,
1584 stream->send_key[0],
1585 stream->send_hmac[0]);
1587 /* Write the packet to the stream */
1588 return ret ? silc_packet_stream_write(stream, FALSE) : FALSE;
1591 /* Sends a packet, extended routine */
1593 SilcBool silc_packet_send_ext(SilcPacketStream stream,
1594 SilcPacketType type, SilcPacketFlags flags,
1595 SilcIdType src_id_type, void *src_id,
1596 SilcIdType dst_id_type, void *dst_id,
1597 const unsigned char *data, SilcUInt32 data_len,
1598 SilcCipher cipher, SilcHmac hmac)
1600 unsigned char src_id_data[32], dst_id_data[32];
1601 SilcUInt32 src_id_len, dst_id_len;
1605 if (!silc_id_id2str(src_id, src_id_type, src_id_data,
1606 sizeof(src_id_data), &src_id_len))
1609 if (!silc_id_id2str(dst_id, dst_id_type, dst_id_data,
1610 sizeof(dst_id_data), &dst_id_len))
1613 ret = silc_packet_send_raw(stream, type, flags,
1614 src_id ? src_id_type : stream->src_id_type,
1615 src_id ? src_id_data : stream->src_id,
1616 src_id ? src_id_len : stream->src_id_len,
1617 dst_id ? dst_id_type : stream->dst_id_type,
1618 dst_id ? dst_id_data : stream->dst_id,
1619 dst_id ? dst_id_len : stream->dst_id_len,
1621 cipher ? cipher : stream->send_key[0],
1622 hmac ? hmac : stream->send_hmac[0]);
1624 /* Write the packet to the stream */
1625 return ret ? silc_packet_stream_write(stream, FALSE) : FALSE;
1628 /* Sends packet after formatting the arguments to buffer */
1630 SilcBool silc_packet_send_va(SilcPacketStream stream,
1631 SilcPacketType type, SilcPacketFlags flags, ...)
1633 SilcBufferStruct buf;
1637 va_start(va, flags);
1639 memset(&buf, 0, sizeof(buf));
1640 if (silc_buffer_format_vp(&buf, va) < 0) {
1645 ret = silc_packet_send(stream, type, flags, silc_buffer_data(&buf),
1646 silc_buffer_len(&buf));
1648 silc_buffer_purge(&buf);
1654 /* Sends packet after formatting the arguments to buffer, extended routine */
1656 SilcBool silc_packet_send_va_ext(SilcPacketStream stream,
1657 SilcPacketType type, SilcPacketFlags flags,
1658 SilcIdType src_id_type, void *src_id,
1659 SilcIdType dst_id_type, void *dst_id,
1660 SilcCipher cipher, SilcHmac hmac, ...)
1662 SilcBufferStruct buf;
1668 memset(&buf, 0, sizeof(buf));
1669 if (silc_buffer_format_vp(&buf, va) < 0) {
1674 ret = silc_packet_send_ext(stream, type, flags, src_id_type, src_id,
1675 dst_id_type, dst_id, silc_buffer_data(&buf),
1676 silc_buffer_len(&buf), cipher, hmac);
1678 silc_buffer_purge(&buf);
1684 /***************************** Packet Receiving *****************************/
1686 /* Checks MAC in the packet. Returns TRUE if MAC is Ok. */
1688 static inline SilcBool silc_packet_check_mac(SilcHmac hmac,
1689 const unsigned char *data,
1690 SilcUInt32 data_len,
1691 const unsigned char *packet_mac,
1692 const unsigned char *packet_seq,
1693 SilcUInt32 sequence)
1696 if (silc_likely(hmac)) {
1697 unsigned char mac[32], psn[4];
1700 SILC_LOG_DEBUG(("Verifying MAC"));
1702 /* Compute HMAC of packet */
1703 silc_hmac_init(hmac);
1706 SILC_PUT32_MSB(sequence, psn);
1707 silc_hmac_update(hmac, psn, 4);
1709 silc_hmac_update(hmac, packet_seq, 4);
1711 silc_hmac_update(hmac, data, data_len);
1712 silc_hmac_final(hmac, mac, &mac_len);
1714 /* Compare the MAC's */
1715 if (silc_unlikely(memcmp(packet_mac, mac, mac_len))) {
1716 SILC_LOG_DEBUG(("MAC failed"));
1720 SILC_LOG_DEBUG(("MAC is Ok"));
1726 /* Increments/sets counter when decrypting in counter mode. */
1728 static inline void silc_packet_receive_ctr_increment(SilcPacketStream stream,
1730 unsigned char *packet_iv)
1732 SilcUInt32 pc1, pc2;
1734 /* If IV Included flag, set the IV from packet to block counter. */
1735 if (stream->iv_included) {
1736 memcpy(iv + 4, packet_iv, 8);
1738 /* Increment 64-bit packet counter. */
1739 SILC_GET32_MSB(pc1, iv + 4);
1740 SILC_GET32_MSB(pc2, iv + 8);
1743 SILC_PUT32_MSB(pc1, iv + 4);
1744 SILC_PUT32_MSB(pc2, iv + 8);
1747 /* Reset block counter */
1748 memset(iv + 12, 0, 4);
1750 SILC_LOG_HEXDUMP(("Counter Block"), iv, 16);
1753 /* Decrypts SILC packet. Handles both normal and special packet decryption.
1754 Return 0 when packet is normal and 1 when it it special, -1 on error. */
1756 static inline int silc_packet_decrypt(SilcCipher cipher, SilcHmac hmac,
1757 SilcUInt32 sequence, SilcBuffer buffer,
1760 if (normal == TRUE) {
1761 if (silc_likely(cipher)) {
1762 /* Decrypt rest of the packet */
1763 SILC_LOG_DEBUG(("Decrypting the packet"));
1764 if (silc_unlikely(!silc_cipher_decrypt(cipher, buffer->data,
1766 silc_buffer_len(buffer), NULL)))
1772 /* Decrypt rest of the header plus padding */
1773 if (silc_likely(cipher)) {
1775 SilcUInt32 block_len = silc_cipher_get_block_len(cipher);
1777 SILC_LOG_DEBUG(("Decrypting the header"));
1779 /* Padding length + src id len + dst id len + header length - 16
1780 bytes already decrypted, gives the rest of the encrypted packet */
1781 silc_buffer_push(buffer, block_len);
1782 len = (((SilcUInt8)buffer->data[4] + (SilcUInt8)buffer->data[6] +
1783 (SilcUInt8)buffer->data[7] + SILC_PACKET_HEADER_LEN) -
1785 silc_buffer_pull(buffer, block_len);
1787 if (silc_unlikely(len > silc_buffer_len(buffer))) {
1788 SILC_LOG_ERROR(("Garbage in header of packet, bad packet length, "
1792 if (silc_unlikely(!silc_cipher_decrypt(cipher, buffer->data,
1793 buffer->data, len, NULL)))
1801 /* Parses the packet. This is called when a whole packet is ready to be
1802 parsed. The buffer sent must be already decrypted before calling this
1805 static inline SilcBool silc_packet_parse(SilcPacket packet)
1807 SilcBuffer buffer = &packet->buffer;
1808 SilcUInt8 padlen = (SilcUInt8)buffer->data[4];
1809 SilcUInt8 src_id_len, dst_id_len, src_id_type, dst_id_type;
1812 SILC_LOG_DEBUG(("Parsing incoming packet"));
1814 /* Parse the buffer. This parses the SILC header of the packet. */
1815 ret = silc_buffer_unformat(buffer,
1818 SILC_STR_UI_CHAR(&src_id_len),
1819 SILC_STR_UI_CHAR(&dst_id_len),
1820 SILC_STR_UI_CHAR(&src_id_type),
1822 if (silc_unlikely(ret == -1)) {
1823 if (!packet->stream->udp &&
1824 !silc_socket_stream_is_udp(packet->stream->stream, NULL))
1825 SILC_LOG_ERROR(("Malformed packet header, packet dropped"));
1829 if (silc_unlikely(src_id_len > SILC_PACKET_MAX_ID_LEN ||
1830 dst_id_len > SILC_PACKET_MAX_ID_LEN)) {
1831 if (!packet->stream->udp &&
1832 !silc_socket_stream_is_udp(packet->stream->stream, NULL))
1833 SILC_LOG_ERROR(("Bad ID lengths in packet (%d and %d)",
1834 packet->src_id_len, packet->dst_id_len));
1838 ret = silc_buffer_unformat(buffer,
1840 SILC_STR_DATA(&packet->src_id, src_id_len),
1841 SILC_STR_UI_CHAR(&dst_id_type),
1842 SILC_STR_DATA(&packet->dst_id, dst_id_len),
1843 SILC_STR_OFFSET(padlen),
1845 if (silc_unlikely(ret == -1)) {
1846 if (!packet->stream->udp &&
1847 !silc_socket_stream_is_udp(packet->stream->stream, NULL))
1848 SILC_LOG_ERROR(("Malformed packet header, packet dropped"));
1852 if (silc_unlikely(src_id_type > SILC_ID_CHANNEL ||
1853 dst_id_type > SILC_ID_CHANNEL)) {
1854 if (!packet->stream->udp &&
1855 !silc_socket_stream_is_udp(packet->stream->stream, NULL))
1856 SILC_LOG_ERROR(("Bad ID types in packet (%d and %d)",
1857 src_id_type, dst_id_type));
1861 packet->src_id_len = src_id_len;
1862 packet->dst_id_len = dst_id_len;
1863 packet->src_id_type = src_id_type;
1864 packet->dst_id_type = dst_id_type;
1866 SILC_LOG_HEXDUMP(("Parsed packet, len %d", silc_buffer_headlen(buffer) +
1867 silc_buffer_len(buffer)), buffer->head,
1868 silc_buffer_headlen(buffer) + silc_buffer_len(buffer));
1870 SILC_LOG_DEBUG(("Incoming packet type: %d (%s)", packet->type,
1871 silc_get_packet_name(packet->type)));
1876 /* Dispatch packet to application. Called with stream->lock locked.
1877 Returns FALSE if the stream was destroyed while dispatching a packet. */
1879 static SilcBool silc_packet_dispatch(SilcPacket packet)
1881 SilcPacketStream stream = packet->stream;
1882 SilcPacketProcess p;
1883 SilcBool default_sent = FALSE;
1886 /* Dispatch packet to all packet processors that want it */
1888 if (silc_likely(!stream->process)) {
1889 /* Send to default processor as no others exist */
1890 SILC_LOG_DEBUG(("Dispatching packet to default callbacks"));
1891 silc_mutex_unlock(stream->lock);
1892 if (silc_unlikely(!stream->sc->engine->callbacks->
1893 packet_receive(stream->sc->engine, stream, packet,
1894 stream->sc->engine->callback_context,
1895 stream->stream_context)))
1896 silc_packet_free(packet);
1897 silc_mutex_lock(stream->lock);
1898 return stream->destroyed == FALSE;
1901 silc_dlist_start(stream->process);
1902 while ((p = silc_dlist_get(stream->process)) != SILC_LIST_END) {
1904 /* If priority is 0 or less, we send to default processor first
1905 because default processor has 0 priority */
1906 if (!default_sent && p->priority <= 0) {
1907 SILC_LOG_DEBUG(("Dispatching packet to default callbacks"));
1908 default_sent = TRUE;
1909 silc_mutex_unlock(stream->lock);
1910 if (stream->sc->engine->callbacks->
1911 packet_receive(stream->sc->engine, stream, packet,
1912 stream->sc->engine->callback_context,
1913 stream->stream_context)) {
1914 silc_mutex_lock(stream->lock);
1915 return stream->destroyed == FALSE;
1917 silc_mutex_lock(stream->lock);
1920 /* Send to processor */
1922 /* Send all packet types */
1923 SILC_LOG_DEBUG(("Dispatching packet to %p callbacks", p->callbacks));
1924 silc_mutex_unlock(stream->lock);
1925 if (p->callbacks->packet_receive(stream->sc->engine, stream, packet,
1926 p->callback_context,
1927 stream->stream_context)) {
1928 silc_mutex_lock(stream->lock);
1929 return stream->destroyed == FALSE;
1931 silc_mutex_lock(stream->lock);
1933 /* Send specific types */
1934 for (pt = p->types; *pt; pt++) {
1935 if (*pt != packet->type)
1937 SILC_LOG_DEBUG(("Dispatching packet to %p callbacks", p->callbacks));
1938 silc_mutex_unlock(stream->lock);
1939 if (p->callbacks->packet_receive(stream->sc->engine, stream, packet,
1940 p->callback_context,
1941 stream->stream_context)) {
1942 silc_mutex_lock(stream->lock);
1943 return stream->destroyed == FALSE;
1945 silc_mutex_lock(stream->lock);
1951 if (!default_sent) {
1952 /* Send to default processor as it has not been sent yet */
1953 SILC_LOG_DEBUG(("Dispatching packet to default callbacks"));
1954 silc_mutex_unlock(stream->lock);
1955 if (stream->sc->engine->callbacks->
1956 packet_receive(stream->sc->engine, stream, packet,
1957 stream->sc->engine->callback_context,
1958 stream->stream_context)) {
1959 silc_mutex_lock(stream->lock);
1960 return stream->destroyed == FALSE;
1962 silc_mutex_lock(stream->lock);
1965 /* If we got here, no one wanted the packet, so drop it */
1966 silc_packet_free(packet);
1967 return stream->destroyed == FALSE;
1970 /* Process incoming data and parse packets. Called with stream->lock
1973 static void silc_packet_read_process(SilcPacketStream stream)
1980 SilcUInt16 packetlen;
1981 SilcUInt32 paddedlen, mac_len, block_len, ivlen, psnlen;
1982 unsigned char tmp[SILC_PACKET_MIN_HEADER_LEN], *header;
1983 unsigned char iv[SILC_CIPHER_MAX_IV_SIZE], *packet_seq = NULL;
1987 /* Get inbuf. If there is already some data for this stream in the buffer
1988 we already have it. Otherwise get the current one from list, it will
1989 include the data. */
1990 inbuf = stream->inbuf;
1992 silc_dlist_start(stream->sc->inbufs);
1993 inbuf = silc_dlist_get(stream->sc->inbufs);
1996 /* Parse the packets from the data */
1997 while (silc_buffer_len(inbuf) > 0) {
1999 cipher = stream->receive_key[0];
2000 hmac = stream->receive_hmac[0];
2003 if (silc_unlikely(silc_buffer_len(inbuf) <
2004 (stream->iv_included ? SILC_PACKET_MIN_HEADER_LEN_IV :
2005 SILC_PACKET_MIN_HEADER_LEN))) {
2006 SILC_LOG_DEBUG(("Partial packet in queue, waiting for the rest"));
2007 silc_dlist_del(stream->sc->inbufs, inbuf);
2008 stream->inbuf = inbuf;
2012 if (silc_likely(hmac))
2013 mac_len = silc_hmac_len(hmac);
2017 /* Decrypt first block of the packet to get the length field out */
2018 if (silc_likely(cipher)) {
2019 block_len = silc_cipher_get_block_len(cipher);
2021 if (stream->iv_included) {
2022 /* SID, IV and sequence number is included in the ciphertext */
2023 sid = (SilcUInt8)inbuf->data[0];
2025 if (silc_cipher_get_mode(cipher) == SILC_CIPHER_MODE_CTR) {
2026 /* Set the CTR mode IV from packet to counter block */
2027 memcpy(iv, silc_cipher_get_iv(cipher), block_len);
2028 silc_packet_receive_ctr_increment(stream, iv, inbuf->data + 1);
2031 /* Get IV from packet */
2032 memcpy(iv, inbuf->data + 1, block_len);
2033 ivlen = block_len + 1;
2037 /* Check SID, and get correct decryption key */
2038 if (sid != stream->sid) {
2039 /* If SID is recent get the previous key and use it */
2040 if (sid > 0 && stream->sid > 0 && stream->sid - 1 == sid &&
2041 stream->receive_key[1] && !stream->receive_hmac[1]) {
2042 cipher = stream->receive_key[1];
2043 hmac = stream->receive_hmac[1];
2045 /* The SID is unknown, drop rest of the data in buffer */
2046 SILC_LOG_DEBUG(("Unknown Security ID %d in packet, expected %d",
2048 silc_mutex_unlock(stream->lock);
2049 SILC_PACKET_CALLBACK_ERROR(stream, SILC_PACKET_ERR_UNKNOWN_SID);
2050 silc_mutex_lock(stream->lock);
2055 memcpy(iv, silc_cipher_get_iv(cipher), block_len);
2057 /* If using CTR mode, increment the counter */
2058 if (silc_cipher_get_mode(cipher) == SILC_CIPHER_MODE_CTR)
2059 silc_packet_receive_ctr_increment(stream, iv, NULL);
2062 if (silc_cipher_get_mode(cipher) == SILC_CIPHER_MODE_CTR)
2063 silc_cipher_set_iv(cipher, NULL);
2064 silc_cipher_decrypt(cipher, inbuf->data + ivlen, tmp, block_len, iv);
2067 if (stream->iv_included) {
2068 /* Take sequence number from packet */
2069 packet_seq = header;
2073 /* Unencrypted packet */
2074 block_len = SILC_PACKET_MIN_HEADER_LEN;
2075 header = inbuf->data;
2078 /* Get packet length and full packet length with padding */
2079 SILC_PACKET_LENGTH(header, packetlen, paddedlen);
2082 if (silc_unlikely(packetlen < SILC_PACKET_MIN_LEN)) {
2083 if (!stream->udp && !silc_socket_stream_is_udp(stream->stream, NULL))
2084 SILC_LOG_ERROR(("Received too short packet"));
2085 silc_mutex_unlock(stream->lock);
2086 SILC_PACKET_CALLBACK_ERROR(stream, SILC_PACKET_ERR_MALFORMED);
2087 silc_mutex_lock(stream->lock);
2088 memset(tmp, 0, sizeof(tmp));
2092 if (silc_buffer_len(inbuf) < paddedlen + ivlen + mac_len) {
2093 SILC_LOG_DEBUG(("Received partial packet, waiting for the rest "
2095 paddedlen + mac_len - silc_buffer_len(inbuf)));
2096 memset(tmp, 0, sizeof(tmp));
2097 silc_dlist_del(stream->sc->inbufs, inbuf);
2098 stream->inbuf = inbuf;
2102 /* Check MAC of the packet */
2103 if (silc_unlikely(!silc_packet_check_mac(hmac, inbuf->data,
2105 inbuf->data + ivlen +
2106 paddedlen, packet_seq,
2107 stream->receive_psn))) {
2108 silc_mutex_unlock(stream->lock);
2109 SILC_PACKET_CALLBACK_ERROR(stream, SILC_PACKET_ERR_MAC_FAILED);
2110 silc_mutex_lock(stream->lock);
2111 memset(tmp, 0, sizeof(tmp));
2116 packet = silc_packet_alloc(stream->sc->engine);
2117 if (silc_unlikely(!packet)) {
2118 silc_mutex_unlock(stream->lock);
2119 SILC_PACKET_CALLBACK_ERROR(stream, SILC_PACKET_ERR_NO_MEMORY);
2120 silc_mutex_lock(stream->lock);
2121 memset(tmp, 0, sizeof(tmp));
2124 packet->stream = stream;
2126 /* Allocate more space to packet buffer, if needed */
2127 if (silc_unlikely(silc_buffer_truelen(&packet->buffer) < paddedlen)) {
2128 if (!silc_buffer_realloc(&packet->buffer,
2129 silc_buffer_truelen(&packet->buffer) +
2131 silc_buffer_truelen(&packet->buffer)))) {
2132 silc_mutex_unlock(stream->lock);
2133 SILC_PACKET_CALLBACK_ERROR(stream, SILC_PACKET_ERR_NO_MEMORY);
2134 silc_mutex_lock(stream->lock);
2135 silc_packet_free(packet);
2136 memset(tmp, 0, sizeof(tmp));
2141 /* Parse packet header */
2142 packet->flags = (SilcPacketFlags)header[2];
2143 packet->type = (SilcPacketType)header[3];
2145 if (stream->sc->engine->local_is_router) {
2146 if (packet->type == SILC_PACKET_PRIVATE_MESSAGE &&
2147 (packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY))
2149 else if (packet->type != SILC_PACKET_CHANNEL_MESSAGE ||
2150 (packet->type == SILC_PACKET_CHANNEL_MESSAGE &&
2151 stream->is_router == TRUE))
2154 if (packet->type == SILC_PACKET_PRIVATE_MESSAGE &&
2155 (packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY))
2157 else if (packet->type != SILC_PACKET_CHANNEL_MESSAGE)
2161 SILC_LOG_HEXDUMP(("Incoming packet (%d) len %d",
2162 stream->receive_psn, paddedlen + ivlen + mac_len),
2163 inbuf->data, paddedlen + ivlen + mac_len);
2165 /* Put the decrypted part, and rest of the encrypted data, and decrypt */
2166 silc_buffer_pull_tail(&packet->buffer, paddedlen);
2167 silc_buffer_put(&packet->buffer, header, block_len - psnlen);
2168 silc_buffer_pull(&packet->buffer, block_len - psnlen);
2169 silc_buffer_put(&packet->buffer, (inbuf->data + ivlen +
2170 psnlen + (block_len - psnlen)),
2171 paddedlen - ivlen - psnlen - (block_len - psnlen));
2172 if (silc_likely(cipher)) {
2173 silc_cipher_set_iv(cipher, iv);
2174 ret = silc_packet_decrypt(cipher, hmac, stream->receive_psn,
2175 &packet->buffer, normal);
2176 if (silc_unlikely(ret < 0)) {
2177 silc_mutex_unlock(stream->lock);
2178 SILC_PACKET_CALLBACK_ERROR(stream, SILC_PACKET_ERR_DECRYPTION_FAILED);
2179 silc_mutex_lock(stream->lock);
2180 silc_packet_free(packet);
2181 memset(tmp, 0, sizeof(tmp));
2185 stream->receive_psn++;
2187 silc_buffer_push(&packet->buffer, block_len);
2189 /* Pull the packet from inbuf thus we'll get the next one in the inbuf. */
2190 silc_buffer_pull(inbuf, paddedlen + mac_len);
2192 /* Parse the packet */
2193 if (silc_unlikely(!silc_packet_parse(packet))) {
2194 silc_mutex_unlock(stream->lock);
2195 SILC_PACKET_CALLBACK_ERROR(stream, SILC_PACKET_ERR_MALFORMED);
2196 silc_mutex_lock(stream->lock);
2197 silc_packet_free(packet);
2198 memset(tmp, 0, sizeof(tmp));
2202 /* Dispatch the packet to application */
2203 if (!silc_packet_dispatch(packet))
2208 /* Add inbuf back to free list, if we owned it. */
2209 if (stream->inbuf) {
2210 silc_dlist_add(stream->sc->inbufs, inbuf);
2211 stream->inbuf = NULL;
2214 silc_buffer_reset(inbuf);
2217 /****************************** Packet Waiting ******************************/
2219 /* Packet wait receive callback */
2221 silc_packet_wait_packet_receive(SilcPacketEngine engine,
2222 SilcPacketStream stream,
2224 void *callback_context,
2225 void *stream_context);
2227 /* Packet waiting callbacks */
2228 static SilcPacketCallbacks silc_packet_wait_cbs =
2230 silc_packet_wait_packet_receive, NULL, NULL
2233 /* Packet waiting context */
2235 SilcMutex wait_lock;
2237 SilcList packet_queue;
2238 unsigned int stopped : 1;
2241 /* Packet wait receive callback */
2244 silc_packet_wait_packet_receive(SilcPacketEngine engine,
2245 SilcPacketStream stream,
2247 void *callback_context,
2248 void *stream_context)
2250 SilcPacketWait pw = callback_context;
2252 /* Signal the waiting thread for a new packet */
2253 silc_mutex_lock(pw->wait_lock);
2255 if (silc_unlikely(pw->stopped)) {
2256 silc_mutex_unlock(pw->wait_lock);
2260 silc_list_add(pw->packet_queue, packet);
2261 silc_cond_broadcast(pw->wait_cond);
2263 silc_mutex_unlock(pw->wait_lock);
2268 /* Initialize packet waiting */
2270 void *silc_packet_wait_init(SilcPacketStream stream, ...)
2276 pw = silc_calloc(1, sizeof(*pw));
2280 /* Allocate mutex and conditional variable */
2281 if (!silc_mutex_alloc(&pw->wait_lock)) {
2285 if (!silc_cond_alloc(&pw->wait_cond)) {
2286 silc_mutex_free(pw->wait_lock);
2291 /* Link to the packet stream for the requested packet types */
2292 va_start(ap, stream);
2293 ret = silc_packet_stream_link_va(stream, &silc_packet_wait_cbs, pw,
2297 silc_cond_free(pw->wait_cond);
2298 silc_mutex_free(pw->wait_lock);
2303 /* Initialize packet queue */
2304 silc_list_init(pw->packet_queue, struct SilcPacketStruct, next);
2309 /* Uninitialize packet waiting */
2311 void silc_packet_wait_uninit(void *waiter, SilcPacketStream stream)
2313 SilcPacketWait pw = waiter;
2316 /* Signal any threads to stop waiting */
2317 silc_mutex_lock(pw->wait_lock);
2319 silc_cond_broadcast(pw->wait_cond);
2320 silc_mutex_unlock(pw->wait_lock);
2322 /* Re-acquire lock and free resources */
2323 silc_mutex_lock(pw->wait_lock);
2324 silc_packet_stream_unlink(stream, &silc_packet_wait_cbs, pw);
2326 /* Free any remaining packets */
2327 silc_list_start(pw->packet_queue);
2328 while ((packet = silc_list_get(pw->packet_queue)) != SILC_LIST_END)
2329 silc_packet_free(packet);
2331 silc_mutex_unlock(pw->wait_lock);
2332 silc_cond_free(pw->wait_cond);
2333 silc_mutex_free(pw->wait_lock);
2337 /* Blocks thread until a packet has been received. */
2339 int silc_packet_wait(void *waiter, int timeout, SilcPacket *return_packet)
2341 SilcPacketWait pw = waiter;
2342 SilcBool ret = FALSE;
2344 silc_mutex_lock(pw->wait_lock);
2346 /* Wait here until packet has arrived */
2347 while (silc_list_count(pw->packet_queue) == 0) {
2348 if (silc_unlikely(pw->stopped)) {
2349 silc_mutex_unlock(pw->wait_lock);
2352 ret = silc_cond_timedwait(pw->wait_cond, pw->wait_lock, timeout);
2356 silc_list_start(pw->packet_queue);
2357 *return_packet = silc_list_get(pw->packet_queue);
2358 silc_list_del(pw->packet_queue, *return_packet);
2360 silc_mutex_unlock(pw->wait_lock);
2362 return ret == TRUE ? 1 : 0;
2365 /************************** Packet Stream Wrapper ***************************/
2367 /* Packet stream wrapper receive callback */
2369 silc_packet_wrap_packet_receive(SilcPacketEngine engine,
2370 SilcPacketStream stream,
2372 void *callback_context,
2373 void *stream_context);
2375 const SilcStreamOps silc_packet_stream_ops;
2377 /* Packet stream wrapper context */
2379 const SilcStreamOps *ops;
2380 SilcPacketStream stream;
2382 void *waiter; /* Waiter context in blocking mode */
2383 SilcPacketWrapCoder coder;
2384 void *coder_context;
2386 SilcStreamNotifier callback;
2389 SilcPacketType type;
2390 SilcPacketFlags flags;
2391 unsigned int closed : 1;
2392 unsigned int blocking : 1;
2393 unsigned int read_more : 1;
2394 } *SilcPacketWrapperStream;
2396 /* Packet wrapper callbacks */
2397 static SilcPacketCallbacks silc_packet_wrap_cbs =
2399 silc_packet_wrap_packet_receive, NULL, NULL
2402 /* Packet stream wrapper receive callback, non-blocking mode */
2405 silc_packet_wrap_packet_receive(SilcPacketEngine engine,
2406 SilcPacketStream stream,
2408 void *callback_context,
2409 void *stream_context)
2411 SilcPacketWrapperStream pws = callback_context;
2413 if (pws->closed || !pws->callback)
2416 silc_mutex_lock(pws->lock);
2417 silc_list_add(pws->in_queue, packet);
2418 silc_mutex_unlock(pws->lock);
2420 /* Call notifier callback */
2421 pws->callback((SilcStream)pws, SILC_STREAM_CAN_READ, pws->context);
2426 /* Task callback to notify more data is available for reading */
2428 SILC_TASK_CALLBACK(silc_packet_wrap_read_more)
2430 SilcPacketWrapperStream pws = context;
2432 if (pws->closed || !pws->callback)
2435 /* Call notifier callback */
2436 pws->callback((SilcStream)pws, SILC_STREAM_CAN_READ, pws->context);
2439 /* Read SILC packet */
2441 int silc_packet_wrap_read(SilcStream stream, unsigned char *buf,
2444 SilcPacketWrapperStream pws = stream;
2446 SilcBool read_more = FALSE;
2452 if (pws->blocking) {
2453 /* Block until packet is received */
2454 if ((silc_packet_wait(pws->waiter, 0, &packet)) < 0)
2459 /* Non-blocking mode */
2460 silc_mutex_lock(pws->lock);
2461 if (!silc_list_count(pws->in_queue)) {
2462 silc_mutex_unlock(pws->lock);
2466 silc_list_start(pws->in_queue);
2467 packet = silc_list_get(pws->in_queue);
2468 silc_list_del(pws->in_queue, packet);
2469 silc_mutex_unlock(pws->lock);
2472 /* Call decoder if set */
2473 if (pws->coder && !pws->read_more)
2474 pws->coder(stream, SILC_STREAM_CAN_READ, &packet->buffer,
2475 pws->coder_context);
2477 len = silc_buffer_len(&packet->buffer);
2478 if (len > buf_len) {
2484 memcpy(buf, packet->buffer.data, len);
2486 if (read_more && !pws->blocking) {
2487 /* More data will be available (in blocking mode not supported). */
2488 silc_buffer_pull(&packet->buffer, len);
2489 silc_list_insert(pws->in_queue, NULL, packet);
2490 silc_schedule_task_add_timeout(pws->stream->sc->schedule,
2491 silc_packet_wrap_read_more, pws, 0, 0);
2492 pws->read_more = TRUE;
2496 pws->read_more = FALSE;
2497 silc_packet_free(packet);
2501 /* Write SILC packet */
2503 int silc_packet_wrap_write(SilcStream stream, const unsigned char *data,
2504 SilcUInt32 data_len)
2506 SilcPacketWrapperStream pws = stream;
2507 SilcBool ret = FALSE;
2509 /* Call decoder if set */
2511 silc_buffer_reset(pws->encbuf);
2512 ret = pws->coder(stream, SILC_STREAM_CAN_WRITE, pws->encbuf,
2513 pws->coder_context);
2516 /* Send the SILC packet */
2518 if (!silc_packet_send_va(pws->stream, pws->type, pws->flags,
2519 SILC_STR_DATA(silc_buffer_data(pws->encbuf),
2520 silc_buffer_len(pws->encbuf)),
2521 SILC_STR_DATA(data, data_len),
2525 if (!silc_packet_send(pws->stream, pws->type, pws->flags, data, data_len))
2534 SilcBool silc_packet_wrap_close(SilcStream stream)
2536 SilcPacketWrapperStream pws = stream;
2541 if (pws->blocking) {
2542 /* Close packet waiter */
2543 silc_packet_wait_uninit(pws->waiter, pws->stream);
2547 silc_packet_stream_unlink(pws->stream, &silc_packet_wrap_cbs, pws);
2554 /* Destroy wrapper stream */
2556 void silc_packet_wrap_destroy(SilcStream stream)
2559 SilcPacketWrapperStream pws = stream;
2562 SILC_LOG_DEBUG(("Destroying wrapped packet stream %p", pws));
2564 silc_stream_close(stream);
2565 silc_list_start(pws->in_queue);
2566 while ((packet = silc_list_get(pws->in_queue)))
2567 silc_packet_free(packet);
2569 silc_mutex_free(pws->lock);
2571 silc_buffer_free(pws->encbuf);
2572 silc_packet_stream_unref(pws->stream);
2577 /* Link stream to receive packets */
2579 SilcBool silc_packet_wrap_notifier(SilcStream stream,
2580 SilcSchedule schedule,
2581 SilcStreamNotifier callback,
2584 SilcPacketWrapperStream pws = stream;
2586 if (pws->closed || pws->blocking)
2589 /* Link to receive packets */
2591 silc_packet_stream_link(pws->stream, &silc_packet_wrap_cbs, pws,
2592 100000, pws->type, -1);
2594 silc_packet_stream_unlink(pws->stream, &silc_packet_wrap_cbs, pws);
2596 pws->callback = callback;
2597 pws->context = context;
2602 /* Return schedule */
2604 SilcSchedule silc_packet_wrap_get_schedule(SilcStream stream)
2609 /* Wraps packet stream into SilcStream. */
2611 SilcStream silc_packet_stream_wrap(SilcPacketStream stream,
2612 SilcPacketType type,
2613 SilcPacketFlags flags,
2614 SilcBool blocking_mode,
2615 SilcPacketWrapCoder coder,
2618 SilcPacketWrapperStream pws;
2620 pws = silc_calloc(1, sizeof(*pws));
2624 SILC_LOG_DEBUG(("Wrapping packet stream %p to stream %p", stream, pws));
2626 pws->ops = &silc_packet_stream_ops;
2627 pws->stream = stream;
2630 pws->blocking = blocking_mode;
2632 pws->coder_context = context;
2634 /* Allocate small amount for encoder buffer. */
2636 pws->encbuf = silc_buffer_alloc(8);
2638 if (pws->blocking) {
2639 /* Blocking mode. Use packet waiter to do the thing. */
2640 pws->waiter = silc_packet_wait_init(pws->stream, pws->type, -1);
2646 /* Non-blocking mode */
2647 silc_mutex_alloc(&pws->lock);
2648 silc_list_init(pws->in_queue, struct SilcPacketStruct, next);
2651 silc_packet_stream_ref(stream);
2653 return (SilcStream)pws;
2656 const SilcStreamOps silc_packet_stream_ops =
2658 silc_packet_wrap_read,
2659 silc_packet_wrap_write,
2660 silc_packet_wrap_close,
2661 silc_packet_wrap_destroy,
2662 silc_packet_wrap_notifier,
2663 silc_packet_wrap_get_schedule,