Exemple #1
0
int AnalyzePacket(u_char *data,int size)
{
u_char	*ptr;
int	lest;
struct ether_header	*eh;

	ptr=data;
	lest=size;

	if(lest<sizeof(struct ether_header)){
		fprintf(stderr,"lest(%d)<sizeof(struct ether_header)\n",lest);
		return(-1);
	}
	eh=(struct ether_header *)ptr;
	ptr+=sizeof(struct ether_header);
	lest-=sizeof(struct ether_header);

	if(ntohs(eh->ether_type)==ETHERTYPE_ARP){
		fprintf(stderr,"Packet[%dbytes]\n",size);
		PrintEtherHeader(eh,stdout);
		AnalyzeArp(ptr,lest);
	}
	else if(ntohs(eh->ether_type)==ETHERTYPE_IP){
		fprintf(stderr,"Packet[%dbytes]\n",size);
		PrintEtherHeader(eh,stdout);
		AnalyzeIp(ptr,lest);
	}
	else if(ntohs(eh->ether_type)==ETHERTYPE_IPV6){
		fprintf(stderr,"Packet[%dbytes]\n",size);
		PrintEtherHeader(eh,stdout);
		AnalyzeIpv6(ptr,lest);
	}

	return(0);
}
Exemple #2
0
int AnalyzePacket(u_char *data, int size) {
  u_char *ptr = NULL;
  int lest = 0;
  struct ether_header *eh = NULL;

  /*

  struct ether_header
  {
    u_int8_t  ether_dhost[ETH_ALEN];   // destination eth addr
    u_int8_t  ether_shost[ETH_ALEN];   // source ether addr
    u_int16_t ether_type;              // packet type ID field
  } __attribute__ ((__packed__));

  */

  ptr = data;
  lest = size;

  if (lest < sizeof(struct ether_header)) {
    ExitWithTooLittleLengthError("ether_header", lest);
  }

  eh = (struct ether_header *) ptr;
  ptr += sizeof(struct ether_header);
  lest -= sizeof(struct ether_header);

  if (ntohs(eh->ether_type) == ETHERTYPE_ARP) {
    fprintf(stderr, "Packet[%dbytes]\n", size);
    PrintEtherHeader(eh, stdout);
    AnalyzeArp(ptr, lest);
  }
  else if (ntohs(eh->ether_type) == ETHERTYPE_IP) {
    fprintf(stderr, "Packet[%dbytes]\n", size);
    PrintEtherHeader(eh, stdout);
    AnalyzeIp(ptr, lest);
  }
  else if (ntohs(eh->ether_type) == ETHERTYPE_IPV6) {
    fprintf(stderr, "Packet[%dbytes]\n", size);
    PrintEtherHeader(eh, stderr);
    AnalyzeIpv6(ptr, lest);
  }

  return 0;
}
Exemple #3
0
int AnalyzePacket(int deviceNo, u_char *data, int size)
{
    u_char	*ptr;
    int	lest;
    struct ether_header	*eh;

    ptr=data;
    lest=size;
    if(lest<sizeof(struct ether_header)) {
        DebugPrintf("[%d]:lest(%d)<sizeof(struct ether_header)\n",deviceNo,lest);
        return(-1);
    }
    eh=(struct ether_header *)ptr;
    ptr+=sizeof(struct ether_header);
    lest-=sizeof(struct ether_header);
    DebugPrintf("[%d]",deviceNo);
    if(DebugOut) {
        PrintEtherHeader(eh,stderr);
    }

    // Get Client Mac Address
    if(ClientMacFlag==OFF) {
        if(ntohs(eh->ether_type)==ETHERTYPE_IP) {
            struct iphdr *iphdr;

            iphdr=(struct iphdr *)ptr;
            ptr+=sizeof(struct iphdr);
            lest-=sizeof(struct iphdr);
            if(strncmp(cliIpAddr, inet_ntoa(*(struct in_addr *)&iphdr->saddr), SIZE_IP)==0) {
                my_ether_ntoa_r(eh->ether_shost, cliMacAddr, sizeof(cliMacAddr));
                ClientMacFlag=ON;
                printf("cliMacAddr: %s\n", cliMacAddr);
            }
        }
    }

    // Check My Protocol
    if(ntohs(eh->ether_type)==MYPROTOCOL) {
        MYPROTO *myproto;
        myproto=(MYPROTO *)ptr;
        ptr+=sizeof(MYPROTO);
        lest-=sizeof(MYPROTO);

        switch(ntohs(myproto->type)) {
        case   OFFER:
            ;
            char offr_dMacAddr[18];
            char offr_sMacAddr[18];
            my_ether_ntoa_r(eh->ether_dhost, offr_dMacAddr, sizeof(offr_dMacAddr));
            my_ether_ntoa_r(eh->ether_shost, offr_sMacAddr, sizeof(offr_sMacAddr));

            if(strncmp(offr_dMacAddr, dev1MacAddr, sizeof(offr_dMacAddr))==0) {
                strncpy(apMacAddr, offr_sMacAddr, SIZE_MAC);
                memcpy(dev1IpAddr, inet_ntoa(*(struct in_addr *)&myproto->ip_dst), SIZE_IP);
                memcpy(apIpAddr, inet_ntoa(*(struct in_addr *)&myproto->ip_src), SIZE_IP);
                if(chgIfIp(NameDev1, inet_addr(dev1IpAddr))==0) {
                    DebugPrintf("Change IP Address\n%s IP: %s\n", NameDev1, dev1IpAddr);

                    printf("Send Approval Packet\n");
                    create_myprotocol(Device[MainDev].soc, dev1MacAddr, apMacAddr, dev1IpAddr, apIpAddr, APPROVAL);
                    StatusFlag=STA_WAIT;

                    return(-1);
                }
            }
            break;
        default:
            break;
        }
    }

    return(0);
}