示例#1
0
static void
recv_from_secure_channel( int fd, void *user_data ) {
  UNUSED( fd );
  UNUSED( user_data );

  // all queued messages should be processed before receiving new messages from remote
  if ( recv_queue->length > 0 ) {
    return;
  }

  if ( fragment_buf == NULL ) {
    fragment_buf = alloc_buffer_with_length( RECEIVE_BUFFFER_SIZE );
  }

  size_t remaining_length = RECEIVE_BUFFFER_SIZE - fragment_buf->length;
  char *recv_buf = ( char * ) fragment_buf->data + fragment_buf->length;
  ssize_t recv_length = read( connection.fd, recv_buf, remaining_length );
  if ( recv_length < 0 ) {
    if ( errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK ) {
      return;
    }
    error( "Receive error ( errno = %s [%d] ).", strerror( errno ), errno );
    return;
  }
  if ( recv_length == 0 ) {
    debug( "Connection closed by peer." );
    disconnected();
    reconnect( NULL );
    return;
  }
  fragment_buf->length += ( size_t ) recv_length;

  size_t read_total = 0;
  while ( fragment_buf->length >= sizeof( struct ofp_header ) ) {
    struct ofp_header *header = fragment_buf->data;
    uint16_t message_length = ntohs( header->length );
    if ( message_length > fragment_buf->length ) {
      break;
    }
    buffer *message = alloc_buffer_with_length( message_length );
    char *p = append_back_buffer( message, message_length );
    memcpy( p, fragment_buf->data, message_length );
    remove_front_buffer( fragment_buf, message_length );
    enqueue_message( recv_queue, message );
    read_total += message_length;
  }

  // remove headroom manually for next call
  if ( read_total > 0 ) {
    memmove( ( char * ) fragment_buf->data - read_total, fragment_buf->data, fragment_buf->length );
    fragment_buf->data = ( char * ) fragment_buf->data - read_total;
  }

  while ( recv_message_from_secure_channel() == true );
}
示例#2
0
buffer*
create_event_forward_operation_reply( enum efi_event_type type, enum efi_result result, list_element *service_list ) {
  buffer *buf = alloc_buffer_with_length( sizeof( event_forward_operation_reply ) + MESSENGER_SERVICE_NAME_LENGTH );
  event_forward_operation_reply *rep = append_back_buffer( buf, sizeof( event_forward_operation_reply ) );
  memset( rep, 0, sizeof( event_forward_operation_reply ) );

  rep->type = ( uint8_t ) type;
  rep->result = ( uint8_t ) result;

  size_t reply_head_offset = sizeof( event_forward_operation_reply );
  uint32_t n_services = 0;
  if ( service_list != NULL ) {
    for ( list_element *e = service_list; e != NULL; e = e->next ) {
      const size_t len = strlen( e->data );
      char *dst = append_back_buffer( buf, len + 1 );
      rep = ( event_forward_operation_reply * )( dst - reply_head_offset );
      reply_head_offset += len + 1;
      strcpy( dst, e->data );
      dst[ len ] = '\0';
      ++n_services;
    }
  }

  rep->n_services = htonl( n_services );
  return buf;
}
示例#3
0
static buffer *
create_openflow_application_message( uint64_t *datapath_id, buffer *data ) {
  openflow_service_header_t *message;
  buffer *buf;
  void *append;
  size_t append_len = 0;

  if ( data != NULL ) {
     append_len = data->length;
  }
  buf = alloc_buffer_with_length( sizeof( openflow_service_header_t ) + append_len );
  message = append_back_buffer( buf, sizeof( openflow_service_header_t ) );
  if ( datapath_id == NULL ) {
    message->datapath_id = ~0U; // FIXME: defined invalid datapath_id
  }
  else {
    message->datapath_id = htonll( *datapath_id );
  }
  message->service_name_length = htons( 0 );
  // TODO: append ipaddress and port
  if ( append_len > 0 ) {
    append = append_back_buffer( buf, append_len );
    memcpy( append, data->data, append_len );
  }

  return buf;
}
示例#4
0
static void
test_packet_type_ether() {
  buffer *buf = alloc_buffer_with_length( sizeof( struct iphdr ) );
  calloc_packet_info( buf );

  assert_false( packet_type_ether( buf ) );

  packet_info *packet_info = buf->user_data;
  packet_info->format |= ETH_DIX;
  assert_true( packet_type_ether( buf ) );
  packet_info->format = 0;

  packet_info->format |= ETH_8023_RAW;
  assert_true( packet_type_ether( buf ) );
  packet_info->format = 0;

  packet_info->format |= ETH_8023_LLC;
  assert_true( packet_type_ether( buf ) );
  packet_info->format = 0;

  packet_info->format |= ETH_8023_SNAP;
  assert_true( packet_type_ether( buf ) );
  packet_info->format = 0;

  free_buffer( buf );
}
示例#5
0
static void
handle_delete_filter_request( const messenger_context_handle *handle, delete_packetin_filter_request *request ) {
  assert( handle != NULL );
  assert( request != NULL );

  buffer *buf = alloc_buffer_with_length( sizeof( delete_packetin_filter_reply ) );
  delete_packetin_filter_reply *reply = append_back_buffer( buf, sizeof( delete_packetin_filter_reply ) );
  reply->status = PACKETIN_FILTER_OPERATION_SUCCEEDED;
  reply->n_deleted = 0;

  struct ofp_match match;
  ntoh_match( &match, &request->criteria.match );
  uint16_t priority = ntohs( request->criteria.priority );
  if ( request->flags & PACKETIN_FILTER_FLAG_MATCH_STRICT ) {
    int n_deleted = delete_packetin_match_entry( match, priority, request->criteria.service_name );
    reply->n_deleted += ( uint32_t ) n_deleted;
  }
  else {
    map_match_table( match, delete_filter_walker, buf );
  }
  reply->n_deleted = htonl( reply->n_deleted );

  bool ret = send_reply_message( handle, MESSENGER_DELETE_PACKETIN_FILTER_REPLY, buf->data, buf->length );
  free_buffer( buf );
  if ( ret == false ) {
    error( "Failed to send a dump filter reply." );
  }
}
示例#6
0
buffer *
create_pcap_packet( void* pcap_header, size_t pcap_len, void* dump_header, size_t dump_len, void* data, size_t data_len ) {
  size_t length = pcap_len + dump_len + data_len;
  assert( length != 0 );
  assert( pcap_header != NULL && dump_header != NULL );

  buffer *buf = alloc_buffer_with_length( length );
  assert( buf != NULL );

  if ( pcap_header != NULL && pcap_len > 0 ) {
    void *d = append_back_buffer( buf, pcap_len );
    assert( d != NULL );
    memcpy( d, pcap_header, pcap_len );
  }

  if ( dump_header != NULL && dump_len > 0 ) {
    void *d = append_back_buffer( buf, dump_len );
    assert( d != NULL );
    memcpy( d, dump_header, dump_len );
  }

  if ( data != NULL && data_len > 0 ) {
    void *d = append_back_buffer( buf, data_len );
    assert( d != NULL );
    memcpy( d, data, data_len );
  }

  return buf;
}
示例#7
0
static void
recv_syslog_message( int fd, void *data ) {
  UNUSED( data );

  char buf[ 1024 ];
  ssize_t ret = read( fd, buf, sizeof( buf ) );

  if ( ret < 0 ) {
    if ( errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK ) {
      return;
    }

    error( "Receive error ( errno = %s [%d] ).", strerror( errno ), errno );

    set_readable( fd, false );
    delete_fd_handler( fd );
    return;
  }

  buffer *message = alloc_buffer_with_length( ( size_t ) ret );
  void *p = append_back_buffer( message, ( size_t ) ret );
  memcpy( p, buf, ( size_t ) ret );

  relay_syslog_message( message );

  free_buffer( message );
}
示例#8
0
static void
read_stdin( int fd, void *data ) {
  UNUSED( data );

  char buf[ 1024 ];
  memset( buf, '\0', sizeof( buf ) );
  ssize_t ret = read( fd, buf, sizeof( buf ) );
  if ( ret < 0 ) {
    if ( errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK ) {
      return;
    }

    set_readable( fd, false );

    error( "Read error ( errno = %s [%d] ).", strerror( errno ), errno );
    return;
  }
  else if ( ret == 0 ) {
    return;
  }

  if ( stdin_read_buffer == NULL ) {
    set_readable( fd, false );

    error( "Read buffer is not allocated yet" );
    return;
  }

  char *p = append_back_buffer( stdin_read_buffer, ( size_t ) ret );
  memcpy( p, buf, ( size_t ) ret );
  char *lf = find_character( stdin_read_buffer->data, stdin_read_buffer->length, '\n' );
  while ( lf != NULL ) {
    size_t length = ( size_t ) ( lf - ( char * ) stdin_read_buffer->data );
    if ( length > 0 ) {
      buffer *string = alloc_buffer_with_length( length );
      p = append_back_buffer( string, length );
      memcpy( p, stdin_read_buffer->data, length );
      relay_string( string );
      free_buffer( string );
      remove_front_buffer( stdin_read_buffer, length + 1 );
    }
    else {
      if ( stdin_read_buffer->length > 0 ) {
        remove_front_buffer( stdin_read_buffer, 1 );
      }
    }
    if ( stdin_read_buffer->length == 0 ) {
      break;
    }
    lf = find_character( stdin_read_buffer->data, stdin_read_buffer->length, '\n' );
  }

  // truncate head room manually
  buffer *truncated = duplicate_buffer( stdin_read_buffer );
  free_buffer( stdin_read_buffer );
  stdin_read_buffer = truncated;
}
示例#9
0
static VALUE
vendor_stats_request_alloc( VALUE klass ) {
  uint16_t length = 128;
  buffer *body = alloc_buffer_with_length( length );
  void *p = append_back_buffer( body, length );
  memset( p, 0xaf, length );
  buffer *vendor_stats_request = create_vendor_stats_request( MY_TRANSACTION_ID, VENDOR_STATS_FLAG, VENDOR_ID, body );
  return Data_Wrap_Struct( klass, NULL, free_buffer, vendor_stats_request );
}
示例#10
0
static void
test_calloc_packet_info_succeeds() {
  buffer *buf = alloc_buffer_with_length( sizeof( struct iphdr ) );

  calloc_packet_info( buf );
  assert_true( buf->user_data != NULL );

  free_buffer( buf );
}
示例#11
0
文件: packet-in.c 项目: amotoki/trema
static VALUE
packet_in_alloc( VALUE klass ) {
  rb_packet_in *_packet_in = xmalloc( sizeof( rb_packet_in ) );
  memset( &_packet_in->packet_in, 0, sizeof( packet_in ) );
  _packet_in->data = alloc_buffer_with_length( 1 );
  parse_packet( _packet_in->data );
  _packet_in->packet_in.data = _packet_in->data;
  return Data_Wrap_Struct( klass, 0, packet_in_free, _packet_in );
}
示例#12
0
文件: error.c 项目: nhst/trema
/*
 * @overload initialize(transaction_id=nil, type=OFPET_HELLO_FAILED, code=OFPHFC_INCOMPATIBLE, user_data=nil)
 *
 * @param [Number] transaction_id
 *   a positive number, not recently attached to any previous pending commands to
 *   guarantee message integrity auto-generated if not specified.
 *
 * @param [Number] type
 *   a command or action that failed. Defaults to +OFPET_HELLO_FAILED+ if 
 *   not specified.
 *
 * @param [Number] code
 *   the reason of the failed type error. Defaults to +OFPHFC_INCOMPATIBLE+ if 
 *   not specified.
 *
 * @param [String] user_data
 *   a more user friendly explanation of the error. Defaults to nil if not 
 *   specified.
 *
 * @example Instantiate with type and code
 *   Error.new(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE)
 *
 * @example Instantiate with transaction_id, type and code.
 *   Error.new(1234, OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR)
 *
 * @example Instantiate with transaction_id, type, code, user_data
 *   Error.new(6789, OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_EMERG_TIMEOUT, "this is a test") 
 *
 * @raise [ArgumentError] if transaction id is negative.
 * @raise [ArgumentError] if user data is not a string.
 *
 * @return [Error] 
 *   an object that encapsulates the +OFPT_ERROR+ openflow message.
 */
