-/* 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. Returns TRUE on success and FALSE on error. */
-
-int silc_packet_receive_process(SilcSocketConnection sock,
- SilcCipher cipher, SilcHmac hmac,
- SilcPacketParserCallback parser,
- void *context)
-{
- SilcPacketParserContext *parse_ctx;
- int packetlen, paddedlen, mac_len = 0;
-
- /* Check whether we received a whole packet. If reading went without
- errors we either read a whole packet or the read packet is
- incorrect and will be dropped. */
- SILC_PACKET_LENGTH(sock->inbuf, packetlen, paddedlen);
- if (sock->inbuf->len < paddedlen || (packetlen < SILC_PACKET_MIN_LEN)) {
- SILC_LOG_DEBUG(("Received incorrect packet, dropped"));
- silc_buffer_clear(sock->inbuf);
- return FALSE;
- }
-
- /* Parse the packets from the data */
- if (sock->inbuf->len - 2 > (paddedlen + mac_len)) {
- /* Received possibly many packets at once */
-
- if (hmac)
- mac_len = hmac->hash->hash->hash_len;
-
- while(sock->inbuf->len > 0) {
- SILC_PACKET_LENGTH(sock->inbuf, packetlen, paddedlen);
-
- if (sock->inbuf->len < paddedlen) {
- SILC_LOG_DEBUG(("Received incorrect packet, dropped"));
- return FALSE;
- }
-
- paddedlen += 2;
- parse_ctx = silc_calloc(1, sizeof(*parse_ctx));
- parse_ctx->packet = silc_calloc(1, sizeof(*parse_ctx->packet));
- parse_ctx->packet->buffer = silc_buffer_alloc(paddedlen + mac_len);
- parse_ctx->sock = sock;
- parse_ctx->cipher = cipher;
- parse_ctx->hmac = hmac;
- 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);
- }
-
- /* All packets are processed, return successfully. */
- silc_buffer_clear(sock->inbuf);
- return TRUE;
-
- } else {
- /* Received one packet */
-
- SILC_LOG_HEXDUMP(("An incoming packet, len %d", sock->inbuf->len),
- sock->inbuf->data, sock->inbuf->len);