Slt
Juste un petit code qui permet d'envoie des pings , j'ai bien sur essayer de simuler au mieu la sortie d'un vrai ping.
Je suis ouvert a toute critique bonne/mauvaise constructive ou non !
Je signale juste que je débute quand même .
Pour le lancer : ./pingz -h [ip] --ttl [ttl] -t [timeout] -c [nombre de ping]
[quote:f2b1e10247]# ./pingz -h 216.239.59.104 -c 6
PING 216.239.59.104 (216.239.59.104) .
28 bytes from 216.239.59.104: icmp_seq=1 ttl=239
28 bytes from 216.239.59.104: icmp_seq=2 ttl=239
28 bytes from 216.239.59.104: icmp_seq=3 ttl=239
28 bytes from 216.239.59.104: icmp_seq=4 ttl=239
28 bytes from 216.239.59.104: icmp_seq=5 ttl=239
28 bytes from 216.239.59.104: icmp_seq=6 ttl=239
<< 216.239.59.104 ping statistics >>
6 packets transmitted, 6 received, 0 packet loss[/quote:f2b1e10247]
[code:1:f2b1e10247]
/****************************************************************************
*****************************************************************************
PING T00L'Z v 1
CODED BY SIMPP
WHY : Just for fun
LICENCE GNU/GPL
Copyright 2008, Simpp ( null.sim@gmail.com )
This file is part of pingz
pingz is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************
****************************************************************************/
/*************************
******** header **********
*************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <net/if.h>
/*************************
******* structure ********
*************************/
typedef struct iphdr_s {
unsigned int ihl:4;
unsigned int version:4;
unsigned char tos;
unsigned short int tot_len;
unsigned short int id;
unsigned short int offset;
unsigned char ttl;
unsigned char protocol;
unsigned short int checksum;
unsigned int src;
unsigned int dst;
} iphdr_t;
typedef struct icmphdr_s {
unsigned char type;
unsigned char code;
unsigned short int checksum;
unsigned short int id;
unsigned short int seq;
} icmphdr_t;
typedef struct socket_s {
int s;
struct sockaddr_in buf;
} socket_t;
typedef struct info_s {
int timeout;
int nbr;
int ttl;
int seq;
char *host;
char *src;
} info_t;
typedef struct info_recev_s {
int seq;
int ttl;
int bytes;
} i_recev_t;
/*************************
******* prototype ********
*************************/
int start(info_t *info);
int check_argument(info_t *info, int argc, char *argv[]);
int ping(info_t *info);
int recv_packet(socket_t *sock, icmphdr_t *icmp, iphdr_t *ip, i_recev_t *i_recev);
void build_header_ip(iphdr_t *ip, info_t *info);
void build_header_icmp(icmphdr_t *icmp, int seq);
int init_struct_info(info_t **info);
int init_struct_socket(socket_t **sock);
int init_struct_ip(iphdr_t **ip, char *packet_send);
int init_struct_icmp(icmphdr_t **icmp, char *packet_send);
void free_socket(socket_t *sock);
void
free_info(info_t *info);
int create_new_socket(socket_t *sock, const char *ip_dst);
unsigned short in_cksum(unsigned short * addr,int len);
int conver_int(const char *str, int *nbr);
int ip_source(info_t *info);
int main(int argc, char *argv[]) {
info_t *info = NULL;
if ( init_struct_info(&info) == -1 )
return -1;
if ( check_argument(info, argc, argv) == -1 ) {
free_info(info);
return -1;
}
if ( start(info) == -1 ) {
free_info(info);
return -1;
}
free_info(info);
return 0;
}
int
start(info_t *info)
{
int ret;
int loss = 0;
int received = 0;
int transmitted = 1;
printf("PING %s (%s) .\n", info->host, info->host);
while ( transmitted <= info->nbr ) {
info->seq = transmitted;
ret = ping(info);
if ( ret == 1 )
received++;
else
loss++;
transmitted++;
}
printf("\n<< %s ping statistics >>\n", info->host);
printf("%d packets transmitted, %d received, %d packet loss\n", transmitted-1, received, loss);
return 0;
}
/*****************************************************************
****************** --- FUNCTION ARGUMENTS --- ********************
*****************************************************************/
int
check_argument(info_t *info, int argc, char *argv[])
{
int i = 1;
info->nbr = 4;
info->timeout = 3;
info->ttl = 255;
info->host = NULL;
info->src = NULL;
while ( i < argc ) {
if ( !strcmp(argv[i], "-c") ) {
i++;
if ( conver_int(argv[i], &info->nbr) == -1 )
return -1;
}
else if ( !strcmp(argv[i], "-t") ) {
i++;
if ( conver_int(argv[i], &info->timeout) == -1 )
return -1;
}
else if ( !strcmp(argv[i], "--ttl") ) {
i++;
if ( conver_int(argv[i], &info->ttl) == -1 )
return -1;
}
else if ( !strcmp(argv[i], "-h") ) {
i++;
info->host = NULL;
info->host = (char *) malloc(strlen(argv[i])+1 * sizeof(char));
if ( info->host == NULL ) {
fprintf(stderr, "malloc() on info->host failed\n");
return -1;
}
strncpy(info->host, argv[i], strlen(argv[i])+1);
}
i++;
}
if ( ip_source(info) == -1 )
return -1;
return 0;
}
/*****************************************************************
******************************************************************
*****************************************************************/
/*****************************************************************
********************* --- FONCTION PING --- **********************
*****************************************************************/
int
ping(info_t *info)
{
int ret, res;
char *packet_send = NULL;
char *packet_recv = NULL;
socket_t *sock = NULL;
icmphdr_t *icmp = NULL;
iphdr_t *ip = NULL;
i_recev_t i_recev;
packet_send = (char *) malloc(sizeof(iphdr_t) + sizeof(icmphdr_t));
if ( packet_send == NULL ) {
fprintf(stderr, "malloc() on packet_send failed\n");
return -1;
}
if ( init_struct_socket(&sock) == -1 ) {
free(packet_send);
return -1;
}
if ( init_struct_ip(&ip, packet_send) == -1 ) {
free(packet_send);
free_socket(sock);
return -1;
}
if ( init_struct_icmp(&icmp, packet_send) == -1 ) {
free(packet_send);
free_socket(sock);
return -1;
}
build_header_ip(ip, info);
build_header_icmp(icmp, info->seq);
if ( create_new_socket(sock, info->host) == -1 ) {
free(packet_send);
free_socket(sock);
return -1;
}
if ( setsockopt(sock->s, IPPROTO_IP, IP_HDRINCL, (void *)&res, (socklen_t) sizeof(int)) == 1 ) {
fprintf(stderr, "setsockopt() failed\n");
free(packet_send);
free_socket(sock);
return -1;
}
if ( sendto(sock->s, packet_send, ip->tot_len, 0,
(struct sockaddr *)&sock->buf , sizeof(sock->buf)) == -1 ) {
free(packet_send);
free_socket(sock);
return -1;
}
ret = recv_packet(sock, icmp, ip, &i_recev);
if ( ret == -1 ) {
free(packet_send);
free_socket(sock);
return -1;
}
else if ( ret == 1 ) {
printf("%d bytes from %s: icmp_seq=%d ttl=%d\n",i_recev.bytes, info->host, i_recev.seq, i_recev.ttl);
return 1;
}
free(packet_send);
free_socket(sock);
return 0;
}
int
recv_packet(socket_t *sock, icmphdr_t *icmp, iphdr_t *ip, i_recev_t *i_recev)
{
char *packet_recv = NULL;
int ret;
struct timeval timev;
fd_set read_set;
timev.tv_sec = 2;
timev.tv_usec = 0;
packet_recv = (char *) malloc(sizeof(iphdr_t) + sizeof(icmphdr_t));
if ( packet_recv == NULL ) {
fprintf(stderr, "malloc() on packet_send failed\n");
return -1;
}
FD_ZERO(&read_set);
FD_SET(sock->s, &read_set);
select(sock->s+1, &read_set, NULL, NULL, &timev);
if ( FD_ISSET(sock->s, &read_set) ) {
ret = recv(sock->s, packet_recv, sizeof(icmphdr_t)+sizeof(iphdr_t), 0);
if ( ret == -1 ) {
fprintf(stderr, "recv() failed\n");
}
else if ( ret == sizeof(iphdr_t)+sizeof(icmphdr_t) ) {
ip = (iphdr_t *) packet_recv;
icmp = (icmphdr_t *) (packet_recv + sizeof(iphdr_t));
if ( icmp->code == 0 && icmp->type == 0 ) {
(*i_recev).seq = icmp->seq;
(*i_recev).ttl = ip->ttl;
(*i_recev).bytes = ret;
free(packet_recv);
return 1;
}
}
}
free(packet_recv);
return 0;
}
/*****************************************************************
******************************************************************
*****************************************************************/
/*****************************************************************
****************** --- HEADER PROTOCOLE --- **********************
*****************************************************************/
void
build_header_ip(iphdr_t *ip, info_t *info)
{
ip->ihl = 5;
ip->version = 4; /* ip v 4 */
ip->tos = 0; /* tos ?? */
ip->tot_len = ( sizeof(iphdr_t) + sizeof(icmphdr_t) ); /* size packet */
ip->id = 0; /* num fragument */
ip->offset = 0; /* no fragment */
ip->ttl = info->ttl; /* time to live */
ip->protocol = IPPROTO_ICMP; /* protocole */
ip->checksum = 0;
ip->src = inet_addr(info->src); /* adresse source */
ip->dst = inet_addr(info->host);
}
void
build_header_icmp(icmphdr_t *icmp, int seq)
{
icmp->type = 8; /* demande reponce echo icmp */
icmp->code = 0; /* demande reponce echo icmp */
icmp->checksum = 0;
icmp->id = getpid(); /* numero unique */
icmp->seq = seq;
icmp->checksum = in_cksum((unsigned short *)icmp, sizeof(icmphdr_t));
}
/*****************************************************************
******************************************************************
*****************************************************************/
/*****************************************************************
******************* --- INIT STRUCTURE --- ***********************
*****************************************************************/
int
init_struct_info(info_t **info)
{
*info = NULL;
*info = (info_t *) malloc(sizeof(info_t));
if ( *info == NULL ) {
fprintf(stderr, "malloc on *info failed\n");
return -1;
}
return 0;
}
int
init_struct_socket(socket_t **sock)
{
*sock = NULL;
*sock = (socket_t *) malloc(sizeof(socket_t));
if ( *sock == NULL ) {
fprintf(stderr, "malloc() on *socket failed\n");
return -1;
}
return 0;
}
int
init_struct_ip(iphdr_t **ip, char *packet_send)
{
*ip = NULL;
*ip = (iphdr_t *) packet_send ;
if ( *ip == NULL ) {
fprintf(stderr, "malloc() on *icmp failed\n");
return -1;
}
return 0;
}
int
init_struct_icmp(icmphdr_t **icmp, char *packet_send)
{
*icmp = NULL;
*icmp = (icmphdr_t *) ( packet_send + sizeof(iphdr_t));
if ( *icmp == NULL ) {
fprintf(stderr, "malloc() on *icmp failed\n");
return -1;
}
return 0;
}
/*****************************************************************
******************************************************************
*****************************************************************/
/*****************************************************************
******************* --- FREE STRUCTURE --- **********************
*****************************************************************/
void
free_socket(socket_t *sock)
{
if ( sock != NULL ) {
if ( sock->s != -1 )
close(sock->s);
free(sock);
sock = NULL;
}
}
void
free_info(info_t *info)
{
if ( info != NULL ) {
if ( info->host != NULL ) {
free(info->host);
info->host = NULL;
}
if ( info->src != NULL ) {
free(info->src);
info->src = NULL;
}
free(info);
info = NULL;
}
}
/*****************************************************************
******************************************************************
*****************************************************************/
/*****************************************************************
********************* --- ELSE FUNCTION --- **********************
*****************************************************************/
int
create_new_socket(socket_t *sock, const char *ip_dst)
{
sock->s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if ( sock->s == -1 ) {
fprintf(stderr, "socket() failed\n");
return -1;
}
sock->buf.sin_family = AF_INET;
sock->buf.sin_addr.s_addr = inet_addr(ip_dst);
return 0;
}
unsigned short
in_cksum(unsigned short * addr,int len)
{
/***************************************************************
* CETTE FONCTION N ET PAS DE MOI *
* NE POUVANT DIRE AVEC PRECISION LE CODE *
* EXACTE AU QUELLE , ELLE APPARTIEN *
* JE NE PEUX DONC PAS DIRE SON AUTEUR !!! *
* IS NOT LEECH , FUCK YOU !!!! *
***************************************************************/
register int sum = 0;
u_short answer = 0;
register u_short * w = addr;
register int nleft = len;
while(nleft > 1) {
sum += *w++;
nleft -= 2;
}
if(nleft == 1) {
*(u_char *)(&answer) = *(u_char *) w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return answer;
}
int
conver_int(const char *str, int *nbr)
{
char *check = NULL;
*nbr = strtol(str, &check, 10);
if ( *check != '\0' ) {
fprintf(stderr, "conver char*->int failed\n");
return -1;
}
return 0;
}
int
ip_source(info_t *info)
{
int s;
int size;
struct ifreq ifr;
struct sockaddr_in *to;
struct if_nameindex *nameindex = if_nameindex();
s = socket(AF_INET, SOCK_STREAM, 0);
if ( s == -1 ) {
fprintf(stderr, "socket() failed\n");
return -1;
}
strncpy(ifr.ifr_name, nameindex[1].if_name, 10);
if (ioctl(s, SIOCGIFADDR, &ifr) == -1 ) {
fprintf(stderr, "ioctl error\n");
return -1;
}
to = (struct sockaddr_in *)&ifr.ifr_ifru.ifru_addr;
size = strlen(inet_ntoa(to->sin_addr));
info->src = malloc(size+1 * sizeof(char));
if ( info->src == NULL ) {
fprintf(stderr, "malloc on info->src failed\n");
return -1;
}
strncpy(info->src, inet_ntoa(to->sin_addr), size+1);
}
/*****************************************************************
******************************************************************
*****************************************************************/
/*****************************************************************
************************ --- EOF --- *****************************
*****************************************************************/[/code:1:f2b1e10247]
J'atten les critiques :)