ERROR: Time

This is a discussion on ERROR: Time within the C Programming forums, part of the General Programming Boards category; Hello, here i am not getting always common time, some thing like this Thu Jan 1]07:30:00: SYN Received: saddr:10.172.1.159 daddr: ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    20

    Post ERROR: Time

    Hello,
    here i am not getting always common time, some thing like this

    Thu Jan 1]07:30:00: SYN Received: saddr:10.172.1.159 daddr: 208.65.153.238 sport: 5 dport: 80
    Thu Jan 1 07:30:00: SYN Received: saddr:10.172.1.159 daddr: 64.34.169.88 sport: 47 dport: 80
    Thu Jan 1 07:30:00: SYN Received: saddr:10.172.1.159 daddr: 64.233.189.166 sport: 84 dport: 80
    Thu Jan 1 07:30:00: SYN Received: saddr:10.172.1.159 daddr: 64.34.169.88 sport: 49 dport: 80
    Thu Jan 1 07:30:00: SYN Received: saddr:10.172.1.159 daddr: 64.34.169.88 sport: 50 dport: 80
    Thu Jan 1 07:30:00: SYN Received: saddr:10.172.1.159 daddr: 64.34.169.88 sport: 50 dport: 80
    Thu Jan 1 07:30:00: SYN Received: saddr:10.172.1.159 daddr: 64.233.189.164 sport: 81 dport: 80

    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <stdio.h>
    #include <linux/if_ether.h>
    #include <linux/if.h>
    #include <time.h>
    #include <stdlib.h>
    #include "connect.h"
    #include <string.h>
    
    /* 
     * SynWatch
     
     */
    #define SIOCGIFFLAGS    0x8913
    #define SIOCSIFFLAGS    0x8914 
    
    #define LOG "/var/syn.log"
    
    
    
    struct ifreq oldifr, ifr;
    struct connection *head;
    
    
    void 
    go(int argc, char *argv[]) 
    {
        int l, i, j;
        int sock, length;
        struct sockaddr_in name;
        unsigned char buf[4096];
        unsigned int feh;
        int size;
        char DEV[255];
        int time = 0;
        int num, choice;
        FILE *fp;
        char *tim;
        
        time_t t;
    
        head = NULL;
    
        if(argc < 2) 
    {
            printf("&#37;s: Ethdevice\n", argv[0]);
            exit(1);
        }
    
        /* 
         * Obtain ip and port from command line
         */
        strcpy(DEV, argv[1]);
    
        /* 
         * Get a socket which will collect all packets
         */
        sock = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL));
    
        if (sock < 0) 
    {
            printf("Cannot open Raw Socket.\n");
            exit(1);
        }
    
        /* 
         * Configure ethernet device
         */
        strcpy(ifr.ifr_name, DEV);
        strcpy(oldifr.ifr_name, DEV);
    
        /*
         * Get flags and place them in ifr structure
         */
        if(ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) 
    {
            printf("Unable to get %s flags\n", DEV);
            exit(1);
        }
    
        /*
         * Get flags and place them in oldifr structure
         *   This will be used later to change ether device characteristics back
         *   to their original value
         */
        if(ioctl(sock, SIOCGIFFLAGS, &oldifr) < 0) 
    {
            printf("Unable to get %s flags\n", DEV);
            exit(1);
        }
    
        /*
         * Set the promiscous flag
         */
        ifr.ifr_flags |= IFF_PROMISC;
    
        /*
         * Set the device flags
         */
        if(ioctl(sock, SIOCSIFFLAGS, &ifr) < 0)
     {
            printf("Unable to set %s flags\n", DEV);
            exit(1);
        } 
    
        /*
         * Set up sockaddr
         */
        name.sin_family = AF_INET;
        name.sin_addr.s_addr = INADDR_ANY;
        name.sin_port = 0;
    
        length = sizeof(name);
    
        if (getsockname(sock, (struct sockaddr *) &name, &length) < 0)
     {
            printf("Error: Can't get socket name\n");
            exit(1);
        }
    
        printf("SYN Watch started\n");
        feh = sizeof(struct sockaddr);
    
        /*
         * Entering the data collection loop
         */
        for( ; ; ) 
    {
            if ((l = recvfrom(sock, buf, 1024, 0, (struct sockaddr *)&name, 
                          &feh)) < 0)
                printf("Error receiving RAW packet\n");
    
    
            /* 
             * Check to make sure this is an IP packet
             *  The number starts high as the ethernet frame is in the buffer as well.
             */
            if(buf[14] == 0x45 || buf[14] == 0x54) 
    {
                /*
                 * Verify that it is protocol 6 & type is SYN.
                 */
                if(buf[23] == 6 && buf[47] == 0x02) 
    {
                    fp = fopen(LOG, "a+");
                    tim = ctime(&t);
                    tim[strlen(tim) - 6] = '\0';
    
                   fprintf(fp, "%s: SYN Received: saddr:%d.%d.%d.%d daddr: %d.%d.%d.%d ",
                   tim, buf[26] & 0xff, buf[27] & 0xff, buf[28] & 0xff, buf[29] & 0xff,
                   buf[30] & 0xff, buf[31] & 0xff, buf[32] & 0xff, buf[33] & 0xff);
                   fprintf(fp, "sport: %d dport: %d\n", 
                         (buf[34] & 0xff) * 256 + buf[35] & 0xff, 
                         (buf[36] & 0xff) * 256 +  buf[37] & 0xff);
                  fclose(fp);
                }
            }
    
        }
    
    close(sock);
    exit(0);
    }
    
    int main(int argc, char *argv[])
      {
      go(argc, argv);
      }
    so how can i get the correct time. please give me feedback.
    Last edited by Salem; 04-23-2008 at 11:13 AM. Reason: Use [code][/code] tags next time.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you need to initialize time t var to the current time before converting it to string

    and use some proper indentation, your code is unreadable
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21