INET Framework for OMNeT++/OMNEST
inet::tcp::TcpLwipConnection Class Reference

#include <TcpLwipConnection.h>

Classes

class  Stats
 

Public Member Functions

 TcpLwipConnection (TCP_lwIP &tcpLwipP, int connIdP, int gateIndexP, TCPDataTransferMode dataTransferModeP)
 
 TcpLwipConnection (TcpLwipConnection &tcpLwipConnectionP, int connIdP, LwipTcpLayer::tcp_pcb *pcbP)
 
 ~TcpLwipConnection ()
 
void sendEstablishedMsg ()
 
void sendIndicationToApp (int code)
 
void listen (L3Address &localAddr, unsigned short localPort)
 
void connect (L3Address &localAddr, unsigned short localPort, L3Address &remoteAddr, unsigned short remotePort)
 
void close ()
 
void abort ()
 
void send (cPacket *msgP)
 
void fillStatusInfo (TCPStatusInfo &statusInfo)
 
void notifyAboutSending (const TCPSegment &tcpsegP)
 
int send_data (void *data, int len)
 
void do_SEND ()
 
INetStreamSocket * getSocket ()
 
void initStats ()
 

Static Public Member Functions

static const char * indicationName (int code)
 

Public Attributes

int connIdM
 
int appGateIndexM
 
LwipTcpLayer::tcp_pcb * pcbM
 
TcpLwipSendQueuesendQueueM
 
TcpLwipReceiveQueuereceiveQueueM
 
TCP_lwIPtcpLwipM
 

Protected Member Functions

 TcpLwipConnection (const TcpLwipConnection &)
 

Protected Attributes

long int totalSentM
 
bool isListenerM
 
bool onCloseM
 
StatsstatsM
 

Constructor & Destructor Documentation

inet::tcp::TcpLwipConnection::TcpLwipConnection ( const TcpLwipConnection )
protected
inet::tcp::TcpLwipConnection::TcpLwipConnection ( TCP_lwIP tcpLwipP,
int  connIdP,
int  gateIndexP,
TCPDataTransferMode  dataTransferModeP 
)
72  :
73  connIdM(connIdP),
74  appGateIndexM(gateIndexP),
75  pcbM(nullptr),
76  sendQueueM(tcpLwipP.createSendQueue(dataTransferModeP)),
77  receiveQueueM(tcpLwipP.createReceiveQueue(dataTransferModeP)),
78  tcpLwipM(tcpLwipP),
79  totalSentM(0),
80  isListenerM(false),
81  onCloseM(false),
82  statsM(nullptr)
83 {
84  pcbM = tcpLwipM.getLwipTcpLayer()->tcp_new(); //FIXME memory leak
85  ASSERT(pcbM);
86 
87  pcbM->callback_arg = this;
88 
91 
93  statsM = new Stats();
94 }
bool recordStatisticsM
Definition: TCP_lwIP.h:157
bool onCloseM
Definition: TcpLwipConnection.h:121
LwipTcpLayer * getLwipTcpLayer()
Definition: TCP_lwIP.h:132
bool isListenerM
Definition: TcpLwipConnection.h:120
LwipTcpLayer::tcp_pcb * pcbM
Definition: TcpLwipConnection.h:113
int connIdM
Definition: TcpLwipConnection.h:111
int appGateIndexM
Definition: TcpLwipConnection.h:112
virtual void setConnection(TcpLwipConnection *connP)
set connection queue, and initialise queue variables.
Definition: TcpLwipQueues.h:101
virtual void setConnection(TcpLwipConnection *connP)
Add a connection queue.
Definition: TcpLwipQueues.h:164
TcpLwipSendQueue * sendQueueM
Definition: TcpLwipConnection.h:114
TCP_lwIP & tcpLwipM
Definition: TcpLwipConnection.h:116
TcpLwipReceiveQueue * receiveQueueM
Definition: TcpLwipConnection.h:115
Stats * statsM
Definition: TcpLwipConnection.h:123
long int totalSentM
Definition: TcpLwipConnection.h:119
inet::tcp::TcpLwipConnection::TcpLwipConnection ( TcpLwipConnection tcpLwipConnectionP,
int  connIdP,
LwipTcpLayer::tcp_pcb *  pcbP 
)
98  :
99  connIdM(connIdP),
100  appGateIndexM(connP.appGateIndexM),
101  pcbM(pcbP),
102  sendQueueM(check_and_cast<TcpLwipSendQueue *>(inet::utils::createOne(connP.sendQueueM->getClassName()))),
103  receiveQueueM(check_and_cast<TcpLwipReceiveQueue *>(inet::utils::createOne(connP.receiveQueueM->getClassName()))),
104  tcpLwipM(connP.tcpLwipM),
105  totalSentM(0),
106  isListenerM(false),
107  onCloseM(false),
108  statsM(nullptr)
109 {
110  pcbM = pcbP;
111  pcbM->callback_arg = this;
112 
113  sendQueueM->setConnection(this);
115 
117  statsM = new Stats();
118 }
bool recordStatisticsM
Definition: TCP_lwIP.h:157
bool onCloseM
Definition: TcpLwipConnection.h:121
bool isListenerM
Definition: TcpLwipConnection.h:120
LwipTcpLayer::tcp_pcb * pcbM
Definition: TcpLwipConnection.h:113
int connIdM
Definition: TcpLwipConnection.h:111
cObject * createOne(const char *className, const char *defaultNamespace)
Like cObjectFactory::createOne(), except it starts searching for the class in the given namespace...
Definition: INETUtils.cc:105
int appGateIndexM
Definition: TcpLwipConnection.h:112
virtual void setConnection(TcpLwipConnection *connP)
set connection queue, and initialise queue variables.
Definition: TcpLwipQueues.h:101
virtual void setConnection(TcpLwipConnection *connP)
Add a connection queue.
Definition: TcpLwipQueues.h:164
TcpLwipSendQueue * sendQueueM
Definition: TcpLwipConnection.h:114
TCP_lwIP & tcpLwipM
Definition: TcpLwipConnection.h:116
TcpLwipReceiveQueue * receiveQueueM
Definition: TcpLwipConnection.h:115
Stats * statsM
Definition: TcpLwipConnection.h:123
long int totalSentM
Definition: TcpLwipConnection.h:119
inet::tcp::TcpLwipConnection::~TcpLwipConnection ( )
121 {
122  if (pcbM)
123  pcbM->callback_arg = nullptr;
124  //FIXME memory leak, who will free pcbM?
125 
126  delete receiveQueueM;
127  delete sendQueueM;
128  delete statsM;
129 }
LwipTcpLayer::tcp_pcb * pcbM
Definition: TcpLwipConnection.h:113
TcpLwipSendQueue * sendQueueM
Definition: TcpLwipConnection.h:114
TcpLwipReceiveQueue * receiveQueueM
Definition: TcpLwipConnection.h:115
Stats * statsM
Definition: TcpLwipConnection.h:123

