source: mainline/uspace/srv/net/inetsrv/icmp.c@ 13be2583

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

Client- and server-side definition of inetping[6]_sdu_t can be shared.

  • Property mode set to 100644
File size: 4.6 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#include <types/inetping.h>
43#include <net/socket_codes.h>
44#include "icmp.h"
45#include "icmp_std.h"
46#include "inetsrv.h"
47#include "inetping.h"
48#include "pdu.h"
49
50/* XXX */
51#define INET_TTL_MAX 255
52
53static int icmp_recv_echo_request(inet_dgram_t *);
54static int icmp_recv_echo_reply(inet_dgram_t *);
55
56int icmp_recv(inet_dgram_t *dgram)
57{
58 uint8_t type;
59
60 log_msg(LOG_DEFAULT, LVL_DEBUG, "icmp_recv()");
61
62 if (dgram->size < 1)
63 return EINVAL;
64
65 type = *(uint8_t *)dgram->data;
66
67 switch (type) {
68 case ICMP_ECHO_REQUEST:
69 return icmp_recv_echo_request(dgram);
70 case ICMP_ECHO_REPLY:
71 return icmp_recv_echo_reply(dgram);
72 default:
73 break;
74 }
75
76 return EINVAL;
77}
78
79static int icmp_recv_echo_request(inet_dgram_t *dgram)
80{
81 icmp_echo_t *request, *reply;
82 uint16_t checksum;
83 size_t size;
84 inet_dgram_t rdgram;
85 int rc;
86
87 log_msg(LOG_DEFAULT, LVL_DEBUG, "icmp_recv_echo_request()");
88
89 if (dgram->size < sizeof(icmp_echo_t))
90 return EINVAL;
91
92 request = (icmp_echo_t *)dgram->data;
93 size = dgram->size;
94
95 reply = calloc(size, 1);
96 if (reply == NULL)
97 return ENOMEM;
98
99 memcpy(reply, request, size);
100
101 reply->type = ICMP_ECHO_REPLY;
102 reply->code = 0;
103 reply->checksum = 0;
104
105 checksum = inet_checksum_calc(INET_CHECKSUM_INIT, reply, size);
106 reply->checksum = host2uint16_t_be(checksum);
107
108 rdgram.iplink = 0;
109 rdgram.src = dgram->dest;
110 rdgram.dest = dgram->src;
111 rdgram.tos = ICMP_TOS;
112 rdgram.data = reply;
113 rdgram.size = size;
114
115 rc = inet_route_packet(&rdgram, IP_PROTO_ICMP, INET_TTL_MAX, 0);
116
117 free(reply);
118
119 return rc;
120}
121
122static int icmp_recv_echo_reply(inet_dgram_t *dgram)
123{
124 log_msg(LOG_DEFAULT, LVL_DEBUG, "icmp_recv_echo_reply()");
125
126 if (dgram->size < sizeof(icmp_echo_t))
127 return EINVAL;
128
129 icmp_echo_t *reply = (icmp_echo_t *) dgram->data;
130
131 inetping_sdu_t sdu;
132
133 uint16_t family = inet_addr_get(&dgram->src, &sdu.src, NULL);
134 if (family != AF_INET)
135 return EINVAL;
136
137 family = inet_addr_get(&dgram->dest, &sdu.dest, NULL);
138 if (family != AF_INET)
139 return EINVAL;
140
141 sdu.seq_no = uint16_t_be2host(reply->seq_no);
142 sdu.data = reply + sizeof(icmp_echo_t);
143 sdu.size = dgram->size - sizeof(icmp_echo_t);
144
145 uint16_t ident = uint16_t_be2host(reply->ident);
146
147 return inetping_recv(ident, &sdu);
148}
149
150int icmp_ping_send(uint16_t ident, inetping_sdu_t *sdu)
151{
152 size_t rsize = sizeof(icmp_echo_t) + sdu->size;
153 void *rdata = calloc(rsize, 1);
154 if (rdata == NULL)
155 return ENOMEM;
156
157 icmp_echo_t *request = (icmp_echo_t *) rdata;
158
159 request->type = ICMP_ECHO_REQUEST;
160 request->code = 0;
161 request->checksum = 0;
162 request->ident = host2uint16_t_be(ident);
163 request->seq_no = host2uint16_t_be(sdu->seq_no);
164
165 memcpy(rdata + sizeof(icmp_echo_t), sdu->data, sdu->size);
166
167 uint16_t checksum = inet_checksum_calc(INET_CHECKSUM_INIT, rdata, rsize);
168 request->checksum = host2uint16_t_be(checksum);
169
170 inet_dgram_t dgram;
171
172 inet_addr_set(sdu->src, &dgram.src);
173 inet_addr_set(sdu->dest, &dgram.dest);
174
175 dgram.iplink = 0;
176 dgram.tos = ICMP_TOS;
177 dgram.data = rdata;
178 dgram.size = rsize;
179
180 int rc = inet_route_packet(&dgram, IP_PROTO_ICMP, INET_TTL_MAX, 0);
181
182 free(rdata);
183 return rc;
184}
185
186/** @}
187 */
Note: See TracBrowser for help on using the repository browser.