Changeset eea65f4 in mainline


Ignore:
Timestamp:
2011-11-21T21:16:23Z (12 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
6896409c
Parents:
004a5fe
Message:

PDU encoding and decoding.

Location:
uspace/srv/net/tl/tcp
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/tl/tcp/header.c

    r004a5fe reea65f4  
    3636
    3737#include <byteorder.h>
     38#include <errno.h>
     39#include <mem.h>
     40#include <stdlib.h>
    3841#include "header.h"
    3942#include "segment.h"
     
    4144#include "tcp_type.h"
    4245
    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
     48static 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
     69static 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
     91static 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
     110static 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);
    51121        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
     125static 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);
    59129        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
     134static 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
     143static 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
     159static tcp_pdu_t *tcp_pdu_new(void)
     160{
     161        return calloc(1, sizeof(tcp_pdu_t));
     162}
     163
     164void tcp_pdu_delete(tcp_pdu_t *pdu)
     165{
     166        free(pdu->text);
     167        free(pdu);
     168}
     169
     170static 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
     186static 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 */
     195int 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 */
     218int 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;
    64246}
    65247
  • uspace/srv/net/tl/tcp/header.h

    r004a5fe reea65f4  
    3939#include "tcp_type.h"
    4040
    41 extern void tcp_header_setup(tcp_conn_t *, tcp_segment_t *, tcp_header_t *);
    42 extern void tcp_phdr_setup(tcp_conn_t *, tcp_segment_t *, tcp_phdr_t *);
     41extern void tcp_pdu_delete(tcp_pdu_t *);
     42extern int tcp_pdu_decode(tcp_pdu_t *, tcp_sockpair_t *, tcp_segment_t **);
     43extern int tcp_pdu_encode(tcp_sockpair_t *, tcp_segment_t *, tcp_pdu_t **);
     44
    4345
    4446#endif
  • uspace/srv/net/tl/tcp/rqueue.c

    r004a5fe reea65f4  
    4141#include <thread.h>
    4242#include "conn.h"
     43#include "header.h"
    4344#include "rqueue.h"
     45#include "segment.h"
    4446#include "state.h"
    4547#include "tcp_type.h"
     48
     49/** Transcode bounced segments.
     50 *
     51 * If defined, segments bounced via the internal debugging loopback will
     52 * be encoded to a PDU and the decoded. Otherwise they will be bounced back
     53 * directly without passing the encoder-decoder.
     54 */
     55#define BOUNCE_TRANSCODE
    4656
    4757static prodcons_t rqueue;
     
    6676        log_msg(LVL_DEBUG, "tcp_rqueue_bounce_seg()");
    6777
     78#ifdef BOUNCE_TRANSCODE
     79        tcp_pdu_t *pdu;
     80        tcp_segment_t *dseg;
     81
     82        if (tcp_pdu_encode(sp, seg, &pdu) != EOK) {
     83                log_msg(LVL_WARN, "Not enough memory. Segment dropped.");
     84                return;
     85        }
     86
     87        if (tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {
     88                log_msg(LVL_WARN, "Not enough memory. Segment dropped.");
     89                return;
     90        }
     91
     92        tcp_pdu_delete(pdu);
     93
     94        /** Insert decoded segment into rqueue */
     95        tcp_rqueue_insert_seg(&rident, dseg);
     96        tcp_segment_delete(seg);
     97#else
    6898        /* Reverse the identification */
    6999        tcp_sockpair_flipped(sp, &rident);
    70100
     101        /* Insert segment back into rqueue */
    71102        tcp_rqueue_insert_seg(&rident, seg);
     103#endif
    72104}
    73105
  • uspace/srv/net/tl/tcp/segment.c

    r004a5fe reea65f4  
    133133        tcp_segment_t *seg;
    134134
    135         assert(ctrl != 0 || size > 0);
    136 
    137135        seg = tcp_segment_new();
    138136        if (seg == NULL)
  • uspace/srv/net/tl/tcp/tcp_type.h

    r004a5fe reea65f4  
    271271} cproc_t;
    272272
     273/** Encoded PDU */
     274typedef struct {
     275        /** Source address */
     276        netaddr_t src_addr;
     277        /** Destination address */
     278        netaddr_t dest_addr;
     279
     280        /** Encoded header */
     281        void *header;
     282        /** Encoded header size */
     283        size_t header_size;
     284        /** Text */
     285        void *text;
     286        /** Text size */
     287        size_t text_size;
     288} tcp_pdu_t;
     289
    273290#endif
    274291
  • uspace/srv/net/tl/tcp/test.c

    r004a5fe reea65f4  
    5858        sock.port = 1024;
    5959        sock.addr.ipv4 = 0x7f000001;
     60        printf("S: User open...\n");
    6061        tcp_uc_open(80, &sock, ap_passive, &conn);
    6162        conn->name = (char *) "S";
    6263
    6364        while (true) {
    64                 printf("User receive...\n");
     65                printf("S: User receive...\n");
    6566                tcp_uc_receive(conn, rcv_buf, RCV_BUF_SIZE, &rcvd, &xflags);
    6667                if (rcvd == 0) {
     
    6970                }
    7071                rcv_buf[rcvd] = '\0';
    71                 printf("User received %zu bytes '%s'.\n", rcvd, rcv_buf);
     72                printf("S: User received %zu bytes '%s'.\n", rcvd, rcv_buf);
    7273
    7374                async_usleep(1000*1000*2);
     
    7677        async_usleep(/*10**/1000*1000);
    7778
    78         printf("test_srv() close connection\n");
     79        printf("S: User close...\n");
    7980        tcp_uc_close(conn);
    8081
     
    9495
    9596        async_usleep(1000*1000*3);
     97        printf("C: User open...\n");
    9698        tcp_uc_open(1024, &sock, ap_active, &conn);
    9799        conn->name = (char *) "C";
    98100
    99101        async_usleep(1000*1000*10);
     102        printf("C: User send...\n");
    100103        tcp_uc_send(conn, (void *)msg, str_size(msg), 0);
    101104
    102105        async_usleep(1000*1000*3/**20*2*/);
     106        printf("C: User close...\n");
    103107        tcp_uc_close(conn);
    104108}
Note: See TracChangeset for help on using the changeset viewer.