Changes in uspace/srv/net/tcp/pdu.c [12df1f1:4a0bc99] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/tcp/pdu.c
r12df1f1 r4a0bc99 1 1 /* 2 * Copyright (c) 201 1Jiri Svoboda2 * Copyright (c) 2015 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 38 38 #include <byteorder.h> 39 39 #include <errno.h> 40 #include <inet/endpoint.h> 40 41 #include <mem.h> 41 42 #include <stdlib.h> 42 #include <net/socket_codes.h>43 43 #include "pdu.h" 44 44 #include "segment.h" … … 126 126 } 127 127 128 static void tcp_header_setup( tcp_sockpair_t *sp, tcp_segment_t *seg, tcp_header_t *hdr)128 static void tcp_header_setup(inet_ep2_t *epp, tcp_segment_t *seg, tcp_header_t *hdr) 129 129 { 130 130 uint16_t doff_flags; 131 131 uint16_t doff; 132 132 133 hdr->src_port = host2uint16_t_be( sp->local.port);134 hdr->dest_port = host2uint16_t_be( sp->foreign.port);133 hdr->src_port = host2uint16_t_be(epp->local.port); 134 hdr->dest_port = host2uint16_t_be(epp->remote.port); 135 135 hdr->seq = host2uint32_t_be(seg->seq); 136 136 hdr->ack = host2uint32_t_be(seg->ack); … … 145 145 } 146 146 147 static uint16_t tcp_phdr_setup(tcp_pdu_t *pdu, tcp_phdr_t *phdr,147 static ip_ver_t tcp_phdr_setup(tcp_pdu_t *pdu, tcp_phdr_t *phdr, 148 148 tcp_phdr6_t *phdr6) 149 149 { 150 150 addr32_t src_v4; 151 151 addr128_t src_v6; 152 uint16_t src_ af= inet_addr_get(&pdu->src, &src_v4, &src_v6);153 152 uint16_t src_ver = inet_addr_get(&pdu->src, &src_v4, &src_v6); 153 154 154 addr32_t dest_v4; 155 155 addr128_t dest_v6; 156 uint16_t dest_ af= inet_addr_get(&pdu->dest, &dest_v4, &dest_v6);157 158 assert(src_ af == dest_af);159 160 switch (src_ af) {161 case AF_INET:156 uint16_t dest_ver = inet_addr_get(&pdu->dest, &dest_v4, &dest_v6); 157 158 assert(src_ver == dest_ver); 159 160 switch (src_ver) { 161 case ip_v4: 162 162 phdr->src = host2uint32_t_be(src_v4); 163 163 phdr->dest = host2uint32_t_be(dest_v4); … … 167 167 host2uint16_t_be(pdu->header_size + pdu->text_size); 168 168 break; 169 case AF_INET6:169 case ip_v6: 170 170 host2addr128_t_be(src_v6, phdr6->src); 171 171 host2addr128_t_be(dest_v6, phdr6->dest); … … 178 178 assert(false); 179 179 } 180 181 return src_ af;180 181 return src_ver; 182 182 } 183 183 … … 191 191 } 192 192 193 static int tcp_header_encode( tcp_sockpair_t *sp, tcp_segment_t *seg,193 static int tcp_header_encode(inet_ep2_t *epp, tcp_segment_t *seg, 194 194 void **header, size_t *size) 195 195 { … … 200 200 return ENOMEM; 201 201 202 tcp_header_setup( sp, seg, hdr);202 tcp_header_setup(epp, seg, hdr); 203 203 *header = hdr; 204 204 *size = sizeof(tcp_header_t); … … 249 249 if (pdu->text != NULL) 250 250 free(pdu->text); 251 free(pdu); 251 252 252 253 return NULL; … … 266 267 tcp_phdr_t phdr; 267 268 tcp_phdr6_t phdr6; 268 269 uint16_t af= tcp_phdr_setup(pdu, &phdr, &phdr6);270 switch ( af) {271 case AF_INET:269 270 ip_ver_t ver = tcp_phdr_setup(pdu, &phdr, &phdr6); 271 switch (ver) { 272 case ip_v4: 272 273 cs_phdr = tcp_checksum_calc(TCP_CHECKSUM_INIT, (void *) &phdr, 273 274 sizeof(tcp_phdr_t)); 274 275 break; 275 case AF_INET6:276 case ip_v6: 276 277 cs_phdr = tcp_checksum_calc(TCP_CHECKSUM_INIT, (void *) &phdr6, 277 278 sizeof(tcp_phdr6_t)); … … 280 281 assert(false); 281 282 } 282 283 283 284 cs_headers = tcp_checksum_calc(cs_phdr, pdu->header, pdu->header_size); 284 285 return tcp_checksum_calc(cs_headers, pdu->text, pdu->text_size); … … 294 295 295 296 /** Decode incoming PDU */ 296 int tcp_pdu_decode(tcp_pdu_t *pdu, tcp_sockpair_t *sp, tcp_segment_t **seg)297 int tcp_pdu_decode(tcp_pdu_t *pdu, inet_ep2_t *epp, tcp_segment_t **seg) 297 298 { 298 299 tcp_segment_t *nseg; … … 308 309 hdr = (tcp_header_t *)pdu->header; 309 310 310 sp->local.port = uint16_t_be2host(hdr->dest_port);311 sp->local.addr = pdu->dest;312 sp->foreign.port = uint16_t_be2host(hdr->src_port);313 sp->foreign.addr = pdu->src;311 epp->local.port = uint16_t_be2host(hdr->dest_port); 312 epp->local.addr = pdu->dest; 313 epp->remote.port = uint16_t_be2host(hdr->src_port); 314 epp->remote.addr = pdu->src; 314 315 315 316 *seg = nseg; … … 318 319 319 320 /** Encode outgoing PDU */ 320 int tcp_pdu_encode( tcp_sockpair_t *sp, tcp_segment_t *seg, tcp_pdu_t **pdu)321 int tcp_pdu_encode(inet_ep2_t *epp, tcp_segment_t *seg, tcp_pdu_t **pdu) 321 322 { 322 323 tcp_pdu_t *npdu; 323 324 size_t text_size; 324 325 uint16_t checksum; 326 int rc; 325 327 326 328 npdu = tcp_pdu_new(); … … 328 330 return ENOMEM; 329 331 330 npdu->src = sp->local.addr; 331 npdu->dest = sp->foreign.addr; 332 tcp_header_encode(sp, seg, &npdu->header, &npdu->header_size); 332 npdu->src = epp->local.addr; 333 npdu->dest = epp->remote.addr; 334 rc = tcp_header_encode(epp, seg, &npdu->header, &npdu->header_size); 335 if (rc != EOK) { 336 free(npdu); 337 return rc; 338 } 333 339 334 340 text_size = tcp_segment_text_size(seg); 335 341 npdu->text = calloc(1, text_size); 336 if (npdu->text == NULL) 342 if (npdu->text == NULL) { 343 free(npdu->header); 344 free(npdu); 337 345 return ENOMEM; 346 } 338 347 339 348 npdu->text_size = text_size;
Note:
See TracChangeset
for help on using the changeset viewer.