tcp: Make tcp_option get_size and fill more readable

This commit is contained in:
Asias He
2015-01-15 03:55:49 +02:00
committed by Avi Kivity
parent 8ed3584a5e
commit f004db89cf
2 changed files with 23 additions and 17 deletions

View File

@@ -59,16 +59,18 @@ uint8_t tcp_option::fill(tcp_hdr* th, uint8_t options_size) {
auto hdr = reinterpret_cast<uint8_t*>(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

View File

@@ -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<InetTraits>::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<tcp_hdr>(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;