Member Function Documentation

void inet::tcp::TcpLwipConnection::abort ( )

Referenced by inet::tcp::TCP_lwIP::process_ABORT().

248 {
249  tcpLwipM.getLwipTcpLayer()->tcp_close(pcbM);
250  onCloseM = false;
251 }
bool onCloseM
Definition: TcpLwipConnection.h:121
LwipTcpLayer * getLwipTcpLayer()
Definition: TCP_lwIP.h:132
LwipTcpLayer::tcp_pcb * pcbM
Definition: TcpLwipConnection.h:113
TCP_lwIP & tcpLwipM
Definition: TcpLwipConnection.h:116
void inet::tcp::TcpLwipConnection::close ( )

Referenced by inet::tcp::TCP_lwIP::process_CLOSE().

238 {
239  onCloseM = true;
240 
241  if (0 == sendQueueM->getBytesAvailable()) {
242  tcpLwipM.getLwipTcpLayer()->tcp_close(pcbM);
243  onCloseM = false;
244  }
245 }
bool onCloseM
Definition: TcpLwipConnection.h:121
LwipTcpLayer * getLwipTcpLayer()
Definition: TCP_lwIP.h:132
LwipTcpLayer::tcp_pcb * pcbM
Definition: TcpLwipConnection.h:113
virtual unsigned long getBytesAvailable() const =0
Utility function: returns how many bytes are available in the queue.
TcpLwipSendQueue * sendQueueM
Definition: TcpLwipConnection.h:114
TCP_lwIP & tcpLwipM
Definition: TcpLwipConnection.h:116
void inet::tcp::TcpLwipConnection::connect ( L3Address localAddr,
unsigned short  localPort,
L3Address remoteAddr,
unsigned short  remotePort 
)

Referenced by inet::tcp::TCP_lwIP::process_OPEN_ACTIVE().

226 {
227  onCloseM = false;
228  struct ip_addr src_addr;
229  src_addr.addr = localAddr;
230  struct ip_addr dest_addr;
231  dest_addr.addr = remoteAddr;
232  tcpLwipM.getLwipTcpLayer()->tcp_bind(pcbM, &src_addr, localPort);
233  tcpLwipM.getLwipTcpLayer()->tcp_connect(pcbM, &dest_addr, (u16_t)remotePort, nullptr);
234  totalSentM = 0;
235 }
bool onCloseM
Definition: TcpLwipConnection.h:121
LwipTcpLayer * getLwipTcpLayer()
Definition: TCP_lwIP.h:132
Definition: ip_addr.h.txt:47
LwipTcpLayer::tcp_pcb * pcbM
Definition: TcpLwipConnection.h:113
uint16_t u16_t
Definition: cc.h:35
TCP_lwIP & tcpLwipM
Definition: TcpLwipConnection.h:116
long int totalSentM
Definition: TcpLwipConnection.h:119
void inet::tcp::TcpLwipConnection::do_SEND ( )

Referenced by inet::tcp::TCP_lwIP::tcp_event_accept(), inet::tcp::TCP_lwIP::tcp_event_conn(), inet::tcp::TCP_lwIP::tcp_event_poll(), inet::tcp::TCP_lwIP::tcp_event_recv(), and inet::tcp::TCP_lwIP::tcp_event_sent().

312 {
313  char buffer[8 * 536];
314  int bytes;
315  int allsent = 0;
316 
317  while (0 != (bytes = sendQueueM->getBytesForTcpLayer(buffer, sizeof(buffer)))) {
318  int sent = send_data(buffer, bytes);
319 
320  if (sent > 0) {
322  allsent += sent;
323  }
324  else {
325  if (sent != ERR_MEM)
326  EV_ERROR << "TCP_lwIP connection: " << connIdM << ": Error do sending, err is " << sent << endl;
327  break;
328  }
329  }
330 
331  totalSentM += allsent;
332  EV_DETAIL << "do_SEND(): " << connIdM
333  << " send:" << allsent
334  << ", unsent:" << sendQueueM->getBytesAvailable()
335  << ", total sent:" << totalSentM
336  << ", all bytes:" << totalSentM + sendQueueM->getBytesAvailable()
337  << endl;
338 
339  if (onCloseM && (0 == sendQueueM->getBytesAvailable())) {
340  tcpLwipM.getLwipTcpLayer()->tcp_close(pcbM);
341  onCloseM = false;
342  }
343 }
bool onCloseM
Definition: TcpLwipConnection.h:121
LwipTcpLayer * getLwipTcpLayer()
Definition: TCP_lwIP.h:132
LwipTcpLayer::tcp_pcb * pcbM
Definition: TcpLwipConnection.h:113
int connIdM
Definition: TcpLwipConnection.h:111
int send_data(void *data, int len)
Definition: TcpLwipConnection.cc:266
virtual unsigned long getBytesAvailable() const =0
Utility function: returns how many bytes are available in the queue.
virtual void dequeueTcpLayerMsg(unsigned int msgLengthP)=0
This function should remove msgLengthP bytes from TCP layer queue.
TcpLwipSendQueue * sendQueueM
Definition: TcpLwipConnection.h:114
#define ERR_MEM
Definition: err.h:57
TCP_lwIP & tcpLwipM
Definition: TcpLwipConnection.h:116
virtual unsigned int getBytesForTcpLayer(void *bufferP, unsigned int bufferLengthP) const =0
Copy data to the buffer for send to LWIP.
long int totalSentM
Definition: TcpLwipConnection.h:119
void inet::tcp::TcpLwipConnection::fillStatusInfo ( TCPStatusInfo statusInfo)

Referenced by inet::tcp::TCP_lwIP::process_STATUS().

