int server::client_do_packet(packet &pk)
{
  int rp=pk.get_read_position();

  int er=0;
  while (!pk.eop() && !er)
  {
    uint8_t cmd;
    if (pk.read(&cmd,1)!=1)
      er=1;
    else
    {
      view *f=NULL;
      int fail=0;
      if (cmd!=SCMD_ADD_VIEW && cmd!=SCMD_SYNC)
      {
    uint16_t player;
    if (pk.read((uint8_t *)&player,2)!=2)
      er=1;
    player=lstl(player);
    for (f=player_list; f && f->player_number!=player; f=f->next);
    if (!f) fail=1;
      }
      if (!fail)
      {
    if (!process_command(f,cmd,pk))
      er=1;
      }
      else
    er=1;
    }
  }
  pk.set_read_position(rp);
  return !er;
}
Example #2
0
    void client_impl::on_decode(packet const& p)
    {
        switch(p.get_frame())
        {
        case packet::frame_message:
        {
            socket::ptr so_ptr = get_socket_locked(p.get_nsp());
            if(so_ptr)so_ptr->on_message_packet(p);
            break;
        }
        case packet::frame_open:
            this->on_handshake(p.get_message());
            break;
        case packet::frame_close:
            //FIXME how to deal?
            this->close_impl(close::status::abnormal_close, "End by server");
            break;
        case packet::frame_pong:
            this->on_pong();
            break;

        default:
            break;
        }
    }
string palringoPacket::generate_payload(string password, packet data)
{
	engine.pl("palPacket-> Generating AUTH", 1);

	//use this instead of the RNG
	string payload		= cipher.hexDec(data.getPayload());
	//string rnd		= data.getHeader(1).value.erase(10,1);
	string rnd			= data.search_headers("TIMESTAMP").erase(10,1);

	//create the IV
	char* IV = new char[9];
	for(int i=16; i<24; i++)
	{
		IV[i-16] = payload.c_str()[i];
	}
	IV[8] = '\0';

	//create some final keys for the salsa
	string authKey		= cipher.dbMD5(password, IV);
	//string sessionKey	= cipher.dbMD5(password, rnd);

	//finally create our data block to be hashed by salsa
	//this information get's hashed
	char* dte = new char[33];
	for(int i=0; i<16; i++)
	{
		dte[i]	= payload.c_str()[i];
		dte[i+16] = rnd.c_str()[i];
	}
	dte[32] = '\0';
	//string dataToEncrypt = dte;

	//generate final output for the packet
	//string result = cipher.salsa20enc(IV, authKey, dte);
	//return cipher.salsa20(IV, authKey, dte);
	string result = cipher.salsa20(IV, authKey, dte);

    if(engine.DEBUG)
    {
        cout << "------------------------------------" << endl;
        cout << "IV" << endl << cipher.hexEnc(IV) << endl << endl;
        cout << "AK" << endl << cipher.hexEnc(authKey) << endl << endl;
        cout << "DE" << endl << cipher.hexEnc(dte) << endl << endl;
        cout << "Auth inPacket" << endl << data.getPayload() << endl << endl;
        cout << "Auth outPacket" << endl << cipher.hexEnc(result) << endl;
        cout << "------------------------------------" << endl;
    }

	return result;
}
Example #4
0
void connection::do_byte_calculation(packet p) {
  assert(this->check_packet(p));

  this->byte_total += p.data_size();

  if (this->src_to_dst(p)) {
    this->byte_src_to_dst_num += p.data_size();
    return;
  }
  if (this->dst_to_src(p)) {
    this->byte_dst_to_src_num += p.data_size();
    return;
  }
  assert(false);
}
Example #5
0
 void packet_manager::encode(packet& pack,encode_callback_function const& override_encode_callback) const
 {
     shared_ptr<string> ptr = make_shared<string>();
     vector<shared_ptr<const string> > buffers;
     const encode_callback_function *cb_ptr = &m_encode_callback;
     if(override_encode_callback)
     {
         cb_ptr = &override_encode_callback;
     }
     if(pack.accept(*ptr,buffers))
     {
         if((*cb_ptr))
         {
             (*cb_ptr)(false,ptr);
         }
         for(auto it = buffers.begin();it!=buffers.end();++it)
         {
             if((*cb_ptr))
             {
                 (*cb_ptr)(true,*it);
             }
         }
     }
     else
     {
         if((*cb_ptr))
         {
             (*cb_ptr)(false,ptr);
         }
     }
 }
