Esempio n. 1
0
static int qcelp_parse_packet(AVFormatContext *ctx, PayloadContext *data,
                              AVStream *st, AVPacket *pkt, uint32_t *timestamp,
                              const uint8_t *buf, int len, int flags)
{
    if (buf)
        return store_packet(ctx, data, st, pkt, timestamp, buf, len);
    else
        return return_stored_frame(ctx, data, st, pkt, timestamp, buf, len);
}
Esempio n. 2
0
static int return_stored_frame(AVFormatContext *ctx, PayloadContext *data,
                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
                               const uint8_t *buf, int len)
{
    InterleavePacket* ip = &data->group[data->interleave_index];
    int frame_size, ret;

    if (data->group_finished && data->interleave_index == 0) {
        *timestamp = data->next_timestamp;
        ret = store_packet(ctx, data, st, pkt, timestamp, data->next_data,
                           data->next_size);
        data->next_size = 0;
        return ret;
    }

    if (ip->size == 0) {
        /* No stored data for this interleave block, output an empty packet */
        if ((ret = av_new_packet(pkt, 1)) < 0)
            return ret;
        pkt->data[0] = 0; // Blank - could also be 14, Erasure
    } else {
        if (ip->pos >= ip->size)
            return AVERROR_INVALIDDATA;
        if (ip->data[ip->pos] >= FF_ARRAY_ELEMS(frame_sizes))
            return AVERROR_INVALIDDATA;
        frame_size = frame_sizes[ip->data[ip->pos]];
        if (ip->pos + frame_size > ip->size)
            return AVERROR_INVALIDDATA;

        if ((ret = av_new_packet(pkt, frame_size)) < 0)
            return ret;
        memcpy(pkt->data, &ip->data[ip->pos], frame_size);

        ip->pos += frame_size;
        data->group_finished = ip->pos >= ip->size;
    }
    pkt->stream_index = st->index;

    if (data->interleave_index == data->interleave_size) {
        data->interleave_index = 0;
        if (!data->group_finished)
            return 1;
        else
            return data->next_size > 0;
    } else {
        data->interleave_index++;
        return 1;
    }
}
Esempio n. 3
0
void process_tcp(const u_char *data, u_int32_t length, u_int32_t src,
		 u_int32_t dst)
{
  struct tcphdr *tcp_header = (struct tcphdr *) data;
  flow_t this_flow;
  u_int tcp_header_len;
  tcp_seq seq;

  if (length < sizeof(struct tcphdr)) {
    DEBUG(6) ("received truncated TCP segment!");
    return;
  }

  /* calculate the total length of the TCP header including options */
  tcp_header_len = tcp_header->th_off * 4;

  /* return if this packet doesn't have any data (e.g., just an ACK) */
  if (length <= tcp_header_len) {
    DEBUG(50) ("got TCP segment with no data");
    return;
  }

  /* fill in the flow_t structure with info that identifies this flow */
  this_flow.src = src;
  this_flow.dst = dst;
  this_flow.sport = ntohs(tcp_header->th_sport);
  this_flow.dport = ntohs(tcp_header->th_dport);
  seq = ntohl(tcp_header->th_seq);

  /* recalculate the beginning of data and its length, moving past the
   * TCP header */
  data += tcp_header_len;
  length -= tcp_header_len;

  /* strip nonprintable characters if necessary */
  if (strip_nonprint)
    data = do_strip_nonprint(data, length);

  /* store or print the output */
  if (console_only) {
    print_packet(this_flow, data, length);
  } else {
    store_packet(this_flow, data, length, seq);
  }
}
Esempio n. 4
0
void process_tcp(const u_char *data, u_int32_t length, u_int32_t src,
		 u_int32_t dst)
{
  struct tcphdr *tcp_header = (struct tcphdr *) data;
  flow_t this_flow;
  u_int tcp_header_len;
  tcp_seq seq;
  flow_state_t *state;

  if (length < sizeof(struct tcphdr)) {
    DEBUG(6) ("received truncated TCP segment!");
    return;
  }

  /* calculate the total length of the TCP header including options */
  tcp_header_len = tcp_header->th_off * 4;

  /* fill in the flow_t structure with info that identifies this flow */
  this_flow.src = src;
  this_flow.dst = dst;
  this_flow.sport = ntohs(tcp_header->th_sport);
  this_flow.dport = ntohs(tcp_header->th_dport);
  seq = ntohl(tcp_header->th_seq);

  /* recalculate the beginning of data and its length, moving past the
   * TCP header */
  data += tcp_header_len;
  length -= tcp_header_len;

  /* see if we have state about this flow; if not, create it */
  if ((state = find_flow_state(this_flow)) == NULL) {
    state = create_flow_state(this_flow, seq);
  }

  /* Handle empty packets */
  if (length == 0) {
    /* examine TCP flags for initial TCP handshake segments:
     * - SYN means that the flow is a client -> server flow
     * - SYN/ACK means that the flow is a server -> client flow. */
    if ((state->isn - seq) == 0) {
      if (IS_SET(tcp_header->th_flags, TH_SYN)
	  && IS_SET(tcp_header->th_flags, TH_ACK)) {
	SET_BIT(state->flags, FLOW_DIR_SC);
	DEBUG(50) ("packet is handshake SYN/ACK");
	/* If the SYN flag is set the first data byte is offset by one,
	   account for it (note: if we're here we have just created
	   state, so it's safe to change isn). */
	state->isn++;
      } else if (IS_SET(tcp_header->th_flags, TH_SYN)) {
	SET_BIT(state->flags, FLOW_DIR_CS);
	DEBUG(50) ("packet is handshake SYN");
	state->isn++;
      }
    }
    DEBUG(50) ("got TCP segment with no data");
    return;
  }

  /* strip nonprintable characters if necessary */
  if (strip_nonprint)
    data = do_strip_nonprint(data, length);

  /* store or print the output */
  if (console_only) {
    print_packet(this_flow, state, data, length);
  } else {
    store_packet(this_flow, state, data, length, seq,
		 IS_SET(tcp_header->th_flags, TH_SYN));
  }
}
Esempio n. 5
0
void process_packet(struct pcap_pkthdr* pkthdr, u_char* packet) {
	struct eth_hdr *eth;
	struct ip_hdr *ip;
	struct tcp_hdr *tcp;
	char *payload;
	int size_ip, size_tcp, payload_size;
	u_int mp3_len;
	struct dump *dp;

	// --------------------------------------------------------

	eth = (struct eth_hdr *) packet;
	if (ntohs (eth->ether_type) != 0x0800)
	{
		return;
	}
	ip = (struct ip_hdr*)(packet + SIZE_ETHERNET);
	if (ip->ip_p != IPPROTO_TCP) {
		return;
	}
	size_ip = IP_HL(ip)*4;
	if (size_ip < 20) {
		printf("Invalid IP header length: %u bytes\n", size_ip);
		return;
	}
	tcp = (struct tcp_hdr*)(packet + SIZE_ETHERNET + size_ip);
	size_tcp = TH_OFF(tcp)*4;
	if (size_tcp < 20) {
		printf("Invalid TCP header length: %u bytes\n", size_tcp);
		return;
	}
	payload = (u_char *)(packet + SIZE_ETHERNET + size_ip + size_tcp);
	payload_size = ntohs(ip->ip_len) - (size_ip + size_tcp);

	// --------------------------------------------------------

	//check_old_streams();

	if ((dp = get_dump_stream(ntohs(tcp->th_dport))) != NULL) {
		if (dp->next_seq != ntohl(tcp->th_seq)) {
			struct payload *old;

			//printf("invalid seq number got: %u  expected: %u\n", ntohl(tcp->th_seq), dp->next_seq);

			store_packet(dp, ntohl(tcp->th_seq), payload, payload_size);

			/* try to find the valid packet */
			old = find_valid_seq(dp, ntohl(tcp->th_seq));
			if (old) {
				write_dump(dp, old->data, old->length);
				return;
			}

			if (++dp->fail_count > 5) {
				close_dump_stream(dp, 1);
			}

			return;

		}
		write_dump(dp, payload, payload_size);
	}
	else if ((mp3_len = contain_mp3(payload, payload_size)) > 0) {
		char *start;
		start = strstr(payload, "\r\n\r\n") + 4;
		dp = create_dump_stream(ntohs(tcp->th_dport), ntohl(tcp->th_seq), mp3_len, payload, payload_size, start);
		dp->current_size += payload_size - (start - payload);
		fwrite(start, 1, dp->current_size, dp->file);
		dp->next_seq += payload_size;
	}
}