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
|
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <windows.h>
#include <unistd.h>
#define IP_SIZE 20
#define ICMP_SIZE 16
#define MAX_PACKET 100
#define ICMP_ECHO 8
SOCKET icmp_socket;
IN_ADDR addr;
SOCKADDR_IN icmp_sock_info;
struct icmp
{
u_char icmp_type;
u_char icmp_code;
u_short icmp_cksum;
u_short icmp_id;
u_short icmp_seq;
//char icmp_data[1];
};
static void init(void)
{
WSADATA wsa;
int err = WSAStartup(MAKEWORD(2, 2), &wsa);
if(err != 0)
{
puts("WSAStartup failed !");
exit(EXIT_FAILURE);
}
}
static void end(void)
{
WSACleanup();
}
int main(void) {
init();
int test;
int lsock =sizeof(icmp_sock_info);
struct icmp* icmp_buf;
char pkt_buf[MAX_PACKET];
icmp_socket= socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (icmp_socket == INVALID_SOCKET)
{
wprintf(L"socket function failed with error = %d\n", WSAGetLastError() );
//exit(1);
}
/* Initialisation de la structure adresse du socket */
icmp_sock_info.sin_family= AF_INET; // Type de socket
icmp_sock_info.sin_addr.s_addr=inet_addr("127.0.0.1");
/* Construction de la trame ICMP */
icmp_buf=(struct icmp *) pkt_buf;
icmp_buf->icmp_type = ICMP_ECHO;
icmp_buf->icmp_code=0;
icmp_buf->icmp_id=1; // ID à définir (id proc)
icmp_buf->icmp_seq=0; // Sequence à incrementer
//gettimeofday((struct timeval *) icmp_buf->icmp_data, NULL);
icmp_buf->icmp_cksum =0; // A DEFINIR
char buffer[100] = "Bonjour\n";
char buffer_reception[100];
test = sendto(icmp_socket, buffer, ICMP_SIZE,0,(SOCKADDR *) &icmp_sock_info,lsock);
if (test <= 0)
perror("Erreur sendto()");
printf("\n-----------TRAME ENVOYEE-----------");
n = recvfrom(icmp_socket, buffer_reception, sizeof(buffer_reception) - 1, 0, (SOCKADDR *)&icmp_sock_info,&lsock);
if (n != SOCKET_ERROR)
{
buffer_reception[n] = '\0';
printf("Un message de %s : %s", inet_ntoa(icmp_sock_info.sin_addr), buffer_reception);
}
else
{
perror("Erreur recvfrom()");
}
closesocket(icmp_socket);
end();
return EXIT_SUCCESS;
} |
Partager