Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/net/udp/pdu.c

    r8d48c7e r12df1f1  
    11/*
    2  * Copyright (c) 2015 Jiri Svoboda
     2 * Copyright (c) 2012 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    4141#include <stdlib.h>
    4242#include <inet/addr.h>
     43#include <net/socket_codes.h>
    4344#include "msg.h"
    4445#include "pdu.h"
     
    8485}
    8586
    86 static ip_ver_t udp_phdr_setup(udp_pdu_t *pdu, udp_phdr_t *phdr,
     87static uint16_t udp_phdr_setup(udp_pdu_t *pdu, udp_phdr_t *phdr,
    8788    udp_phdr6_t *phdr6)
    8889{
    8990        addr32_t src_v4;
    9091        addr128_t src_v6;
    91         ip_ver_t src_ver = inet_addr_get(&pdu->src, &src_v4, &src_v6);
    92 
     92        uint16_t src_af = inet_addr_get(&pdu->src, &src_v4, &src_v6);
     93       
    9394        addr32_t dest_v4;
    9495        addr128_t dest_v6;
    95         ip_ver_t dest_ver = inet_addr_get(&pdu->dest, &dest_v4, &dest_v6);
    96 
    97         assert(src_ver == dest_ver);
    98 
    99         switch (src_ver) {
    100         case ip_v4:
     96        uint16_t dest_af = inet_addr_get(&pdu->dest, &dest_v4, &dest_v6);
     97       
     98        assert(src_af == dest_af);
     99       
     100        switch (src_af) {
     101        case AF_INET:
    101102                phdr->src_addr = host2uint32_t_be(src_v4);
    102103                phdr->dest_addr = host2uint32_t_be(dest_v4);
     
    105106                phdr->udp_length = host2uint16_t_be(pdu->data_size);
    106107                break;
    107         case ip_v6:
     108        case AF_INET6:
    108109                host2addr128_t_be(src_v6, phdr6->src_addr);
    109110                host2addr128_t_be(dest_v6, phdr6->dest_addr);
     
    115116                assert(false);
    116117        }
    117 
    118         return src_ver;
     118       
     119        return src_af;
    119120}
    120121
     
    135136        udp_phdr_t phdr;
    136137        udp_phdr6_t phdr6;
    137 
    138         ip_ver_t ver = udp_phdr_setup(pdu, &phdr, &phdr6);
    139         switch (ver) {
    140         case ip_v4:
     138       
     139        uint16_t af = udp_phdr_setup(pdu, &phdr, &phdr6);
     140        switch (af) {
     141        case AF_INET:
    141142                cs_phdr = udp_checksum_calc(UDP_CHECKSUM_INIT, (void *) &phdr,
    142143                    sizeof(udp_phdr_t));
    143144                break;
    144         case ip_v6:
     145        case AF_INET6:
    145146                cs_phdr = udp_checksum_calc(UDP_CHECKSUM_INIT, (void *) &phdr6,
    146147                    sizeof(udp_phdr6_t));
     
    149150                assert(false);
    150151        }
    151 
     152       
    152153        return udp_checksum_calc(cs_phdr, pdu->data, pdu->data_size);
    153154}
     
    162163
    163164/** Decode incoming PDU */
    164 int udp_pdu_decode(udp_pdu_t *pdu, inet_ep2_t *epp, udp_msg_t **msg)
     165int udp_pdu_decode(udp_pdu_t *pdu, udp_sockpair_t *sp, udp_msg_t **msg)
    165166{
    166167        udp_msg_t *nmsg;
     
    179180        hdr = (udp_header_t *)pdu->data;
    180181
    181         epp->local_link = pdu->iplink;
    182         epp->remote.port = uint16_t_be2host(hdr->src_port);
    183         epp->remote.addr = pdu->src;
    184         epp->local.port = uint16_t_be2host(hdr->dest_port);
    185         epp->local.addr = pdu->dest;
     182        sp->foreign.port = uint16_t_be2host(hdr->src_port);
     183        sp->foreign.addr = pdu->src;
     184        sp->local.port = uint16_t_be2host(hdr->dest_port);
     185        sp->local.addr = pdu->dest;
    186186
    187187        length = uint16_t_be2host(hdr->length);
     
    197197                return ENOMEM;
    198198
     199        nmsg->data = text;
    199200        nmsg->data_size = length - sizeof(udp_header_t);
    200         nmsg->data = malloc(nmsg->data_size);
    201         if (nmsg->data == NULL)
    202                 return ENOMEM;
    203 
    204         memcpy(nmsg->data, text, nmsg->data_size);
    205201
    206202        *msg = nmsg;
     
    209205
    210206/** Encode outgoing PDU */
    211 int udp_pdu_encode(inet_ep2_t *epp, udp_msg_t *msg, udp_pdu_t **pdu)
     207int udp_pdu_encode(udp_sockpair_t *sp, udp_msg_t *msg, udp_pdu_t **pdu)
    212208{
    213209        udp_pdu_t *npdu;
     
    219215                return ENOMEM;
    220216
    221         npdu->iplink = epp->local_link;
    222         npdu->src = epp->local.addr;
    223         npdu->dest = epp->remote.addr;
     217        npdu->src = sp->local.addr;
     218        npdu->dest = sp->foreign.addr;
    224219
    225220        npdu->data_size = sizeof(udp_header_t) + msg->data_size;
     
    231226
    232227        hdr = (udp_header_t *)npdu->data;
    233         hdr->src_port = host2uint16_t_be(epp->local.port);
    234         hdr->dest_port = host2uint16_t_be(epp->remote.port);
     228        hdr->src_port = host2uint16_t_be(sp->local.port);
     229        hdr->dest_port = host2uint16_t_be(sp->foreign.port);
    235230        hdr->length = host2uint16_t_be(npdu->data_size);
    236231        hdr->checksum = 0;
Note: See TracChangeset for help on using the changeset viewer.