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

C Discussion :

Erreur de Segmentation avec un Unsigned long


Sujet :

C

  1. #1
    Membre actif
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Mai 2014
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Administrateur de base de données
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2014
    Messages : 33
    Par défaut Erreur de Segmentation avec un Unsigned long
    Bonjour voila j'ai coder un script qui permet de scanner des IP d'allant d'une ranger A a une B en utilisant un systeme de multi threading et j'exporte les IP's qui ont le port 80 ouvert dans un fichier .txt

    voila le probleme est que quand je vais pour compiler le programme j'ai sa comme warning deja



    Nom : Screenshot_5.png
Affichages : 255
Taille : 14,0 Ko


    et quand je l'execute sa me met une erreur de segmentation

    voici le code source

    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
    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 <pthread.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <signal.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <math.h>
    #include <ctype.h>
    #include <errno.h>
    #include <arpa/inet.h>
    #include <netinet/ip.h>
    #include <netinet/udp.h>
    #include <netdb.h>
     
    volatile int running_threads = 0;
    volatile int found_srvs = 0;
    volatile unsigned long per_thread = 0;
    volatile unsigned long start = 0;
    volatile unsigned long scanned = 0;
    volatile unsigned long hosts_done = 0;
    int   sd;         
       int    rval;         
       char    responce[1024];      
       char   *message="shell";      
       struct hostent *hostaddr;   
       struct sockaddr_in servaddr;   
    FILE *fd;
     
    void *flood(void *par1)
    {
    	running_threads++;
    	int thread_id = (int)par1;
    	unsigned long start_ip = htonl(ntohl(start)+(per_thread*thread_id));
    	unsigned long end = htonl(ntohl(start)+(per_thread*(thread_id+1)));
    	unsigned long w;
    	int y;
     
    	for(w=ntohl(start_ip);w<htonl(end);w++)
    	{
     
    		sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); 
    		if (sd == -1)
    		{
    			printf ("erreur\n");
     
    		}  
     
    	memset( &servaddr, 0, sizeof(servaddr));
    	servaddr.sin_family = AF_INET;
    	servaddr.sin_port = htons(80); 
    	hostaddr = gethostbyname(w); 
     
    	memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
     
    	rval = connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr));
     
    	if (rval == -1)
       {
     
        close(sd);
       }
     
       else
       {
     
    	fprintf(fd,"%s", w);
    	fflush(fd);
    	close(sd);        
       }	
    		scanned++;
    		hosts_done++;
     
    	}
     
    	running_threads--;
    	return;
    }
     
    void sighandler(int sig)
    {
    	fclose(fd);
    	printf("\n");
    	exit(0);
    }
     
     
     
    int main(int argc, char *argv[ ])
    {
     
    	if(argc < 5){
    		fprintf(stderr, "Paramteres non valides \n\n");
    		fprintf(stdout, "Utilisation  :   %s [Debut de la Ranger] [Fin de la ranger] [Fichier .txt] [Threads] \n", argv[0]);
    		exit(-1);
    	}
    	fd = fopen(argv[3], "a");
     
     
    	signal(SIGINT, &sighandler);
     
    	int threads = atoi(argv[4]);
     
    	pthread_t thread;
     
     
     
    	char *str_start = malloc(18);
    	memset(str_start, 0, 18);
    	str_start = argv[1];
    	char *str_end = malloc(18);
    	memset(str_end, 0, 18);
    	str_end = argv[2];
    	start = inet_addr(str_start);
    	per_thread = (ntohl(inet_addr(str_end)) - ntohl(inet_addr(str_start))) / threads;
    	unsigned long toscan = (ntohl(inet_addr(str_end)) - ntohl(inet_addr(str_start)));
    	int i;
    	for(i = 0;i<threads;i++){
    		pthread_create( &thread, NULL, &flood, (void *) i);
    	}
    	sleep(1);
    	printf("Scan en cours \n");
    	char *temp = (char *)malloc(17);
    	memset(temp, 0, 17);
    	sprintf(temp, "IP Trouver");
    	printf("%-16s", temp);
    	memset(temp, 0, 17);
    	sprintf(temp, "IP/s");
    	printf("%-16s", temp);
    	memset(temp, 0, 17);
    	sprintf(temp, "Threads");
    	printf("%-16s", temp);
    	memset(temp, 0, 17);
    	sprintf(temp, "Pourcentage");
    	printf("%s", temp);
    	printf("\n");
     
    	char *new;
    	new = (char *)malloc(16*6);
    	while (running_threads > 0)
    	{
    		printf("\r");
    		memset(new, '\0', 16*6);
    		sprintf(new, "%s|%-15lu", new, found_srvs);
    		sprintf(new, "%s|%-15d", new, scanned);
    		sprintf(new, "%s|%-15d", new, running_threads);
    		memset(temp, 0, 17);
    		int percent_done=((double)(hosts_done)/(double)(toscan))*100;
    		sprintf(temp, "%d%%", percent_done);
    		sprintf(new, "%s|%s", new, temp);
    		printf("%s", new);
    		fflush(stdout);
     
    		scanned = 0;
    		sleep(1);
    	}
    	printf("\n");
    	fclose(fd);
    	return 0;
    }

    j'ai direct compris que sa venais du gethostbyname mais je ne comprends pas pourquoi la variable w ne marche pas j'ai donc decider de la remplacer
    gethostbyname("127.0.0.1"); pour tester et la sa marche niquel
    mais j'aimerai que sa marche avec w

    Merci d'avances pour vos reponses

  2. #2
    Expert éminent
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 390
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 390
    Par défaut
    Le warning est pourtant simple: Le code n'apprécie pas que tu castes un int en pointeur (même si dans la circonstance précise où tu fais ça -- la création de thread avec un int en paramètre -- c'est acceptable, et tu peux faire taire le compilo en castant le int en intptr_t d'abord).

    Par contre, ton erreur semble plutôt venir de tes chaînes, car tu sembles ne pas avoir bien compris comment t'en servir. Par exemple, ici:
    Code C mauvais : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    	char *str_start = malloc(18);
    	memset(str_start, 0, 18);
    	str_start = argv[1];
    Tu causes une fuite de mémoire.

    Et ici:
    Code C surcompliqué : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    	char *temp = (char *)malloc(17);
    	memset(temp, 0, 17);
    	sprintf(temp, "IP Trouver");
    	printf("%-16s", temp);
    Je ne vois pas pourquoi tu ne fais pas tout simplement printf("%-16s", "IP Trouver");[sic]
    SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

    "Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
    Apparently everyone.
    -- Raymond Chen.
    Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.

  3. #3
    Membre actif
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Mai 2014
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Administrateur de base de données
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2014
    Messages : 33
    Par défaut
    Le probleme vient pas du start ip ni de l'affichage des IP, mais de la variable w qui ne va pas avec le gethostbyname faudrait trouver une solution

    j'avais penser a le remplacer par sa

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    char address[INET_ADDRSTRLEN] = {0};
    unsigned long w2 = htonl(w);
    hostaddr = gethostbyname(inet_ntop(AF_INET, &w2, address, sizeof address));

  4. #4
    Membre actif
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Mai 2014
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Administrateur de base de données
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2014
    Messages : 33
    Par défaut
    Voici le code source sans erreur de memoire ni segmentation

    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
    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
    166
    167
    168
     
     
     
     
    #include <pthread.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <signal.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <math.h>
    #include <ctype.h>
    #include <errno.h>
    #include <arpa/inet.h>
    #include <netinet/ip.h>
    #include <netinet/udp.h>
    #include <netdb.h>
     
    volatile int running_threads = 0;
    volatile int found_srvs = 0;
    volatile unsigned long per_thread = 0;
    volatile unsigned long start = 0;
    volatile unsigned long scanned = 0;
    volatile unsigned long hosts_done = 0;
    int   sd;         
       int    rval;         
       char    responce[1024];      
       char   *message="shell";      
       struct hostent *hostaddr;   
       struct sockaddr_in servaddr;   
    FILE *fd;
     
    void *flood(void *par1)
    {
    	running_threads++;
    	int thread_id = (int)par1;
    	unsigned long start_ip = htonl(ntohl(start)+(per_thread*thread_id));
    	unsigned long end = htonl(ntohl(start)+(per_thread*(thread_id+1)));
    	unsigned long w;
    	int y;
     
    	for(w=ntohl(start_ip);w<htonl(end);w++)
    	{
     
    		sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); 
    		if (sd == -1)
    		{
    			printf ("erreur\n");
     
    		}  
    	char address[INET_ADDRSTRLEN] = {0};
    	unsigned long w2 = htonl(w);
    	memset( &servaddr, 0, sizeof(servaddr));
    	servaddr.sin_family = AF_INET;
    	servaddr.sin_port = htons(80); 
    	hostaddr = gethostbyname(inet_ntop(AF_INET, &w2, address, sizeof address));
     
    	memcpy(&servaddr.sin_addr, hostaddr->h_addr, hostaddr->h_length);
     
    	rval = connect(sd, (struct sockaddr *) &servaddr, sizeof(servaddr));
     
    	if (rval == -1)
       {
     
        close(sd);
       }
     
       else
       {
     
    	fprintf(fd,"%s", w);
    	fflush(fd);
    	close(sd);        
       }	
    		scanned++;
    		hosts_done++;
     
    	}
     
    	running_threads--;
    	return;
    }
     
    void sighandler(int sig)
    {
    	fclose(fd);
    	printf("\n");
    	exit(0);
    }
     
     
     
    int main(int argc, char *argv[ ])
    {
     
    	if(argc < 5){
    		fprintf(stderr, "Paramteres non valides \n\n");
    		fprintf(stdout, "Utilisation  :   %s [Debut de la Ranger] [Fin de la ranger] [Fichier .txt] [Threads] \n", argv[0]);
    		exit(-1);
    	}
    	fd = fopen(argv[3], "a");
     
     
    	signal(SIGINT, &sighandler);
     
    	int threads = atoi(argv[4]);
     
    	pthread_t thread;
     
     
     
    	char *str_start = malloc(18);
    	memset(str_start, 0, 18);
    	str_start = argv[1];
    	char *str_end = malloc(18);
    	memset(str_end, 0, 18);
    	str_end = argv[2];
    	start = inet_addr(str_start);
    	per_thread = (ntohl(inet_addr(str_end)) - ntohl(inet_addr(str_start))) / threads;
    	unsigned long toscan = (ntohl(inet_addr(str_end)) - ntohl(inet_addr(str_start)));
    	int i;
    	for(i = 0;i<threads;i++){
    		pthread_create( &thread, NULL, &flood, (void *) i);
    	}
    	sleep(1);
    	printf("Scan en cours \n");
    	char *temp = (char *)malloc(17);
    	memset(temp, 0, 17);
    	sprintf(temp, "IP Trouver");
    	printf("%-16s", temp);
    	memset(temp, 0, 17);
    	sprintf(temp, "IP/s");
    	printf("%-16s", temp);
    	memset(temp, 0, 17);
    	sprintf(temp, "Threads");
    	printf("%-16s", temp);
    	memset(temp, 0, 17);
    	sprintf(temp, "Pourcentage");
    	printf("%s", temp);
    	printf("\n");
     
    	char *new;
    	new = (char *)malloc(16*6);
    	while (running_threads > 0)
    	{
    		printf("\r");
    		memset(new, '\0', 16*6);
    		sprintf(new, "%s|%-15lu", new, found_srvs);
    		sprintf(new, "%s|%-15d", new, scanned);
    		sprintf(new, "%s|%-15d", new, running_threads);
    		memset(temp, 0, 17);
    		int percent_done=((double)(hosts_done)/(double)(toscan))*100;
    		sprintf(temp, "%d%%", percent_done);
    		sprintf(new, "%s|%s", new, temp);
    		printf("%s", new);
    		fflush(stdout);
     
    		scanned = 0;
    		sleep(1);
    	}
    	printf("\n");
    	fclose(fd);
    	return 0;
    }

    je le compile tout va bien mais quand je vais pour scaner sa n'a pas l'air de scanner + de quand je cat le http.txt il n'y a rien dedans regardez

    Nom : Screenshot_3.png
Affichages : 147
Taille : 3,0 Ko

  5. #5
    Expert éminent

    Femme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 202
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 202
    Par défaut
    Bonjour,

    Les erreurs signalées par Medinoc sont toujours présentes.
    Tu n'as pas un appel de free par appel de malloc, ton code donc est nécessairement faux.

    Tant que ce ne sera pas réglé, tu ne peux pas prétendre avoir réglé les problèmes de mémoire.

    Je parie que le moindre profileur devrait crier pendant des pages.

Discussions similaires

  1. Erreur de segmentation avec fclose
    Par sylvanus35 dans le forum Débuter
    Réponses: 7
    Dernier message: 09/08/2008, 05h33
  2. Erreur de segmentation avec libxml2
    Par DevMg dans le forum XML
    Réponses: 0
    Dernier message: 18/02/2008, 14h44
  3. Erreur de segmentation avec RSA_verify
    Par LorDjidane dans le forum C
    Réponses: 7
    Dernier message: 14/06/2007, 11h29
  4. Erreur de segmentation avec un main vide
    Par matique dans le forum Réseau
    Réponses: 10
    Dernier message: 22/04/2007, 07h07
  5. petit probleme avec un (unsigned long int)
    Par Micks71 dans le forum C
    Réponses: 6
    Dernier message: 19/05/2006, 12h08

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