示例#1
0
static void
make_path( sliceable_switch *sliceable_switch, uint64_t in_datapath_id, uint16_t in_port, uint16_t in_vid,
           uint64_t out_datapath_id, uint16_t out_port, uint16_t out_vid, const buffer *packet ) {
  dlist_element *hops = resolve_path( sliceable_switch->pathresolver, in_datapath_id, in_port, out_datapath_id, out_port );
  if ( hops == NULL ) {
    warn( "No available path found ( %#" PRIx64 ":%u -> %#" PRIx64 ":%u ).",
          in_datapath_id, in_port, out_datapath_id, out_port );
    discard_packet_in( in_datapath_id, in_port, packet );
    return;
  }

  // check if the packet is ARP or not
  if ( sliceable_switch->handle_arp_with_packetout && packet_type_arp( packet ) ) {
    // send packet out for tail switch
    free_hop_list( hops );
    output_packet( packet, out_datapath_id, out_port, out_vid );
    return;
  }

  const uint32_t wildcards = 0;
  struct ofp_match match;
  set_match_from_packet( &match, in_port, wildcards, packet );

  if ( lookup_path( in_datapath_id, match, PRIORITY ) != NULL ) {
    warn( "Duplicated path found." );
    output_packet( packet, out_datapath_id, out_port, out_vid );
    return;
  }

  const uint16_t hard_timeout = 0;
  path *p = create_path( match, PRIORITY, sliceable_switch->idle_timeout, hard_timeout );
  assert( p != NULL );
  for ( dlist_element *e = get_first_element( hops ); e != NULL; e = e->next ) {
    pathresolver_hop *rh = e->data;
    hop *h = create_hop( rh->dpid, rh->in_port_no, rh->out_port_no, NULL );
    assert( h != NULL );
    append_hop_to_path( p, h );
  } // for(;;)

  dlist_element *e = get_last_element( hops );
  pathresolver_hop *last_hop = e->data;
  packet_out_params *params = xmalloc( sizeof( struct packet_out_params ) );
  params->packet = duplicate_buffer( packet );
  params->out_datapath_id = last_hop->dpid;
  params->out_port_no = last_hop->out_port_no;
  params->out_vid = out_vid;

  bool ret = setup_path( p, handle_setup, params, NULL, NULL );
  if ( ret != true ) {
    error( "Failed to set up path." );
    output_packet( packet, out_datapath_id, out_port, out_vid );
    free_buffer( params->packet );
    xfree( params );
  }

  delete_path( p );

  // free them
  free_hop_list( hops );
}
static void
setup_reverse_path( int status, const path *p, void *user_data ) {
  assert(user_data);

  packet_out_params *params = user_data;
  if ( status != SETUP_SUCCEEDED ) {
    error( "Failed to set up path ( status = %d ).", status );
    output_packet( params->packet, params->out_datapath_id, params->out_port_no, params->out_vid );
    free_buffer( params->packet );
    xfree( params );
    return;
  }

  struct ofp_match rmatch;
  set_ipv4_reverse_match( &rmatch, &(p->match) );
  rmatch.dl_vlan = params->out_vid;

  openflow_actions *vlan_actions;
  vlan_actions = create_openflow_actions_to_update_vid( params->out_vid, params->in_vid );

  path *rpath = create_path( rmatch, p->priority, p->idle_timeout, p->hard_timeout );
  assert( rpath != NULL );
  list_element *hops = p->hops;
  dlist_element *rhops = create_dlist();
  while ( hops != NULL ) {
    hop *h = hops->data;
    assert( h != NULL );
    hop *rh = create_hop( h->datapath_id, h->out_port, h->in_port, vlan_actions );
    if ( vlan_actions ) {
      delete_actions( vlan_actions );
      vlan_actions = NULL;
    }
    assert( rh != NULL );
    rhops = insert_before_dlist( rhops, rh );
    hops = hops->next;
  }
  while ( rhops != NULL && rhops->data != NULL ) {
    append_hop_to_path( rpath, ( hop * ) rhops->data );
    rhops = rhops->next;
  }
  bool ret = setup_path( rpath, handle_setup, params, NULL, NULL );
  if ( ret != true ) {
    error( "Failed to set up reverse path." );
    output_packet( params->packet, params->out_datapath_id, params->out_port_no, params->out_vid );
    free_buffer( params->packet );
    xfree( params );
  }

  delete_path( rpath );
  delete_dlist( rhops );
}
示例#3
0
static void
handle_setup( int status, const path *p, void *user_data ) {
  UNUSED(p);
  assert(user_data);

  packet_out_params *params = user_data;
  if ( status != SETUP_SUCCEEDED ) {
    error( "Failed to set up path ( status = %d ).", status );
  }
  output_packet( params->packet, params->out_datapath_id, params->out_port_no, params->out_vid );
  free_buffer( params->packet );
  xfree( params );
}
EXPORT int start_bitstream (void *actx)
{
	int ret = 0;
	struct lsInput* input = NULL;
	struct liveStream *ctx = (struct liveStream *)actx;
	AVPacket packet;

	while(1)
	{
		input = get_best_input(ctx);
		if(!input)
		{
			ret = 0;
			break;
		}

		ret = get_input_packet(input,&packet);
		if (ret == AVERROR(EAGAIN))
		{
			continue;
		}
		else if (ret == AVERROR_EOF)
		{
			output_packet(input,NULL);
			input->eof_reached = 1;
			continue;
		}
		if(ret < 0)
		{
			av_log(NULL,AV_LOG_ERROR,"No Input packet %x\n",ret);
			break;
		}
		ret = av_interleaved_write_frame(ctx->oc, &packet);
	}
	return ret;
}
EXPORT int start_capture(void *actx)
{
	struct liveStream *ctx = (struct liveStream *)actx;
	int got_frame;
	int ret;
	AVPacket packet;
	AVFormatContext *ic;
	long long start_time;
	struct lsInput* input = NULL;
	AVRational av_time_base_q = {1, AV_TIME_BASE};

	if(!ctx)
	{
		ret = -1;
		goto end;
	}


	while(1)
	{
		AVCodecContext *dec_ctx = NULL;
		input = get_best_input(ctx);
		if(!input)
		{
			continue;
		}
		dec_ctx = input->dec_ctx;
		ic = input->ic;
		if (ic->start_time != AV_NOPTS_VALUE)
			start_time = ic->start_time;

		ret = get_input_packet(input,&packet);
		if (ret == AVERROR(EAGAIN))
		{
			continue;
		}
		else if (ret == AVERROR_EOF)
		{
			output_packet(input,NULL);
			input->eof_reached = 1;
			continue;
		}
		if(ret < 0)
		{
			av_log(NULL,AV_LOG_ERROR,"No Input packet %x\n",ret);
			break;
		}
		if(input->id != 1)
		{
			if (packet.pts != AV_NOPTS_VALUE)
			{
				packet.pts -= av_rescale_q(start_time, av_time_base_q, ic->streams[0]->time_base);
			}

			if (packet.dts != AV_NOPTS_VALUE)
				packet.dts -= av_rescale_q(start_time, av_time_base_q, ic->streams[0]->time_base);
		}
		if(packet.stream_index == 0)
		{
			ret = avcodec_decode_video2(dec_ctx, input->InFrame, &got_frame, &packet);
			if (ret < 0)
			{
				av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");
				goto end;
			}
			if(!got_frame)
				continue;
		}
		else
		{
			continue;
		}


		av_free_packet(&packet);
		input->InFrame->pts = av_frame_get_best_effort_timestamp(input->InFrame);

		take_filter_lock(&ctx->filter_lock);
		if (av_buffersrc_add_frame_flags(input->in_filter, input->InFrame, AV_BUFFERSRC_FLAG_PUSH) < 0)
		{
			av_log(NULL, AV_LOG_ERROR,
					"Error while feeding the filtergraph\n");
		}
		give_filter_lock(&ctx->filter_lock);
		reap_filter(ctx);

	}
	av_frame_unref(input->InFrame);
end:
	return ret;
}
/*
 * Return
 * - 0 -- one packet was read and processed
 * - AVERROR(EAGAIN) -- no packets were available for selected file,
 *   this function should be called again
 * - AVERROR_EOF -- this function should not be called again
 */
static int process_input(int file_index)
{
    InputFile *ifile = input_files[file_index];
    AVFormatContext *is;
    InputStream *ist;
    AVPacket pkt;
    int ret, i, j;

    is  = ifile->ctx;
    // ****<<  Capture a frame: audio/video/subtitle
    ret = get_input_packet(ifile, &pkt); // ****<<  Call stack: av_read_frame() --> read_frame_internal() --> ff_read_packet() --> s->iformat->read_packet(s, pkt); --> dshow_read_frame();

    if (ret == AVERROR(EAGAIN)) {
        ifile->eagain = 1;
        return ret;
    }
    if (ret < 0) {
        if (ret != AVERROR_EOF) {
            print_error(is->filename, ret);
            if (exit_on_error)
                exit_program(1);
        }
        ifile->eof_reached = 1;

        for (i = 0; i < ifile->nb_streams; i++) {
            ist = input_streams[ifile->ist_index + i];
            if (ist->decoding_needed)
                output_packet(ist, NULL);

            /* mark all outputs that don't go through lavfi as finished */
            for (j = 0; j < nb_output_streams; j++) {
                OutputStream *ost = output_streams[j];

                if (ost->source_index == ifile->ist_index + i &&
                        (ost->stream_copy || ost->enc->type == AVMEDIA_TYPE_SUBTITLE))
                    close_output_stream(ost);
            }
        }

        return AVERROR(EAGAIN);
    }

    reset_eagain();

    if (do_pkt_dump) {
        av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump,
                         is->streams[pkt.stream_index]);
    }
    /* the following test is needed in case new streams appear
       dynamically in stream : we ignore them */
    if (pkt.stream_index >= ifile->nb_streams) {
        report_new_stream(file_index, &pkt);
        goto discard_packet;
    }

    ist = input_streams[ifile->ist_index + pkt.stream_index];

    ist->data_size += pkt.size;
    ist->nb_packets++;

    if (ist->discard)
        goto discard_packet;

    if (debug_ts) {
        av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d type:%s "
               "next_dts:%s next_dts_time:%s next_pts:%s next_pts_time:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
               ifile->ist_index + pkt.stream_index, av_get_media_type_string(ist->dec_ctx->codec_type),
               av_ts2str(ist->next_dts), av_ts2timestr(ist->next_dts, &AV_TIME_BASE_Q),
               av_ts2str(ist->next_pts), av_ts2timestr(ist->next_pts, &AV_TIME_BASE_Q),
               av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ist->st->time_base),
               av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ist->st->time_base),
               av_ts2str(input_files[ist->file_index]->ts_offset),
               av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q));
    }

    if(!ist->wrap_correction_done && is->start_time != AV_NOPTS_VALUE && ist->st->pts_wrap_bits < 64) {
        int64_t stime, stime2;
        // Correcting starttime based on the enabled streams
        // FIXME this ideally should be done before the first use of starttime but we do not know which are the enabled streams at that point.
        //       so we instead do it here as part of discontinuity handling
        if (   ist->next_dts == AV_NOPTS_VALUE
                && ifile->ts_offset == -is->start_time
                && (is->iformat->flags & AVFMT_TS_DISCONT)) {
            int64_t new_start_time = INT64_MAX;
            for (i=0; i<is->nb_streams; i++) {
                AVStream *st = is->streams[i];
                if(st->discard == AVDISCARD_ALL || st->start_time == AV_NOPTS_VALUE)
                    continue;
                new_start_time = FFMIN(new_start_time, av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q));
            }
            if (new_start_time > is->start_time) {
                av_log(is, AV_LOG_VERBOSE, "Correcting start time by %"PRId64"\n", new_start_time - is->start_time);
                ifile->ts_offset = -new_start_time;
            }
        }

        stime = av_rescale_q(is->start_time, AV_TIME_BASE_Q, ist->st->time_base);
        stime2= stime + (1ULL<<ist->st->pts_wrap_bits);
        ist->wrap_correction_done = 1;

        if(stime2 > stime && pkt.dts != AV_NOPTS_VALUE && pkt.dts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
            pkt.dts -= 1ULL<<ist->st->pts_wrap_bits;
            ist->wrap_correction_done = 0;
        }
        if(stime2 > stime && pkt.pts != AV_NOPTS_VALUE && pkt.pts > stime + (1LL<<(ist->st->pts_wrap_bits-1))) {
            pkt.pts -= 1ULL<<ist->st->pts_wrap_bits;
            ist->wrap_correction_done = 0;
        }
    }

    /* add the stream-global side data to the first packet */
    if (ist->nb_packets == 1)
        if (ist->st->nb_side_data)
            av_packet_split_side_data(&pkt);
    for (i = 0; i < ist->st->nb_side_data; i++) {
        AVPacketSideData *src_sd = &ist->st->side_data[i];
        uint8_t *dst_data;

        if (av_packet_get_side_data(&pkt, src_sd->type, NULL))
            continue;

        dst_data = av_packet_new_side_data(&pkt, src_sd->type, src_sd->size);
        if (!dst_data)
            exit_program(1);

        memcpy(dst_data, src_sd->data, src_sd->size);
    }

    // ****<<  pkt.pts:   采集时间戳(以毫秒为单位; 一般为系统时间)
    // ****<<  ts_offset: 起始时间戳(以毫秒为单位; 一般为第一帧采集时间戳的相反数)
    // ****<<  最终.pts:  相对时间戳(以毫秒为单位; 从第一帧到现在的相对时间)
    if (pkt.dts != AV_NOPTS_VALUE)
        pkt.dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
    if (pkt.pts != AV_NOPTS_VALUE)
        pkt.pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);

    if (pkt.pts != AV_NOPTS_VALUE)
        pkt.pts *= ist->ts_scale;
    if (pkt.dts != AV_NOPTS_VALUE)
        pkt.dts *= ist->ts_scale;

    if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts == AV_NOPTS_VALUE && !copy_ts
            && (is->iformat->flags & AVFMT_TS_DISCONT) && ifile->last_ts != AV_NOPTS_VALUE) {
        int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
        int64_t delta   = pkt_dts - ifile->last_ts;
        if(delta < -1LL*dts_delta_threshold*AV_TIME_BASE ||
                (delta > 1LL*dts_delta_threshold*AV_TIME_BASE &&
                 ist->dec_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE)) {
            ifile->ts_offset -= delta;
            av_log(NULL, AV_LOG_DEBUG,
                   "Inter stream timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
                   delta, ifile->ts_offset);
            pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
            if (pkt.pts != AV_NOPTS_VALUE)
                pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
        }
    }

    if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE &&
            !copy_ts) {
        int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
        int64_t delta   = pkt_dts - ist->next_dts;
        if (is->iformat->flags & AVFMT_TS_DISCONT) {
            if (delta < -1LL*dts_delta_threshold*AV_TIME_BASE ||
                    (delta > 1LL*dts_delta_threshold*AV_TIME_BASE &&
                     ist->dec_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE) ||
                    pkt_dts + AV_TIME_BASE/10 < FFMAX(ist->pts, ist->dts)) {
                ifile->ts_offset -= delta;
                av_log(NULL, AV_LOG_DEBUG,
                       "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
                       delta, ifile->ts_offset);
                pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
                if (pkt.pts != AV_NOPTS_VALUE)
                    pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
            }
        } else {
            if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE ||
                    (delta > 1LL*dts_error_threshold*AV_TIME_BASE && ist->dec_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE)) {
                av_log(NULL, AV_LOG_WARNING, "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n", pkt.dts, ist->next_dts, pkt.stream_index);
                pkt.dts = AV_NOPTS_VALUE;
            }
            if (pkt.pts != AV_NOPTS_VALUE) {
                int64_t pkt_pts = av_rescale_q(pkt.pts, ist->st->time_base, AV_TIME_BASE_Q);
                delta   = pkt_pts - ist->next_dts;
                if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE ||
                        (delta > 1LL*dts_error_threshold*AV_TIME_BASE && ist->dec_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE)) {
                    av_log(NULL, AV_LOG_WARNING, "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n", pkt.pts, ist->next_dts, pkt.stream_index);
                    pkt.pts = AV_NOPTS_VALUE;
                }
            }
        }
    }

    if (pkt.dts != AV_NOPTS_VALUE)
        ifile->last_ts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);

    if (debug_ts) {
        av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
               ifile->ist_index + pkt.stream_index, av_get_media_type_string(ist->dec_ctx->codec_type),
               av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ist->st->time_base),
               av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ist->st->time_base),
               av_ts2str(input_files[ist->file_index]->ts_offset),
               av_ts2timestr(input_files[ist->file_index]->ts_offset, &AV_TIME_BASE_Q));
    }

    sub2video_heartbeat(ist, pkt.pts);

    ret = output_packet(ist, &pkt); // ****<<  see output_packet.c
    if (ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d: %s\n",
               ist->file_index, ist->st->index, av_err2str(ret));
        if (exit_on_error)
            exit_program(1);
    }

