IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Réseau C Discussion :

J'ai un problème dans ce code source ping en C sous linux


Sujet :

Réseau C

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2011
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Avril 2011
    Messages : 12
    Points : 0
    Points
    0
    Par défaut J'ai un problème dans ce code source ping en C sous linux
    Code c : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <linux/ip.h>
    #include <linux/icmp.h>
    #include <string.h>
    #include <unistd.h>
     
    unsigned short in_cksum(unsigned short *, int);
    int main()
    {
    int addrlen;
    int k;
     
    char d_addr[17];
    struct iphdr *ip;
    struct icmphdr *icmp;
    struct icmphdr *icmp_reply;
    struct iphdr *ipreply;
    char *packet;
    char *buffer;
    int raw;
    char *n;
    struct sockaddr_in serv;
    int optval=1;
    int sent;
     
    printf("Enter the destination address : ");
    fgets(d_addr,17,stdin);
     
    n= strchr(d_addr, '\n'); /* search for newline character */
     
    if ( n != NULL )
     
    {
     
    *n = '\0'; /* overwrite trailing newline */
     
    }
     
     
    // Allocate space 
     
    packet=malloc(sizeof(struct iphdr)+sizeof(struct icmphdr));
    buffer=malloc(sizeof(struct iphdr)+sizeof(struct icmphdr));
     
     
    //setting up the ip header
     
    ip=(struct iphdr *)packet;
     
    //Setting the values for the ip header
     
    ip->ihl=5;  //header length
    ip->version=4; //version 
    ip->tos=0;    //type of service
    ip->tot_len=htons(sizeof(struct iphdr)+sizeof(struct icmphdr)); //toatal length
    ip->id=htons(random()); //Assign id as a random number
    ip->ttl=255; //time to live;
    ip->protocol=IPPROTO_ICMP;//ICMP header follow next to the ip header
    ip->check=0; //Assign checksum as zero for now
    ip->saddr=	INADDR_ANY;
    ip->daddr=inet_addr(d_addr);
     
    //Creating a raw socket
     
    if((raw=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))==-1)
    {
    perror("Something wrong with socket call :");
    }
     
    //setting the iphdrincl option
     
    setsockopt(raw, IPPROTO_IP, IP_HDRINCL, &optval, sizeof(int));
     
    //creating the icmp header
    icmp=(struct icmphdr *)(packet+sizeof(struct iphdr));
     
    icmp->type=ICMP_ECHO;		
    icmp->code		= 0;
        icmp->un.echo.id		= 0;
        icmp->un.echo.sequence	= 0;
        icmp->checksum 		= 0;
        icmp-> checksum		= in_cksum((unsigned short *)icmp, sizeof(struct icmphdr));
     
        ip->check			= in_cksum((unsigned short *)ip, sizeof(struct iphdr));
     
    serv.sin_family=AF_INET;
    serv.sin_addr.s_addr=ip->daddr;
     
    sent=sendto(raw,packet,ntohs(ip->tot_len),0,(struct sockaddr *)&serv,sizeof(struct sockaddr));
     
     
     /*
         *listen for response
         */
        addrlen = sizeof(serv);
        if ((k=recvfrom(raw, buffer, (sizeof(struct iphdr) + sizeof(struct icmphdr)), 0, (struct sockaddr *)&serv, &addrlen)) == -1)
        {
    	perror("recv");
        }
     
     
    ipreply=(struct iphdr *)buffer;
    icmp_reply=(struct icmphdr *)(buffer + sizeof (struct iphdr));
    struct in_addr inad;
     
     
     
    	printf("Received %d byte reply from: %s\n", k, inet_ntoa (serv.sin_addr));
        inad.s_addr = ipreply->saddr;
        printf("Src IP: %s\n", inet_ntoa (inad));
        inad.s_addr = ipreply->daddr;
        printf("Dst IP: %s\n", inet_ntoa (inad));
        printf("ID: %d\n", ntohs(ipreply->id));
        printf("TTL: %d\n", ipreply->ttl);
        printf("ICMP type: %d\n",icmp_reply->type);
        printf("ICMP code: %d\n",icmp_reply->code);
        printf("ICMP echo ID: %d\n",icmp_reply->un.echo.id);
        printf("ICMP echo seq: %d\n",icmp_reply->un.echo.sequence);
        close(raw);
    	free(packet);
        return 0;
     
     
     
    }
    /*
     * in_cksum --
     * Checksum routine for Internet Protocol
     * family headers (C Version)
     */
     
    unsigned short in_cksum(unsigned short *addr, int len)
    {
        register int sum = 0;
        u_short answer = 0;
        register u_short *w = addr;
        register int nleft = len;
        /*
         * Our algorithm is simple, using a 32 bit accumulator (sum), we add
         * sequential 16 bit words to it, and at the end, fold back all the
         * carry bits from the top 16 bits into the lower 16 bits.
         */
        while (nleft > 1)
        {
    	  sum += *w++;
    	  nleft -= 2;
        }
        /* mop up an odd byte, if necessary */
        if (nleft == 1)
        {
    	  *(u_char *) (&answer) = *(u_char *) w;
    	  sum += answer;
        }
        /* add back carry outs from top 16 bits to low 16 bits */
        sum = (sum >> 16) + (sum & 0xffff);		/* add hi 16 to low 16 */
        sum += (sum >> 16);				/* add carry */
        answer = ~sum;				/* truncate to 16 bits */
        return (answer);
    }


    s'il vous plait aider moi. et merci d'avance.

  2. #2
    Expert éminent sénior Avatar de frp31
    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Juillet 2006
    Messages
    5 196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Juillet 2006
    Messages : 5 196
    Points : 12 262
    Points
    12 262
    Par défaut
    tu te trompe de forum (programmation c), tu mets pas les balises,
    tu donnes pas assez d'informations sur ton problème...

    comment veux tu avoir des réponses pertinantes ?


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    francois@trillian:~/tmp/c$ gcc p.c
    francois@trillian:~/tmp/c$ ls -lrth
    total 12K
    -rw------- 1 francois francois 3,7K  5 mai   18:38 p.c
    -rwx------ 1 francois francois 7,2K  5 mai   18:38 a.out
    francois@trillian:~/tmp/c$ ./a.out
    Enter the destination address : 192.168.0.2
    Something wrong with socket call :: Operation not permitted
    recv: Bad file descriptor
    Received -1 byte reply from: 192.168.0.2
    Src IP: 0.0.0.0
    Dst IP: 0.0.0.0
    ID: 0
    TTL: 0
    ICMP type: 0
    ICMP code: 0
    ICMP echo ID: 0
    ICMP echo seq: 0
    francois@trillian:~/tmp/c$ su -
    Mot de passe : 
    root@trillian:~# cd /home/francois/tmp/c
    root@trillian:/home/francois/tmp/c# ./a.out
    Enter the destination address : 192.168.0.2
    Received 28 byte reply from: 192.168.0.2
    Src IP: 192.168.0.2
    Dst IP: 192.168.0.161
    ID: 55168
    TTL: 64
    ICMP type: 0
    ICMP code: 0
    ICMP echo ID: 0
    ICMP echo seq: 0
    root@trillian:/home/francois/tmp/c# cd ..
    root@trillian:/home/francois/tmp# rm -rf c
    root@trillian:/home/francois/tmp# exit
    logout
    francois@trillian:~/tmp/c$
    c'est l'obligation d’être root qui te pose problème ??????

  3. #3
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2011
    Messages
    12
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Avril 2011
    Messages : 12
    Points : 0
    Points
    0
    Par défaut
    Citation Envoyé par frp31 Voir le message
    tu te trompe de forum (programmation c), tu mets pas les balises,
    tu donnes pas assez d'informations sur ton problème...

    comment veux tu avoir des réponses pertinantes ?


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    francois@trillian:~/tmp/c$ gcc p.c
    francois@trillian:~/tmp/c$ ls -lrth
    total 12K
    -rw------- 1 francois francois 3,7K  5 mai   18:38 p.c
    -rwx------ 1 francois francois 7,2K  5 mai   18:38 a.out
    francois@trillian:~/tmp/c$ ./a.out
    Enter the destination address : 192.168.0.2
    Something wrong with socket call :: Operation not permitted
    recv: Bad file descriptor
    Received -1 byte reply from: 192.168.0.2
    Src IP: 0.0.0.0
    Dst IP: 0.0.0.0
    ID: 0
    TTL: 0
    ICMP type: 0
    ICMP code: 0
    ICMP echo ID: 0
    ICMP echo seq: 0
    francois@trillian:~/tmp/c$ su -
    Mot de passe : 
    root@trillian:~# cd /home/francois/tmp/c
    root@trillian:/home/francois/tmp/c# ./a.out
    Enter the destination address : 192.168.0.2
    Received 28 byte reply from: 192.168.0.2
    Src IP: 192.168.0.2
    Dst IP: 192.168.0.161
    ID: 55168
    TTL: 64
    ICMP type: 0
    ICMP code: 0
    ICMP echo ID: 0
    ICMP echo seq: 0
    root@trillian:/home/francois/tmp/c# cd ..
    root@trillian:/home/francois/tmp# rm -rf c
    root@trillian:/home/francois/tmp# exit
    logout
    francois@trillian:~/tmp/c$
    c'est l'obligation d’être root qui te pose problème ??????



    je vous remercie: mais j'ai pas compris quel est ma faute.
    mon problème c'est de pinger une hôte dans un réseau et de connaitre est-ce qu'elle est en exécution ou non (vivant ou non).

    ./ping-test3
    Enter the destination address : 192.168.1.2
    Something wrong with socket call :: Operation not permitted
    Sent 4 byte packet to
    recv: Bad file descriptor
    Received -1 byte reply from: 192.168.1.2
    Src IP: 0.0.0.0
    Dst IP: 0.0.0.0
    ID: 0
    TTL: 0

  4. #4
    Modérateur
    Avatar de gangsoleil
    Homme Profil pro
    Manager / Cyber Sécurité
    Inscrit en
    Mai 2004
    Messages
    10 150
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Manager / Cyber Sécurité

    Informations forums :
    Inscription : Mai 2004
    Messages : 10 150
    Points : 28 129
    Points
    28 129
    Par défaut
    Citation Envoyé par talelh Voir le message
    je vous remercie: mais j'ai pas compris quel est ma faute.
    Il faut etre root pour pouvoir lancer ton programme et que celui-ci fonctionne. C'est tout (si on oublie le fait que le code que tu as ecrit n'est pas merveilleux).

Discussions similaires

  1. [Débutant] Problème dans le code source
    Par fraisa1985 dans le forum MATLAB
    Réponses: 1
    Dernier message: 05/12/2009, 20h34
  2. [MySQL] Un problème dans le code PHP
    Par jack_1981 dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 31/07/2006, 11h06
  3. Réponses: 8
    Dernier message: 15/07/2006, 18h59
  4. Affichage de caractère spéciaux absent dans le code source
    Par HNT dans le forum Général Conception Web
    Réponses: 4
    Dernier message: 03/11/2005, 22h38
  5. Quel est le problème dans ce code ?
    Par Luther13 dans le forum C
    Réponses: 12
    Dernier message: 26/08/2003, 16h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo