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 :

sendto:Permission Denied (socket raw)


Sujet :

Réseau C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 13
    Par défaut sendto:Permission Denied (socket raw)
    Bonjour,
    J'ai fait un programme utilisant un socket raw (je gère moi même les headers IP et TCP), tout se passe bien sauf au moment de l'execution. En effet, je suis loggué en root, mais j'ai tout de même un message d'erreur "Permission Denied" (perror) lors de l'utilisation de sendto(). J'ai utilisé setsockopt pour mettre l'option HDRINCL sur mon socket indiquant à mon OS de ne pas s'occuper des headers.

    Voilà le code :
    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
    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
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <string.h>
    #include <stdio.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <linux/ip.h>
    #include <linux/tcp.h>
     
    #define DATAGRAM_SIZE 4096
    #define PORT_DST 80
    #define PORT_SRC 1234
    #define IP_SRC 127.0.0.1
    #define IP_DST 213.246.63.35 
     
     
    unsigned short csum (unsigned short *buf, int nwords)
    {
    	unsigned long sum;
    	for (sum = 0; nwords > 0; nwords--)
    		sum += *buf++;
    	sum = (sum >> 16) + (sum & 0xffff);
    	sum += (sum >> 16);
    	return ~sum;
    }
     
    int main(void)
    {
    	char datagram[DATAGRAM_SIZE];
    	struct iphdr *iph = (struct iphdr *) datagram;
    	struct tcphdr *tcph = (struct tcphdr *) datagram + sizeof(struct iphdr);
    	struct sockaddr_in sain;
    	int sockfd = socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
    	int on;
     
    	sain.sin_family = AF_INET;
    	sain.sin_port = htons(PORT_DST);
    	sain.sin_addr.s_addr = inet_addr("IP_DST");
    	memset(&(sain.sin_zero),0,8);
     
    	memset(datagram,0,DATAGRAM_SIZE);
    	iph->version = 4;
    	iph->ihl = 5;
    	iph->tos = 0;
    	iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
    	iph->id = htonl(8147);
    	iph->frag_off = 0;
    	iph->ttl = 255;
    	iph->protocol = 6;
    	iph->check = 0;
    	iph->saddr = inet_addr("IP_SRC");
    	iph->daddr = sain.sin_addr.s_addr;
    	tcph->source = htons(PORT_SRC);
    	tcph->dest = htons(PORT_DST);
    	tcph->seq = 0;
    	tcph->ack_seq = 0;
    	tcph->res1 = 0;
    	tcph->doff = 0;
    	tcph->fin = 0;
    	tcph->syn = 1;
    	tcph->rst = 0;
    	tcph->psh = 0;
    	tcph->ack = 0;
    	tcph->urg = 0;
    	tcph->ece = 0;
    	tcph->cwr = 0;
    	tcph->window = htonl(65535);
    	tcph->check = 0;
    	tcph->urg_ptr = 0;
     
    	iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
     
    	if (setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL,(char *)&on, sizeof (on)) < 0)
    	{
    		puts("Warning: Cannot set HDRINCL!\n");
    	}
     
    	if (sendto(sockfd, datagram, iph->tot_len, 0, (struct sockaddr *) &sain, sizeof (sain)) < 0)
    	{
    		perror("sendto");
    	}
    	else
    	{
    		puts("ok\n");
    	}
    	return 0;
    }
    Je ne comprends pas vraiment cette erreur étant donné que je suis bel et bien loggué en root lors de l'execution du programme.

    Je vous remercie d'avance pour votre aide.

  2. #2
    Expert éminent
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 68
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Par défaut
    Citation Envoyé par olbat
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    #define IP_SRC 127.0.0.1
    #define IP_DST 213.246.63.35 
     
    	sain.sin_addr.s_addr = inet_addr("IP_DST");
    	iph->saddr = inet_addr("IP_SRC");
    Il n'est pas raisonnable de s'attaquer à des problèmes aussi complexes que les Raw Sockets si on ne maîtrise pas le C et donc son préprocesseur...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    #define IP_SRC "127.0.0.1"
    #define IP_DST "213.246.63.35" 
     
    	sain.sin_addr.s_addr = inet_addr(IP_DST);
    	iph->saddr = inet_addr(IP_SRC);
    Je n'ai pas regardé le reste...

  3. #3
    Membre émérite

    Profil pro
    Inscrit en
    Août 2003
    Messages
    878
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2003
    Messages : 878
    Par défaut
    Citation Envoyé par Emmanuel Delahaye
    Je n'ai pas regardé le reste...
    Tu as bien fait : son problème venait de là (je viens de tester).

  4. #4
    Membre habitué
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 13
    Par défaut
    Merci pour cette réponse, j'ai fait les réctifications nécessaires (j'avais en effet pas fait attention aux ""), mais j'ai toujours le même problème. Une autre idée ?

  5. #5
    Membre émérite

    Profil pro
    Inscrit en
    Août 2003
    Messages
    878
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2003
    Messages : 878
    Par défaut
    Citation Envoyé par olbat
    Merci pour cette réponse, j'ai fait les réctifications nécessaires (j'avais en effet pas fait attention aux ""), mais j'ai toujours le même problème. Une autre idée ?
    Je viens de tester et cela fonctionne chez moi : reposte ton code actuel pour voir ce qui diffère par rapport au mien.

  6. #6
    Membre habitué
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    13
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2006
    Messages : 13
    Par défaut
    Voilà mon code actuel :
    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
    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
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <string.h>
    #include <stdio.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <linux/ip.h>
    #include <linux/tcp.h>
     
    #define DATAGRAM_SIZE 4096
    #define PORT_DST 80
    #define PORT_SRC 1234
    #define IP_SRC "127.0.0.1"
    #define IP_DST "213.246.63.35"
     
     
    unsigned short csum (unsigned short *buf, int nwords)
    {
    	unsigned long sum;
    	for (sum = 0; nwords > 0; nwords--)
    		sum += *buf++;
    	sum = (sum >> 16) + (sum & 0xffff);
    	sum += (sum >> 16);
    	return ~sum;
    }
     
    int main(void)
    {
    	char datagram[DATAGRAM_SIZE];
    	struct iphdr *iph = (struct iphdr *) datagram;
    	struct tcphdr *tcph = (struct tcphdr *) datagram + sizeof(struct iphdr);
    	struct sockaddr_in sain;
    	int sockfd = socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
    	int on;
     
    	sain.sin_family = AF_INET;
    	sain.sin_port = htons(PORT_DST);
    	sain.sin_addr.s_addr = inet_addr(IP_DST);
    	memset(&(sain.sin_zero),0,8);
     
    	memset(datagram,0,DATAGRAM_SIZE);
    	iph->version = 4;
    	iph->ihl = 5;
    	iph->tos = 0;
    	iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
    	iph->id = htonl(8147);
    	iph->frag_off = 0;
    	iph->ttl = 255;
    	iph->protocol = 6;
    	iph->check = 0;
    	iph->saddr = inet_addr(IP_SRC);
    	iph->daddr = sain.sin_addr.s_addr;
    	tcph->source = htons(PORT_SRC);
    	tcph->dest = htons(PORT_DST);
    	tcph->seq = 0;
    	tcph->ack_seq = 0;
    	tcph->res1 = 0;
    	tcph->doff = 5;
    	tcph->fin = 0;
    	tcph->syn = 1;
    	tcph->rst = 0;
    	tcph->psh = 0;
    	tcph->ack = 0;
    	tcph->urg = 0;
    	tcph->ece = 0;
    	tcph->cwr = 0;
    	tcph->window = htonl(65535);
    	tcph->check = 0;
    	tcph->urg_ptr = 0;
     
    	iph->check = csum ((unsigned short *) datagram, iph->tot_len >> 1);
     
    	if (setsockopt(sockfd, SOL_IP, IP_HDRINCL,(char *)&on, sizeof (on)) < 0)
    	{
    		puts("Warning: Cannot set HDRINCL!\n");
    	}
    	if (sendto(sockfd, datagram, iph->tot_len, 0, (struct sockaddr *) &sain, sizeof (sain)) < 0)
    	{
    		perror("sendto");
    	}
    	else
    	{
    		puts("ok\n");
    	}
    	return 0;
    }

Discussions similaires

  1. Socket Permission denied java 7u51
    Par gbalieu dans le forum API standards et tierces
    Réponses: 2
    Dernier message: 28/05/2014, 13h06
  2. SendTo : Permission denied
    Par kyrillos52 dans le forum Réseau
    Réponses: 14
    Dernier message: 20/06/2011, 09h07
  3. [socket RAW] sendto ne marche pas !
    Par poporiding dans le forum C++
    Réponses: 4
    Dernier message: 18/01/2006, 13h14
  4. [PostgreSQL]permission denied
    Par alex2205 dans le forum PostgreSQL
    Réponses: 3
    Dernier message: 13/02/2003, 13h27
  5. App Socket Raw avec VC++6
    Par Martin Soucy dans le forum Développement
    Réponses: 3
    Dernier message: 04/12/2002, 05h07

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