Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm using a sniffer that is asking me which interdface I would like to sniff. I'm using a machine with only 1 interface so I don't need to be asked. How can I circumvent this prompt? Thanks.

C++
//Retrive the available IPs of the local host
local = gethostbyname(hostname);
printf("\nAvailable Network Interfaces : \n");
if (local == NULL) 
{
printf("Error : %d.\n",WSAGetLastError());
return 1;
}
  
for (i = 0; local->h_addr_list[i] != 0; ++i) 
{
memcpy(&addr, local->h_addr_list[i], sizeof(struct in_addr));
printf("Interface Number : %d Address : %s\n",i,inet_ntoa(addr));
}
  
printf("Enter the interface number you would like to sniff : ");
scanf("%d",&in);


DWORD    dwLen = 0; //
memset(&dest, 0, sizeof(dest));
memcpy(&dest.sin_addr.s_addr,local->h_addr_list[in],sizeof(dest.sin_addr.s_addr));
dest.sin_family      = AF_INET;
dest.sin_port        = 0;
      
printf("\nBinding socket to local system and port 0 ...");
if (bind(sniffer,(struct sockaddr *)&dest,sizeof(dest)) == SOCKET_ERROR)
{
printf("bind(%s) failed.\n", inet_ntoa(addr));
return 1;
}
printf("Binding successful"); 
Posted

If you want to hardcode it, you can comment out the printf and scanf in the middle and add a in = 0; line, or more sensibly wrap them in an if block and check the value of i after the for loop.

Could you really not figure this out for yourself?
 
Share this answer
 
Comments
Member 7766180 18-Aug-11 17:18pm    
Thank you, it works. Wouldn't it be nice just to help. Not help AND browbeat? If I could do it, I would. I'm new to this, and learning, in thanks to the many generous souls on this site. Once again,thank you.
Chuck O'Toole 18-Aug-11 23:14pm    
Sorry, I agree with Graham. You are using or playing with a sniffer, which is pretty deep in IP and TCP protocols. A certain knowledge is assumed if you are going to make sense out of raw IP packets. Yet your question is so basic that it demonstrates a lack of the sophistication necessary to decode raw packets. If you're that new to all this, maybe a sniffer is not the place to start.
Member 7766180 19-Aug-11 1:53am    
If one is to learn you have to push your limits. Otherwise you will never grow! Any help that you can offer is appreciated! Thank you!!!
Just count how many interfaces you have, and if you only have one, then use it rather than prompting for it:

C++
int count = 0;
while (local->h_addr_list[count] != 0)
    count++

if (count == 1) 
{
    in = 0;
} 
else 
}
    for (i = 0; local->h_addr_list[i] != 0; ++i) 
    {
        memcpy(&addr, local->h_addr_list[i], sizeof(struct in_addr));
        printf("Interface Number : %d Address : %s\n",i,inet_ntoa(addr));
    }
  
    printf("Enter the interface number you would like to sniff : ");
    scanf("%d",&in);
}
 
Share this answer
 
Comments
Member 7766180 18-Aug-11 17:19pm    
Thank you so much, this is good!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900