discard_packet:
    av_free_packet(&pkt);

    return 0;
}
示例#7
0
static void
output_packet_from_last_switch( const pathresolver_hop *last_hop, buffer *packet ) {
  output_packet( packet, last_hop->dpid, last_hop->out_port_no );
}
示例#8
0
/*
 * Converts MIDI commands to USB MIDI packets.
 */
static void snd_usbmidi_transmit_byte(usbmidi_out_port_t* port,
				      uint8_t b, struct urb* urb)
{
	uint8_t p0 = port->cable;

	if (b >= 0xf8) {
		output_packet(urb, p0 | 0x0f, b, 0, 0);
	} else if (b >= 0xf0) {
		switch (b) {
		case 0xf0:
			port->data[0] = b;
			port->state = STATE_SYSEX_1;
			break;
		case 0xf1:
		case 0xf3:
			port->data[0] = b;
			port->state = STATE_1PARAM;
			break;
		case 0xf2:
			port->data[0] = b;
			port->state = STATE_2PARAM_1;
			break;
		case 0xf4:
		case 0xf5:
			port->state = STATE_UNKNOWN;
			break;
		case 0xf6:
			output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
			port->state = STATE_UNKNOWN;
			break;
		case 0xf7:
			switch (port->state) {
			case STATE_SYSEX_0:
				output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
				break;
			case STATE_SYSEX_1:
				output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0);
				break;
			case STATE_SYSEX_2:
				output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7);
				break;
			}
			port->state = STATE_UNKNOWN;
			break;
		}
	} else if (b >= 0x80) {
		port->data[0] = b;
		if (b >= 0xc0 && b <= 0xdf)
			port->state = STATE_1PARAM;
		else
			port->state = STATE_2PARAM_1;
	} else { /* b < 0x80 */
		switch (port->state) {
		case STATE_1PARAM:
			if (port->data[0] < 0xf0) {
				p0 |= port->data[0] >> 4;
			} else {
				p0 |= 0x02;
				port->state = STATE_UNKNOWN;
			}
			output_packet(urb, p0, port->data[0], b, 0);
			break;
		case STATE_2PARAM_1:
			port->data[1] = b;
			port->state = STATE_2PARAM_2;
			break;
		case STATE_2PARAM_2:
			if (port->data[0] < 0xf0) {
				p0 |= port->data[0] >> 4;
				port->state = STATE_2PARAM_1;
			} else {
示例#9
0
文件: midirender.c 项目: bkero/hdjmod
/*
 * Converts MIDI commands to USB MIDI packets.
 */
static void snd_hdjmidi_transmit_byte(struct hdjmidi_out_port* port,
				      uint8_t b, struct urb* urb,
				      struct snd_hdjmidi_out_endpoint* ep)
{
	int midi_channel;
	void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
		port->ep->umidi->usb_protocol_ops->output_packet;
	
	if (b >= 0xf8) {
		output_packet(urb, b, 0, 0, 1);
	} else if (b >= 0xf0) {
		switch (b) {
		case 0xf0:
			port->data[0] = b;
			port->state = STATE_SYSEX_1;
			break;
		case 0xf1:
		case 0xf3:
			port->data[0] = b;
			port->state = STATE_1PARAM;
			break;
		case 0xf2:
			port->data[0] = b;
			port->state = STATE_2PARAM_1;
			break;
		case 0xf4:
		case 0xf5:
			port->state = STATE_UNKNOWN;
			break;
		case 0xf6:
			output_packet(urb, 0xf6, 0, 0, 1);
			port->state = STATE_UNKNOWN;
			break;
		case 0xf7:
			switch (port->state) {
			case STATE_SYSEX_0:
				output_packet(urb, 0xf7, 0, 0, 1);
				break;
			case STATE_SYSEX_1:
				output_packet(urb, port->data[0], 0xf7, 0, 2);
				break;
			case STATE_SYSEX_2:
				output_packet(urb, port->data[0], port->data[1], 0xf7, 3);
				break;
			}
			port->state = STATE_UNKNOWN;
			break;
		}
	} else if (b >= 0x80) {
		port->data[0] = b;
		midi_channel = atomic_read(&ep->umidi->channel);
		/* Put in MIDI channel for the case of the devices with MIDI channel non-volatile storage:
		 *  The firmware expects the same channel which it has been submitted to it during
		 *  MIDI initialization (through the correct vendor request).  If we have failed to 
		 *  set the MIDI channel in the device for some reason, then we do nothing.  
		 */
		if (midi_channel!=MIDI_INVALID_CHANNEL &&
		   ep->umidi->chip->caps.non_volatile_channel==1) {
			port->data[0] &= 0xf0;
			port->data[0] |= midi_channel&0xf;
		}
		if (b >= 0xc0 && b <= 0xdf)
			port->state = STATE_1PARAM;
		else
			port->state = STATE_2PARAM_1;
	} else { /* b < 0x80 */
		switch (port->state) {
		case STATE_1PARAM:
			if (port->data[0] >= 0xf0) {
				port->state = STATE_UNKNOWN;
			}
			output_packet(urb, port->data[0], b, 0, 2);
			break;
		case STATE_2PARAM_1:
			port->data[1] = b;
			port->state = STATE_2PARAM_2;
			break;
		case STATE_2PARAM_2:
			if (port->data[0] < 0xf0) {
				port->state = STATE_2PARAM_1;
			} else {
				port->state = STATE_UNKNOWN;
			}
			output_packet(urb, port->data[0], port->data[1], b, 3);
			break;
		case STATE_SYSEX_0:
			port->data[0] = b;
			port->state = STATE_SYSEX_1;
			break;
		case STATE_SYSEX_1:
			port->data[1] = b;
			port->state = STATE_SYSEX_2;
			break;
		case STATE_SYSEX_2:
			output_packet(urb, port->data[0], port->data[1], b, 3);
			port->state = STATE_SYSEX_0;
			break;
		}
	}
}
示例#10
0
void main_loop(pcap_t *handler)
{
    int res;
    struct pcap_pkthdr *header;
    const u_char *pkt_data;
    int state;
    u_char packet[0x40];
    u_char clientmac[6];
    //int sockfd = mksocket();
    u_char username[20];

    int sockfd;
    struct sockaddr_in address;
    FILE *logfile;

    if ((logfile = fopen(LogFileName, "w")) == NULL) {
        fprintf(stderr, "Could not create logfile\n");
        logfile = stderr;
    }

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("59.77.33.124");
    address.sin_port = htons(65001);

    username[19] = '\0';

    state = LISTENING;

    while(1) {
        if(state == LISTENING) {
            fprintf(stderr,"#");
            if((res = pcap_next_ex(handler, &header, &pkt_data)) > 0) {
                if(pkt_data[0x0f] == 0x01) {
                    fprintf(stderr,"Start packet got\n");
                    memcpy(clientmac, pkt_data + 6, 6);
                } else {
                    if(pkt_data[0x0f] == 0xbf) {
                        fprintf(stderr, ".");
                    } else if(pkt_data[0x0f] == 0xc0 && pkt_data[0x27] == 0x53) {
                        memcpy(username, packet+0x27, 19);
                        fprintf(stdout,"%s\n",username);
                        username[19] = '\n';
                        sendto(sockfd, username, sizeof(username), 0, (struct sockaddr *)&address, sizeof(address));
                    } else {
                        fprintf(stderr, "Not a start packet, ignore\n");
                    }
                    print2mac(pkt_data);
                    continue;
                }
            } else {
                if(res == 0) { /* time out */
                    fprintf(stderr,".");
                    continue;
                } else {
                    fprintf(stderr, "packet receive error\n");
                }
            }
        } else if(state == STARTGOT) {
            memset(packet,0,sizeof(packet));
            make_idreq(clientmac, ServerMac, packet);
            if (pcap_sendpacket(handler, packet, sizeof(packet) /* size */) != 0) {
                fprintf(stderr, "Error sending idreq\n");
                continue;
            }
        } else if(state == REQ1SENT) {
            if((res = pcap_next_ex(handler, &header, &pkt_data)) > 0) {
                if(memcmp(pkt_data+6 ,clientmac,sizeof(clientmac)) != 0) {
                    fprintf(stderr, "Not this client\n");
                    continue;
                }
                if(pkt_data[0x12] == 0x02 && pkt_data[0x16] == 0x01) {
                    fprintf(stderr, "id confirmed\n");
                    getusername( pkt_data,username);
                    fprintf(logfile,"%s\n",username);
                    fprintf(stderr,"%s\n",username);
                    username[19] = '\n';
                    sendto(sockfd, username, sizeof(username), 0, (struct sockaddr *)&address, sizeof(address));
                } else {
                    fprintf(stderr, "not id confirm\n");
                    continue;
                }
            } else {
                if(res == 0) { /* time out */
                    fprintf(stderr,".");
                    state = 0;
                    continue;
                } else {
                    fprintf(stderr, "packet receive error\n");
                }
            }
        } else if(state == USERGOT) {
            memset(packet,0,sizeof(packet));
            make_md5req(clientmac, ServerMac, packet);
            if (pcap_sendpacket(handler, packet, sizeof(packet) /* size */) != 0) {
                fprintf(stderr, "Error sending md5req\n");
                continue;
            }
        } else if(state == MD5CHALLENGESENT) {
            if((res = pcap_next_ex(handler, &header, &pkt_data)) > 0) {
                if(memcmp(pkt_data+6 ,clientmac,sizeof(clientmac)) != 0) {
                    fprintf(stderr, "Not this client\n");
                    continue;
                }
                if(pkt_data[0x12] == 0x02 && pkt_data[0x16] == 0x04) {
                    fprintf(stderr, "md5 confirmed\n");
                    output_packet(pkt_data, 0x18, 0x28, logfile);
                    output_packet(pkt_data, 0x18, 0x28, stderr);
                } else {
                    continue;
                }
            } else {
                if(res == 0) { /* time out */
                    fprintf(stderr,".");
                    state = 0;
                    continue;
                } else {
                    fprintf(stderr, "packet receive error\n");
                }
            }
        } else if(state == MD5GOT) {
            memset(packet,0,sizeof(packet));
            make_success(clientmac, ServerMac, packet);
            if (pcap_sendpacket(handler, packet, sizeof(packet) /* size */) != 0) {
                fprintf(stderr, "Error sending success\n");
                continue;
            }
            fprintf(stderr, "Success\n");
        }
        state = (state + 1) % 6;

    }

}
示例#11
0
文件: main.c 项目: ivaano/massdns
void massdns_handle_packet(ldns_pkt *packet, struct sockaddr_storage ns, void *ctx)
{
    if (!packet || ldns_pkt_qdcount(packet) != 1)
    {
        return;
    }
    struct timeval now;
    gettimeofday(&now, NULL);
    lookup_context_t *context = (lookup_context_t *) ctx;
    ldns_pkt_rcode response_code = ldns_pkt_get_rcode(packet);
    ldns_rr_list l = ldns_pkt_question(packet)[0];
    ldns_rr *question = ldns_rr_list_rr(&l, 0);
    ldns_rdf* owner = ldns_rr_owner(question);
    char* name = ldns_rdf2str(owner);
    size_t name_len = strlen(name);
    if(name_len > 0 && name[name_len - 1] == '.')
    {
        name[name_len - 1] = 0;
    }
    lookup_t *lookup = hashmapGet(context->map, name);
    free(name);
    if (lookup == NULL)
    {
        return;
    }
    if (response_code == LDNS_RCODE_NOERROR || response_code == LDNS_RCODE_NXDOMAIN ||
        lookup->tries == context->cmd_args.resolve_count)
    {
        switch (response_code)
        {
            case LDNS_RCODE_NOERROR:
                stats.noerr++;
                break;
            case LDNS_RCODE_FORMERR:
                stats.formerr++;
                break;
            case LDNS_RCODE_SERVFAIL:
                stats.servfail++;
                break;
            case LDNS_RCODE_NXDOMAIN:
                stats.nxdomain++;
                break;
            case LDNS_RCODE_NOTIMPL:
                stats.notimp++;
                break;
            case LDNS_RCODE_REFUSED:
                stats.refused++;
                break;
            default:
                stats.other++;
                break;
        }
        context->current_rate++;
        ldns_buffer *buf = ldns_buffer_new(LDNS_MAX_PACKETLEN);
        if(buf == NULL)
        {
            abort();
        }
        if(LDNS_STATUS_OK != output_packet(buf, packet, ns, context))
        {
            abort();
        }
        char* packetstr = ldns_buffer_export2str(buf);
        if(packetstr == NULL)
        {
            abort();
        }
        fprintf(stdout, "%s", packetstr);
        free(packetstr);
        if (timediff(&now, &context->next_update) <= 0)
        {
            print_stats(context);
        }
        ldns_buffer_free(buf);
        hashmapRemove(context->map, lookup->domain);
        free(lookup->domain);
        free(lookup);
    }
}
示例#12
0
文件: switch.c 项目: iqm/apps
static void
output_packet_from_last_switch( dlist_element *hops, const buffer *packet ) {
  dlist_element *e = get_last_element( hops );
  path_resolver_hop *last_hop = e->data;
  output_packet( packet, last_hop->dpid, last_hop->out_port_no );
}