+ SilcUInt16 name_len;
+ unsigned char *channel_name;
+ SilcUInt16 id_len;
+ unsigned char *channel_id;
+ SilcUInt32 mode;
+};
+
+/* Parses channel payload returning new channel payload structure. */
+
+SilcChannelPayload silc_channel_payload_parse(const unsigned char *payload,
+ SilcUInt32 payload_len)
+{
+ SilcBufferStruct buffer;
+ SilcChannelPayload new;
+ int ret;
+
+ SILC_LOG_DEBUG(("Parsing channel payload"));
+
+ silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
+ new = silc_calloc(1, sizeof(*new));
+
+ /* Parse the Channel Payload. Ignore the padding. */
+ ret = silc_buffer_unformat(&buffer,
+ SILC_STR_UI16_NSTRING_ALLOC(&new->channel_name,
+ &new->name_len),
+ SILC_STR_UI16_NSTRING_ALLOC(&new->channel_id,
+ &new->id_len),
+ SILC_STR_UI_INT(&new->mode),
+ SILC_STR_END);
+ if (ret == -1)
+ goto err;
+
+ if ((new->name_len < 1 || new->name_len > buffer.len) ||
+ (new->id_len < 1 || new->id_len > buffer.len)) {
+ SILC_LOG_ERROR(("Incorrect channel payload in packet, packet dropped"));
+ goto err;
+ }
+
+ return new;
+
+ err:
+ silc_channel_payload_free(new);
+ return NULL;
+}
+
+/* Parses list of channel payloads returning list of payloads. */
+
+SilcDList silc_channel_payload_parse_list(const unsigned char *payload,
+ SilcUInt32 payload_len)
+{
+ SilcBufferStruct buffer;
+ SilcDList list;
+ SilcChannelPayload new;
+ int len, ret;
+
+ SILC_LOG_DEBUG(("Parsing channel payload list"));
+
+ silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
+ list = silc_dlist_init();
+
+ while (buffer.len) {
+ new = silc_calloc(1, sizeof(*new));
+ ret = silc_buffer_unformat(&buffer,
+ SILC_STR_UI16_NSTRING_ALLOC(&new->channel_name,
+ &new->name_len),
+ SILC_STR_UI16_NSTRING_ALLOC(&new->channel_id,
+ &new->id_len),
+ SILC_STR_UI_INT(&new->mode),
+ SILC_STR_END);
+ if (ret == -1)
+ goto err;
+
+ if ((new->name_len < 1 || new->name_len > buffer.len) ||
+ (new->id_len < 1 || new->id_len > buffer.len)) {
+ SILC_LOG_ERROR(("Incorrect channel payload in packet, packet dropped"));
+ goto err;
+ }
+
+ len = 2 + new->name_len + 2 + new->id_len + 4;
+ if (buffer.len < len)
+ break;
+ silc_buffer_pull(&buffer, len);
+
+ silc_dlist_add(list, new);
+ }
+
+ return list;
+
+ err:
+ silc_channel_payload_list_free(list);
+ return NULL;
+}
+
+/* Encode new channel payload and returns it as buffer. */
+
+SilcBuffer silc_channel_payload_encode(const unsigned char *channel_name,
+ SilcUInt16 channel_name_len,
+ const unsigned char *channel_id,
+ SilcUInt32 channel_id_len,
+ SilcUInt32 mode)
+{
+ SilcBuffer buffer;
+
+ SILC_LOG_DEBUG(("Encoding message payload"));
+
+ buffer = silc_buffer_alloc(2 + channel_name_len + 2 + channel_id_len + 4);
+ silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+
+ /* Encode the Channel Payload */
+ silc_buffer_format(buffer,
+ SILC_STR_UI_SHORT(channel_name_len),
+ SILC_STR_UI_XNSTRING(channel_name, channel_name_len),
+ SILC_STR_UI_SHORT(channel_id_len),
+ SILC_STR_UI_XNSTRING(channel_id, channel_id_len),
+ SILC_STR_UI_INT(mode),
+ SILC_STR_END);
+
+ return buffer;
+}
+
+/* Frees Channel Payload */
+
+void silc_channel_payload_free(SilcChannelPayload payload)
+{
+ silc_free(payload->channel_name);
+ silc_free(payload->channel_id);
+ silc_free(payload);
+}
+
+/* Free's list of Channel Payloads */
+
+void silc_channel_payload_list_free(SilcDList list)
+{
+ SilcChannelPayload entry;
+
+ silc_dlist_start(list);
+ while ((entry = silc_dlist_get(list)) != SILC_LIST_END) {
+ silc_free(entry->channel_name);
+ silc_free(entry->channel_id);
+ silc_dlist_del(list, entry);
+ silc_free(entry);
+ }
+
+ silc_dlist_uninit(list);
+}
+
+/* Return the channel name */
+
+unsigned char *silc_channel_get_name(SilcChannelPayload payload,
+ SilcUInt32 *channel_name_len)
+{
+ if (channel_name_len)
+ *channel_name_len = payload->name_len;
+
+ return payload->channel_name;
+}
+
+/* Return the channel ID */
+
+unsigned char *silc_channel_get_id(SilcChannelPayload payload,
+ SilcUInt32 *channel_id_len)
+{
+ if (channel_id_len)
+ *channel_id_len = payload->id_len;
+
+ return payload->channel_id;
+}
+
+/* Return the channel ID as parsed ID. */
+
+SilcChannelID *silc_channel_get_id_parse(SilcChannelPayload payload)
+{
+ return silc_id_str2id(payload->channel_id, payload->id_len,
+ SILC_ID_CHANNEL);
+}
+
+/* Return the mode. The mode is arbitrary. It can be the mode of the
+ channel or perhaps the mode of the client on the channel. The protocol
+ dictates what the usage of the mode is in different circumstances. */
+
+SilcUInt32 silc_channel_get_mode(SilcChannelPayload payload)
+{
+ return payload->mode;
+}
+
+/******************************************************************************
+
+ Channel Message Payload
+
+******************************************************************************/
+
+#define SILC_CHANNEL_MESSAGE_PAD(__payloadlen) (16 - (__payloadlen) % 16)
+
+/* Channel Message Payload structure. Contents of this structure is parsed
+ from SILC packets. */
+struct SilcChannelMessagePayloadStruct {
+ SilcMessageFlags flags;
+ SilcUInt16 data_len;