commit 1a130db732387503360a9280f4cfb02769735d68
parent 5c0dd3494dd18880648a578741692df9d3c809d3
Author: CNLohr <lohr85@gmail.com>
Date: Sun, 22 Sep 2019 22:00:58 -0400
Merge pull request #56 from Michael137/master
Add FreeBSD support for cnping
Diffstat:
2 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/cnping.c b/cnping.c
@@ -11,6 +11,10 @@
#endif
#include <windows.h>
#else
+ #ifdef __FreeBSD__
+ #include <sys/types.h>
+ #include <netinet/in.h>
+ #endif
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
diff --git a/ping.c b/ping.c
@@ -27,11 +27,14 @@
#include <ws2tcpip.h>
#include <stdint.h>
#else
+ #ifdef __FreeBSD__
+ #include <netinet/in.h>
+ #endif
#include <unistd.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
- #ifdef __APPLE__
+ #if defined(__APPLE__) || defined(__FreeBSD__)
#ifndef SOL_IP
#define SOL_IP IPPROTO_IP
#endif
@@ -69,8 +72,13 @@ int precise_ping;
struct packet
{
+#ifdef __FreeBSD__
+ struct icmp hdr;
+ unsigned char msg[PACKETSIZE-sizeof(struct icmp)];
+#else
struct icmphdr hdr;
unsigned char msg[PACKETSIZE-sizeof(struct icmphdr)];
+#endif
};
int sd;
@@ -131,8 +139,14 @@ void listener()
if( buf[9] != 1 ) continue; //ICMP
if( addr.sin_addr.s_addr != psaddr.sin_addr.s_addr ) continue;
+ // sizeof(packet.hdr) + 20
+#ifdef __FreeBSD__
+ int offset = 48;
+#else
+ int offset = 28;
+#endif
if ( bytes > 0 )
- display(buf + 28, bytes - 28 );
+ display(buf + offset, bytes - offset );
else
{
ERRM("Error: recvfrom failed");
@@ -166,12 +180,19 @@ void ping(struct sockaddr_in *addr )
{
int rsize = load_ping_packet( pckt.msg, sizeof( pckt.msg ) );
memset( &pckt.hdr, 0, sizeof( pckt.hdr ) ); //This needs to be here, but I don't know why, since I think the struct is fully populated.
-
+#ifdef __FreeBSD__
+ pckt.hdr.icmp_code = 0;
+ pckt.hdr.icmp_type = ICMP_ECHO;
+ pckt.hdr.icmp_id = pid;
+ pckt.hdr.icmp_seq = cnt++;
+ pckt.hdr.icmp_cksum = checksum((const unsigned char *)&pckt, sizeof( pckt.hdr ) + rsize );
+#else
pckt.hdr.code = 0;
pckt.hdr.type = ICMP_ECHO;
pckt.hdr.un.echo.id = pid;
pckt.hdr.un.echo.sequence = cnt++;
pckt.hdr.checksum = checksum((const unsigned char *)&pckt, sizeof( pckt.hdr ) + rsize );
+#endif
int sr = sendto(sd, (char*)&pckt, sizeof( pckt.hdr ) + rsize , 0, (struct sockaddr*)addr, sizeof(*addr));