Exemple #1
0
PyPacket* EVEClientSession::_HandleFuncResult( PyRep* rep )
{
    CryptoHandshakeResult hr;
    if( !hr.Decode( &rep ) )
        sLog.Error("Network", "%s: Received invalid crypto handshake result!", GetAddress().c_str());
    else if( _VerifyFuncResult( hr ) )
        mPacketHandler = &EVEClientSession::_HandlePacket;

    // recurse
    return PopPacket();
}
Exemple #2
0
PyPacket* EVEClientSession::_HandleCrypto( PyRep* rep )
{
    CryptoRequestPacket cr;
    if( !cr.Decode( &rep ) )
        sLog.Error("Network", "%s: Received invalid crypto request!", GetAddress().c_str());
    else if( _VerifyCrypto( cr ) )
        mPacketHandler = &EVEClientSession::_HandleAuthentication;

    // recurse
    return PopPacket();
}
Exemple #3
0
PyPacket* EVEClientSession::_HandleAuthentication( PyRep* rep )
{
    //just to be sure
    CryptoChallengePacket ccp;
    if( !ccp.Decode( &rep ) )
        sLog.Error("Network", "%s: Received invalid crypto challenge!", GetAddress().c_str());
    else if( _VerifyLogin( ccp ) )
        mPacketHandler = &EVEClientSession::_HandleFuncResult;

    return PopPacket();
}
Exemple #4
0
PyPacket* EVEClientSession::_HandleCommand( PyRep* rep )
{
    //check if it actually is tuple
    if( !rep->IsTuple() )
    {
        sLog.Error("Network", "%s: Invalid packet during waiting for command (tuple expected).", GetAddress().c_str());
    }
    // decode
    else if( rep->AsTuple()->size() == 2 )
    {
        //QC = Queue Check
        NetCommand_QC cmd;
        if( !cmd.Decode( &rep ) )
        {
            sLog.Error("Network", "%s: Failed to decode 2-arg command.", GetAddress().c_str());
        }
        else
        {
            sLog.Debug("Network", "%s: Got Queue Check command.", GetAddress().c_str());

            //they return position in queue
            PyRep* rsp = new PyInt( _GetQueuePosition() );
            mNet->QueueRep( rsp );
            PyDecRef( rsp );

            //now reset connection
            Reset();
        }
    }
    else if( rep->AsTuple()->size() == 3 )
    {
        //this is sent when client is logging in
        NetCommand_VK cmd;
        if( !cmd.Decode( &rep ) )
        {
            sLog.Error("Network", "%s: Failed to decode 3-arg command.", GetAddress().c_str());
        }
        else
        {
            sLog.Debug("Network", "%s: Got VK command, vipKey=%s.", GetAddress().c_str(), cmd.vipKey.c_str());

            if( _VerifyVIPKey( cmd.vipKey ) )
                mPacketHandler = &EVEClientSession::_HandleCrypto;
        }
    }
    else
    {
        _log(NET__PRES_ERROR, "%s: Received invalid command packet:", GetAddress().c_str());
        rep->Dump(NET__PRES_ERROR, "  ");
    }

    // recurse
    return PopPacket();
}
Exemple #5
0
PyPacket* EVEClientSession::_HandleVersion( PyRep* rep )
{
    //we are waiting for their version information...
    VersionExchangeClient ve;
    if( !ve.Decode( &rep ) )
        sLog.Error("Network", "%s: Received invalid version exchange!", GetAddress().c_str());
    else if( _VerifyVersion( ve ) )
        mPacketHandler = &EVEClientSession::_HandleCommand;

    // recurse
    return PopPacket();
}
Exemple #6
0
PyPacket* EVEClientSession::_HandlePacket( PyRep* rep )
{
    //take the PyRep and turn it into a PyPacket
    PyPacket* p = new PyPacket;
    if( !p->Decode( &rep ) ) //rep is consumed here
    {
        sLog.Error("Network", "%s: Failed to decode packet rep", GetAddress().c_str());
        SafeDelete( p );
    }
    else
        return p;

    // recurse
    return PopPacket();
}
Exemple #7
0
void TFtp::Session::Process(const VirDir &dir) {
  Packet::Ptr pkt_ptr = PopPacket();
  if (not pkt_ptr) {
    return;
  }
  switch (pkt_ptr->get_opcode()) {
    case Packet::kRRQ: {
      const PacketRRQ &rrq = static_cast<const PacketRRQ&>(*pkt_ptr);
      VirDir::const_iterator f_it = dir.find(rrq.get_filename());
      if (f_it == dir.end()) {
        // TODO: отправка ошибки!
        std::cout << "Файл ненайден! "
                  << std::endl;
        break;
      }
      if (_task.type == Task::kIdling) {
        _task.type = Task::kUploading;
        _task.mode = rrq.get_mode();
        _task.file->open(f_it->second, std::fstream::in | std::fstream::binary);
        _task.DetectFileSize();
        ProcessTask(1);
      }
      return;
    }
    case Packet::kACK: {
      const PacketACK &ack = static_cast<const PacketACK&>(*pkt_ptr);
      if (_task.type == Task::kUploading && 
          ProcessTask(ack.get_block_id() + 1)) {
        return;
      }
      break;
    }
    case Packet::kError: {
      const PacketError &err = static_cast<const PacketError&>(*pkt_ptr);
      std::cout << "Ошибка передачи данных: " << (unsigned)err.get_code() << ": "
                << err.get_msg() << "; "
                << std::endl;
      break;    
    }
    default:
      break;
  };
  Close();
}
Exemple #8
0
// ------------------------------------------------------------------------------------------------
int main(int argc, const char **argv)
{
    // Common variables
    NetBuf *inPkt;
    TcpHeader *inHdr;
    Packet *outPkt;
    TcpHeader *outHdr;
    TcpConn *conn;

    TestSetup();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_CLOSED, "RST", "segment dropped");

    inPkt = NetAllocBuf();
    inHdr = (TcpHeader *)inPkt->start;
    inHdr->srcPort = 100;
    inHdr->dstPort = 101;
    inHdr->seq = 1;
    inHdr->ack = 2;
    inHdr->off = 5 << 4;
    inHdr->flags = TCP_RST;
    inHdr->windowSize = TCP_WINDOW_SIZE;
    inHdr->checksum = 0;
    inHdr->urgent = 0;
    TcpInput(inPkt);

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_CLOSED, "ACK", "RST sent");

    inPkt = NetAllocBuf();
    inHdr = (TcpHeader *)inPkt->start;
    inHdr->srcPort = 100;
    inHdr->dstPort = 101;
    inHdr->seq = 1;
    inHdr->ack = 2;
    inHdr->off = 5 << 4;
    inHdr->flags = TCP_ACK;
    inHdr->windowSize = TCP_WINDOW_SIZE;
    inHdr->checksum = 0;
    inHdr->urgent = 0;
    TcpInput(inPkt);

    outPkt = PopPacket();
    outHdr = (TcpHeader *)outPkt->data;
    TcpSwap(outHdr);
    ASSERT_EQ_UINT(outHdr->srcPort, 101);
    ASSERT_EQ_UINT(outHdr->dstPort, 100);
    ASSERT_EQ_UINT(outHdr->seq, 2);
    ASSERT_EQ_UINT(outHdr->ack, 0);
    ASSERT_EQ_HEX8(outHdr->flags, TCP_RST);
    free(outPkt);

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_CLOSED, "no ACK", "RST/ACK sent");

    inPkt = NetAllocBuf();
    inHdr = (TcpHeader *)inPkt->start;
    inHdr->srcPort = 100;
    inHdr->dstPort = 101;
    inHdr->seq = 1;
    inHdr->ack = 2;
    inHdr->off = 5 << 4;
    inHdr->flags = 0;
    inHdr->windowSize = TCP_WINDOW_SIZE;
    inHdr->checksum = 0;
    inHdr->urgent = 0;
    TcpInput(inPkt);

    outPkt = PopPacket();
    outHdr = (TcpHeader *)outPkt->data;
    TcpSwap(outHdr);
    ASSERT_EQ_UINT(outHdr->srcPort, 101);
    ASSERT_EQ_UINT(outHdr->dstPort, 100);
    ASSERT_EQ_UINT(outHdr->seq, 0);
    ASSERT_EQ_UINT(outHdr->ack, 1);
    ASSERT_EQ_HEX8(outHdr->flags, TCP_RST | TCP_ACK);
    free(outPkt);

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_CLOSED, "connect", "goto SYN_SENT");

    conn = CreateConn();

    ASSERT_TRUE(TcpConnect(conn, &s_ipAddr, 80));

    outPkt = PopPacket();
    outHdr = (TcpHeader *)outPkt->data;
    TcpSwap(outHdr);
    ASSERT_TRUE(outHdr->srcPort >= 49152);
    ASSERT_EQ_UINT(outHdr->dstPort, 80);
    ASSERT_EQ_UINT(outHdr->seq, conn->iss);
    ASSERT_EQ_UINT(outHdr->ack, 0);
    ASSERT_EQ_HEX8(outHdr->flags, TCP_SYN);
    ASSERT_EQ_UINT(outHdr->windowSize, TCP_WINDOW_SIZE);
    ASSERT_EQ_UINT(outHdr->urgent, 0);
    free(outPkt);

    ExitState(conn, TCP_SYN_SENT);

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_SYN_SENT, "Bad ACK, no RST", "RST sent");

    conn = CreateConn();
    EnterState(conn, TCP_SYN_SENT);

    inPkt = NetAllocBuf();
    inHdr = PrepareInPkt(conn, inPkt, 1000, conn->iss, TCP_ACK);
    TcpInput(inPkt);

    outPkt = PopPacket();
    outHdr = (TcpHeader *)outPkt->data;
    TcpSwap(outHdr);
    ASSERT_EQ_UINT(outHdr->srcPort, conn->localPort);
    ASSERT_EQ_UINT(outHdr->dstPort, conn->remotePort);
    ASSERT_EQ_UINT(outHdr->seq, inHdr->ack);
    ASSERT_EQ_UINT(outHdr->ack, 0);
    ASSERT_EQ_HEX8(outHdr->flags, TCP_RST);
    free(outPkt);

    ExitState(conn, TCP_SYN_SENT);

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_SYN_SENT, "Bad ACK, RST", "segment dropped");

    conn = CreateConn();
    EnterState(conn, TCP_SYN_SENT);

    inPkt = NetAllocBuf();
    inHdr = PrepareInPkt(conn, inPkt, 1000, conn->iss, TCP_RST | TCP_ACK);
    TcpInput(inPkt);

    ExitState(conn, TCP_SYN_SENT);

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_SYN_SENT, "ACK, RST", "conn locally reset");

    conn = CreateConn();
    EnterState(conn, TCP_SYN_SENT);

    inPkt = NetAllocBuf();
    inHdr = PrepareInPkt(conn, inPkt, 1000, conn->iss + 1, TCP_RST | TCP_ACK);
    TcpInput(inPkt);

    ExpectError(TCP_CONN_RESET);
    ExitState(conn, TCP_CLOSED);

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_SYN_SENT, "no ACK, RST", "segment dropped");

    conn = CreateConn();
    EnterState(conn, TCP_SYN_SENT);

    inPkt = NetAllocBuf();
    inHdr = PrepareInPkt(conn, inPkt, 1000, conn->iss + 1, TCP_RST);
    TcpInput(inPkt);

    ExitState(conn, TCP_SYN_SENT);

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_SYN_SENT, "SYN, ACK", "goto ESTABLISHED");

    conn = CreateConn();
    EnterState(conn, TCP_SYN_SENT);

    inPkt = NetAllocBuf();
    inHdr = PrepareInPkt(conn, inPkt, 1000, conn->iss + 1, TCP_SYN | TCP_ACK);
    TcpInput(inPkt);

    ASSERT_EQ_UINT(conn->irs, 1000);
    ASSERT_EQ_UINT(conn->rcvNxt, 1001);

    outPkt = PopPacket();
    outHdr = (TcpHeader *)outPkt->data;
    TcpSwap(outHdr);
    ASSERT_EQ_UINT(outHdr->srcPort, conn->localPort);
    ASSERT_EQ_UINT(outHdr->dstPort, conn->remotePort);
    ASSERT_EQ_UINT(outHdr->seq, conn->iss + 1);
    ASSERT_EQ_UINT(outHdr->ack, 1001);
    ASSERT_EQ_HEX8(outHdr->flags, TCP_ACK);
    free(outPkt);

    ExitState(conn, TCP_ESTABLISHED);

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_SYN_SENT, "SYN, no ACK", "goto SYN_RECEIVED, resend SYN,ACK");

    conn = CreateConn();
    EnterState(conn, TCP_SYN_SENT);

    inPkt = NetAllocBuf();
    inHdr = PrepareInPkt(conn, inPkt, 1000, 0, TCP_SYN);
    TcpInput(inPkt);

    ASSERT_EQ_UINT(conn->irs, 1000);
    ASSERT_EQ_UINT(conn->rcvNxt, 1001);

    outPkt = PopPacket();
    outHdr = (TcpHeader *)outPkt->data;
    TcpSwap(outHdr);
    ASSERT_EQ_UINT(outHdr->srcPort, conn->localPort);
    ASSERT_EQ_UINT(outHdr->dstPort, conn->remotePort);
    ASSERT_EQ_UINT(outHdr->seq, conn->iss);
    ASSERT_EQ_UINT(outHdr->ack, 1001);
    ASSERT_EQ_HEX8(outHdr->flags, TCP_SYN | TCP_ACK);
    free(outPkt);

    ExitState(conn, TCP_SYN_RECEIVED);

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    uint generalStates[] =
    {
        TCP_SYN_RECEIVED,
        TCP_ESTABLISHED,
        TCP_FIN_WAIT_1,
        TCP_FIN_WAIT_2,
        TCP_CLOSE_WAIT,
        TCP_CLOSING,
        TCP_LAST_ACK,
        TCP_TIME_WAIT,
        0,
    };

    for (uint *pState = generalStates; *pState; ++pState)
    {
        uint state = *pState;

        TestCaseBegin(state, "Bad seq, no RST", "resend ACK");

        conn = CreateConn();
        EnterState(conn, state);

        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt - 1, conn->sndNxt, TCP_ACK);
        TcpInput(inPkt);

        outPkt = PopPacket();
        outHdr = (TcpHeader *)outPkt->data;
        TcpSwap(outHdr);
        ASSERT_EQ_UINT(outHdr->srcPort, conn->localPort);
        ASSERT_EQ_UINT(outHdr->dstPort, conn->remotePort);
        ASSERT_EQ_UINT(outHdr->seq, conn->sndNxt);
        ASSERT_EQ_UINT(outHdr->ack, conn->rcvNxt);
        ASSERT_EQ_HEX8(outHdr->flags, TCP_ACK);
        free(outPkt);

        ExitState(conn, state);

        TestCaseEnd();
    }

    // --------------------------------------------------------------------------------------------
    for (uint *pState = generalStates; *pState; ++pState)
    {
        uint state = *pState;

        TestCaseBegin(state, "Bad seq, RST", "segment dropped");

        conn = CreateConn();
        EnterState(conn, state);

        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt - 1, conn->sndNxt, TCP_RST | TCP_ACK);
        TcpInput(inPkt);

        ExitState(conn, state);

        TestCaseEnd();
    }

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_SYN_RECEIVED, "RST, active", "conn refused");

    conn = CreateConn();
    EnterState(conn, TCP_SYN_RECEIVED);

    inPkt = NetAllocBuf();
    inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, 0, TCP_RST);
    TcpInput(inPkt);

    ExpectError(TCP_CONN_REFUSED);

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    uint rstStates1[] =
    {
        TCP_ESTABLISHED,
        TCP_FIN_WAIT_1,
        TCP_FIN_WAIT_2,
        TCP_CLOSE_WAIT,
        0,
    };

    for (uint *pState = rstStates1; *pState; ++pState)
    {
        uint state = *pState;

        TestCaseBegin(state, "RST", "conn reset");

        conn = CreateConn();
        EnterState(conn, state);

        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, 0, TCP_RST);
        TcpInput(inPkt);

        ExpectError(TCP_CONN_RESET);

        TestCaseEnd();
    }

    // --------------------------------------------------------------------------------------------
    uint rstStates2[] =
    {
        TCP_CLOSING,
        TCP_LAST_ACK,
        TCP_TIME_WAIT,
        0,
    };

    for (uint *pState = rstStates2; *pState; ++pState)
    {
        uint state = *pState;

        TestCaseBegin(state, "RST", "conn closed");

        conn = CreateConn();
        EnterState(conn, state);

        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, 0, TCP_RST);
        TcpInput(inPkt);

        TestCaseEnd();
    }

    // --------------------------------------------------------------------------------------------
    for (uint *pState = generalStates; *pState; ++pState)
    {
        uint state = *pState;

        TestCaseBegin(state, "SYN", "conn reset, RST sent");

        conn = CreateConn();
        EnterState(conn, state);

        u16 localPort = conn->localPort;
        u16 remotePort = conn->remotePort;
        u32 rcvNxt = conn->rcvNxt;

        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt, TCP_SYN);
        TcpInput(inPkt);

        outPkt = PopPacket();
        outHdr = (TcpHeader *)outPkt->data;
        TcpSwap(outHdr);
        ASSERT_EQ_UINT(outHdr->srcPort, localPort);
        ASSERT_EQ_UINT(outHdr->dstPort, remotePort);
        ASSERT_EQ_UINT(outHdr->seq, 0);
        ASSERT_EQ_UINT(outHdr->ack, rcvNxt);
        ASSERT_EQ_HEX8(outHdr->flags, TCP_RST | TCP_ACK);
        free(outPkt);

        ExpectError(TCP_CONN_RESET);

        TestCaseEnd();
    }

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_SYN_RECEIVED, "bad ACK", "RST sent");

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_SYN_RECEIVED, "ACK", "goto ESTABLISHED");

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    uint ackStates[] =
    {
        TCP_ESTABLISHED,
        TCP_FIN_WAIT_1,
        TCP_FIN_WAIT_2,
        TCP_CLOSE_WAIT,
        TCP_CLOSING,
        0
    };

    for (uint *pState = ackStates; *pState; ++pState)
    {
        uint state = *pState;
        TestCaseBegin(state, "ACK", "update pointers");

        TestCaseEnd();
    }

    // --------------------------------------------------------------------------------------------
    for (uint *pState = ackStates; *pState; ++pState)
    {
        uint state = *pState;
        TestCaseBegin(state, "dup ACK", "ignore");

        TestCaseEnd();
    }

    // --------------------------------------------------------------------------------------------
    for (uint *pState = ackStates; *pState; ++pState)
    {
        uint state = *pState;
        TestCaseBegin(state, "unsent ACK", "resend ACK");

        TestCaseEnd();
    }

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_FIN_WAIT_1, "ACK, FIN not ACK'd", "ignore");

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_CLOSING, "ACK, FIN not ACK'd", "ignore");

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_FIN_WAIT_1, "ACK, FIN ACK'd", "goto FIN-WAIT-2");

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_CLOSING, "ACK, FIN ACK'd", "goto TIME-WAIT");

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_LAST_ACK, "ACK, FIN not ACK'd", "ignore");

    conn = CreateConn();
    EnterState(conn, TCP_LAST_ACK);

    inPkt = NetAllocBuf();
    inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt - 1, TCP_ACK);
    TcpInput(inPkt);

    ExitState(conn, TCP_LAST_ACK);

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_LAST_ACK, "ACK, FIN ACK'd", "goto CLOSED");

    conn = CreateConn();
    EnterState(conn, TCP_LAST_ACK);

    inPkt = NetAllocBuf();
    inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt, TCP_ACK);
    TcpInput(inPkt);

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_TIME_WAIT, "ACK, no FIN", "ignore");

    conn = CreateConn();
    EnterState(conn, TCP_TIME_WAIT);

    inPkt = NetAllocBuf();
    inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt, TCP_ACK);
    TcpInput(inPkt);

    ExitState(conn, TCP_TIME_WAIT);

    TestCaseEnd();

    // --------------------------------------------------------------------------------------------
    TestCaseBegin(TCP_TIME_WAIT, "FIN", "reset 2MSL timer");

    conn = CreateConn();
    EnterState(conn, TCP_TIME_WAIT);

    g_pitTicks += 1000;
    inPkt = NetAllocBuf();
    inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt, TCP_FIN | TCP_ACK);
    TcpInput(inPkt);

    outPkt = PopPacket();
    outHdr = (TcpHeader *)outPkt->data;
    TcpSwap(outHdr);
    ASSERT_EQ_UINT(outHdr->srcPort, conn->localPort);
    ASSERT_EQ_UINT(outHdr->dstPort, conn->remotePort);
    ASSERT_EQ_UINT(outHdr->seq, conn->sndNxt);
    ASSERT_EQ_UINT(outHdr->ack, conn->rcvNxt);
    ASSERT_EQ_HEX8(outHdr->flags, TCP_ACK);
    free(outPkt);

    ASSERT_EQ_UINT(conn->mslWait, g_pitTicks + 2 * TCP_MSL);

    ExitState(conn, TCP_TIME_WAIT);

    TestCaseEnd();

    return EXIT_SUCCESS;
}
Exemple #9
0
// ------------------------------------------------------------------------------------------------
static void ExitState(TcpConn *conn, uint state)
{
    NetBuf *inPkt;
    TcpHeader *inHdr;
    Packet *outPkt;
    TcpHeader *outHdr;

    ASSERT_EQ_UINT(conn->state, state);
    ASSERT_TRUE(ListIsEmpty(&s_outPackets));

    switch (state)
    {
    case TCP_CLOSED:
        TcpClose(conn);
        break;

    case TCP_SYN_SENT:
        TcpClose(conn);
        break;

    case TCP_SYN_RECEIVED:
        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt, TCP_ACK);
        TcpInput(inPkt);

        ExitState(conn, TCP_ESTABLISHED);
        break;

    case TCP_ESTABLISHED:
        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt, TCP_FIN | TCP_ACK);
        TcpInput(inPkt);

        outPkt = PopPacket();
        outHdr = (TcpHeader *)outPkt->data;
        TcpSwap(outHdr);
        ASSERT_EQ_UINT(outHdr->srcPort, conn->localPort);
        ASSERT_EQ_UINT(outHdr->dstPort, conn->remotePort);
        ASSERT_EQ_UINT(outHdr->seq, conn->sndNxt);
        ASSERT_EQ_UINT(outHdr->ack, conn->rcvNxt);
        ASSERT_EQ_HEX8(outHdr->flags, TCP_ACK);
        free(outPkt);

        ExitState(conn, TCP_CLOSE_WAIT);
        break;

    case TCP_FIN_WAIT_1:
        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt, TCP_FIN | TCP_ACK);
        TcpInput(inPkt);

        outPkt = PopPacket();
        outHdr = (TcpHeader *)outPkt->data;
        TcpSwap(outHdr);
        ASSERT_EQ_UINT(outHdr->srcPort, conn->localPort);
        ASSERT_EQ_UINT(outHdr->dstPort, conn->remotePort);
        ASSERT_EQ_UINT(outHdr->seq, conn->sndNxt);
        ASSERT_EQ_UINT(outHdr->ack, conn->rcvNxt);
        ASSERT_EQ_HEX8(outHdr->flags, TCP_ACK);
        free(outPkt);

        ExitState(conn, TCP_TIME_WAIT);
        break;

    case TCP_FIN_WAIT_2:
        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt, TCP_FIN | TCP_ACK);
        TcpInput(inPkt);

        outPkt = PopPacket();
        outHdr = (TcpHeader *)outPkt->data;
        TcpSwap(outHdr);
        ASSERT_EQ_UINT(outHdr->srcPort, conn->localPort);
        ASSERT_EQ_UINT(outHdr->dstPort, conn->remotePort);
        ASSERT_EQ_UINT(outHdr->seq, conn->sndNxt);
        ASSERT_EQ_UINT(outHdr->ack, conn->rcvNxt);
        ASSERT_EQ_HEX8(outHdr->flags, TCP_ACK);
        free(outPkt);

        ExitState(conn, TCP_TIME_WAIT);
        break;

    case TCP_CLOSE_WAIT:
        TcpClose(conn);

        outPkt = PopPacket();
        outHdr = (TcpHeader *)outPkt->data;
        TcpSwap(outHdr);
        ASSERT_EQ_UINT(outHdr->srcPort, conn->localPort);
        ASSERT_EQ_UINT(outHdr->dstPort, conn->remotePort);
        ASSERT_EQ_UINT(outHdr->seq, conn->sndNxt - 1);
        ASSERT_EQ_UINT(outHdr->ack, conn->rcvNxt);
        ASSERT_EQ_HEX8(outHdr->flags, TCP_FIN | TCP_ACK);
        free(outPkt);

        ExitState(conn, TCP_LAST_ACK);
        break;

    case TCP_CLOSING:
        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt, TCP_ACK);
        TcpInput(inPkt);

        ExitState(conn, TCP_TIME_WAIT);
        break;

    case TCP_LAST_ACK:
        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt, TCP_ACK);
        TcpInput(inPkt);
        break;

    case TCP_TIME_WAIT:
        g_pitTicks += 2 * TCP_MSL;
        TcpPoll();
        break;

    default:
        ASSERT_EQ_UINT(state, 0);
        break;
    }

    ASSERT_EQ_UINT(conn->state, TCP_CLOSED);
}
Exemple #10
0
// ------------------------------------------------------------------------------------------------
static void EnterState(TcpConn *conn, uint state)
{
    NetBuf *inPkt;
    TcpHeader *inHdr;
    Packet *outPkt;
    //TcpHeader *outHdr;

    switch (state)
    {
    case TCP_SYN_SENT:
        ASSERT_TRUE(TcpConnect(conn, &s_ipAddr, 80));

        outPkt = PopPacket();
        free(outPkt);
        break;

    case TCP_SYN_RECEIVED:
        EnterState(conn, TCP_SYN_SENT);

        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, 1000, 0, TCP_SYN);
        TcpInput(inPkt);

        outPkt = PopPacket();
        free(outPkt);
        break;

    case TCP_ESTABLISHED:
        EnterState(conn, TCP_SYN_SENT);

        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt, TCP_SYN | TCP_ACK);
        TcpInput(inPkt);

        outPkt = PopPacket();
        free(outPkt);
        break;

    case TCP_FIN_WAIT_1:
        EnterState(conn, TCP_ESTABLISHED);

        TcpClose(conn);

        outPkt = PopPacket();
        free(outPkt);
        break;

    case TCP_FIN_WAIT_2:
        EnterState(conn, TCP_FIN_WAIT_1);

        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt, TCP_ACK);
        TcpInput(inPkt);
        break;

    case TCP_CLOSE_WAIT:
        EnterState(conn, TCP_ESTABLISHED);

        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt, TCP_FIN | TCP_ACK);
        TcpInput(inPkt);

        outPkt = PopPacket();
        free(outPkt);
        break;

    case TCP_CLOSING:
        EnterState(conn, TCP_FIN_WAIT_1);

        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt - 1, TCP_FIN | TCP_ACK);
        TcpInput(inPkt);

        outPkt = PopPacket();
        free(outPkt);
        break;

    case TCP_LAST_ACK:
        EnterState(conn, TCP_CLOSE_WAIT);

        TcpClose(conn);

        outPkt = PopPacket();
        free(outPkt);
        break;

    case TCP_TIME_WAIT:
        EnterState(conn, TCP_FIN_WAIT_1);

        inPkt = NetAllocBuf();
        inHdr = PrepareInPkt(conn, inPkt, conn->rcvNxt, conn->sndNxt, TCP_FIN | TCP_ACK);
        TcpInput(inPkt);

        outPkt = PopPacket();
        free(outPkt);
        break;

    default:
        ASSERT_EQ_UINT(state, 0);
        break;
    }

    ASSERT_EQ_UINT(conn->state, state);
    ASSERT_TRUE(ListIsEmpty(&s_outPackets));
}
Exemple #11
0
int main( )
{
	int r, i;
	cli();
	PORTA = PORTB = 0;
	CCP = 0xD8;
	CLKPR = 0x00;  //Disable scaling.
//	OSCCAL0 = 0x80; //Changing this will make it not use factory cal.

	ConfigI2C();

	//Now we set up the hardware...
	OCR1BL = 255;
	OCR1AL = 255;
	OCR2BL = 255;
	OCR2AL = 255;

	TCCR1A = _BV(WGM10) | _BV(COM1B1) | _BV(COM1A1) | _BV(COM1B0) | _BV(COM1A0);
	TCCR1B = _BV(WGM12) | _BV(CS10);
	TCCR2A = _BV(WGM20) | _BV(COM2B1) | _BV(COM2A1) | _BV(COM2B0) | _BV(COM2A0);
	TCCR2B = _BV(WGM22) | _BV(CS20);

	//Outputs on TOCC0,1,2,7
	TOCPMSA0 = _BV(TOCC0S0) //Map 1B to TOCC0 (MTR0) (OCR1BL)
			| _BV(TOCC1S0)  //Map 1A to TOCC1 (MTR1) (OCR1AL)
			| _BV(TOCC2S1); //Map 2B to TOCC2 (MTR2) (OCR2BL)
	TOCPMSA1 = _BV(TOCC7S1);//Map 2A to TOCC7 (MTR3) (OCR2AL)

	TOCPMCOE = _BV(TOCC0OE) | _BV(TOCC1OE) | _BV(TOCC2OE) | _BV(TOCC7OE);

	//Enable Motor outputs
	DDRA |= _BV(1) | _BV(2) | _BV(3);
	DDRB |= _BV(2);

	//Set up ADC to read battery voltage.
	ADMUXA = 0b001101; //1.1v for reading
	ADMUXB = 0; //Vcc for reference
	ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADATE) | _BV(ADPS2) | _BV(ADPS1);
	ADCSRB = _BV(ADLAR);

	//Set up TWI pullups to help the bus.
	PUEB |= _BV(0) | _BV(1);

	//Set up the main system ticker.
	TCCR0A = _BV(WGM00) | _BV(WGM01); //Fast PWM (though we don't use the PWM)
	TCCR0B = _BV(WGM02) | _BV(WGM02) | _BV(CS02); //8MHz / 256 = 31.25kHz
	OCR0A = 250; // Use 250 for your divisor, you'll get a 125Hz? Clock  (Should it be 249?)

	//Set up the SPI Bus
	DDRA |= _BV(5); //MISO
	SPCR = 0;
	SPCR |= _BV(SPIE);
	SPCR |= _BV(SPE);

retry:
	i++;
	_delay_ms(1);
	r = InitLSM9DS1();
	PromptVal( PSTR("LSM:"), r );
	if( r ) { i++; if( i < 100 ) goto retry; }
	r = InitBMP280();
	PromptVal( PSTR("BMP280:"), r );
	if( r ) { i++; if( i < 200 ) goto retry; }

	sei();

/*	uint16_t ccv[6];
	r = GetBMPCalVals( 0, (uint8_t*)ccv );
	PromptVal( PSTR("BMPGV:"), r );

	for( i = 0; i < 6; i++ )
	{
		PromptVal( PSTR("LR:"), ccv[i] );
	}
*/
	uint16_t i = 0;
	uint16_t reads = 0;

	while(1)
	{

		uint8_t rxsize = (rxhead - rxtail + RXBS)%RXBS;

		if( rxsize )
		{
			uint8_t exp = rxbuffer[rxtail] & 0x0f;
			if( rxsize >= exp )
			{
				PopPacket();
			}
		}

		//Create magnetometer data at ~30 Hz.
		{
			int16_t lastmag[3];
			if( ReadM(lastmag) == 0 )
			{
				cummag[0] += lastmag[0] >> MAG_FILTER;
				cummag[1] += lastmag[1] >> MAG_FILTER;
				cummag[2] += lastmag[2] >> MAG_FILTER;
				if( ++got_mag_data  == (1<<MAG_FILTER) )
				{
					//PromptVal( PSTR("MAG:"), cummag[0] );	

					sendmag[0] = cummag[0];
					sendmag[1] = cummag[1];
					sendmag[2] = cummag[2];
					got_mag_data = 0;


					//XXX Do something with this data.
					mag_ready = 1;
					cummag[0] = cummag[1] = cummag[2] = 0;

				}
			}
		}


		{
			//int16_t agtdata[7];
			r = ReadAG( last_agt );
			if( r )
			{
//				PromptVal( PSTR("AX:"), last_agt[3] );

				//XXX Do something with this data.
				reads++;
			}
		}


		if( TIFR0 & _BV(OCF0A) )
		{
			TIFR0 |= _BV(OCF0A);

			i++;
			OCR1BL = i;
			OCR1AL = i;
			OCR2BL = i;
			OCR2AL = i;

			if( ( i%125 ) == 0 )
			{
				PromptVal( PSTR("39aaa:"), reads ); reads = 0;
//				PromptVal( PSTR("E:"), r );
//				PromptVal( PSTR("K:"), last_agt[0] );

				//ReadAGM( lrcur );
				//GetBMPTelem( bmp280dat );
			}
		}


//		PromptVal( "LR0:", (bmp280dat[0]<<8) + bmp280dat[1] );
//		PromptVal( "LR:", ccv[i] );
//		PromptVal( PSTR("LR:"), ccv[i] );

//		i++;
//		if( i == 6 ) i = 0;
/*		PromptVal( "LR1:", lrcur[1] );
		PromptVal( "LR2:", lrcur[2] );
		PromptVal( "LR3:", lrcur[3] );
		PromptVal( "LR4:", lrcur[4] );
		PromptVal( "LR5:", lrcur[5] );
		PromptVal( "LR6:", lrcur[6] );
		PromptVal( "LR7:", lrcur[7] );
		PromptVal( "LR8:", lrcur[8] );
		PromptVal( "LR9:", lrcur[9] );*/
	}
void StreamPacketizer::ClearBuffers()
{
    Buffer* buf;
    while( ( buf = PopPacket() ) )
        SafeDelete( buf );
}