Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define SOURCEMACLENGHT 6
- #define USERALIASLENGTH 12
- #include "pcap.h"
- #include "conio.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- pcap_if_t *alldevs;
- pcap_if_t *d;
- pcap_t *fp;
- pcap_t *adhandle;
- char errbuf[PCAP_ERRBUF_SIZE];
- int roomNumber;
- int interfaceNumber;
- int sourceMac[SOURCEMACLENGHT];
- bool hasRoomAvailable;
- bool hasSameMac;
- char userAlias[USERALIASLENGTH];
- int res;
- struct pcap_pkthdr *header;
- const u_char *pkt_data;
- void ListAllDevices()
- {
- cout << "-> Type an interface number: " << endl << endl;
- if(pcap_findalldevs(&alldevs, errbuf) == -1)
- {
- cout << stderr << "-> Error in pcap_findalldevs: " << errbuf << endl;
- exit(1);
- }
- int index = 1;
- for(d = alldevs; d; d = d -> next)
- {
- cout << index++ << ". " << d -> name;
- if(d -> description)
- {
- cout << "\t" << d -> description << endl;
- }
- else
- {
- cout << "-> No description available." << endl;
- }
- }
- if(index == 0)
- {
- cout << "-> No interfaces found! Make sure WinPcap is installed." << endl;
- }
- cout << "-> Your choice: " << endl;
- cin >> interfaceNumber;
- if(interfaceNumber < 1 || interfaceNumber > index)
- {
- cout << "-> Error. Interface number out of range." << endl;
- /// Free the device list.
- pcap_freealldevs(alldevs);
- }
- /// Jump to the selected adapter.
- for(d = alldevs, index = 0; index < interfaceNumber - 1; d = d -> next, index++);
- /// Open the device.
- if((adhandle = pcap_open_live(
- d -> name, /// Name of the device.
- 65536, /// Portion of the packet to capture. 65536 guarantees that the whole packet will be captured on all the link layers.
- 1, /// Promiscuous mode.
- 1000, /// Read timeout. Authentication on the remote machine.
- errbuf /// Error buffer.
- )) == NULL)
- {
- cout << stderr << "-> Error. Unable to open the adapter." << d -> name << " is not supported by WinPcap." << endl;
- /// Free the device list.
- pcap_freealldevs(alldevs);
- }
- /// At this point, we don't need any more the device list. Free it.
- pcap_freealldevs(alldevs);
- cout << endl;
- }
- void SetUserData()
- {
- cout << "-> Type Source MAC (XX:XX:XX:XX:XX:XX) :" << endl;
- scanf("%x:%x:%x:%x:%x:%x", &sourceMac[0], &sourceMac[1], &sourceMac[2], &sourceMac[3], &sourceMac[4], &sourceMac[5]);
- do
- {
- cout << "-> Write the room number: " << endl;
- cin >> roomNumber;
- if(roomNumber >= 0 && roomNumber <= 255)
- {
- hasRoomAvailable = true;
- }
- else
- {
- hasRoomAvailable = false;
- cout << "-> Select another room." << endl;
- }
- }
- while(hasRoomAvailable == false);
- cout << "-> Type an alias: " << endl;
- fflush(stdin);
- gets(userAlias);
- fflush(stdin);
- if(strlen(userAlias) < USERALIASLENGTH)
- {
- for(int i = strlen(userAlias); i < USERALIASLENGTH; i++)
- {
- userAlias[i] = '\0';
- }
- }
- cout << endl;
- }
- bool CheckSourceMac()
- {
- for (int i = 0; i < SOURCEMACLENGHT; i++)
- {
- if (sourceMac[i] < 0 || sourceMac[i]>255)
- {
- return false;
- }
- }
- return true;
- }
- void ShowTypedData()
- {
- cout << "-> Data typed: " << endl << endl;
- cout << "-> Origin MAC Address: " << endl;
- for(int i = 0; i < SOURCEMACLENGHT; i++)
- {
- printf("%x", sourceMac[i]);
- if(i + 1 == SOURCEMACLENGHT)
- {
- cout << endl;
- }
- else
- {
- cout << ":";
- }
- }
- cout << "-> Room Number: " << endl;
- cout << roomNumber << endl;
- cout << "-> Alias: " << endl;
- for(int i = 0; i < sizeof(userAlias); i++)
- {
- cout << userAlias[i];
- if(i + 1 == sizeof(userAlias))
- {
- cout << endl << endl;
- }
- }
- }
- void SendMessage(pcap_t *adhandle, int *sourceMac, int roomNumber, char *userAlias)
- {
- char textMsg[1000];
- int newMessageSize = 0;
- fflush(stdin);
- gets(textMsg);
- fflush(stdin);
- if (strlen(textMsg) < 37)
- {
- for (int i = strlen(textMsg); i <= 37; i++)
- {
- textMsg[i] = '\0';
- }
- newMessageSize = 37;
- }
- else if (strlen(textMsg) > 993)
- {
- newMessageSize = 993;
- }
- else
- {
- newMessageSize = strlen(textMsg);
- }
- u_char packet[newMessageSize + 23];
- for (int i = 0; i <= 5; i++)
- {
- packet[i] = 255;
- }
- for (int i = 6, counter = 0; i <= 11; i++, counter++)
- {
- packet[i] = sourceMac[counter];
- }
- packet[12] = 0x07;
- packet[13] = 0x00;
- packet[14] = roomNumber;
- for (int i = 15, counter = 0; i <= 27; i++, counter++)
- {
- packet[i] = userAlias[counter];
- }
- for (int i = 0; i < newMessageSize; i++)
- {
- packet[i + 27] = (u_char)textMsg[i];
- }
- /// Send down the packet.
- if (pcap_sendpacket(
- adhandle, /// Adapter.
- packet, /// Buffer with the packet.
- sizeof(packet) /// Size.
- ) != 0)
- {
- cout << stderr << "-> Error sending the packet: " << pcap_geterr(adhandle) << endl;
- }
- for (int i = 15; i <= 22; i++)
- {
- cout << packet[i];
- }
- cout << " >> ";
- for (int i = 23; i < sizeof(packet); i++)
- {
- cout << packet[i];
- }
- cout << endl;
- }
- void ListenMessages(struct pcap_pkthdr *header, const u_char *pkt_data)
- {
- for (int i = 15; i <= 22; i++)
- {
- cout << pkt_data[i];
- }
- cout << " >> ";
- for (int i = 23; i < header->len; i++)
- {
- cout << pkt_data[i];
- }
- cout << endl;
- }
- void ChatSendRecieve()
- {
- cout << "-> Type the message: " << endl;
- while((res = pcap_next_ex(adhandle, &header, &pkt_data)) >= 0)
- {
- if(kbhit())
- SendMessage(adhandle, sourceMac, roomNumber, userAlias);
- if (res == 0)
- continue;
- for(int i = 0; i < SOURCEMACLENGHT; i++)
- {
- if(sourceMac[i] == pkt_data[i + SOURCEMACLENGHT])
- hasSameMac = true;
- else
- {
- hasSameMac = false;
- break;
- }
- }
- if(pkt_data[12] == 07 && pkt_data[13] == 00 && pkt_data[14] == roomNumber && !hasSameMac)
- {
- ListenMessages(header, pkt_data); //We check here if the packet is from the same room number and doesn't have the same mac address.
- }
- }
- if(res == -1)
- {
- cout << "-> Error reading the packets: " << pcap_geterr(adhandle) << endl;
- }
- pcap_close(fp);
- }
- int main()
- {
- ListAllDevices();
- SetUserData();
- ShowTypedData();
- ChatSendRecieve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement