From f004db89cf2b83b8d63ad027efbeb72f985f5f6b Mon Sep 17 00:00:00 2001 From: Asias He Date: Thu, 15 Jan 2015 03:55:49 +0200 Subject: [PATCH] tcp: Make tcp_option get_size and fill more readable --- net/tcp.cc | 22 ++++++++++++++-------- net/tcp.hh | 18 +++++++++--------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/net/tcp.cc b/net/tcp.cc index 9b4a7e15e5..3bd7bf858c 100644 --- a/net/tcp.cc +++ b/net/tcp.cc @@ -59,16 +59,18 @@ uint8_t tcp_option::fill(tcp_hdr* th, uint8_t options_size) { auto hdr = reinterpret_cast(th); auto off = hdr + sizeof(tcp_hdr); uint8_t size = 0; + bool syn_on = th->f_syn; + bool ack_on = th->f_ack; - if (th->f_syn) { - if (_mss_received || !th->f_ack) { + if (syn_on) { + if (_mss_received || !ack_on) { auto mss = new (off) tcp_option::mss; mss->mss = _local_mss; off += mss->len; size += mss->len; *mss = hton(*mss); } - if (_win_scale_received || !th->f_ack) { + if (_win_scale_received || !ack_on) { auto win_scale = new (off) tcp_option::win_scale; win_scale->shift = _local_win_scale; off += win_scale->len; @@ -91,12 +93,16 @@ uint8_t tcp_option::fill(tcp_hdr* th, uint8_t options_size) { return size; } -uint8_t tcp_option::get_size(bool foreign_syn_received) { +uint8_t tcp_option::get_size(bool syn_on, bool ack_on) { uint8_t size = 0; - if (_mss_received || !foreign_syn_received) - size += option_len::mss; - if (_win_scale_received || !foreign_syn_received) - size += option_len::win_scale; + if (syn_on) { + if (_mss_received || !ack_on) { + size += option_len::mss; + } + if (_win_scale_received || !ack_on) { + size += option_len::win_scale; + } + } if (size > 0) { size += option_len::eol; // Insert NOP option to align on 32-bit diff --git a/net/tcp.hh b/net/tcp.hh index e886642a16..e3e30dfb49 100644 --- a/net/tcp.hh +++ b/net/tcp.hh @@ -68,7 +68,7 @@ struct tcp_option { void parse(uint8_t* beg, uint8_t* end); uint8_t fill(tcp_hdr* th, uint8_t option_size); - uint8_t get_size(bool foreign_syn_received); + uint8_t get_size(bool syn_on, bool ack_on); // For option negotiattion bool _mss_received = false; @@ -889,21 +889,21 @@ void tcp::tcb::output_one() { return; } - uint8_t options_size = 0; packet p = get_transmit_packet(); uint16_t len = p.len(); + bool syn_on = !_local_syn_acked; + bool ack_on = _foreign_syn_received; - if (!_local_syn_acked) { - options_size = _option.get_size(_foreign_syn_received); - } + auto options_size = _option.get_size(syn_on, ack_on); auto th = p.prepend_header(options_size); + th->src_port = _local_port; th->dst_port = _foreign_port; - th->f_syn = !_local_syn_acked; - _local_syn_sent |= th->f_syn; - th->f_ack = _foreign_syn_received; - if (th->f_ack) { + th->f_syn = syn_on; + _local_syn_sent |= syn_on; + th->f_ack = ack_on; + if (ack_on) { clear_delayed_ack(); } th->f_urg = false;