Monday, March 2, 2015

C program for classify frame in mac level

Objective of this program:

if the coming frame's destination mac address is reserved mac address as per the standard means thent those frames should be printed in the console

#include<stdio.h>
#include<string.h>
main(int argc, char *argv[])
{
    unsigned char mac[6],temp[30]={0},*ptr="01:80:c2:00:00:00";
    unsigned char reserved_mac[]={0x01,0x80,0xc2,0x00,0x00,0x00};
    int i;
    if(argc!=2)
    {
        printf("\nusage: ./a.out <mac in hex>\n");
        exit(0);
    }
    //printf("\ngiven mac address is:\t%s\n",argv[1]);
    //printf("\ngiven mac address is:\t%s\n",ptr);
    strcpy(temp,argv[1]);
    //strcpy(temp,ptr);
    //printf("\ngiven mac address is:\t%s\n",temp);
    sscanf(temp,"%x:%x:%x:%x:%x:%x",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]);
    //printf("\ngiven mac in hex format\t%x:%x:%x:%x:%x:%x",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
    /*
    for(i=0;i<16;i++)
    {
        printf("\ngenerated mac in hex format\t%x:%x:%x:%x:%x:%x",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
        mac[5]=(mac[5] + 1);
    }
    */
    if(memcmp(reserved_mac,mac,5)==0)
    {
        for(i=0;i<16;i++)
        {
            if(mac[5]==reserved_mac[5])
            {
                printf("\ngenerated mac in hex format\t%x:%x:%x:%x:%x:%x",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
                printf("\ngiven mac is one of the control frame's mac address\n");
                return;
            }

            reserved_mac[5]=(reserved_mac[5] + 1);
        }
    }   
}

input:
#cat shell
#!/bin/bash
./a.out 01:80:c2:00:00:00
./a.out 01:80:c2:00:00:01
./a.out 01:80:c2:00:00:02
./a.out 01:80:c2:00:00:03
./a.out 01:80:c2:00:00:04
./a.out 01:80:c2:00:00:05
./a.out 01:80:c2:00:00:06
./a.out 01:80:c2:00:00:07
./a.out 01:80:c2:00:00:08
./a.out 01:80:c2:00:00:09
./a.out 01:80:c2:00:00:0a
./a.out 01:80:c2:00:00:0b
./a.out 01:80:c2:00:00:0c
./a.out 01:80:c2:00:00:0d
./a.out 01:80:c2:00:00:0e
./a.out 01:80:c2:00:00:0f


output:


generated mac in hex format    1:80:c2:0:0:0
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:1
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:2
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:3
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:4
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:5
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:6
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:7
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:8
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:9
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:a
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:b
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:c
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:d
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:e
given mac is one of the control frame's mac address

generated mac in hex format    1:80:c2:0:0:f
given mac is one of the control frame's mac address

No comments:

Post a Comment