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
|
char request[100] = "GET / HTTP/1.1\r\nHost: ";
strcat(request, hostname);
strcat(request, "\r\nContent-type: text/html\r\nConnection: keep-alive\r\n\r\n");
hostinfo = gethostbyname(hostname);
/* Manque du code, mais sans importance ici (quelques vérifications) */
struct ipv4_header *ih = malloc(sizeof(struct ipv4_header));
struct tcp_header *th = malloc(sizeof(struct tcp_header));
size_t size_request = sizeof(request) + 1;
size_t size_tcp = sizeof(struct tcp_header) + size_request;
size_t size_ip = sizeof(struct ipv4_header) + size_tcp;
char *packet = malloc(size_ip);
if(ih == NULL || th == NULL || packet == NULL) {
printf("L'allocation a echoue\n");
exit(0);
}
/* On remplie les infos pour le socket */
ZeroMemory(&sin, sizeof(sin));
sin.sin_addr = *(IN_ADDR*)hostinfo->h_addr;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
/* On créer notre paquet */
ih->vers_ihl = 4;
ih->tos = 0;
ih->tlen = (u_short)size_ip;
ih->identification = htons(1);
ih->flags_fo = 0;
ih->ttl = 100;
ih->proto = IPPROTO_TCP;
ih->saddr = inet_addr("172.20.167.34");
ih->daddr = inet_ntoa(sin.sin_addr);
ih->op_pad = 0;
ih->crc = in_cksum((unsigned short *)&ih, sizeof(struct ipv4_header)); // calcul du checksum avec la fonction
/* TCP HEADER */
th->sport = 2500;
th->dport = 80;
th->sequence = 1;
th->uck_seq = 1;
th->flag = TCP_ACK;
th->window = 5;
th->checksum = in_cksum((unsigned short*)th, sizeof(struct tcp_header));
memcpy(packet, ih, sizeof(struct ipv4_header));
memcpy(packet + sizeof(ih), th, sizeof(struct tcp_header));
memcpy(packet + sizeof(ih) + sizeof(th), request, sizeof(request));
/* Et enfin, la connexion au serveur, puis l'envoie du paquet */
if(connect(sock, (SOCKADDR*)&sin, sizeof(SOCKADDR)) != SOCKET_ERROR) {
printf("La connexion au serveur %s (%s) est operationnelle\n", hostinfo->h_name, inet_ntoa(sin.sin_addr));
if(send(sock, packet, size_ip, 0) != INVALID_SOCKET) {
char buf[2048];
if(recv(sock, buf, sizeof(buf), 0) != INVALID_SOCKET) {
/* On affiche le résultat */
printf("\n\nResultat de la requete :\n");
printf("%s\n", buf);
getchar();
} else perror("recv()");
} else perror("send()");
} else { perror("connect()"); } |
Partager