+static inline SilcBool silc_packet_send_raw(SilcPacketStream stream,
+ SilcPacketType type,
+ SilcPacketFlags flags,
+ SilcIdType src_id_type,
+ unsigned char *src_id,
+ SilcUInt32 src_id_len,
+ SilcIdType dst_id_type,
+ unsigned char *dst_id,
+ SilcUInt32 dst_id_len,
+ const unsigned char *data,
+ SilcUInt32 data_len,
+ SilcCipher cipher,
+ SilcHmac hmac);
+
+/************************ Static utility functions **************************/
+
+/* Injects packet to new stream created with silc_packet_stream_add_remote. */
+
+SILC_TASK_CALLBACK(silc_packet_stream_inject_packet)
+{
+ SilcPacket packet = context;
+ SilcPacketStream stream = packet->stream;
+
+ SILC_LOG_DEBUG(("Injecting packet %p to stream %p", packet, packet->stream));
+
+ silc_mutex_lock(stream->lock);
+ if (!stream->destroyed)
+ silc_packet_dispatch(packet);
+ silc_mutex_unlock(stream->lock);
+ silc_packet_stream_unref(stream);
+}
+
+/* Write data to the stream. Must be called with ps->lock locked. Unlocks
+ the lock inside this function, unless no_unlock is TRUE. Unlocks always
+ in case it returns FALSE. */
+
+static inline SilcBool silc_packet_stream_write(SilcPacketStream ps,
+ SilcBool no_unlock)
+{
+ SilcStream stream;
+ SilcBool connected;
+ int i;
+
+ if (ps->udp)
+ stream = ((SilcPacketStream)ps->stream)->stream;
+ else
+ stream = ps->stream;
+
+ if (ps->udp && silc_socket_stream_is_udp(stream, &connected)) {
+ if (!connected) {
+ /* Connectionless UDP stream */
+ while (silc_buffer_len(&ps->outbuf) > 0) {
+ i = silc_net_udp_send(stream, ps->remote_udp->remote_ip,
+ ps->remote_udp->remote_port,
+ ps->outbuf.data, silc_buffer_len(&ps->outbuf));
+ if (silc_unlikely(i == -2)) {
+ /* Error */
+ silc_buffer_reset(&ps->outbuf);
+ SILC_PACKET_CALLBACK_ERROR(ps, SILC_PACKET_ERR_WRITE);
+ return FALSE;
+ }
+
+ if (silc_unlikely(i == -1)) {
+ /* Cannot write now, write later. */
+ if (!no_unlock)
+ silc_mutex_unlock(ps->lock);
+ return TRUE;
+ }
+
+ /* Wrote data */
+ silc_buffer_pull(&ps->outbuf, i);
+ }
+
+ silc_buffer_reset(&ps->outbuf);
+ if (!no_unlock)
+ silc_mutex_unlock(ps->lock);
+
+ return TRUE;
+ }
+ }
+
+ /* Write the data to the stream */
+ while (silc_buffer_len(&ps->outbuf) > 0) {
+ i = silc_stream_write(stream, ps->outbuf.data,
+ silc_buffer_len(&ps->outbuf));
+ if (silc_unlikely(i == 0)) {
+ /* EOS */
+ silc_buffer_reset(&ps->outbuf);
+ silc_mutex_unlock(ps->lock);
+ SILC_PACKET_CALLBACK_EOS(ps);
+ return FALSE;
+ }
+
+ if (silc_unlikely(i == -2)) {
+ /* Error */
+ silc_buffer_reset(&ps->outbuf);
+ silc_mutex_unlock(ps->lock);
+ SILC_PACKET_CALLBACK_ERROR(ps, SILC_PACKET_ERR_WRITE);
+ return FALSE;
+ }
+
+ if (silc_unlikely(i == -1)) {
+ /* Cannot write now, write later. */
+ if (!no_unlock)
+ silc_mutex_unlock(ps->lock);
+ return TRUE;
+ }
+
+ /* Wrote data */
+ silc_buffer_pull(&ps->outbuf, i);
+ }
+
+ silc_buffer_reset(&ps->outbuf);
+ if (!no_unlock)
+ silc_mutex_unlock(ps->lock);
+
+ return TRUE;
+}
+
+/* Reads data from stream. Must be called with ps->lock locked. If this
+ returns FALSE the lock has been unlocked. If this returns packet stream
+ to `ret_ps' its lock has been acquired and `ps' lock has been unlocked.
+ It is returned if the stream is UDP and remote UDP stream exists for
+ the sender of the packet. */
+
+static inline SilcBool silc_packet_stream_read(SilcPacketStream ps,
+ SilcPacketStream *ret_ps)
+{
+ SilcStream stream = ps->stream;
+ SilcBuffer inbuf;
+ SilcBool connected;
+ int ret;
+
+ /* Get inbuf. If there is already some data for this stream in the buffer
+ we already have it. Otherwise get the current one from list, it will
+ include the data. */
+ inbuf = ps->inbuf;
+ if (!inbuf) {
+ silc_dlist_start(ps->sc->inbufs);
+ inbuf = silc_dlist_get(ps->sc->inbufs);
+ if (!inbuf) {
+ /* Allocate new data input buffer */
+ inbuf = silc_buffer_alloc(SILC_PACKET_DEFAULT_SIZE * 65);
+ if (!inbuf) {
+ silc_mutex_unlock(ps->lock);
+ return FALSE;
+ }
+ silc_buffer_reset(inbuf);
+ silc_dlist_add(ps->sc->inbufs, inbuf);
+ }
+ }
+
+ /* Make sure there is enough room to read */
+ if (SILC_PACKET_DEFAULT_SIZE * 2 > silc_buffer_taillen(inbuf))
+ silc_buffer_realloc(inbuf, silc_buffer_truelen(inbuf) +
+ (SILC_PACKET_DEFAULT_SIZE * 2));
+
+ if (silc_socket_stream_is_udp(stream, &connected)) {
+ if (!connected) {
+ /* Connectionless UDP stream, read one UDP packet */
+ char remote_ip[64], tuple[64];
+ int remote_port;
+ SilcPacketStream remote;
+
+ ret = silc_net_udp_receive(stream, remote_ip, sizeof(remote_ip),
+ &remote_port, inbuf->tail,
+ silc_buffer_taillen(inbuf));
+
+ if (silc_unlikely(ret < 0)) {
+ silc_mutex_unlock(ps->lock);
+ if (ret == -1) {
+ /* Cannot read now, do it later. */
+ return FALSE;
+ }
+
+ /* Error */
+ silc_buffer_reset(inbuf);
+ SILC_PACKET_CALLBACK_ERROR(ps, SILC_PACKET_ERR_READ);
+ return FALSE;
+ }
+
+ /* See if remote packet stream exist for this sender */
+ silc_snprintf(tuple, sizeof(tuple), "%d%s", remote_port, remote_ip);
+ silc_mutex_lock(ps->sc->engine->lock);
+ if (silc_hash_table_find(ps->sc->engine->udp_remote, tuple, NULL,
+ (void *)&remote)) {
+ silc_mutex_unlock(ps->sc->engine->lock);
+ SILC_LOG_DEBUG(("UDP packet from %s:%d for stream %p", remote_ip,
+ remote_port, remote));
+ silc_mutex_unlock(ps->lock);
+ silc_mutex_lock(remote->lock);
+ *ret_ps = remote;
+ return TRUE;
+ }
+ silc_mutex_unlock(ps->sc->engine->lock);
+
+ /* Unknown sender */
+ if (!ps->remote_udp) {
+ ps->remote_udp = silc_calloc(1, sizeof(*ps->remote_udp));
+ if (silc_unlikely(!ps->remote_udp)) {
+ silc_mutex_unlock(ps->lock);
+ SILC_PACKET_CALLBACK_ERROR(ps, SILC_PACKET_ERR_NO_MEMORY);
+ return FALSE;
+ }
+ }
+
+ /* Save sender IP and port */
+ silc_free(ps->remote_udp->remote_ip);
+ ps->remote_udp->remote_ip = strdup(remote_ip);
+ ps->remote_udp->remote_port = remote_port;
+
+ silc_buffer_pull_tail(inbuf, ret);
+ return TRUE;
+ }
+ }
+
+ /* Read data from the stream */
+ ret = silc_stream_read(stream, inbuf->tail, silc_buffer_taillen(inbuf));
+ if (silc_unlikely(ret <= 0)) {
+ silc_mutex_unlock(ps->lock);
+ if (ret == 0) {
+ /* EOS */
+ silc_buffer_reset(inbuf);
+ SILC_PACKET_CALLBACK_EOS(ps);
+ return FALSE;
+ }
+
+ if (ret == -1) {
+ /* Cannot read now, do it later. */
+ return FALSE;
+ }
+
+ /* Error */
+ silc_buffer_reset(inbuf);
+ SILC_PACKET_CALLBACK_ERROR(ps, SILC_PACKET_ERR_READ);
+ return FALSE;
+ }
+
+ silc_buffer_pull_tail(inbuf, ret);
+ return TRUE;
+}
+
+/* Our stream IO notifier callback. */
+
+static void silc_packet_stream_io(SilcStream stream, SilcStreamStatus status,
+ void *context)
+{
+ SilcPacketStream remote = NULL, ps = context;
+
+ silc_mutex_lock(ps->lock);
+
+ if (silc_unlikely(ps->destroyed)) {
+ silc_mutex_unlock(ps->lock);
+ return;
+ }
+
+ switch (status) {
+ case SILC_STREAM_CAN_READ:
+ /* Reading is locked also with stream->lock because we may be reading
+ at the same time other thread is writing to same underlaying stream. */
+ SILC_LOG_DEBUG(("Reading data from stream %p, ps %p", ps->stream, ps));
+
+ /* Read data from stream */
+ if (!silc_packet_stream_read(ps, &remote))
+ return;
+
+ /* Now process the data */
+ silc_packet_stream_ref(ps);
+ if (!remote) {
+ silc_packet_read_process(ps);
+ silc_mutex_unlock(ps->lock);
+ } else {
+ silc_packet_read_process(remote);
+ silc_mutex_unlock(remote->lock);
+ }
+ silc_packet_stream_unref(ps);
+ break;
+
+ case SILC_STREAM_CAN_WRITE:
+ SILC_LOG_DEBUG(("Writing pending data to stream %p, ps %p",
+ ps->stream, ps));
+
+ if (silc_unlikely(!silc_buffer_headlen(&ps->outbuf))) {
+ silc_mutex_unlock(ps->lock);
+ return;
+ }
+
+ /* Write pending data to stream */
+ silc_packet_stream_write(ps, FALSE);
+ break;
+
+ default:
+ silc_mutex_unlock(ps->lock);
+ break;
+ }
+}
+
+/* Allocate packet */
+
+static SilcPacket silc_packet_alloc(SilcPacketEngine engine)
+{
+ SilcPacket packet;