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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
| #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <linux/if_packet.h> #include <linux/if_ether.h> #include <sys/ioctl.h> #include <linux/if_arp.h>
#define _GNU_SOURCE #include <stddef.h> #include <sys/types.h> #include <dirent.h> #include <string.h>
#include <signal.h> #include <sys/time.h> #include <sys/select.h>
#define max(a,b) ((a)>(b)?(a):(b))
#define ETH_FRAME_LEN 500 #define NICNUM 2 #define RECVDEBUG #define SENDDEBUG
typedef struct EthernetInfo { int dev; int active; unsigned char mac_local[8]; unsigned char mac_next[8]; } EthernetInfo;
struct ipheader { unsigned char ip_hl:4, ip_v:4; unsigned char ip_tos; unsigned short int ip_len; unsigned short int ip_id; unsigned short int ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short int ip_sum; unsigned int ip_src; unsigned int ip_dst; };
struct itimerval monitor_interval;
void monitor_timer(); int lookupethdev(EthernetInfo *eif); int main(int argc, char *argv[]){ int i, m, n, cnt; struct sockaddr_ll sockadr[2], adrfrom; struct ifreq ifst[2]; int sock[2]; char *devname[] = {"eth0","eth1"}; EthernetInfo ethinfo[NICNUM];
unsigned char *buffer; unsigned char *data; struct ipheader *iphdr;
fd_set rset; int maxfd;
lookupethdev(ðinfo); m = 0; printf("In Main ---\n Eth%d's MAC %x:%x:%x:%x:%x:%x\n", m, ethinfo[m].mac_local[0], ethinfo[m].mac_local[1], ethinfo[m].mac_local[2], ethinfo[m].mac_local[3], ethinfo[m].mac_local[4], ethinfo[m].mac_local[5]); buffer = (unsigned char*)malloc(ETH_FRAME_LEN); data = buffer + 14;
for(n=0; n<NICNUM; n++){ if ((sock[n]=socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL)))==-1){ perror("When creat raw socket: "); exit(EXIT_FAILURE); }
strcpy(ifst[n].ifr_name,devname[n]); ioctl(sock[n],SIOCGIFINDEX,&ifst[n]); printf(" Eth%d--> ifst.ifr_ifindex = %d \n",n,ifst[n].ifr_ifindex);
sockadr[n].sll_family = AF_PACKET; sockadr[n].sll_protocol = htons(ETH_P_ALL); sockadr[n].sll_ifindex = ifst[n].ifr_ifindex;
i = bind(sock[n], &(sockadr[n]), sizeof(struct sockaddr_ll)); if(i != 0){ perror("Binding False: "); exit(0); } else printf("Binding Sock[%d] successfully\n", n); }
monitor_interval.it_interval.tv_sec = 0; monitor_interval.it_interval.tv_usec = 0; monitor_interval.it_value.tv_sec = 2; monitor_interval.it_value.tv_usec = 0; signal(SIGALRM,monitor_timer); setitimer(ITIMER_REAL,&monitor_interval,NULL); while(1){ cnt++; printf("---------------cnt = %d------------\n", cnt); FD_ZERO(&rset); maxfd = 0; for(n=0; n<NICNUM; n++){ FD_SET(sock[n], &rset); maxfd = max(sock[n], maxfd); } maxfd = maxfd + 1;
if(select(maxfd, &rset, NULL, NULL, NULL) < 1) continue;
for (n = 0; n <NICNUM; n++){ if (FD_ISSET(sock[n], &rset) == 0) continue;
recvfrom(sock[n], buffer, ETH_FRAME_LEN, 0, (struct sockaddr*)&adrfrom, sizeof(struct sockaddr_ll));
#ifdef RECVDEBUG printf("Eth%d-----ProtocAtFrom = %d DestMac: ", n, adrfrom.sll_protocol); for(i=0; i<6; i++) printf("%2x-", buffer[i]); printf(" SourceMac: ", adrfrom.sll_protocol); for(i=6; i<12; i++) printf("%2x-", buffer[i]); printf(" FrameType = %2x-%2x\n", buffer[12], buffer[13]); if(buffer[12] != 0x08 || buffer[13] != 0x00) continue; iphdr = (struct ipheader *) (&buffer[14]); data = (unsigned char *)&(iphdr->ip_dst); printf("S_IP = %8x %d.%d.%d.%d\n", iphdr->ip_dst, data[0], data[1], data[2], data[3]); #endif RECVDEBUG
m = (n + 1) % 1; sockadr[m].sll_family = AF_PACKET; sockadr[m].sll_protocol = htons(ETH_P_IP); sockadr[m].sll_ifindex = ifst[m].ifr_ifindex; sockadr[m].sll_hatype = ARPHRD_ETHER; sockadr[m].sll_pkttype = PACKET_OTHERHOST; sockadr[m].sll_halen = ETH_ALEN; memcpy(sockadr[m].sll_addr, ethinfo[m].mac_local, ETH_ALEN); memset(buffer, 0xff, ETH_ALEN); memcpy(buffer+ETH_ALEN, ethinfo[m].mac_local, ETH_ALEN); buffer[12] = 0x08; buffer[13] = 0x00;
#ifdef SENDDEBUG printf("Sendto Eth%d-----ProtocAtFrom = %d DestMac: ", n, adrfrom.sll_protocol); for(i=0; i<6; i++) printf("%2x-", buffer[i]); printf(" SourceMac: ", adrfrom.sll_protocol); for(i=6; i<12; i++) printf("%2x-", buffer[i]); printf(" FrameType = %2x-%2x IPSize = 0x%x\n", buffer[12], buffer[13], iphdr->ip_len); #endif SENDDEBUG
i = sendto(sock[m],buffer,iphdr->ip_len + 14,0,(struct sockaddr*)&sockadr[m],sizeof(struct sockaddr_ll)); if(i ==-1){ perror("Error --> When sendto: "); } else printf("Sendto Success eth%d --> eth%d Size = %d\n", n, m, iphdr->ip_len); } } }
int lookupethdev(EthernetInfo *eif){ return 1; }
void monitor_timer(){ int i,j,k,l,m,n; static int timer_counter; monitor_interval.it_interval.tv_sec = 0; monitor_interval.it_interval.tv_usec = 0; monitor_interval.it_value.tv_sec = 2; monitor_interval.it_value.tv_usec = 0; setitimer(ITIMER_REAL,&monitor_interval,NULL); timer_counter++; return; }
|