The OpenNET Project / Index page

[ новости /+++ | форум | теги | ]




Версия для распечатки Пред. тема | След. тема
Новые ответы [ Отслеживать ]
Прием и отправка пактов, !*! TEK, 15-Июн-04, 23:10  [смотреть все]
Здравсвуйте уважаемые!!!
Я тут прогу написал которая ловит пакеты которые идут к нашему компу и отправляет.
И используеться библиотека ip.h
Посмотрите пожайлуста как она компилиться под другие оси круме Alt Linux2.0 и прокоментируйте плз мое ошибки если таковы обнаружиться

Исходник:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "ip.h"


void sp()
main(int argc, char *argv[]) {
  struct ip _ip;
  int      sock1,i,sock ;
FILE *f;
     char s[1000];
     char comm;
     f=fopen("ip.txt","r");
     while (fgets(s,1000,f)!=NULL)
     printf("%s",s);
         fclose(f);

     return 0;


  sock=socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
  if(sock<0) {
    perror("Receive socket");
    exit(1);
  }

  for(;;) {
    if(recv(sock, &_ip, sizeof(_ip), 0)<0) {
      printf("Error: Receiving packet.\n");
    }

  for(i=1;i<10;i++){
  if(s[i]=inet_ntoa(_ip->iph.ip_src.s_addr)){
  scanf(comm);
//shlem packet

sp();
  sock1=socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
  if(recv(sock, &_ip, sizeof(_ip), 0)<0) {
      printf("Error: Receiving packet.\n");
    }
    printf(" ", _ip->iph.ip_opt.opt_data);
  }
  }

  }

  close(sock);
  exit(0);
}

void sp() {
struct sockaddr_in sin;
  struct in_addr     src;
  struct ip         *ip;
  int                sock;
  int                res;
  int                on=1;

  //“ь—Нѓ`ѓFѓbѓN
  if(argc!=3) {
    printusage(argv[0]);
    exit(1);
  }

  memset(&sin, 0, sizeof(struct sockaddr_in));
  src.s_addr=inet_addr(argv[1]);
  sin.sin_family=PF_INET;
  sin.sin_addr.s_addr=inet_addr(argv[2]);
  sin.sin_port=0;

  /* ѓ\ѓPѓbѓg‚МЌмђ¬ */
  sock=socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
  if(sock==-1) {
    perror("socket()");
    exit(1);
  }

  /* ѓ\ѓPѓbѓg‚МѓIѓvѓVѓ‡ѓ“‚МђЭ’и */
  res=setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on, sizeof(int));
  if(res==-1) {
    perror("setsockopt");
    exit(1);
  }

  /* ѓpѓPѓbѓg‚Мѓoѓbѓtѓ@Љm•Ы */
  ip=(struct ip *)malloc(sizeof(struct ip));

  /* IP Header */
  ip->iph.ip_v = 4;
  ip->iph.ip_ihl = sizeof(struct ip_header)/4;
  ip->iph.ip_tos = 0;
  ip->iph.ip_len = htons(sizeof(struct ip));
  ip->iph.ip_id = htons(242);
  ip->iph.ip_off = htons(0x4000);
  ip->iph.ip_ttl = 255;
  ip->iph.ip_p = IPPROTO_RAW;
  ip->iph.ip_sum = 0;
  ip->iph.ip_src.s_addr = src.s_addr;
  ip->iph.ip_dst.s_addr = sin.sin_addr.s_addr;
  /* IP Header Option */
  ip->iph.ip_opt.opt_flag=0xA0;
  ip->iph.ip_opt.opt_len=sizeof(struct ip_option);
  ip->iph.ip_opt.opt_data=0x00;

  /* Header‚МЏo—Н */
  printheader(ip);

  /* ѓpѓPѓbѓg‚М‘—ђM */
  res=sendto(sock, ip, sizeof(struct ip), 0, (struct sockaddr *) &sin, sizeof(sin));

  }
}

