Changes in uspace/srv/net/tcp/pdu.c [4a0bc99:12df1f1] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/tcp/pdu.c
r4a0bc99 r12df1f1 1 1 /* 2 * Copyright (c) 201 5Jiri Svoboda2 * Copyright (c) 2011 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 38 38 #include <byteorder.h> 39 39 #include <errno.h> 40 #include <inet/endpoint.h>41 40 #include <mem.h> 42 41 #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( inet_ep2_t *epp, tcp_segment_t *seg, tcp_header_t *hdr)128 static void tcp_header_setup(tcp_sockpair_t *sp, 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( epp->local.port);134 hdr->dest_port = host2uint16_t_be( epp->remote.port);133 hdr->src_port = host2uint16_t_be(sp->local.port); 134 hdr->dest_port = host2uint16_t_be(sp->foreign.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 ip_ver_t tcp_phdr_setup(tcp_pdu_t *pdu, tcp_phdr_t *phdr,147 static uint16_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_ ver= inet_addr_get(&pdu->src, &src_v4, &src_v6);153 152 uint16_t src_af = 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_ 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: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: 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 ip_v6:169 case AF_INET6: 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_ ver;180 181 return src_af; 182 182 } 183 183 … … 191 191 } 192 192 193 static int tcp_header_encode( inet_ep2_t *epp, tcp_segment_t *seg,193 static int tcp_header_encode(tcp_sockpair_t *sp, tcp_segment_t *seg, 194 194 void **header, size_t *size) 195 195 { … … 200 200 return ENOMEM; 201 201 202 tcp_header_setup( epp, seg, hdr);202 tcp_header_setup(sp, 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);252 251 253 252 return NULL; … … 267 266 tcp_phdr_t phdr; 268 267 tcp_phdr6_t phdr6; 269 270 ip_ver_t ver= tcp_phdr_setup(pdu, &phdr, &phdr6);271 switch ( ver) {272 case ip_v4:268 269 uint16_t af = tcp_phdr_setup(pdu, &phdr, &phdr6); 270 switch (af) { 271 case AF_INET: 273 272 cs_phdr = tcp_checksum_calc(TCP_CHECKSUM_INIT, (void *) &phdr, 274 273 sizeof(tcp_phdr_t)); 275 274 break; 276 case ip_v6:275 case AF_INET6: 277 276 cs_phdr = tcp_checksum_calc(TCP_CHECKSUM_INIT, (void *) &phdr6, 278 277 sizeof(tcp_phdr6_t)); … … 281 280 assert(false); 282 281 } 283 282 284 283 cs_headers = tcp_checksum_calc(cs_phdr, pdu->header, pdu->header_size); 285 284 return tcp_checksum_calc(cs_headers, pdu->text, pdu->text_size); … … 295 294 296 295 /** Decode incoming PDU */ 297 int tcp_pdu_decode(tcp_pdu_t *pdu, inet_ep2_t *epp, tcp_segment_t **seg)296 int tcp_pdu_decode(tcp_pdu_t *pdu, tcp_sockpair_t *sp, tcp_segment_t **seg) 298 297 { 299 298 tcp_segment_t *nseg; … … 309 308 hdr = (tcp_header_t *)pdu->header; 310 309 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;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; 315 314 316 315 *seg = nseg; … … 319 318 320 319 /** Encode outgoing PDU */ 321 int tcp_pdu_encode( inet_ep2_t *epp, tcp_segment_t *seg, tcp_pdu_t **pdu)320 int tcp_pdu_encode(tcp_sockpair_t *sp, tcp_segment_t *seg, tcp_pdu_t **pdu) 322 321 { 323 322 tcp_pdu_t *npdu; 324 323 size_t text_size; 325 324 uint16_t checksum; 326 int rc;327 325 328 326 npdu = tcp_pdu_new(); … … 330 328 return ENOMEM; 331 329 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 } 330 npdu->src = sp->local.addr; 331 npdu->dest = sp->foreign.addr; 332 tcp_header_encode(sp, seg, &npdu->header, &npdu->header_size); 339 333 340 334 text_size = tcp_segment_text_size(seg); 341 335 npdu->text = calloc(1, text_size); 342 if (npdu->text == NULL) { 343 free(npdu->header); 344 free(npdu); 336 if (npdu->text == NULL) 345 337 return ENOMEM; 346 }347 338 348 339 npdu->text_size = text_size;
Note:
See TracChangeset
for help on using the changeset viewer.