void static_net_config::print_brief(const boost::asio::ip::udp::endpoint& /*sender*/, const packet& response) const
{
    BOOST_FOREACH(const field& f, response.fields())
        if(f.type() == field::FT_RESULT_MSG)
        {
            std::cout << f.value<std::string>() << std::endl;
            break;
        }
}
Example #7
0
void connection::recv_packet(packet p) {
  using namespace std;
  assert(this->check_packet(p));

  this->window_sizes.push_back(p.window_size());

  this->do_packet_calculation(p);

  this->do_byte_calculation(p);

  this->do_rtt_calculation(p);

  if (p.rst()) {
    this->connection_reset = true;
    this->change_state(std::shared_ptr<reset>(new reset));
  } 

  this->state->recv_packet(p, this);
}
Example #8
0
connection::connection(packet p) {
  this->src_addr         = p.src_addr(); 
  this->dst_addr         = p.dst_addr();
  this->src_port         = p.src_port(); 
  this->dst_port         = p.dst_port();
  this->connection_reset = p.rst();
  this->start_time = p.ts_milli() + p.ts_sec()*1000000;
  this->change_state(std::shared_ptr<s0f0>(new s0f0));
  //this->recv_packet(p);
}
Example #9
0
void_result remote_node::send(const packet &p)
{
  // Do not send packets marked as local_only
  if(p.local_only()) return success();
  
  // Check to see if anyone is actually listening
  // for this on this branch of the network.
  auto it = _parent->_subscription_count.find(p.meta().topic);
  const bool internal_check = _mode == remote_node::allow_internal && p.meta().topic == topic::internal.name();
  if(!internal_check && it == _parent->_subscription_count.end()) return success();

  packet mod = p;
  mod.meta().path.push_back(_parent->id());
  mod.meta().id = _parent->id();
  mod.build();
  
  bool should_send = true;
  
#ifdef SPLAT_ENABLED
  bool splattable = p.meta().droppable && p.meta().origin_id == _parent->_id;
  // cout << "REMOTE NODE SEND " << p.meta().topic << ": " << p.packed()->len << " (splat: " << splattable << ")" << endl;
  if(splattable)
  {
    auto ret = _parent->push_splat(mod);
    if(!ret)
    {
      cout << "Warning: Failed to push splat: " << ret.message() << endl;
    }
  }
  // FIXME: This is not the correct condition.
  // We still want to send to *truly* remote clients
  // over TCP, but there is currently no way to
  // identify unique systems on the network to
  // generate disconnected splat neighborhoods.
  should_send = !splattable;
#endif
  
  return should_send ? _parent->_thread.send(_link.get(), mod) : success();
}
Example #10
0
int unix_out_socket::get(packet &pk)
{ 
  pk.ro=pk.wo=2;

  ushort size=0;
  if (pk_buffer_last==pk_buffer_ro)
    fill_buffer();

  if (pk_buffer_last-pk_buffer_ro<2)  // make sure the two byte for the size are in the same packet
  {
    uchar two[2];
    two[0]=pk_buffer[pk_buffer_ro];
    fill_buffer();
    if (pk_buffer_last-pk_buffer_ro<2)  // if still not enough info, something is screwy
    {
      printf("Incomplete packet\n");
      return 0;
    }

    two[1]=pk_buffer[pk_buffer_ro];
    pk_buffer_ro++;
    size=lstl((*((ushort *)two)));
  } else
  {
    memcpy(&size,pk_buffer+pk_buffer_ro,2); pk_buffer_ro+=2;
    size=lstl(size);
  }
  pk.rend=size+2;
  pk.make_bigger(pk.rend);

  uchar *pk_off=pk.buf+2;
  int rs;
  while (size)
  {
    if (pk_buffer_last==pk_buffer_ro)
      fill_buffer();    
    if (pk_buffer_last-pk_buffer_ro>size)
      rs=size;
    else
      rs=pk_buffer_last-pk_buffer_ro;
    
    memcpy(pk_off,pk_buffer+pk_buffer_ro,rs);
    pk_buffer_ro+=rs;
    size-=rs;
    pk_off+=rs;         
  }
  
  return 1;
}
    virtual void receive(packet payload)
    {
        m_last_packet = payload;
        m_new_packet = true;

        if (m_decoder->is_complete())
        {
            std::string counter_id =
                node_id()+"_waste_from_"+payload.get_sender();

            ++m_counter[counter_id];
        }
        else
        {
            std::copy(payload.data_begin(), payload.data_end(),
                      &m_decode_buffer[0]);

            uint32_t rank = m_decoder->rank();
            m_decoder->read_payload(&m_decode_buffer[0]);

            if (rank < m_decoder->rank())
            {
                std::string counter_id =
                    node_id()+"_innovative_from_"+payload.get_sender();

                ++m_counter[counter_id];
            }
            else
            {
                std::string counter_id =
                    node_id()+"_linear_dept_from_"+payload.get_sender();

                ++m_counter[counter_id];
            }
        }
    }
    /// Receives a payload
    virtual void receive(packet payload)
    {
        for(uint32_t j = 0; j < receiver_count(); ++j)
        {
            std::string recv_id = get_receiver(j)->node_id();
            std::string src_id = payload.get_sender();

            // If true we drop
            if(m_channel_condition->generate())
            {
                ++m_counter[node_id()+"_"+src_id+"_to_"+recv_id+"_dropped"];
            }
            else
            {
                ++m_counter[node_id()+"_"+src_id+"_to_"+recv_id+"_ok"];

                // Deliver packet to receiver j
                forward(j, payload);
            }
        }

    }
void server::add_change_log(view *f, packet &pk, int number)
{
  if (f->view_changed())
  {
    uint8_t cmd=SCMD_VIEW_RESIZE;
    pk.write(&cmd,1);
    if (number)
    {
      uint16_t pn=lstl(f->player_number);
      pk.write((uint8_t *)&pn,2);
      dprintf("Server : %s resized view %d %d %d %d\n",f->name,
          f->suggest.cx1,f->suggest.cy1,f->suggest.cx2,f->suggest.cy2);
      f->resize_view(f->suggest.cx1,f->suggest.cy1,f->suggest.cx2,f->suggest.cy2);
      f->suggest.send_view=0;
    } else dprintf("sending resize to server\n");
    uint32_t view_size[8];
    view_size[0]=lltl(f->suggest.cx1);
    view_size[1]=lltl(f->suggest.cy1);
    view_size[2]=lltl(f->suggest.cx2);
    view_size[3]=lltl(f->suggest.cy2);
    view_size[4]=lltl(f->suggest.pan_x);
    view_size[5]=lltl(f->suggest.pan_y);
    view_size[6]=lltl(f->suggest.shift_down);
    view_size[7]=lltl(f->suggest.shift_right);
    pk.write((uint8_t *)view_size,8*4);
  }

  if (f->weapon_changed())
  {
    uint8_t cmd=SCMD_WEAPON_CHANGE;
    pk.write(&cmd,1);
    if (number)
    {
      uint16_t pn=lstl(f->player_number);
      pk.write((uint8_t *)&pn,2);
      dprintf("Server : %s change weapon to %d\n",f->name,f->suggest.new_weapon);
      f->current_weapon=f->suggest.new_weapon;
      f->suggest.send_weapon_change=0;
    } else dprintf("sending resize to server\n");
    uint32_t nw=lltl(f->suggest.new_weapon);
    pk.write((uint8_t *)&nw,4);
  }
}
int server::process_command(view *f, uint8_t command, packet &pk)
{
  switch (command)
  {
    case SCMD_QUIT :                          // delete player
    {
      dprintf("Player %d has quit\n",f->player_number);
      return 0;
    } break;

    case SCMD_VIEW_RESIZE :                          // change view area
    {
      uint32_t view_size[8];
      if (pk.read((uint8_t *)view_size,8*4)!=8*4)
      return 0;
      else
      {
    f->resize_view(lltl(view_size[0]),lltl(view_size[1]),lltl(view_size[2]),lltl(view_size[3]));
    f->pan_x=lltl(view_size[4]);
    f->pan_y=lltl(view_size[5]);
    f->shift_down=lltl(view_size[6]);
    f->shift_right=lltl(view_size[7]);
    f->suggest.send_view=0;
    if (is_server)                  // if we are a server, tell everybody about this.
    {
      uint8_t cmd=SCMD_VIEW_RESIZE;
      next_out.write((uint8_t *)&cmd,1);
      uint16_t pn=lstl(f->player_number);
      next_out.write((uint8_t *)&pn,2);
      next_out.write((uint8_t *)view_size,8*4);
    }
      }
    } break;

    case SCMD_WEAPON_CHANGE :                          // change weapon
    {
      uint32_t new_weap;
      if (pk.read((uint8_t *)&new_weap,4)!=4)
        return 0;
      else
      {
    f->current_weapon=lltl(new_weap);
    f->suggest.send_weapon_change=0;
    if (is_server)                      // if we are a server, tell everybody about this.
    {
      uint8_t cmd=SCMD_WEAPON_CHANGE;
      next_out.write((uint8_t *)&cmd,1);
      uint16_t pn=lstl(f->player_number);
      next_out.write((uint8_t *)&pn,2);
      next_out.write((uint8_t *)&new_weap,4);
    }
      }
    } break;


    case SCMD_SET_INPUT :                        // set the input from this player
    {
      signed char inp[5];
      if (pk.read((uint8_t *)inp,5)!=5)
        return 0;
      else
        f->set_input(inp[0],inp[1],inp[2],inp[3],inp[4]);
    } break;

    case SCMD_ADD_VIEW :
    {
      view *v=add_view(pk);
      if (v)
      {
    for (view *f=player_list; f && f->next; f=f->next);
    if (f) f->next=v;
    else player_list=f;
      }
    } break;
    case SCMD_SYNC :
    {
      uint32_t x;
      if (pk.read((uint8_t *)&x,4)!=4)
        return 0;
      else
      {
    uint32_t s=make_sync_uint32();
    if (lltl(x)!=s)
      printf("Out of sync, %x!=%x\n",lltl(x),s);
    return 1;
      }
    } break;

    default :
      return 0;
  }
  return 1;
}
Example #15
0
bool connection::dst_to_src(packet p) {
  bool addr = (p.dst_addr() == this->src_addr) && (p.src_addr() == this->dst_addr);
  bool port = (p.dst_port() == this->src_port) && (p.src_port() == this->dst_port);
  return addr && port;
}
Example #16
0
 bool operator>(const packet& arg) const { return A > arg.comp(); }
Example #17
0
 bool operator<(const packet& arg) const { return A < arg.comp(); }
Example #18
0
void connection::do_rtt_calculation(packet p) {
  assert(this->check_packet(p));

  if (this->src_to_dst(p)) {
    if (p.syn() && !p.ack()) {
      this->seq_num = p.seq_number();
      auto it = dst_packets.find(p.seq_number());
      if (it != dst_packets.end()) {
        dst_packets.erase(it);
        this->rtts.push_back(p.ts() - it->second.ts());
      }
      this->nxt_ack = p.seq_number() + 1;
      src_packets[this->nxt_ack] = p;
    }
    if (p.ack() && !p.syn()) {
      this->seq_num = p.seq_number();
      auto it = dst_packets.find(p.seq_number());
      if (it != dst_packets.end()) {
        dst_packets.erase(it);
        this->rtts.push_back(p.ts() - it->second.ts());
      }
      this->nxt_ack = p.seq_number() + p.data_size();
      src_packets[this->nxt_ack] = p;
    }
    if (p.fin()) {
      this->fin_set = true;
      this->rtt_t0 = p.ts();
    }
  }
  
  if (this->dst_to_src(p)) {
    if (p.ack() && p.fin()) {
      if (this->fin_set) {
        this->rtts.push_back(p.ts() - this->rtt_t0);
      }
    }
    if (p.syn() && p.ack()) {
      auto it = src_packets.find(p.ack_number());
      if (it != src_packets.end()) {
        src_packets.erase(it);
        this->rtts.push_back(p.ts() - it->second.ts());
      }
      dst_packets[p.ack_number()] = p;
    }
    if (p.ack() && !p.syn()) {
      auto it = src_packets.find(p.ack_number());
      if (it != src_packets.end()) {
        src_packets.erase(it);
        this->rtts.push_back(p.ts() - it->second.ts());
      }
      dst_packets[p.ack_number()] = p;
    }
  }
}