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
| #include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#define MAX_MESSAGE 100
#define IP_SOURCE "127.0.0.1"
#define IP_DEST "127.0.0.1"
#define PORT_SOURCE 5050
#define PORT_DEST 5050
typedef struct ipheader {
UCHAR hlen : 4, version : 4;
UCHAR tos;
USHORT tot_len;
USHORT id;
USHORT offset : 13, flags : 3;
UCHAR ttl;
UCHAR protocole;
USHORT somme;
ULONG ip_source;
ULONG ip_dest;
} IPHEADER;
typedef struct udpheader {
USHORT port_source;
USHORT port_dest;
USHORT tot_len;
USHORT somme;
} UDPHEADER;
USHORT checksum(void * lpData, size_t size);
int main()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
fprintf(stderr, "La fonction WSAStartup a echoue.\n");
else
{
SOCKET s;
s = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
if (s == INVALID_SOCKET)
fprintf(stderr, "La fonction socket a echoue.\n");
else
{
DWORD sockopt = TRUE;
if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&sockopt, sizeof(sockopt)) == SOCKET_ERROR)
fprintf(stderr, "La fonction setsockopt a echoue.\n");
else
{
char message[MAX_MESSAGE] = "Bonjour.", * buf;
USHORT message_len, udp_len, ip_len;
message_len = (USHORT)(strlen(message) + 1); /* Longueur du message */
udp_len = (USHORT)(sizeof(UDPHEADER) + message_len); /* Longueur du paquet UDP */
ip_len = (USHORT)(sizeof(IPHEADER) + udp_len); /* Longueur du paquet IP */
buf = malloc(ip_len);
if (buf == NULL)
fprintf(stderr, "La fonction malloc a echoue.\n");
else
{
SOCKADDR_IN dest;
IPHEADER ip;
UDPHEADER udp;
/* Remplissage de la structure ip (en-tete du paquet) */
/* Rappel : Attention a l'ordre des octets (pour les mots de plus de 1 octet). */
/* Utilisez htons et htonl pour mettre les shorts et les longs au bon format. */
/* Cela ne concerne pas les chars car la taille d'un char est de 1 octet. */
ip.version = 4;
ip.hlen = 5; /* sizeof(ip) / 4 */
ip.tos = 0;
ip.tot_len = htons(ip_len);
ip.id = htons(1);
ip.flags = 0;
ip.offset = 0;
ip.ttl = 100;
ip.protocole = IPPROTO_UDP;
ip.somme = 0;
ip.ip_source = inet_addr(IP_SOURCE);
ip.ip_dest = inet_addr(IP_DEST);
ip.somme = checksum(&ip, sizeof(ip));
udp.port_source = htons(PORT_SOURCE);
udp.port_dest = htons(PORT_DEST);
udp.tot_len = htons(udp_len);
udp.somme = 0;
memcpy(buf, &ip, sizeof(ip));
memcpy(buf + sizeof(ip), &udp, sizeof(udp));
memcpy(buf + sizeof(ip) + sizeof(udp), message, message_len);
ZeroMemory(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr(IP_DEST);
dest.sin_port = htons(PORT_DEST);
if (sendto(s, buf, ip_len, 0, (SOCKADDR *)&dest, sizeof(dest)) == SOCKET_ERROR)
fprintf(stderr, "La fonction sendto a echoue.\n");
else
printf("Message envoye !\n");
free(buf);
}
}
closesocket(s);
}
WSACleanup();
}
return 0;
}
USHORT checksum(void * lpData, size_t size)
{
USHORT * t = lpData;
DWORD somme = 0;
size_t i, n = size / sizeof(USHORT);
for(i = 0; i < n; i++)
somme += t[i];
if (size % 2)
{
UCHAR * u = lpData;
somme += u[size - 1];
}
while (HIWORD(somme))
somme = HIWORD(somme) + LOWORD(somme);
return LOWORD(~somme);
} |
Partager