source: mainline/uspace/srv/ethip/pdu.c@ 1493811

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1493811 was 1493811, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Ethernet PDU encoding and decoding.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Copyright (c) 2012 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup inet
30 * @{
31 */
32/**
33 * @file
34 * @brief
35 */
36
37#include <byteorder.h>
38#include <errno.h>
39#include <io/log.h>
40#include <mem.h>
41#include <stdlib.h>
42
43#include "ethip.h"
44#include "std.h"
45#include "pdu.h"
46
47static void mac48_encode(mac48_addr_t *addr, void *buf);
48static void mac48_decode(void *data, mac48_addr_t *addr);
49
50#define MAC48_BYTES 6
51
52/** Encode Ethernet PDU. */
53int eth_pdu_encode(eth_frame_t *frame, void **rdata, size_t *rsize)
54{
55 void *data;
56 size_t size;
57 eth_header_t *hdr;
58
59 size = sizeof(eth_header_t) + frame->size;
60
61 data = calloc(size, 1);
62 if (data == NULL)
63 return ENOMEM;
64
65 hdr = (eth_header_t *)data;
66 mac48_encode(&frame->src, hdr->src);
67 mac48_encode(&frame->dest, hdr->dest);
68 hdr->etype_len = host2uint16_t_be(frame->etype_len);
69
70 memcpy((uint8_t *)data + sizeof(eth_header_t), frame->data,
71 frame->size);
72
73 *rdata = data;
74 *rsize = size;
75 return EOK;
76}
77
78/** Decode Ethernet PDU. */
79int eth_pdu_decode(void *data, size_t size, eth_frame_t *frame)
80{
81 eth_header_t *hdr;
82
83 log_msg(LVL_DEBUG, "eth_pdu_decode()");
84
85 if (size < sizeof(eth_header_t)) {
86 log_msg(LVL_DEBUG, "PDU too short (%zu)", size);
87 return EINVAL;
88 }
89
90 hdr = (eth_header_t *)data;
91
92 frame->size = size - sizeof(eth_header_t);
93 frame->data = calloc(frame->size, 1);
94 if (frame->data == NULL)
95 return ENOMEM;
96
97 mac48_decode(hdr->src, &frame->src);
98 mac48_decode(hdr->dest, &frame->dest);
99 frame->etype_len = uint16_t_be2host(hdr->etype_len);
100
101 memcpy(frame->data, (uint8_t *)data + sizeof(eth_header_t),
102 frame->size);
103
104 return EOK;
105}
106
107static void mac48_encode(mac48_addr_t *addr, void *buf)
108{
109 uint64_t val;
110 uint8_t *bbuf = (uint8_t *)buf;
111 int i;
112
113 val = addr->addr;
114 for (i = 0; i < MAC48_BYTES; i++) {
115 bbuf[i] = (val >> 8*(MAC48_BYTES - i - 1)) & 0xff;
116 val = val >> 8;
117 }
118}
119
120static void mac48_decode(void *data, mac48_addr_t *addr)
121{
122 uint64_t val;
123 uint8_t *bdata = (uint8_t *)data;
124 int i;
125
126 val = 0;
127 for (i = 0; i < MAC48_BYTES; i++)
128 val |= ((uint64_t)bdata[i]) << (MAC48_BYTES - i - 1);
129
130 addr->addr = val;
131}
132
133/** @}
134 */
Note: See TracBrowser for help on using the repository browser.