static VALUE
error_new( int argc, VALUE *argv, VALUE klass ) {
  buffer *data = NULL;
  uint32_t xid = get_transaction_id();
  VALUE xid_r;
  VALUE user_data;
  VALUE type_r;
  VALUE code_r;
  uint16_t type;
  uint16_t code;

  switch ( argc ) {
    case 2:
      // type, code specified.
      rb_scan_args( argc, argv, "02", &type_r, &code_r );
      type = ( uint16_t ) NUM2UINT( type_r );
      code = ( uint16_t ) NUM2UINT( code_r );
      break;
    case 3:
      // transaction id, type, code specified.
      rb_scan_args( argc, argv, "03", &xid_r, &type_r, &code_r );
      if ( NUM2INT( xid_r ) < 0 ) {
        rb_raise( rb_eArgError, "Transaction ID must be >= 0" );
      }
      xid = ( uint32_t ) NUM2UINT( xid_r );
      type = ( uint16_t ) NUM2UINT( type_r );
      code = ( uint16_t ) NUM2UINT( code_r );
      break;
    case 4:
      rb_scan_args( argc, argv, "04", &xid_r, &type_r, &code_r, &user_data );
      if ( NUM2INT( xid_r ) < 0 ) {
        rb_raise( rb_eArgError, "Transaction ID must be >= 0" );
      }
      if ( rb_obj_is_kind_of( user_data, rb_cString ) == Qfalse ) {
        rb_raise( rb_eArgError, "User data must be a string" );
      }
      xid = ( uint32_t ) NUM2UINT( xid_r );
      type = ( uint16_t ) NUM2UINT( type_r );
      code = ( uint16_t ) NUM2UINT( code_r );
      uint16_t length = ( u_int16_t ) RSTRING_LEN( user_data );
      data = alloc_buffer_with_length( length );
      void *p = append_back_buffer( data, length );
      memcpy( p, RSTRING_PTR( user_data ), length );
      break;
    default:
      type = OFPET_HELLO_FAILED;
      code = OFPHFC_INCOMPATIBLE;
      break;
  }
  buffer *error = create_error( xid, type, code, data );
  if ( data != NULL ) {
    free_buffer( data );
  }
  return Data_Wrap_Struct( klass, NULL, free_buffer, error );
}
示例#13
0
static bool
init_stdin_relay( int *argc, char **argv[] ) {
  parse_options( argc, argv );

  stdin_read_buffer = alloc_buffer_with_length( 1024 );

  set_fd_handler( STDIN_FILENO, read_stdin, NULL, NULL, NULL );
  set_readable( STDIN_FILENO, true );

  return true;
}
示例#14
0
文件: ether_test.c 项目: nhst/trema
void
test_fill_ether_padding_succeeds_if_length_is_less_than_ETH_MINIMUM_LENGTH() {
  buffer *buffer = alloc_buffer_with_length( ( size_t ) ETH_MINIMUM_LENGTH - ETH_FCS_LENGTH - 1 );
  append_back_buffer( buffer, ETH_MINIMUM_LENGTH - ETH_FCS_LENGTH - 1 );

  fill_ether_padding( buffer );

  assert_int_equal ( ( int ) buffer->length, ETH_MINIMUM_LENGTH - ETH_FCS_LENGTH );

  free_buffer( buffer );
}
示例#15
0
文件: switch.c 项目: s-zenke/trema
static void
service_recv( uint16_t message_type, void *data, size_t data_len ) {
  buffer *buf;
  void *msg;

  buf = alloc_buffer_with_length( data_len );

  msg = append_back_buffer( buf, data_len );
  memcpy( msg, data, data_len );

  service_recv_from_application( message_type, buf );
}
示例#16
0
static void
test_packet_type_ipv4() {
  buffer *buf = alloc_buffer_with_length( sizeof( struct iphdr ) );
  calloc_packet_info( buf );

  assert_false( packet_type_ipv4( buf ) );

  packet_info *packet_info = buf->user_data;
  packet_info->format |= NW_IPV4;
  assert_true( packet_type_ipv4( buf ) );

  free_buffer( buf );
}
示例#17
0
文件: ether_test.c 项目: nhst/trema
static buffer *
setup_dummy_ether_packet( size_t length ) {
  buffer *ether_buffer = alloc_buffer_with_length( length );
  alloc_packet( ether_buffer );
  append_back_buffer( ether_buffer, length );
  packet_info( ether_buffer )->l2_data.eth = ether_buffer->data;

  memcpy( ( char * ) packet_info( ether_buffer )->l2_data.eth->macda, macda, ETH_ADDRLEN );
  memcpy( ( char * ) packet_info( ether_buffer )->l2_data.eth->macsa, macsa, ETH_ADDRLEN );
  packet_info( ether_buffer )->l2_data.eth->type = htons( ETH_ETHTYPE_ARP );

  return ether_buffer;
}
buffer *
pack_experimenter_multipart_request( VALUE options ) {
  uint32_t xid = get_transaction_id();
  VALUE r_xid = HASH_REF( options, transaction_id );
  if ( !NIL_P( r_xid ) ) {
    xid = NUM2UINT( r_xid );
  }

  uint16_t flags = 0;
  VALUE r_flags = HASH_REF( options, flags );
  if ( !NIL_P( r_flags ) ) {
    flags = ( uint16_t ) NUM2UINT( r_flags );
  }

  uint32_t experimenter = 0;
  VALUE r_experimenter = HASH_REF( options, experimenter );
  if ( !NIL_P( r_experimenter ) ) {
    experimenter = NUM2UINT( r_experimenter );
  }

  uint32_t exp_type = 0;
  VALUE r_exp_type = HASH_REF( options, exp_type );
  if ( !NIL_P( r_exp_type ) ) {
    exp_type = NUM2UINT( r_exp_type );
  }

  VALUE r_body = HASH_REF( options, user_data );
  buffer *body = NULL;
  if ( !NIL_P( r_body ) ) {
    if ( TYPE( r_body ) == T_ARRAY ) {
      uint16_t buffer_len = ( uint16_t ) RARRAY_LEN( r_body );

      body = alloc_buffer_with_length( ( size_t ) RARRAY_LEN( r_body ) );
      append_back_buffer( body, buffer_len );
      uint8_t *buf = body->data;

        
      for ( int i = 0; i < buffer_len && i < RARRAY_LEN( r_body ); i++ ) {
        buf[ i ]= ( uint8_t ) FIX2INT( rb_ary_entry( r_body , i ) );
      }
    }
    else {
      rb_raise( rb_eTypeError, "experimenter user data must be specified as an array of bytes" );
    }
  }
  buffer *experimenter_multipart_request = create_experimenter_multipart_request( xid, flags, experimenter, exp_type, body );

  return experimenter_multipart_request;
}
buffer *
create_ovs_set_flow_format( const uint32_t transaction_id, const uint32_t format ) {
  size_t length = sizeof( ovs_set_flow_format );
  buffer *message = alloc_buffer_with_length( length );
  ovs_set_flow_format *sff = append_back_buffer( message, length );
  sff->header.version = OFP_VERSION;
  sff->header.type = OFPT_VENDOR;
  sff->header.length = htons( sizeof( ovs_set_flow_format ) );
  sff->header.xid = htonl( transaction_id );
  sff->vendor = htonl( OVS_VENDOR_ID );
  sff->subtype = htonl( OVST_SET_FLOW_FORMAT );
  sff->format = htonl( format );

  return message;
}
示例#20
0
buffer *
get_switches( void ) {
  assert( dpid_table != NULL );

  buffer *buf = alloc_buffer_with_length( sizeof( uint64_t ) );

  hash_iterator iter;
  hash_entry *entry;
  init_hash_iterator( dpid_table, &iter );
  while ( ( entry = iterate_hash_next( &iter ) ) != NULL ) {
    uint64_t *dpid = append_back_buffer( buf, sizeof( uint64_t ) );
    *dpid = htonll( *( uint64_t * )( entry->value ) );
  }

  return buf;
}
buffer *
create_ovs_flow_mod_table_id( const uint32_t transaction_id, const uint8_t set ) {
  uint16_t length = ( uint16_t ) sizeof( ovs_flow_mod_table_id );
  buffer *buf = alloc_buffer_with_length( length );
  ovs_flow_mod_table_id *flow_mod_table_id = append_back_buffer( buf, length );

  flow_mod_table_id->header.version = OFP_VERSION;
  flow_mod_table_id->header.type = OFPT_VENDOR;
  flow_mod_table_id->header.length = htons( length );
  flow_mod_table_id->header.xid = htonl( transaction_id );
  flow_mod_table_id->vendor = ntohl( OVS_VENDOR_ID );
  flow_mod_table_id->subtype = ntohl( OVST_FLOW_MOD_TABLE_ID );
  flow_mod_table_id->set = set;

  return buf;
}
示例#22
0
buffer *
r_array_to_buffer( VALUE r_array ) {
  buffer *data = NULL;

  if ( !NIL_P( r_array ) ) {
    Check_Type( r_array, T_ARRAY );
    uint32_t length = ( uint32_t ) RARRAY_LEN( r_array );

    data = alloc_buffer_with_length( length );
    append_back_buffer( data, length );
    uint8_t *data_ptr = data->data;
    for ( uint32_t i = 0; i < length; i++ ) {
      data_ptr[ i ] = ( uint8_t ) FIX2INT( RARRAY_PTR( r_array ) [ i ] );
    }
  }
  return data;
}
示例#23
0
static buffer *
setup_dummy_ether_packet( size_t length, uint16_t type ) {
  buffer *buf = alloc_buffer_with_length( length );
  alloc_packet( buf );
  append_back_buffer( buf, length );
  packet_info( buf )->l2_data.eth = buf->data;
  packet_info( buf )->ethtype = type;
  ether_header_t *ether = packet_info( buf )->l2_data.eth;
  ether->type = htons( type );

  memcpy( ( char * ) ether->macda, macda, ETH_ADDRLEN );
  memcpy( ( char * ) ether->macsa, macsa, ETH_ADDRLEN );

  packet_info( buf )->l3_data.l3 = ( char * ) packet_info( buf )->l2_data.l2 + sizeof( ether_header_t );
  vlantag_header_t *vtag = ( vlantag_header_t * ) ( ( void * ) ( ether + 1 ) );
  packet_info( buf )->vtag = vtag;

  return buf;
}
示例#24
0
static void
relay_string( buffer *string ) {
  // retrieve current time
  struct timespec now;
  if ( clock_gettime( CLOCK_REALTIME, &now ) == -1 ) {
    error( "Failed to retrieve system-wide real-time clock ( %s [%d] ).", strerror( errno ), errno );
    return;
  }

  // allocate buffer
  char *service_name = xstrdup( get_trema_name() );
  uint16_t service_name_length = ( uint16_t ) ( strlen( service_name ) + 1 );
  size_t buffer_length = sizeof( message_dump_header ) + service_name_length + sizeof( syslog_dump_header ) + string->length + 1;
  buffer *buf = alloc_buffer_with_length( buffer_length );

  // syslog_dump_header + service_name
  message_dump_header *mdh = append_back_buffer( buf, sizeof( message_dump_header ) );
  mdh->sent_time.sec = htonl( ( uint32_t ) now.tv_sec );
  mdh->sent_time.nsec = htonl( ( uint32_t ) now.tv_nsec );
  mdh->app_name_length = htons( 0 );
  mdh->service_name_length = htons( service_name_length );
  mdh->data_length = htonl( ( uint32_t ) ( sizeof( text_dump_header ) + string->length + 1 ) );
  void *svn = append_back_buffer( buf, service_name_length );
  memcpy( svn, service_name, service_name_length );
  xfree( service_name );

  // text_dump_header
  text_dump_header *tdh = append_back_buffer( buf, sizeof( text_dump_header ) );
  tdh->sent_time.sec = htonl( ( uint32_t ) now.tv_sec );
  tdh->sent_time.nsec = htonl( ( uint32_t ) now.tv_nsec );

  // message
  void *p = append_back_buffer( buf, string->length + 1 );
  memset( p, '\0', string->length + 1 );
  memcpy( p, string->data, string->length );

  bool ret = send_message( dump_service_name, MESSENGER_DUMP_TEXT, buf->data, buf->length );
  if ( !ret ) {
    error( "Failed to relay syslog message." );
  }
  free_buffer( buf );
}
示例#25
0
static void
test_packet_type_igmp_v3_membership_report() {
  buffer *buf = alloc_buffer_with_length( sizeof( struct iphdr ) );
  calloc_packet_info( buf );

  assert_false( packet_type_igmp( buf ) );

  packet_info *packet_info = buf->user_data;

  packet_info->format |= NW_IGMP;
  packet_info->igmp_type = IGMP_TYPE_V3_MEMBERSHIP_REPORT;

  assert_false( packet_type_igmp_membership_query( buf ) );
  assert_false( packet_type_igmp_v1_membership_report( buf ) );
  assert_false( packet_type_igmp_v2_membership_report( buf ) );
  assert_false( packet_type_igmp_v2_leave_group( buf ) );
  assert_true( packet_type_igmp_v3_membership_report( buf ) );

  free_buffer( buf );
}
示例#26
0
static void
handle_packet( u_char *args, const struct pcap_pkthdr *header, const u_char *packet ) {
  // allocate buffer
  char *app_name = interface_name;
  uint16_t app_name_length = ( uint16_t ) ( strlen( interface_name ) + 1 );
  char *service_name = xstrdup( get_trema_name() );
  uint16_t service_name_length = ( uint16_t ) ( strlen( service_name ) + 1 );
  size_t buffer_length = sizeof( message_dump_header ) + app_name_length + service_name_length + sizeof( pcap_dump_header ) + sizeof( struct pcap_pkthdr_private ) + header->caplen;
  buffer *buf = alloc_buffer_with_length( buffer_length );

  // message_dump_header + app_name + service_name
  message_dump_header *mdh = append_back_buffer( buf, sizeof( message_dump_header ) );
  mdh->sent_time.sec = htonl( ( uint32_t ) header->ts.tv_sec );
  mdh->sent_time.nsec = htonl( ( uint32_t ) ( header->ts.tv_usec * 1000 ) );
  mdh->app_name_length = htons( app_name_length );
  mdh->service_name_length = htons( service_name_length );
  mdh->data_length = htonl( ( uint32_t ) ( sizeof( pcap_dump_header ) + sizeof( struct pcap_pkthdr_private ) + header->caplen ) );
  void *apn = append_back_buffer( buf, app_name_length );
  memcpy( apn, app_name, app_name_length );
  void *svn = append_back_buffer( buf, service_name_length );
  memcpy( svn, service_name, service_name_length );
  xfree( service_name );

  // pcap_dump_header
  pcap_dump_header *pdh = append_back_buffer( buf, sizeof( pcap_dump_header ) );
  int *dlt = ( int * ) args;
  pdh->datalink = htonl( ( uint32_t ) *dlt );
  strncpy( ( char * ) pdh->interface, interface_name, sizeof( pdh->interface ) );
  pdh->interface[ sizeof( pdh->interface ) - 1 ] = '\0';

  // pcap_pkthdr_private + packet
  struct pcap_pkthdr_private *pph = append_back_buffer( buf, sizeof( struct pcap_pkthdr_private ) );
  pph->ts.tv_sec = ( bpf_int32 ) htonl( ( uint32_t ) header->ts.tv_sec );
  pph->ts.tv_usec = ( bpf_int32 ) htonl( ( uint32_t ) header->ts.tv_usec );
  pph->caplen = htonl( header->caplen );
  pph->len = htonl( header->len );
  void *pkt = append_back_buffer( buf, header->caplen );
  memcpy( pkt, packet, header->caplen );

  enqueue( packet_queue, buf );
}
示例#27
0
static VALUE
pack_experimenter( VALUE self, VALUE actions, VALUE options ) {
  VALUE r_experimenter = HASH_REF( options, experimenter );
  VALUE r_body = Qnil;

  if ( ( r_body = HASH_REF( options, body ) ) != Qnil ) {
    Check_Type( r_body, T_ARRAY );
    uint16_t length = ( uint16_t ) RARRAY_LEN( r_body );
    buffer *body = alloc_buffer_with_length( length );
    void *p = append_back_buffer( body, length );
    for ( int i = 0; i < length; i++ ) {
      ( ( uint8_t * ) p )[ i ] = ( uint8_t ) FIX2INT( RARRAY_PTR( r_body )[ i ] );
    }
    append_action_experimenter( openflow_actions_ptr( actions ), NUM2UINT( r_experimenter ), body );
    free_buffer( body );
  }
  else {
    append_action_experimenter( openflow_actions_ptr( actions ), NUM2UINT( r_experimenter ), NULL );
  }
  return self;
}
示例#28
0
static bool
send_efi_event_config_request( const char *service_name, enum switch_management_command command, enum efi_event_type type, list_element *service_list, event_forward_entry_operation_callback callback, void *user_data ) {
  maybe_init_event_forward_interface();
  if ( service_name == NULL ) {
    return false;
  }
  switch( command ) {
  case EVENT_FORWARD_ENTRY_ADD:
  case EVENT_FORWARD_ENTRY_DELETE:
  case EVENT_FORWARD_ENTRY_DUMP:
  case EVENT_FORWARD_ENTRY_SET:
    // do nothing;
    break;
  default:
    return false;
  }

  buffer *all_req = alloc_buffer_with_length( sizeof( management_application_request ) + sizeof( event_forward_operation_request ) + MESSENGER_SERVICE_NAME_LENGTH );

  management_application_request *apreq = append_back_buffer( all_req, sizeof( management_application_request ) );
  apreq->header.type = htons( MANAGEMENT_APPLICATION_REQUEST );
  apreq->application_id = htonl( command );
  create_event_forward_operation_request( all_req, type, service_list );
  apreq = all_req->data;
  apreq->header.length = htonl( ( uint32_t ) all_req->length );

  struct event_forward_operation_request_param *param = xcalloc( 1, sizeof( struct event_forward_operation_request_param ) );
  param->callback = callback;
  param->user_data = user_data;

  bool sent = send_request_message( service_name, efi_queue_name, MESSENGER_MANAGEMENT_REQUEST,
                                   all_req->data, all_req->length, param );
  if ( !sent ) {
    xfree( param );
  }
  free_buffer( all_req );

  return sent;
}
示例#29
0
int
cc_send_msg_to_switch(sw_info* cc_sw_info, buffer* buf)
{

	size_t packet_len = sizeof(struct ofp_packet_out) + actions_length + buf->length;
	buffer* new_;
	new_ = alloc_buffer_with_length(packet_len);
	
	struct ofp_packet_out* opo;
	opo = (struct ofp_packet_out*)malloc(sizeof(struct ofp_packet_out));
	memset(opo, 0, sizeof(struct ofp_packet_out));
	opo->header.type = OFP_VERSION;
	opo->header.type = OFPT_PACKET_OUT;
	opo->header.length = htons(packet_len);

	/*buffer id should be UINT32_MAX, when i want to send packet which is create by myseld
	*/
	opo->buffer_id = htonl(UINT32_MAX);
	opo->in_port = htons(OFPP_LOCAL);//OFPP_LOCAL is virtual port in switch
	opo->actions_len = htons(actions_len);
	memcpy(opo.actions, acitions, cations_len);
	memcpy((uint8_t *)opo->actions + actions_len, buf->data, buf->length);

	buf->data = (void*)opo;

	buffer* new_buf;
	uint32_t xid;
	xid = cc_generate_xid(cc_sw_info);
	uint32_t buffer_id = htons(UINT32_MAX);// in order to send packet in data
	uint16_t in_port = OFPP_LOCAL;
	openflow_actions* actions;
	actions->n_actions = 1;
	actions->list->data =  recv_from_app;
	
	cc_create_packet_out(xid,const uint32_t buffer_id, in_port, actions, new_buf);
	cc_send_to_secure_channel(cc_sw_info, buf);

	return CC_SUCCESS;
}
示例#30
0
static void
handle_dump_filter_request( const messenger_context_handle *handle, dump_packetin_filter_request *request ) {
  assert( handle != NULL );
  assert( request != NULL );

  buffer *buf = alloc_buffer_with_length( 2048 );
  dump_packetin_filter_reply *reply = append_back_buffer( buf, offsetof( dump_packetin_filter_reply, entries ) );
  reply->status = PACKETIN_FILTER_OPERATION_SUCCEEDED;
  reply->n_entries = 0;

  struct ofp_match match;
  ntoh_match( &match, &request->criteria.match );
  uint16_t priority = ntohs( request->criteria.priority );
  if ( request->flags & PACKETIN_FILTER_FLAG_MATCH_STRICT ) {
    list_element *services = lookup_match_strict_entry( match, priority );
    while ( services != NULL ) {
      if ( strcmp( services->data, request->criteria.service_name ) == 0 ) {
        packetin_filter_entry *entry = append_back_buffer( buf, sizeof( packetin_filter_entry ) );
        reply->n_entries++;
        entry->match = request->criteria.match;
        entry->priority = request->criteria.priority;
        strncpy( entry->service_name, services->data, sizeof( entry->service_name ) );
        entry->service_name[ sizeof( entry->service_name ) - 1 ] = '\0';
      }
      services = services->next;
    }
  }
  else {
    map_match_table( match, dump_filter_walker, buf );
  }
  reply->n_entries = htonl( reply->n_entries );

  bool ret = send_reply_message( handle, MESSENGER_DUMP_PACKETIN_FILTER_REPLY, buf->data, buf->length );
  free_buffer( buf );
  if ( ret == false ) {
    error( "Failed to send a dump packetin filter reply." );
  }
}