source: mainline/uspace/srv/net/inetsrv/icmp.c@ 1038a9c

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

Rename app/inetcfg to app/inet, srv/inet to srv/inetsrv. Administration tools are run more often from the command line than the services they configure. It makes sense for them to have the shorter name.

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