| 1 |
/* |
| 2 |
Copyright (C) 2001-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include <sys/types.h> |
| 22 |
#include <sys/socket.h> |
| 23 |
#include <sys/time.h> |
| 24 |
|
| 25 |
#include <netinet/in_systm.h> |
| 26 |
#include <netinet/in.h> |
| 27 |
#include <netinet/ip.h> |
| 28 |
/*#include <netinet/ip_icmp.h> -- deliberately not including this */ |
| 29 |
#include <arpa/inet.h> |
| 30 |
#include <icmp.h> |
| 31 |
|
| 32 |
int |
| 33 |
icmp_generic_encode (unsigned char *buffer, size_t bufsize, int type, |
| 34 |
int ident, int seqno) |
| 35 |
{ |
| 36 |
icmphdr_t *icmp; |
| 37 |
|
| 38 |
if (bufsize < ICMP_MINLEN) |
| 39 |
return -1; |
| 40 |
icmp = (icmphdr_t *) buffer; |
| 41 |
icmp->icmp_type = type; |
| 42 |
icmp->icmp_code = 0; |
| 43 |
icmp->icmp_cksum = 0; |
| 44 |
icmp->icmp_seq = htons (seqno); |
| 45 |
icmp->icmp_id = htons (ident); |
| 46 |
|
| 47 |
icmp->icmp_cksum = icmp_cksum (buffer, bufsize); |
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
int |
| 52 |
icmp_generic_decode (unsigned char *buffer, size_t bufsize, |
| 53 |
struct ip **ipp, icmphdr_t **icmpp) |
| 54 |
{ |
| 55 |
size_t hlen; |
| 56 |
unsigned short cksum; |
| 57 |
struct ip *ip; |
| 58 |
icmphdr_t *icmp; |
| 59 |
|
| 60 |
/* IP header */ |
| 61 |
ip = (struct ip *) buffer; |
| 62 |
hlen = ip->ip_hl << 2; |
| 63 |
if (bufsize < hlen + ICMP_MINLEN) |
| 64 |
return -1; |
| 65 |
|
| 66 |
/* ICMP header */ |
| 67 |
icmp = (icmphdr_t *) (buffer + hlen); |
| 68 |
|
| 69 |
/* Prepare return values */ |
| 70 |
*ipp = ip; |
| 71 |
*icmpp = icmp; |
| 72 |
|
| 73 |
/* Recompute checksum */ |
| 74 |
cksum = icmp->icmp_cksum; |
| 75 |
icmp->icmp_cksum = 0; |
| 76 |
icmp->icmp_cksum = icmp_cksum ((unsigned char *) icmp, bufsize - hlen); |
| 77 |
if (icmp->icmp_cksum != cksum) |
| 78 |
return 1; |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
int |
| 83 |
icmp_echo_encode (unsigned char *buffer, size_t bufsize, int ident, int seqno) |
| 84 |
{ |
| 85 |
return icmp_generic_encode (buffer, bufsize, ICMP_ECHO, ident, seqno); |
| 86 |
} |
| 87 |
|
| 88 |
int |
| 89 |
icmp_echo_decode (unsigned char *buffer, size_t bufsize, |
| 90 |
struct ip **ipp, icmphdr_t **icmpp) |
| 91 |
{ |
| 92 |
return icmp_generic_decode (buffer, bufsize, ipp, icmpp); |
| 93 |
} |