1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/*
* wip, a small TCP/IP stack.
* Copyright (C) 2025 Xavier Del Campo Romero
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <wip/wip.h>
#include <wip/prv/io.h>
#include <wip/prv/log.h>
#include <wip/prv/icmp.h>
#include <wip/prv/icmp/types.h>
#include <stddef.h>
#include <string.h>
enum
{
TYPE,
CHECKSUM = 2,
ID = 4,
SEQ_NUM = 6,
DATA = 8
};
#if 0
static int get_type(struct wip_icmp *const icmp,
const unsigned char *const p, const size_t n)
{
unsigned char type;
if (icmp->read)
return 0;
type = p[0];
if (type != ECHO_REQUEST)
return -1;
wip_log("%s: got echo request\n");
icmp->type = type;
return 0;
}
static void get_checksum(struct wip_icmp *const icmp,
const unsigned char *const p, const size_t n)
{
size_t off;
struct wip_be16 v;
enum {END = CHECKSUM + sizeof v};
if (icmp->read >= END || n < END - icmp->read)
return;
off = CHECKSUM - icmp->read;
v.v[0] = p[off];
v.v[1] = p[off + 1];
icmp->exp_checksum = wip_be16(&v);
}
static void get_id(struct wip_icmp *const icmp, const unsigned char *const p,
const size_t n)
{
size_t off;
struct wip_be16 v;
enum {END = ID + sizeof v};
if (icmp->read >= END || n < END - icmp->read)
return;
off = ID - icmp->read;
v.v[0] = p[off];
v.v[1] = p[off + 1];
icmp->id = wip_be16(&v);
}
static void get_seqnum(struct wip_icmp *const icmp, const unsigned char *const p,
const size_t n)
{
size_t off;
struct wip_be16 v;
enum {END = SEQ_NUM + sizeof v};
if (icmp->read >= END || n < END - icmp->read)
return;
off = SEQ_NUM - icmp->read;
v.v[0] = p[off];
v.v[1] = p[off + 1];
icmp->seqnum = wip_be16(&v);
wip_log("chksum=%#x, id=%#x, seq_num=%#x\n", icmp->exp_checksum,
icmp->id, icmp->seqnum);
}
static enum wip_state get_data(struct wip *const w,
struct wip_icmp *const icmp, const unsigned char *const p, const size_t n)
{
const struct wip_cfg *const cfg = &w->cfg;
const size_t offset = icmp->read + n;
const void *src = p;
size_t sz = n;
if (offset < DATA)
return WIP_OK;
else if (!icmp->buf)
{
sz = n - DATA - icmp->read;
src = p + DATA - icmp->read;
if (!(icmp->buf = cfg->alloc(sz, cfg->user)))
return WIP_FATAL;
}
else
{
const size_t nsz = icmp->n + n;
enum {MAXSZ = 40};
void *buf;
if (nsz > MAXSZ)
return WIP_INVALID;
else if (!(buf = cfg->realloc(icmp->buf, nsz, cfg->user)))
return WIP_FATAL;
icmp->buf = buf;
}
memcpy((char *)icmp->buf + icmp->read, src, sz);
icmp->n += sz;
wip_log("data (%lu bytes): {", (unsigned long)icmp->n);
{
size_t i;
for (i = 0; i < icmp->n; i++)
{
wip_log("%#hhx", *((const char *)icmp->buf + i));
if (i + 1 < icmp->n)
wip_log(", ");
}
}
wip_log("}\n");
return WIP_OK;
}
#endif
enum wip_state wip_icmp_rx(struct wip *const w, const void *buf,
size_t n, void *const args)
{
struct wip_icmp *const icmp = args;
enum wip_state ret;
size_t r;
while ((ret = icmp->next(w, buf, n, icmp, &r)) == WIP_AGAIN)
{
buf = (const char *)buf + r;
n -= r;
}
return ret;
#if 0
enum wip_state (*next)(struct wip *, const void *, size_t,
struct wip_icmp *)
const struct wip_rx_sm_h *const h = &w->rx.sm.h;
const unsigned char *const p = buf;
const unsigned short dlen = wip_be16(&h->len) - h->header_sz;
enum wip_state ret;
size_t i;
if (get_type(icmp, buf, n))
return 0;
get_checksum(icmp, buf, n);
get_id(icmp, buf, n);
get_seqnum(icmp, buf, n);
if ((ret = get_data(w, icmp, buf, n)))
return ret;
for (i = 0; i < n; i++)
/* Avoid suming checksum */
if (icmp->read >= CHECKSUM || ((i != 2) && (i != 3)))
icmp->checksum += p[i];
if (icmp->read += n >= dlen)
{
wip_log("expected checksum=%#x, got=%#x\n",
icmp->exp_checksum, icmp->checksum);
}
return WIP_OK;
#endif
}
|