Changeset eea65f4 in mainline for uspace/srv/net/tl/tcp/header.c
- Timestamp:
- 2011-11-21T21:16:23Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 6896409c
- Parents:
- 004a5fe
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/tl/tcp/header.c
r004a5fe reea65f4 36 36 37 37 #include <byteorder.h> 38 #include <errno.h> 39 #include <mem.h> 40 #include <stdlib.h> 38 41 #include "header.h" 39 42 #include "segment.h" … … 41 44 #include "tcp_type.h" 42 45 43 void tcp_header_setup(tcp_conn_t *conn, tcp_segment_t *seg, tcp_header_t *hdr) 44 { 45 hdr->src_port = host2uint16_t_be(conn->ident.local.port); 46 hdr->dest_port = host2uint16_t_be(conn->ident.foreign.port); 47 hdr->seq = 0; 48 hdr->ack = 0; 49 hdr->doff_flags = 0; 50 hdr->window = 0; 46 #define TCP_CHECKSUM_INIT 0xffff 47 48 static uint16_t tcp_checksum_calc(uint16_t ivalue, void *data, size_t size) 49 { 50 uint16_t sum; 51 size_t words, i; 52 uint8_t *bdata; 53 54 sum = ~ivalue; 55 words = size / 2; 56 bdata = (uint8_t *)data; 57 58 for (i = 0; i < words; i++) { 59 sum += ~(((uint16_t)bdata[2*i] << 8) + bdata[2*i + 1]); 60 } 61 62 if (size % 2 != 0) { 63 sum += ~((uint16_t)bdata[2*words] << 8); 64 } 65 66 return ~sum; 67 } 68 69 static void tcp_header_decode_flags(uint16_t doff_flags, tcp_control_t *rctl) 70 { 71 tcp_control_t ctl; 72 73 ctl = 0; 74 75 if ((doff_flags & DF_URG) != 0) 76 ctl |= 0 /* XXX */; 77 if ((doff_flags & DF_ACK) != 0) 78 ctl |= CTL_ACK; 79 if ((doff_flags & DF_PSH) != 0) 80 ctl |= 0 /* XXX */; 81 if ((doff_flags & DF_RST) != 0) 82 ctl |= CTL_RST; 83 if ((doff_flags & DF_SYN) != 0) 84 ctl |= CTL_SYN; 85 if ((doff_flags & DF_FIN) != 0) 86 ctl |= CTL_FIN; 87 88 *rctl = ctl; 89 } 90 91 static void tcp_header_encode_flags(tcp_control_t ctl, uint16_t doff_flags0, 92 uint16_t *rdoff_flags) 93 { 94 uint16_t doff_flags; 95 96 doff_flags = doff_flags0; 97 98 if ((ctl & CTL_ACK) != 0) 99 doff_flags |= DF_ACK; 100 if ((ctl & CTL_RST) != 0) 101 doff_flags |= DF_RST; 102 if ((ctl & CTL_SYN) != 0) 103 doff_flags |= DF_SYN; 104 if ((ctl & CTL_FIN) != 0) 105 doff_flags |= DF_FIN; 106 107 *rdoff_flags = doff_flags; 108 } 109 110 static void tcp_header_setup(tcp_sockpair_t *sp, tcp_segment_t *seg, tcp_header_t *hdr) 111 { 112 uint16_t doff_flags; 113 114 hdr->src_port = host2uint16_t_be(sp->local.port); 115 hdr->dest_port = host2uint16_t_be(sp->foreign.port); 116 hdr->seq = host2uint32_t_be(seg->seq); 117 hdr->ack = host2uint32_t_be(seg->ack); 118 tcp_header_encode_flags(seg->ctrl, 0, &doff_flags); 119 hdr->doff_flags = host2uint16_t_be(doff_flags); 120 hdr->window = host2uint16_t_be(seg->wnd); 51 121 hdr->checksum = 0; 52 hdr->urg_ptr = 0;53 } 54 55 void tcp_phdr_setup(tcp_conn_t *conn, tcp_segment_t *seg, tcp_phdr_t *phdr)56 { 57 phdr->src_addr = conn->ident.local.addr.ipv4;58 phdr->dest_addr = conn->ident.foreign.addr.ipv4;122 hdr->urg_ptr = host2uint16_t_be(seg->up); 123 } 124 125 static void tcp_phdr_setup(tcp_pdu_t *pdu, tcp_phdr_t *phdr) 126 { 127 phdr->src_addr = host2uint32_t_be(pdu->src_addr.ipv4); 128 phdr->dest_addr = host2uint32_t_be(pdu->dest_addr.ipv4); 59 129 phdr->zero = 0; 60 phdr->protocol = 0; /* XXX */ 61 62 /* XXX This will only work as long as we don't have any header options */ 63 phdr->tcp_length = sizeof(tcp_header_t) + tcp_segment_text_size(seg); 130 phdr->protocol = 6; /* XXX Magic number */ 131 phdr->tcp_length = host2uint16_t_be(pdu->header_size + pdu->text_size); 132 } 133 134 static void tcp_header_decode(tcp_header_t *hdr, tcp_segment_t *seg) 135 { 136 tcp_header_decode_flags(uint16_t_be2host(hdr->doff_flags), &seg->ctrl); 137 seg->seq = uint32_t_be2host(hdr->seq); 138 seg->ack = uint32_t_be2host(hdr->ack); 139 seg->wnd = uint16_t_be2host(hdr->window); 140 seg->up = uint16_t_be2host(hdr->urg_ptr); 141 } 142 143 static int tcp_header_encode(tcp_sockpair_t *sp, tcp_segment_t *seg, 144 void **header, size_t *size) 145 { 146 tcp_header_t *hdr; 147 148 hdr = calloc(1, sizeof(tcp_header_t)); 149 if (hdr == NULL) 150 return ENOMEM; 151 152 tcp_header_setup(sp, seg, hdr); 153 *header = hdr; 154 *size = sizeof(tcp_header_t); 155 156 return EOK; 157 } 158 159 static tcp_pdu_t *tcp_pdu_new(void) 160 { 161 return calloc(1, sizeof(tcp_pdu_t)); 162 } 163 164 void tcp_pdu_delete(tcp_pdu_t *pdu) 165 { 166 free(pdu->text); 167 free(pdu); 168 } 169 170 static uint16_t tcp_pdu_checksum_calc(tcp_pdu_t *pdu) 171 { 172 uint16_t cs_phdr; 173 uint16_t cs_headers; 174 uint16_t cs_all; 175 tcp_phdr_t phdr; 176 177 tcp_phdr_setup(pdu, &phdr); 178 cs_phdr = tcp_checksum_calc(TCP_CHECKSUM_INIT, (void *)&phdr, 179 sizeof(tcp_phdr_t)); 180 cs_headers = tcp_checksum_calc(cs_phdr, pdu->header, pdu->header_size); 181 cs_all = tcp_checksum_calc(cs_headers, pdu->text, pdu->text_size); 182 183 return cs_all; 184 } 185 186 static void tcp_pdu_set_checksum(tcp_pdu_t *pdu, uint16_t checksum) 187 { 188 tcp_header_t *hdr; 189 190 hdr = (tcp_header_t *)pdu->header; 191 hdr->checksum = host2uint16_t_le(checksum); 192 } 193 194 /** Encode outgoing PDU */ 195 int tcp_pdu_decode(tcp_pdu_t *pdu, tcp_sockpair_t *sp, tcp_segment_t **seg) 196 { 197 tcp_segment_t *nseg; 198 tcp_header_t *hdr; 199 200 nseg = tcp_segment_make_data(0, pdu->text, pdu->text_size); 201 if (nseg == NULL) 202 return ENOMEM; 203 204 tcp_header_decode(pdu->header, nseg); 205 206 hdr = (tcp_header_t *)pdu->header; 207 208 sp->local.port = uint16_t_be2host(hdr->dest_port); 209 sp->local.addr = pdu->dest_addr; 210 sp->foreign.port = uint16_t_be2host(hdr->src_port); 211 sp->foreign.addr = pdu->src_addr; 212 213 *seg = nseg; 214 return EOK; 215 } 216 217 /** Decode incoming PDU */ 218 int tcp_pdu_encode(tcp_sockpair_t *sp, tcp_segment_t *seg, tcp_pdu_t **pdu) 219 { 220 tcp_pdu_t *npdu; 221 size_t text_size; 222 uint16_t checksum; 223 224 npdu = tcp_pdu_new(); 225 if (npdu == NULL) 226 return ENOMEM; 227 228 npdu->src_addr = sp->local.addr; 229 npdu->dest_addr = sp->foreign.addr; 230 tcp_header_encode(sp, seg, &npdu->header, &npdu->header_size); 231 232 text_size = tcp_segment_text_size(seg); 233 npdu->text = calloc(1, text_size); 234 if (npdu->text == NULL) 235 return ENOMEM; 236 237 npdu->text_size = text_size; 238 memcpy(npdu->text, seg->data, text_size); 239 240 /* Checksum calculation */ 241 checksum = tcp_pdu_checksum_calc(npdu); 242 tcp_pdu_set_checksum(npdu, checksum); 243 244 *pdu = npdu; 245 return EOK; 64 246 } 65 247
Note:
See TracChangeset
for help on using the changeset viewer.