186 {
187 //TODO statusInfo.setState(fsm.getState());
188 //TODO statusInfo.setStateName(stateName(fsm.getState()));
189 
190  statusInfo.setLocalAddr((pcbM->local_ip.addr));
191  statusInfo.setLocalPort(pcbM->local_port);
192  statusInfo.setRemoteAddr((pcbM->remote_ip.addr));
193  statusInfo.setRemotePort(pcbM->remote_port);
194 
195  statusInfo.setSnd_mss(pcbM->mss);
196 //TODO statusInfo.setSnd_una(pcbM->snd_una);
197  statusInfo.setSnd_nxt(pcbM->snd_nxt);
198 //TODO statusInfo.setSnd_max(pcbM->snd_max);
199  statusInfo.setSnd_wnd(pcbM->snd_wnd);
200 //TODO statusInfo.setSnd_up(pcbM->snd_up);
201  statusInfo.setSnd_wl1(pcbM->snd_wl1);
202  statusInfo.setSnd_wl2(pcbM->snd_wl2);
203 //TODO statusInfo.setIss(pcbM->iss);
204  statusInfo.setRcv_nxt(pcbM->rcv_nxt);
205  statusInfo.setRcv_wnd(pcbM->rcv_wnd);
206 //TODO statusInfo.setRcv_up(pcbM->rcv_up);
207 //TODO statusInfo.setIrs(pcbM->irs);
208 //TODO statusInfo.setFin_ack_rcvd(pcbM->fin_ack_rcvd);
209 }
LwipTcpLayer::tcp_pcb * pcbM
Definition: TcpLwipConnection.h:113
INetStreamSocket* inet::tcp::TcpLwipConnection::getSocket ( )
const char * inet::tcp::TcpLwipConnection::indicationName ( int  code)
static

Referenced by sendIndicationToApp().

152 {
153 #define CASE(x) case x: \
154  s = #x + 6; break
155  const char *s = "unknown";
156 
157  switch (code) {
158  CASE(TCP_I_DATA);
167  }
168 
169  return s;
170 #undef CASE
171 }
Definition: TCPCommand_m.h:102
Definition: TCPCommand_m.h:101
Definition: TCPCommand_m.h:100
Definition: TCPCommand_m.h:104
Definition: TCPCommand_m.h:99
value< double, units::s > s
Definition: Units.h:1049
Definition: TCPCommand_m.h:106
Definition: TCPCommand_m.h:98
#define CASE(x)
Definition: TCPCommand_m.h:103
Definition: TCPCommand_m.h:105
void inet::tcp::TcpLwipConnection::initStats ( )
void inet::tcp::TcpLwipConnection::listen ( L3Address localAddr,
unsigned short  localPort 
)

Referenced by inet::tcp::TCP_lwIP::process_OPEN_PASSIVE().

212 {
213  onCloseM = false;
214  tcpLwipM.getLwipTcpLayer()->tcp_bind(pcbM, nullptr, localPort);
215  // The next returns a tcp_pcb: need to do some research on how
216  // it works; does it actually accept a connection as well? It
217  // shouldn't, as there is a tcp_accept
218  LwipTcpLayer::tcp_pcb *pcb = pcbM;
219  pcbM = nullptr; // unlink old pcb from this, otherwise lwip_free_pcb_event destroy this conn.
220  pcbM = tcpLwipM.getLwipTcpLayer()->tcp_listen(pcb);
221  totalSentM = 0;
222 }
bool onCloseM
Definition: TcpLwipConnection.h:121
LwipTcpLayer * getLwipTcpLayer()
Definition: TCP_lwIP.h:132
LwipTcpLayer::tcp_pcb * pcbM
Definition: TcpLwipConnection.h:113
TCP_lwIP & tcpLwipM
Definition: TcpLwipConnection.h:116
long int totalSentM
Definition: TcpLwipConnection.h:119
void inet::tcp::TcpLwipConnection::notifyAboutSending ( const TCPSegment tcpsegP)

Referenced by inet::tcp::TCP_lwIP::ip_output().

259 {
261 
262  if (statsM)
263  statsM->recordSend(tcpsegP);
264 }
virtual void notifyAboutSending(const TCPSegment *tcpsegP)=0
notify the queue about output messages
void recordSend(const TCPSegment &tcpsegP)
Definition: TcpLwipConnection.cc:52
TcpLwipReceiveQueue * receiveQueueM
Definition: TcpLwipConnection.h:115
Stats * statsM
Definition: TcpLwipConnection.h:123
void inet::tcp::TcpLwipConnection::send ( cPacket *  msgP)

Referenced by inet::tcp::TCP_lwIP::process_SEND().

254 {
255  sendQueueM->enqueueAppData(msgP);
256 }
virtual void enqueueAppData(cPacket *msgP)=0
Called on SEND app command, it inserts in the queue the data the user wants to send.
TcpLwipSendQueue * sendQueueM
Definition: TcpLwipConnection.h:114
int inet::tcp::TcpLwipConnection::send_data ( void *  data,
int  len 
)

Referenced by do_SEND().

