How do i interact my C++ application with my ASP GUI.

Hi All,

I'm right now facing an issue. I was told to program a Network Traffic Analysis to keep track of all the traffic that is flowing within the Virtual Environment. The application now is running fine and able to track all the network traffic that is running across the virtual environment. I have a GUI(ASP.net). Now i have to actually design a GUI which has a start button. Before hitting the start button, there is 2 text box. One which specifies the Adapter that you wanna track, and the other is the ip address. All the traffic that is captured will be sent to the desired ip address of what you've entered. Of course the receiver side will have to also launch an application called a receiver to receive all the packets.

My objective now is to add in another textbox and allow user to enter which protocol they actually wanna filter in. let's say for example if they want to filter it according to TCP, only TCP packets should be sent over.


Main Program:

int main(int argc, char **argv)
{
// Interfaces
// pcap structures
int lRetVal = 0;
pcap_if_t *lAllDevs = NULL;
pcap_if_t *lDevice = NULL;
char lTemp[PCAP_ERRBUF_SIZE];
char lAdapter[MAX_BUF_SIZE + 1];
int lCounter = 1;
// int lIFCnum = 0;
pcap_t *lIFCHandle = NULL;
struct bpf_program lFCode;
unsigned int lNetMask = 0;
char filter_exp[] = ""; /* The filter expression */
bpf_u_int32 mask; /* The netmask of our sniffing device */
bpf_u_int32 net; /* The IP of our sniffing device */
char sth[255];
char desc[1000];

MYSQL *conn; // pointer to MySQL structure

char cmd5[1000];
char cmd6[255];

conn = mysql_init(NULL); // connection handler

// Connect to MySQL database
// host address = localhost, username = root, database = project
mysql_real_connect(conn, "localhost", "root", "", "project", 0, NULL, 0);

sprintf(cmd6, "DELETE FROM start");
mysql_real_query(conn, cmd6 , strlen(cmd6));

/*
* Open device list.
* Retrieve the device list from the local machine
*/

if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &lAllDevs, lTemp) == -1)
{
printf("Error in pcap_findalldevs_ex() : %s\n", lTemp);
lRetVal = 1;
goto END;
}

/*
* Print device name + description.
*
*/

lCounter = 1;

for(lDevice = lAllDevs; lDevice; lDevice = lDevice->next, lCounter++)
{
printf("\n%d. %s\n ", lCounter, lDevice->name);
sprintf(sth,"%s ", lDevice->name);
sprintf(cmd5, "INSERT INTO start VALUES('%s',%d)",sth,lCounter);

if (lDevice->name)
{
printf(" (%s)\n", lDevice->description);
sprintf(desc,"%s",lDevice->description);

// sprintf(cmd5, "INSERT INTO start VALUES('%s',NULL)",sth);
// sprintf(cmd5, "INSERT INTO start VALUES('%s',NULL)",desc);
printf("%s",desc);
}
else
printf(" (%d : No description available)\n", lCounter);

//sprintf(cmd5, "INSERT INTO start VALUES('%s(%s)',NULL)",sth,desc);



mysql_real_query(conn, cmd5 , strlen(cmd5));



}


/*
* Dialog which adapter to open.
*
*/

MYSQL_RES *result;
MYSQL_ROW row;
char AdapterNo[255];
int num_fields;
u_int inum, i=0;

mysql_query(conn, "SELECT AdapterNo from setting");

result = mysql_store_result(conn); // get result set

num_fields = mysql_num_fields(result); // get number of fields in the table

/*
Transmitting process while transmit = 1 in database
*/
while ((row = mysql_fetch_row(result))) // fetch the row
{
for(i = 0; i < num_fields; i++)
{
printf("%s ", row[i] ? row[i] : "NULL");
sprintf(AdapterNo,"%s ", row[i] ? row[i] : "NULL");
}
}

int lIFCnum = atoi(AdapterNo);



printf("\n\nOn which adapter do you want to start the sniffer : ");
// scanf("%d", &lIFCnum);

printf("%d",lIFCnum);
// system("PAUSE");

if ((lIFCnum > 9 && lIFCnum < 0))
{
printf("The adapter you chose does not exist\n");
lRetVal = 2;
goto END;
}

ZeroMemory(lAdapter, sizeof(lAdapter));
lCounter = 1;

for(lDevice = lAllDevs; lDevice; lDevice = lDevice->next, lCounter++)
{
if (lCounter == lIFCnum)
{
strcpy(lAdapter, lDevice->name);
break;
}
}

/*
* Open interface.
*
*/

if ((lIFCHandle = pcap_open(lAdapter, // name of the device
65536, // portion of the packet to capture.
// 65536 grants that the whole packet will be captured on all the MACs
0, //PCAP_OPENFLAG_PROMISCUOUS (nonzeron promiscuous mode)
1000, // read timeout
NULL, // error buffer
lTemp)) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter.\n");
lRetVal = 5;
goto END;
}


/*
* Compiling + setting the filter
*
*/

if (lDevice->addresses != NULL)
/* Retrieve the mask of the first address of the interface */
net=((struct sockaddr_in *)(lDevice->addresses->netmask))->sin_addr.S_un.S_addr;
else
/* If the interface is without an address we suppose to be in a C class network */
net=0xffffff;

if (pcap_compile(lIFCHandle, &lFCode, filter_exp, 0, net) == -1) {
fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(lIFCHandle));
return(2);
}
if (pcap_setfilter(lIFCHandle, &lFCode) == -1) {
fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(lIFCHandle));
return(2);
}

// We dont need this list anymore.
pcap_freealldevs(lAllDevs);

// Start intercepting data packets.
pcap_loop(lIFCHandle, 0, packet_handler, NULL);

END:

/*
* Release all allocated resources.
*
*/

if (lAllDevs)
pcap_freealldevs(lAllDevs);

return(lRetVal);

mysql_free_result(result); // free the resources

mysql_close(conn); // close database connection
}


/*
* Callback function invoked by libpcap for every incoming packet
*
*/

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Filtering Part :

char filter_exp[] = "what user enter in asp.net text box"; /* The filter expression */
bpf_u_int32 mask; /* The netmask of our sniffing device */
bpf_u_int32 net; /* The IP of our sniffing device */



/*
* Compiling + setting the filter
*
*/

if (lDevice->addresses != NULL)
/* Retrieve the mask of the first address of the interface */
net=((struct sockaddr_in *)(lDevice->addresses->netmask))->sin_addr.S_un.S_addr;
else
/* If the interface is without an address we suppose to be in a C class network */
net=0xffffff;

if (pcap_compile(lIFCHandle, &lFCode, filter_exp, 0, net) == -1) {
fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(lIFCHandle));
return(2);
}
if (pcap_setfilter(lIFCHandle, &lFCode) == -1) {
fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(lIFCHandle));
return(2);
}


is there anyway to link this program with asp.net so that if a user enter TCP. The variable for char filter_exp[] will be ="tcp"?
Last edited on
Topic archived. No new replies allowed.