Библиотека ip.h

#include <string.h>
#include <netinet/in.h>

/* IP Packet - Header - Option */
struct ip_option {         /* 32bit x N */
  unsigned char  opt_flag; /*  8bit */ /* Copied Flag + Class + Number */
  unsigned char  opt_len;  /*  8bit */ /* Option Length */
  unsigned short opt_data; /* 16bit */ /* Data */
  /*
   *  If you want more data value, please add following definition.
   *  Data length is 32bit x n.
   *  unsigned long opt_data[n];
   */
};

/* IP Packet - Header */
struct ip_header {
#if __BYTE_ORDER == __LITTLE_ENDIAN    /* Intel */
  unsigned char    ip_ihl:4;           /* header length */
  unsigned char    ip_v:4;             /* version */
#endif
#if __BYTE_ORDER == __BIG_ENDIAN       /* Motorola */
  unsigned int     ip_v:4;             /* version */
  unsigned int     ip_ihl:4;           /* header length */
#endif
  unsigned char    ip_tos;             /* type of service */
  unsigned short   ip_len;             /* total length */
  unsigned short   ip_id;              /* identification */
  unsigned short   ip_off;             /* fragment offset field */
  unsigned char    ip_ttl;             /* time to live (TTL) */
  unsigned char    ip_p;               /* protocol */
  unsigned short   ip_sum;             /* header checksum */
  struct in_addr   ip_src;             /* source ip address */
  struct in_addr   ip_dst;             /* destination ip address */
  struct ip_option ip_opt;             /* option */
};

/* IP Packet */
struct ip {
  struct ip_header iph;
  char data[65500];
};

/*
* For Debug
*/

void protocol_id(char *, int);
void printheader(struct ip *);
void printbit(int, int);

void protocol_id(char * protocol_name, int id) {
  strcpy(protocol_name, "Unknown");
  if(id==0) strcpy(protocol_name, "IP");
  if(id==1) strcpy(protocol_name, "ICMP");
  if(id==6) strcpy(protocol_name, "TCP");
  if(id==17) strcpy(protocol_name, "UDP");
  if(id==255) strcpy(protocol_name, "RAW");
}

void printheader(struct ip * _ip) {
  char str[20];
  strcpy(str, "Unknown");
  protocol_id(str, _ip->iph.ip_p);

  printf("---------- IP Header ----------\n");
  printf("Version                : %d\n", _ip->iph.ip_v);
  printf("Internet Header Length : %d\n", _ip->iph.ip_ihl);
  printf("Total Length           : %d\n", _ip->iph.ip_tos);
  printf("Identification         : %d\n", _ip->iph.ip_len);
  printf("Fragment               : %d\n", _ip->iph.ip_off);
  printf("Time To Live           : %d\n", _ip->iph.ip_ttl);
  printf("Protocol               : %d, %s\n", _ip->iph.ip_p, str);
  printf("Checksum               : %d\n", _ip->iph.ip_sum);
  printf("Source IP              : %s\n", inet_ntoa(_ip->iph.ip_src.s_addr));
  printf("Destination IP         : %s\n", inet_ntoa(_ip->iph.ip_dst.s_addr));
  printf("---------- IP Header Option ----------\n");
  printf("Option Flag            : %d, ", _ip->iph.ip_opt.opt_flag);
  printbit(_ip->iph.ip_opt.opt_flag, 8);
  printf("\n");
  printf("Option Length          : %d\n", _ip->iph.ip_opt.opt_len);
  printf("Data                   : %d\n", _ip->iph.ip_opt.opt_data);
}

void printbit(int n, int p) {
  int i=0, z=0;

  for(i=p-1; i>=0; i--) {
    z=(n>>i)&0x01;
    printf("", z);
  }
}
Зарание спасибо за помощь




Партнёры:
PostgresPro
Inferno Solutions
Hosting by Hoster.ru
Хостинг:

Закладки на сайте
Проследить за страницей
Created 1996-2024 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру