Salut tout le monde!
J'ai qlq probléme au niveau de l'utilisation de sprintf ou peut être au niveau de l'utilisation du pointeur,
Je vous donne le code:
Ici c'est le netflow.h
qui contient les structures que j'utilisent
Ici c'est le programme principale:
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 #define CONST_V5FLOWS_PER_PAK 30 struct flow_ver5_hdr { uint16_t version; /* Current version=5*/ uint16_t count; /* The number of records in PDU. */ uint32_t sysUptime; /* Current time in msecs since router booted */ uint32_t unix_secs; /* Current seconds since 0000 UTC 1970 */ uint32_t unix_nsecs; /* Residual nanoseconds since 0000 UTC 1970 */ uint32_t flow_sequence; /* Sequence number of total flows seen */ uint8_t engine_type; /* Type of flow switching engine (RP,VIP,etc.)*/ uint8_t engine_id; /* Slot number of the flow switching engine */ }; struct flow_ver5_rec { struct in_addr srcIp; struct in_addr dstIp; uint32_t nexthop; /* Next hop router's IP Address */ uint16_t input; /* Input interface index */ uint16_t output; /* Output interface index */ uint32_t dPkts; /* Packets sent in Duration (milliseconds between 1st & last packet in this flow)*/ uint32_t dOctets; /* Octets sent in Duration (milliseconds between 1st & last packet in this flow)*/ uint32_t First; /* SysUptime at start of flow */ uint32_t Last; /* and of last packet of the flow */ uint16_t srcport; /* TCP/UDP source port number (.e.g, FTP, Telnet, etc.,or equivalent) */ uint16_t dstport; /* TCP/UDP destination port number (.e.g, FTP, Telnet, etc.,or equivalent) */ uint8_t pad1; /* pad to word boundary */ uint8_t tcp_flags; /* Cumulative OR of tcp flags */ uint8_t prot; /* IP protocol, e.g., 6=TCP, 17=UDP, etc... */ uint8_t tos; /* IP Type-of-Service */ uint16_t dst_as; /* dst peer/origin Autonomous System */ uint16_t src_as; /* source peer/origin Autonomous System */ uint8_t dst_mask; /* destination route's mask bits */ uint8_t src_mask; /* source route's mask bits */ uint16_t pad2; /* pad to word boundary */ }; struct Netflow5 { struct flow_ver5_hdr flowHeader; struct flow_ver5_rec flowRecord[CONST_V5FLOWS_PER_PAK+1 /* safe against buffer overflows */]; };
Comme vous voyez c'est un programme réseau,mais le probléme ne se situe pas dans la programmation réseau,mais dans le langage C en génerale
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 #include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<stdlib.h> #include<errno.h> #include<string.h> #include<signal.h> #include<unistd.h> #include<mysql/mysql.h> #include "netflow.h" MYSQL mysql; void connection(){ mysql_init(&mysql); mysql_connect(&mysql,"localhost","root",""); mysql_select_db(&mysql,"projet"); } main(){ char *query; FILE *log; int i; struct flow_ver5_hdr *header; struct flow_ver5_rec *record; struct sockaddr_in server; int sock,port,l_izy,cl; char buff[9000]; header=malloc(sizeof(struct flow_ver5_hdr)); record=malloc(sizeof(struct flow_ver5_rec)); sock=socket(AF_INET,SOCK_DGRAM,0); if(sock<0){ perror("Erreur socket"); exit(1); } port=2000; server.sin_port=htons(port); server.sin_family=AF_INET; server.sin_addr.s_addr=INADDR_ANY; if(bind(sock,(struct sockaddr*)&server,sizeof server)<0){ perror("Erreur sur bind"); exit(1); } connection(); for(;;){ if(read(sock,buff,9000)<0){ perror("Erreur sur read"); exit(1); } header=(struct flow_ver5_hdr*)buff; for(i=0;i<(unsigned long int)ntohs(header->count);i++){ record=(struct flow_ver5_rec*)&buff[sizeof(struct flow_ver5_hdr)+i*sizeof(struct flow_ver5_rec)]; //C'est ici le probléme sprintf(query," insert into netflow values(\"%s\",\"%s\",%u,%u,%lu);" ,(char*)inet_ntoa(record->srcIp) ,(char*)inet_ntoa(record->dstIp) ,(unsigned int)ntohs(record->srcport) ,(unsigned int)ntohs(record->dstport) ,(unsigned long int)ntohl(record->dOctets) ); mysql_query(&mysql,query); } } close(sock); }
En faite (char*)inet_ntoa(record->srcIp) et (char*)inet_ntoa(record->dstIp) ont toujours les même valeur,il ya toujours insertion de même valeurs au niveau de la base de donné dans le champ Ipsrc et Ipdst.
de même si j'essai seulement de l'afficher en ecran.
Mais si je fais:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 printf("%s\t%s\n",(char*)inet_ntoa(record->srcIp),(char*)inet_ntoa(record->dstIp) );
Le programme m'envoie l'Ipsource et l'Ipdestination convenablement!
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 printf("%s\t",(char*)inet_ntoa(record->srcIp)); printf("%s\n",(char*)inet_ntoa(record->dstIp));
Alors là je me perds totalement,est-ce que vous pouvez m'aider!
Et merci d'avance!!!
Partager