-/* Processes the received data. This checks the received data and
- calls parser callback that handles the actual packet decryption
- and parsing. If more than one packet was received this calls the
- parser multiple times. The parser callback will get context
- SilcPacketParserContext that includes the packet and the `context'
- sent to this function. */
-
-void silc_packet_receive_process(SilcSocketConnection sock,
- SilcCipher cipher, SilcHmac hmac,
- SilcPacketParserCallback parser,
- void *context)
-{
- SilcPacketParserContext *parse_ctx;
- int packetlen, paddedlen, count, mac_len = 0;
-
- /* We need at least 2 bytes of data to be able to start processing
- the packet. */
- if (sock->inbuf->len < 2)
- return;
-
- if (hmac)
- mac_len = hmac->hmac->len;
-
- /* Parse the packets from the data */
- count = 0;
- while (sock->inbuf->len > 0) {
- SILC_PACKET_LENGTH(sock->inbuf, packetlen, paddedlen);
- paddedlen += 2;
- count++;
-
- if (packetlen < SILC_PACKET_MIN_LEN) {
- SILC_LOG_DEBUG(("Received invalid packet, dropped"));
- silc_buffer_clear(sock->inbuf);
- return;
- }
-
- if (sock->inbuf->len < paddedlen + mac_len) {
- SILC_LOG_DEBUG(("Received partial packet, waiting for the rest"));
- return;
- }
-
- parse_ctx = silc_calloc(1, sizeof(*parse_ctx));
- parse_ctx->packet = silc_packet_context_alloc();
- parse_ctx->packet->buffer = silc_buffer_alloc(paddedlen + mac_len);
- parse_ctx->sock = sock;
- parse_ctx->context = context;
-
- silc_buffer_pull_tail(parse_ctx->packet->buffer,
- SILC_BUFFER_END(parse_ctx->packet->buffer));
- silc_buffer_put(parse_ctx->packet->buffer, sock->inbuf->data,
- paddedlen + mac_len);
-
- SILC_LOG_HEXDUMP(("Incoming packet, len %d",
- parse_ctx->packet->buffer->len),
- parse_ctx->packet->buffer->data,
- parse_ctx->packet->buffer->len);
-
- /* Call the parser */
- if (parser)
- (*parser)(parse_ctx);
-
- /* Pull the packet from inbuf thus we'll get the next one
- in the inbuf. */
- silc_buffer_pull(sock->inbuf, paddedlen);
- if (hmac)
- silc_buffer_pull(sock->inbuf, mac_len);
- }
-
- SILC_LOG_DEBUG(("Clearing inbound buffer"));
- silc_buffer_clear(sock->inbuf);
-}