Skip to content

Commit

Permalink
[tcp] restrict inclusion of third_party/tcplp/tcplp.h
Browse files Browse the repository at this point in the history
  • Loading branch information
samkumar authored and jwhui committed Apr 6, 2022
1 parent 58e4f85 commit a1721ea
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 38 deletions.
59 changes: 55 additions & 4 deletions src/core/net/tcp6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ using ot::Encoding::BigEndian::HostSwap32;

RegisterLogModule("Tcp");

static_assert(sizeof(struct tcpcb) == sizeof(Tcp::Endpoint::mTcb), "mTcb field in otTcpEndpoint is sized incorrectly");
static_assert(alignof(struct tcpcb) == alignof(decltype(Tcp::Endpoint::mTcb)),
"mTcb field in otTcpEndpoint is aligned incorrectly");
static_assert(offsetof(Tcp::Endpoint, mTcb) == 0, "mTcb field in otTcpEndpoint has nonzero offset");

static_assert(sizeof(struct tcpcb_listen) == sizeof(Tcp::Listener::mTcbListen),
"mTcbListen field in otTcpListener is sized incorrectly");
static_assert(alignof(struct tcpcb_listen) == alignof(decltype(Tcp::Listener::mTcbListen)),
"mTcbListen field in otTcpListener is aligned incorrectly");
static_assert(offsetof(Tcp::Listener, mTcbListen) == 0, "mTcbListen field in otTcpEndpoint has nonzero offset");

Tcp::Tcp(Instance &aInstance)
: InstanceLocator(aInstance)
, mTimer(aInstance, Tcp::HandleTimer)
Expand Down Expand Up @@ -259,6 +270,11 @@ Error Tcp::Endpoint::Deinitialize(void)
return error;
}

bool Tcp::Endpoint::IsClosed(void) const
{
return GetTcb().t_state == TCP6S_CLOSED;
}

uint8_t Tcp::Endpoint::TimerFlagToIndex(uint8_t aTimerFlag)
{
uint8_t timerIndex = 0;
Expand Down Expand Up @@ -412,6 +428,26 @@ bool Tcp::Endpoint::FirePendingTimers(TimeMilli aNow, bool &aHasFutureTimer, Tim
return calledUserCallback;
}

Address &Tcp::Endpoint::GetLocalIp6Address(void)
{
return *reinterpret_cast<Address *>(&GetTcb().laddr);
}

const Address &Tcp::Endpoint::GetLocalIp6Address(void) const
{
return *reinterpret_cast<const Address *>(&GetTcb().laddr);
}

Address &Tcp::Endpoint::GetForeignIp6Address(void)
{
return *reinterpret_cast<Address *>(&GetTcb().faddr);
}

const Address &Tcp::Endpoint::GetForeignIp6Address(void) const
{
return *reinterpret_cast<const Address *>(&GetTcb().faddr);
}

bool Tcp::Endpoint::Matches(const MessageInfo &aMessageInfo) const
{
bool matches = false;
Expand Down Expand Up @@ -494,6 +530,21 @@ Error Tcp::Listener::Deinitialize(void)
return error;
}

bool Tcp::Listener::IsClosed(void) const
{
return GetTcbListen().t_state == TCP6S_CLOSED;
}

Address &Tcp::Listener::GetLocalIp6Address(void)
{
return *reinterpret_cast<Address *>(&GetTcbListen().laddr);
}

const Address &Tcp::Listener::GetLocalIp6Address(void) const
{
return *reinterpret_cast<const Address *>(&GetTcbListen().laddr);
}

bool Tcp::Listener::Matches(const MessageInfo &aMessageInfo) const
{
bool matches = false;
Expand Down Expand Up @@ -551,9 +602,9 @@ Error Tcp::HandleMessage(ot::Ip6::Header &aIp6Header, Message &aMessage, Message
endpoint = mEndpoints.FindMatching(aMessageInfo, endpointPrev);
if (endpoint != nullptr)
{
struct signals sig;
int nextAction;
struct tcpcb * tp = &endpoint->GetTcb();
struct tcplp_signals sig;
int nextAction;
struct tcpcb * tp = &endpoint->GetTcb();

otLinkedBuffer *priorHead = lbuf_head(&tp->sendbuf);

Expand Down Expand Up @@ -583,7 +634,7 @@ Error Tcp::HandleMessage(ot::Ip6::Header &aIp6Header, Message &aMessage, Message
return error;
}

void Tcp::ProcessSignals(Endpoint &aEndpoint, otLinkedBuffer *aPriorHead, struct signals &aSignals)
void Tcp::ProcessSignals(Endpoint &aEndpoint, otLinkedBuffer *aPriorHead, struct tcplp_signals &aSignals)
{
VerifyOrExit(IsInitialized(aEndpoint) && !aEndpoint.IsClosed());
if (aEndpoint.mSendDoneCallback != nullptr)
Expand Down
47 changes: 21 additions & 26 deletions src/core/net/tcp6.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,17 @@
#include "net/ip6_headers.hpp"
#include "net/socket.hpp"

#include "../../third_party/tcplp/tcplp.h"
/*
* These structures and functions are forward-declared here to avoid
* #includ'ing third_party/tcplp/tcplp.h in this header file.
*/
extern "C" {
struct tcpcb;
struct tcpcb_listen;
struct tcplp_signals;
void tcplp_sys_set_timer(struct tcpcb *aTcb, uint8_t aTimerFlag, uint32_t aDelay);
void tcplp_sys_stop_timer(struct tcpcb *aTcb, uint8_t aTimerFlag);
}

namespace ot {
namespace Ip6 {
Expand Down Expand Up @@ -352,12 +362,11 @@ class Tcp : public InstanceLocator, private NonCopyable
/**
* Checks if this Endpoint is in the closed state.
*/
bool IsClosed(void) const { return GetTcb().t_state == TCP6S_CLOSED; }
bool IsClosed(void) const;

private:
friend void ::tcplp_sys_set_timer(struct tcpcb *aTcb, uint8_t aTimerFlag, uint32_t aDelay);
friend void ::tcplp_sys_stop_timer(struct tcpcb *aTcb, uint8_t aTimerFlag);
friend void ::tcplp_sys_connection_lost(struct tcpcb *aTcb, uint8_t aErrNum);

enum : uint8_t
{
Expand All @@ -375,18 +384,13 @@ class Tcp : public InstanceLocator, private NonCopyable
void CancelTimer(uint8_t aTimerFlag);
bool FirePendingTimers(TimeMilli aNow, bool &aHasFutureTimer, TimeMilli &aEarliestFutureExpiry);

Address & GetLocalIp6Address(void) { return *reinterpret_cast<Address *>(&GetTcb().laddr); }
const Address &GetLocalIp6Address(void) const { return *reinterpret_cast<const Address *>(&GetTcb().laddr); }
Address & GetForeignIp6Address(void) { return *reinterpret_cast<Address *>(&GetTcb().faddr); }
const Address &GetForeignIp6Address(void) const { return *reinterpret_cast<const Address *>(&GetTcb().faddr); }
Address & GetLocalIp6Address(void);
const Address &GetLocalIp6Address(void) const;
Address & GetForeignIp6Address(void);
const Address &GetForeignIp6Address(void) const;
bool Matches(const MessageInfo &aMessageInfo) const;
};

static_assert(sizeof(struct tcpcb) == sizeof(Endpoint::mTcb), "mTcb field in otTcpEndpoint is sized incorrectly");
static_assert(alignof(struct tcpcb) == alignof(decltype(Endpoint::mTcb)),
"mTcb field in otTcpEndpoint is aligned incorrectly");
static_assert(offsetof(Endpoint, mTcb) == 0, "mTcb field in otTcpEndpoint has nonzero offset");

/**
* This class represents a TCP/IPv6 listener.
*
Expand Down Expand Up @@ -506,23 +510,14 @@ class Tcp : public InstanceLocator, private NonCopyable
/**
* Checks if this Listener is in the closed state.
*/
bool IsClosed(void) const { return GetTcbListen().t_state == TCP6S_CLOSED; }
bool IsClosed(void) const;

private:
Address & GetLocalIp6Address(void) { return *reinterpret_cast<Address *>(&GetTcbListen().laddr); }
const Address &GetLocalIp6Address(void) const
{
return *reinterpret_cast<const Address *>(&GetTcbListen().laddr);
}
bool Matches(const MessageInfo &aMessageInfo) const;
Address & GetLocalIp6Address(void);
const Address &GetLocalIp6Address(void) const;
bool Matches(const MessageInfo &aMessageInfo) const;
};

static_assert(sizeof(struct tcpcb_listen) == sizeof(Listener::mTcbListen),
"mTcbListen field in otTcpListener is sized incorrectly");
static_assert(alignof(struct tcpcb_listen) == alignof(decltype(Listener::mTcbListen)),
"mTcbListen field in otTcpListener is aligned incorrectly");
static_assert(offsetof(Listener, mTcbListen) == 0, "mTcbListen field in otTcpEndpoint has nonzero offset");

/**
* This class implements TCP header parsing.
*
Expand Down Expand Up @@ -661,7 +656,7 @@ class Tcp : public InstanceLocator, private NonCopyable
kDynamicPortMax = 65535, ///< Service Name and Transport Protocol Port Number Registry
};

void ProcessSignals(Endpoint &aEndpoint, otLinkedBuffer *aPriorHead, struct signals &aSignals);
void ProcessSignals(Endpoint &aEndpoint, otLinkedBuffer *aPriorHead, struct tcplp_signals &aSignals);

static Error BsdErrorToOtError(int aBsdError);
bool CanBind(const SockAddr &aSockName);
Expand Down
6 changes: 3 additions & 3 deletions third_party/tcplp/bsdtcp/tcp_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ static void tcp_dooptions(struct tcpopt *, uint8_t *, int, int);
static void
tcp_do_segment(struct ip6_hdr* ip6, struct tcphdr *th, otMessage* msg,
struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos,
struct signals* sig);
struct tcplp_signals* sig);
static void tcp_xmit_timer(struct tcpcb *, int);
void tcp_hc_get(/*struct in_conninfo *inc*/ struct tcpcb* tp, struct hc_metrics_lite *hc_metrics_lite);
static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
Expand Down Expand Up @@ -431,7 +431,7 @@ tcp_dropwithreset(struct ip6_hdr* ip6, struct tcphdr *th, struct tcpcb *tp, otIn
/* NOTE: tcp_fields_to_host(th) must be called before this function is called. */
int
tcp_input(struct ip6_hdr* ip6, struct tcphdr* th, otMessage* msg, struct tcpcb* tp, struct tcpcb_listen* tpl,
struct signals* sig)
struct tcplp_signals* sig)
{
/*
* samkumar: I significantly modified this function, compared to the
Expand Down Expand Up @@ -948,7 +948,7 @@ tcp_input(struct ip6_hdr* ip6, struct tcphdr* th, otMessage* msg, struct tcpcb*
static void
tcp_do_segment(struct ip6_hdr* ip6, struct tcphdr *th, otMessage* msg,
struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos,
struct signals* sig)
struct tcplp_signals* sig)
{
/*
* samkumar: All code pertaining to locks, stats, and debug has been
Expand Down
2 changes: 1 addition & 1 deletion third_party/tcplp/bsdtcp/tcp_reass.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
* not need to update it if only part of the segment is trimmed off.
*/
int
tcp_reass(struct tcpcb* tp, struct tcphdr* th, int* tlenp, otMessage* data, off_t data_offset, struct signals* sig)
tcp_reass(struct tcpcb* tp, struct tcphdr* th, int* tlenp, otMessage* data, off_t data_offset, struct tcplp_signals* sig)
{
size_t mergeable, written;
size_t offset;
Expand Down
6 changes: 3 additions & 3 deletions third_party/tcplp/bsdtcp/tcp_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ struct tcpcb_listen {
#define SACKHOLE_POOL_SIZE MAX_SACKHOLES
#define SACKHOLE_BMP_SIZE BITS_TO_BYTES(SACKHOLE_POOL_SIZE)

struct signals;
struct tcplp_signals;

/*
* Tcp control block, one per tcp; fields:
Expand Down Expand Up @@ -577,14 +577,14 @@ int tcp_twcheck(struct tcpcb*, struct tcphdr *, int);
void tcp_dropwithreset(struct ip6_hdr* ip6, struct tcphdr *th, struct tcpcb *tp, otInstance* instance,
int tlen, int rstreason);
int tcp_input(struct ip6_hdr* ip6, struct tcphdr* th, otMessage* msg, struct tcpcb* tp, struct tcpcb_listen* tpl,
struct signals* sig);
struct tcplp_signals* sig);
int tcp_output(struct tcpcb *);
void tcpip_maketemplate(struct tcpcb *, struct tcptemp*);
void tcpip_fillheaders(struct tcpcb *, otMessageInfo *, void *);
uint64_t tcp_maxmtu6(struct tcpcb*, struct tcp_ifcap *);
int tcp_addoptions(struct tcpopt *, uint8_t *);
int tcp_mssopt(struct tcpcb*);
int tcp_reass(struct tcpcb *, struct tcphdr *, int *, otMessage *, off_t, struct signals*);
int tcp_reass(struct tcpcb *, struct tcphdr *, int *, otMessage *, off_t, struct tcplp_signals*);
void tcp_sack_init(struct tcpcb *); // Sam: new function that I added
void tcp_sack_doack(struct tcpcb *, struct tcpopt *, tcp_seq);
void tcp_update_sack_list(struct tcpcb *tp, tcp_seq rcv_laststart, tcp_seq rcv_lastend);
Expand Down
2 changes: 1 addition & 1 deletion third_party/tcplp/tcplp.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extern "C" {
#define RELOOKUP_REQUIRED -1
#define CONN_LOST_NORMAL 0

struct signals {
struct tcplp_signals {
int links_popped;
bool conn_established;
bool recvbuf_notempty;
Expand Down

0 comments on commit a1721ea

Please sign in to comment.