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
|
void main(int argc, char **argv)
{
printf("IP de www.berkeley.edu -> %s\n",getIp("www.berkeley.edu"));
/* Declaration des variable */
struct iphdr *ip, *ip_rep;
struct udphdr* udp;
char* paquet;
char* buffer;
int optval;
/* Initialisation des variables */
ip = malloc(sizeof(struct iphdr));
ip_rep = malloc(sizeof(struct iphdr));
udp = malloc(sizeof(struct udphdr));
paquet = malloc(sizeof(struct iphdr) + sizeof(struct udphdr));
buffer = malloc(sizeof(struct iphdr) + sizeof(struct udphdr));
/* Definition de l'espace memoire */
ip = (struct iphdr*) paquet;
udp = (struct udphdr*) (paquet + sizeof(struct iphdr));
/* struct ip */
ip->ihl = 5;
ip->version = 4;
ip->tos = 0;
ip->tot_len = sizeof(struct iphdr) + sizeof(struct udphdr);
ip->id = htons(random());
ip->ttl = 255;
ip->protocol = IPPROTO_UDP;
ip->check = 0;
inet_pton(AF_INET,getIp("www.berkeley.edu"),(struct sockaddr *)&ip->saddr);
inet_pton(AF_INET,getIp("www.berkeley.edu"),(struct sockaddr *)&ip->daddr);
/* struct udp */
udp->source = htons(33434);
udp->dest = htons(33434);
udp->len = sizeof(struct udphdr);
udp->check = 0;
udp->check = checksum((unsigned short *)udp, sizeof(struct udphdr));
ip->check = checksum((unsigned short *)ip, sizeof(struct iphdr));
/* UDP */
int sockfd;
socklen_t addrlen;
struct sockaddr_in connection;
// check the number of args on command line
/*if(argc != 4)
{
printf("USAGE: %s @dest port_num string\n", argv[0]);
exit(-1);
}*/
// socket factory
if((sockfd = socket(AF_INET,SOCK_RAW,IPPROTO_UDP)) == -1)
{
perror("Probleme ouverture socket");
exit(EXIT_FAILURE);
}
/* empeche que le systeme remplisse automatiquement les entètes */
setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &optval, sizeof(int));
/* On remplie la struct sockaddr_in */
connection.sin_family = AF_INET;
connection.sin_port = htons(33434);
addrlen = sizeof(struct sockaddr);
// get addr from command line and convert ip
if(inet_pton(AF_INET,getIp("www.berkeley.edu"),&(connection.sin_addr)) != 1)
{
perror("Probleme inet_pton");
close(sockfd);
exit(EXIT_FAILURE);
}
// Envoi du paquet
if(sendto(sockfd,paquet,ip->tot_len,0,(struct sockaddr *)&connection,addrlen) == -1)
{
perror("Erreur lors de l'envoi du message");
close(sockfd);
exit(EXIT_FAILURE);
}
printf("Message Envoyer !\n");
addrlen = sizeof(connection);
if (recvfrom(sockfd, buffer, sizeof(struct iphdr) + sizeof(struct icmphdr), 0, (struct sockaddr *)&connection, &addrlen) == -1)
perror("recv");
else
{
char ipRouteur[INET_ADDRSTRLEN];
ip_rep = (struct iphdr*) buffer;
inet_ntop(AF_INET,&(ip_rep->saddr),ipRouteur,sizeof(ipRouteur));
printf("IP: %s\n", ipRouteur);
// printf("IP: %s\n", inet_ntoa(((struct sockaddr_in*)&connection)->sin_addr));
printf("ID: %d\n", ntohs(ip_rep->id));
printf("TTL: %d\n", ip_rep->ttl);
}
// close the socket
close(sockfd);
} |
Partager