]> sjero.net Git - dccpping/blobdiff - dccpping.c
Add README, License, changelog, and copyright messages.
[dccpping] / dccpping.c
index 9dc31a945b28a93ef343341b94ca643a2808fabd..064c079c37840956a8bd40b834cd627644c70430 100644 (file)
@@ -1,9 +1,23 @@
 /******************************************************************************
-Author: Samuel Jero <sj323707@ohio.edu>
+Utility to ping hosts using DCCP REQ packets to test for DCCP connectivity.
+
+Copyright (C) 2012  Samuel Jero <sj323707@ohio.edu>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
-Date: 10/2012
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
 
-Description: Program to ping hosts using DCCP REQ packets to test for DCCP connectivity.
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+Author: Samuel Jero <sj323707@ohio.edu>
+Date: 11/2012
 ******************************************************************************/
 #include <stdarg.h>
 #include <stdlib.h>
@@ -32,8 +46,10 @@ Description: Program to ping hosts using DCCP REQ packets to test for DCCP conne
 
 /*Use the DCCP source port to multiplex DCCP Ping streams by PID*/
 #define SRC_PORT_AS_PID_MULTIPLEX 1
+#define DCCP_SERVICE_CODE 0x50455246
+#define DEFAULT_PORT 33434
 
-
+#define DCCPPING_VERSION 1.0
 #define MAX(x,y) (x>y ? x : y)
 extern int errno;
 
@@ -134,6 +150,7 @@ int logResponse(ipaddr_ptr_t *src, int seq, int type);
 void clearQueue();
 void sigHandler();
 void usage();
+void version();
 void sanitize_environment();
 void dbgprintf(int level, const char *fmt, ...);
 
@@ -155,7 +172,7 @@ int main(int argc, char *argv[])
        ping_stats.rtt_min=0;
        ping_stats.errors=0;
        parms.count=-1;
-       parms.dest_port=33434;
+       parms.dest_port=DEFAULT_PORT;
        parms.ttl=64;
        parms. interval=1000;
        parms.ip_type=AF_UNSPEC;
@@ -164,7 +181,7 @@ int main(int argc, char *argv[])
 
        sanitize_environment();
 
-       while ((c = getopt(argc, argv, "64c:p:i:dt:S:")) != -1) {
+       while ((c = getopt(argc, argv, "64vhc:p:i:dt:S:")) != -1) {
                switch (c) {
                        case '6':
                                parms.ip_type=AF_INET6;
@@ -201,6 +218,11 @@ int main(int argc, char *argv[])
                        case 'S':
                                src=optarg;
                                break;
+                       case 'v':
+                               version();
+                               break;
+                       case 'h':
+                               /*Intentional Fall-through*/
                        default:
                                usage();
                                break;
@@ -479,7 +501,7 @@ void handleDCCPpacket(int rcv_socket, int send_socket){
 
        /*Receive Packet*/
        rcv_addr_len=sizeof(struct sockaddr_storage);
-       if((rlen=recvfrom(rcv_socket, &rbuffer, 1000,0,rcv_addr.gen,&rcv_addr_len))<0){
+       if((rlen=recvfrom(rcv_socket, &rbuffer, 1500,0,rcv_addr.gen,&rcv_addr_len))<0){
                if(errno!=EINTR){
                        dbgprintf(0, "Error on receive from DCCP socket (%s)\n",strerror(errno));
                }
@@ -599,6 +621,7 @@ void handleICMP4packet(int rcv_socket){
        struct dccp_hdr *dhdr;
        struct dccp_hdr_ext *dhdre;
        struct iphdr* ip4hdr;
+       struct iphdr* iph;
        int type;
 
        /*Memory for socket address*/
@@ -610,7 +633,7 @@ void handleICMP4packet(int rcv_socket){
        }
 
        /*Receive Packet*/
-       if((rlen=recvfrom(rcv_socket, &rbuffer, 1000,0,rcv_addr.gen,&rcv_addr_len))<0){
+       if((rlen=recvfrom(rcv_socket, &rbuffer, 1500,0,rcv_addr.gen,&rcv_addr_len))<0){
                if(errno!=EINTR){
                        dbgprintf(0, "Error on receive from ICMPv4 socket (%s)\n",strerror(errno));
                }
@@ -619,13 +642,16 @@ void handleICMP4packet(int rcv_socket){
                return;
        }
 
-       if(rlen < sizeof(struct icmphdr)){ //check packet size
+       iph=(struct iphdr*)rbuffer;
+
+
+       if(rlen < sizeof(struct icmphdr)+sizeof(struct iphdr)){ //check packet size
                dbgprintf(1, "Packet smaller than possible ICMPv4 packet!\n");
                free(rcv_addr.gen);
                return;
        }
 
-       icmp4=(struct icmphdr*)rbuffer;
+       icmp4=(struct icmphdr*)(rbuffer+iph->ihl*4);
        if(icmp4->type!=ICMP_DEST_UNREACH && icmp4->type!=ICMP_TIME_EXCEEDED){ //check icmp types
                dbgprintf(1, "Tossing ICMPv4 packet of type %i\n", icmp4->type);
                free(rcv_addr.gen);
@@ -633,14 +659,14 @@ void handleICMP4packet(int rcv_socket){
        }
 
        /*Check packet size again*/
-       if(rlen<sizeof(struct icmphdr)+sizeof(struct iphdr)+sizeof(struct dccp_hdr)+sizeof(struct dccp_hdr_ext)){
+       if(rlen<sizeof(struct icmphdr)+2*sizeof(struct iphdr)+4){
                dbgprintf(1, "Tossing ICMPv4 packet that's too small to contain DCCP header!\n");
                free(rcv_addr.gen);
                return;
        }
 
        /*Decode IPv4 header*/
-       ip4hdr=(struct iphdr*)(rbuffer+sizeof(struct icmphdr));
+       ip4hdr=(struct iphdr*)(rbuffer+iph->ihl*4+sizeof(struct icmphdr));
        if(memcmp(&parms.src_addr.ipv4->sin_addr,&ip4hdr->saddr,sizeof(parms.src_addr.ipv4->sin_addr))!=0){
                /*Source address doesn't match*/
                dbgprintf(1,"Tossing ICMPv4 packet because the embedded IPv4 source address isn't us\n");
@@ -661,7 +687,7 @@ void handleICMP4packet(int rcv_socket){
        }
 
        /*Decode DCCP header*/
-       dhdr=(struct dccp_hdr*)(rbuffer+sizeof(struct icmphdr)+ip4hdr->ihl*4);
+       dhdr=(struct dccp_hdr*)(rbuffer+iph->ihl*4+sizeof(struct icmphdr)+ip4hdr->ihl*4);
        if(dhdr->dccph_dport!=htons(parms.dest_port)){
                /*DCCP Destination Ports don't match*/
                dbgprintf(1,"Tossing ICMPv4 packet because the embedded packet doesn't have our DCCP destination port\n");
@@ -674,7 +700,7 @@ void handleICMP4packet(int rcv_socket){
                free(rcv_addr.gen);
                return;
        }
-       dhdre=(struct dccp_hdr_ext*)(rbuffer+sizeof(struct icmphdr)+ip4hdr->ihl*4+sizeof(struct dccp_hdr));
+       dhdre=(struct dccp_hdr_ext*)(rbuffer+iph->ihl*4+sizeof(struct icmphdr)+ip4hdr->ihl*4+sizeof(struct dccp_hdr));
 
        /*Log*/
        if(icmp4->type==ICMP_DEST_UNREACH){
@@ -683,7 +709,11 @@ void handleICMP4packet(int rcv_socket){
        if(icmp4->type==ICMP_TIME_EXCEEDED){
                type=TTL_EXPIRATION;
        }
-       logResponse(&rcv_addr,ntohl(dhdre->dccph_seq_low),type);
+       if(rlen<sizeof(struct icmphdr)+2*sizeof(struct iphdr)+sizeof(struct dccp_hdr)+sizeof(struct dccp_hdr_ext)){
+               logResponse(&rcv_addr,-1,type);
+       }else{
+               logResponse(&rcv_addr,ntohl(dhdre->dccph_seq_low),type);
+       }
        free(rcv_addr.gen);
        return;
 }
@@ -708,7 +738,7 @@ void handleICMP6packet(int rcv_socket){
        }
 
        /*Receive Packet*/
-       if((rlen=recvfrom(rcv_socket, &rbuffer, 1000,0,rcv_addr.gen,&rcv_addr_len))<0){
+       if((rlen=recvfrom(rcv_socket, &rbuffer, 1500,0,rcv_addr.gen,&rcv_addr_len))<0){
                dbgprintf(0, "Error on receive from ICMPv6 socket (%s)\n",strerror(errno));
        }
 
@@ -848,7 +878,7 @@ void buildRequestPacket(unsigned char* buffer, int *len, int seq){
        dhdr->dccph_seq2=htonl(0); //Reserved if using 48 bit sequence numbers
        dhdr->dccph_seq=htonl(0);  //High 16bits of sequence number. Always make 0 for simplicity.
        dhdre->dccph_seq_low=htonl(seq);
-       dhdrr->dccph_req_service= htonl(0x50455246);
+       dhdrr->dccph_req_service=htonl(DCCP_SERVICE_CODE);
 
        /*Checksums*/
        if(parms.ip_type==AF_INET){
@@ -1149,8 +1179,16 @@ int logResponse(ipaddr_ptr_t *src, int seq, int type){
        }
 
        if(cur==NULL){
-               dbgprintf(1,"Response received but no requests sent with sequence number %i!\n", seq);
-               return -1;
+               if(parms.ip_type==AF_INET && seq==-1){
+                       /*IPv4 didn't include enough of the packet to get sequence numbers!*/
+                       printf("%s from %s\n",response_label[type],
+                                                               inet_ntop(parms.ip_type, (void*)&src->ipv4->sin_addr, pbuf, 1000));
+                       ping_stats.errors++;
+                       return 0;
+               }else{
+                       dbgprintf(1,"Response received but no requests sent with sequence number %i!\n", seq);
+                       return -1;
+               }
        }
 
        diff=(cur->reply.tv_usec + 1000000*cur->reply.tv_sec) - (cur->sent.tv_usec + 1000000*cur->sent.tv_sec);
@@ -1223,6 +1261,7 @@ void sigHandler(){
        double ploss;
 
        /*Print Stats*/
+       gettimeofday(&ping_stats.stop,NULL);
        if(parms.ip_type==AF_INET){
                printf("-----------%s PING STATISTICS-----------\n",
                                inet_ntop(parms.ip_type, (void*)&parms.dest_addr.ipv4->sin_addr, pbuf, 1000));
@@ -1248,7 +1287,21 @@ void sigHandler(){
 /*Usage information for program*/
 void usage()
 {
-       dbgprintf(0, "dccpping: [-d] [-6|-4] [-c count] [-p port] [-i interval] [-t ttl] [-S srcaddress] remote_host\n");
+       dbgprintf(0, "dccpping: [-d] [-v] [-h] [-6|-4] [-c count] [-p port] [-i interval]\n");
+       dbgprintf(0, "          [-t ttl] [-S srcaddress] remote_host\n");
+       dbgprintf(0, "\n");
+       dbgprintf(0, "          -d   Debug. May be repeated for aditional verbosity\n");
+       dbgprintf(0, "          -v   Version information\n");
+       dbgprintf(0, "          -h   Help\n");
+       dbgprintf(0, "          -6   Force IPv6 mode\n");
+       dbgprintf(0, "          -4   Force IPv4 mode\n");
+       exit(0);
+}
+
+void version(){
+       dbgprintf(0, "dccpping version %.1f\nCopyright (C) 2012 Samuel Jero <sj323707@ohio.edu>\n", DCCPPING_VERSION);
+       dbgprintf(0, "This program comes with ABSOLUTELY NO WARRANTY.\n");
+       dbgprintf(0, "This is free software, and you are welcome to\nredistribute it under certain conditions.\n");
        exit(0);
 }