267 {
268  int error;
269  int written = 0;
270 
271  if (datalen > 0xFFFF)
272  datalen = 0xFFFF; // tcp_write() length argument is uint16_t
273 
274  u32_t ss = pcbM->snd_lbb;
275  error = tcpLwipM.getLwipTcpLayer()->tcp_write(pcbM, data, datalen, 1);
276 
277  if (error == ERR_OK) {
278  written = datalen;
279  }
280  else if (error == ERR_MEM) {
281  // Chances are that datalen is too large to fit in the send
282  // buffer. If it is really large (larger than a typical MSS,
283  // say), we should try segmenting the data ourselves.
284  while (1) {
285  u16_t snd_buf = pcbM->snd_buf;
286  if (0 == snd_buf)
287  break;
288 
289  if (datalen < snd_buf)
290  break;
291 
292  error = tcpLwipM.getLwipTcpLayer()->tcp_write(
293  pcbM, ((const char *)data) + written, snd_buf, 1);
294 
295  if (error != ERR_OK)
296  break;
297 
298  written += snd_buf;
299  datalen -= snd_buf;
300  }
301  }
302 
303  if (written > 0) {
304  ASSERT(pcbM->snd_lbb - ss == (u32_t)written);
305  return written;
306  }
307 
308  return error;
309 }
LwipTcpLayer * getLwipTcpLayer()
Definition: TCP_lwIP.h:132
LwipTcpLayer::tcp_pcb * pcbM
Definition: TcpLwipConnection.h:113
#define ERR_OK
Definition: err.h:56
uint16_t u16_t
Definition: cc.h:35
uint32_t u32_t
Definition: cc.h:39
#define ERR_MEM
Definition: err.h:57
TCP_lwIP & tcpLwipM
Definition: TcpLwipConnection.h:116
void inet::tcp::TcpLwipConnection::sendEstablishedMsg ( )

Referenced by inet::tcp::TCP_lwIP::sendEstablishedMsg(), inet::tcp::TCP_lwIP::tcp_event_accept(), and inet::tcp::TCP_lwIP::tcp_event_conn().

132 {
133  cMessage *msg = new cMessage("TCP_I_ESTABLISHED");
134  msg->setKind(TCP_I_ESTABLISHED);
135 
136  TCPConnectInfo *tcpConnectInfo = new TCPConnectInfo();
137 
138  L3Address localAddr(pcbM->local_ip.addr), remoteAddr(pcbM->remote_ip.addr);
139 
140  tcpConnectInfo->setConnId(connIdM);
141  tcpConnectInfo->setLocalAddr(localAddr);
142  tcpConnectInfo->setRemoteAddr(remoteAddr);
143  tcpConnectInfo->setLocalPort(pcbM->local_port);
144  tcpConnectInfo->setRemotePort(pcbM->remote_port);
145 
146  msg->setControlInfo(tcpConnectInfo);
147 
148  tcpLwipM.send(msg, "appOut", appGateIndexM);
149 }
Definition: TCPCommand_m.h:100
LwipTcpLayer::tcp_pcb * pcbM
Definition: TcpLwipConnection.h:113
int connIdM
Definition: TcpLwipConnection.h:111
int appGateIndexM
Definition: TcpLwipConnection.h:112
TCP_lwIP & tcpLwipM
Definition: TcpLwipConnection.h:116
void inet::tcp::TcpLwipConnection::sendIndicationToApp ( int  code)

Referenced by inet::tcp::TCP_lwIP::tcp_event_err(), and inet::tcp::TCP_lwIP::tcp_event_recv().

174 {
175  const char *nameOfIndication = indicationName(code);
176  EV_DETAIL << "Notifying app: " << nameOfIndication << "\n";
177  cMessage *msg = new cMessage(nameOfIndication);
178  msg->setKind(code);
179  TCPCommand *ind = new TCPCommand();
180  ind->setConnId(connIdM);
181  msg->setControlInfo(ind);
182  tcpLwipM.send(msg, "appOut", appGateIndexM);
183 }
static const char * indicationName(int code)
Definition: TcpLwipConnection.cc:151
int connIdM
Definition: TcpLwipConnection.h:111
int appGateIndexM
Definition: TcpLwipConnection.h:112
TCP_lwIP & tcpLwipM
Definition: TcpLwipConnection.h:116

Member Data Documentation

bool inet::tcp::TcpLwipConnection::isListenerM
protected
bool inet::tcp::TcpLwipConnection::onCloseM
protected

Referenced by abort(), close(), connect(), do_SEND(), and listen().

TcpLwipSendQueue* inet::tcp::TcpLwipConnection::sendQueueM
Stats* inet::tcp::TcpLwipConnection::statsM
protected
long int inet::tcp::TcpLwipConnection::totalSentM
protected

Referenced by connect(), do_SEND(), and listen().


The documentation for this class was generated from the following files: