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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
Partie Client:
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#define SRV_HOST_ADDR "192.168.12.129"
#define SRV_UDP_PORT 3222
//Fonction calculant le checksum
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 (unsigned short)(~sum);
}
int main(void)
{
int sock,n;
char msg[1000]="Salut !";
struct sockaddr_in srvaddr, cliaddr;
struct ip *ip = (struct ip *) msg; //Notre structure d'en-tete ip
struct udphdr *udp = (struct udphdr *) (msg + sizeof(struct ip)); //Notre structure d'en-tete udp
int one=1;
const int *val=&one;
memset(&srvaddr,0,sizeof(struct sockaddr_in));
srvaddr.sin_family=AF_INET;
srvaddr.sin_addr.s_addr=inet_addr(SRV_HOST_ADDR);
srvaddr.sin_port=htons(SRV_UDP_PORT);
sock=socket(PF_INET,SOCK_RAW,IPPROTO_UDP);
if (sock < 0)
{
printf("Erreur dans la creation de la socket...\n");
return 1;
}
else printf("Creation de la socket reussie...\n");
memset(&cliaddr,0,sizeof(struct sockaddr_in));
cliaddr.sin_family=AF_INET;
cliaddr.sin_addr.s_addr=htonl(INADDR_ANY);
cliaddr.sin_port=htons(0);
//Assignation des valeurs pour l'en-tete ip
ip->ip_hl=5;
ip->ip_v=4;
ip->ip_tos=0;
ip->ip_len=sizeof(struct ip)+sizeof(struct udphdr);
ip->ip_id=htonl(54321);
ip->ip_off=0;
ip->ip_ttl=200;
ip->ip_p=17;
ip->ip_sum=0;
ip->ip_src.s_addr=inet_addr(SRV_HOST_ADDR);
ip->ip_dst.s_addr=inet_addr("192.168.12.128");
//Assignation des valeurs pour l'en-tete udp
udp->source=htons(SRV_UDP_PORT);
udp->dest=htons(3222);
udp->len=htons(sizeof(struct udphdr));
udp->check=0;
ip->ip_sum=csum((unsigned short *) msg, sizeof(struct ip) + sizeof(struct udphdr));
printf("Valeur du checksum:%X\n",ip->ip_sum);
/*
if(setsockopt(sock, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0 )
{
printf("Attention : Alerte Error !....\n");
}
else printf("Le HDRINCL est OK....\n");
*/
n=sendto(sock,msg,ip->ip_len,0,(struct sockaddr *)&srvaddr,sizeof(srvaddr));
if (n < 0)
{
//if (sendto(sock,(char*)msg,sizeof(msg),0,(struct sockaddr *)&srvaddr,sizeof(srvaddr)) < 0)
//{
printf("L'envoi de la socket raw n'a pas abouti....\n");
return 1;
}
else printf("L'envoi est en cours....\n");
close(sock);
return 0;
}
Partie Serveur:
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#define SRV_HOST_ADDR "192.168.12.128"
#define SRV_UDP_PORT 3222
//Fonction calculant le checksum
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 (unsigned short)(~sum);
}
int main(void)
{
int sock;
char msg[10000];
struct sockaddr_in srvaddr, cliaddr;
int fromlen=sizeof(cliaddr);
struct ip *ip = (struct ip *) msg;
struct udphdr *udp = (struct udphdr *) (msg + sizeof(struct ip));
sock=socket(PF_INET,SOCK_RAW,IPPROTO_UDP);
if (sock<0)
{
printf("Erreur dans la creation de la socket...\n");
return 1;
}
else printf("Creation de la socket reussie...\n");
memset(&srvaddr,0,sizeof(struct sockaddr_in));
srvaddr.sin_family=AF_INET;
srvaddr.sin_addr.s_addr=inet_addr(SRV_HOST_ADDR);
srvaddr.sin_port=htons(SRV_UDP_PORT);
memset(msg,0,100);
//Assignation des valeurs pour l'en-tete ip
ip->ip_hl=5;
ip->ip_v=4;
ip->ip_tos=0;
ip->ip_len=sizeof(struct ip)+sizeof(struct udphdr);
ip->ip_id=htonl(54321);
ip->ip_off=0;
ip->ip_ttl=200;
ip->ip_p=17;
ip->ip_sum=0;
ip->ip_src.s_addr=inet_addr(SRV_HOST_ADDR);
ip->ip_dst.s_addr=inet_addr("192.168.12.129");
//Assignation des valeurs pour l'en-tete udp
udp->source=htons(SRV_UDP_PORT);
udp->dest=htons(3222);
udp->len=htons(sizeof(struct udphdr));
udp->check=0;
ip->ip_sum=csum((unsigned short *)msg, sizeof(struct ip) + sizeof(struct udphdr));
while(1)
{
if(recvfrom(sock,msg,100,0,(struct sockaddr *)&cliaddr,&fromlen) < 0)
{
printf("Problème dans la réception du message...\n");
return 1;
}
else printf("Message du client : %s\n",msg);
}
return 0;
} |
Partager