4

I need to build a tool (c++) very much like "Wireless Network Watcher" which is a small utility that scans your wireless network and displays the list of all computers and devices that are currently connected to your network. here's the existing tool http://www.nirsoft.net/utils/wireless_network_watcher.html

I need to know what are the win32 sdk functions to use to build this kind of functionality: scan the wireless network I am connected to and display all computers and devices connected to it.

Bart
  • 19,692
  • 7
  • 68
  • 77
ovi
  • 460
  • 4
  • 18

3 Answers3

2

ok, it seems is done this way: first sent an ARP request packet to each possible IP address in the network (you calculate them based on the net mask and the interface ip), for this step you can use SendARP functions. Then you have to call getnameinfo for each IP that responded previously, or you can send an NetBios request packet (port 137) to retreive the name of the device, if it has one, or know how to respond to that request. for some networks this can take awhile (very long time).

ovi
  • 460
  • 4
  • 18
0
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/ip_icmp.h>
#include <time.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>

// Define the Packet Constants
// ping packet size
#define PING_PKT_S 64

// Automatic port number
#define PORT_NO 0

// Automatic port number
#define PING_SLEEP_RATE 1000000

// Gives the timeout delay for receiving packets
// in seconds
#define RECV_TIMEOUT 1

// Performs a DNS lookup
char* dns_lookup(char* addr_host, struct sockaddr_in* addr_con) {
  // printf("\nResolving DNS..\n");
  struct hostent* host_entity;
  char* ip = (char*)malloc(NI_MAXHOST * sizeof(char));
  int i;

  if ((host_entity = gethostbyname(addr_host)) == NULL) {
    // No ip found for hostname
    return NULL;
  }

  // filling up address structure
  strcpy(ip, inet_ntoa(*(struct in_addr*)host_entity->h_addr));

  (*addr_con).sin_family = host_entity->h_addrtype;
  (*addr_con).sin_port = htons(PORT_NO);
  (*addr_con).sin_addr.s_addr = *(long*)host_entity->h_addr;

  return ip;
}

// Resolves the reverse lookup of the hostname
char* reverse_dns_lookup(char* ip_addr) {
  struct sockaddr_in temp_addr;
  socklen_t len;
  char buf[NI_MAXHOST], *ret_buf;

  temp_addr.sin_family = AF_INET;
  temp_addr.sin_addr.s_addr = inet_addr(ip_addr);
  len = sizeof(struct sockaddr_in);

  if (getnameinfo((struct sockaddr*)&temp_addr, len, buf, sizeof(buf), NULL, 0,
                  NI_NAMEREQD)) {
    // printf("Could not resolve reverse lookup of hostname\n");
    return NULL;
  }
  ret_buf = (char*)malloc((strlen(buf) + 1) * sizeof(char));
  strcpy(ret_buf, buf);
  return ret_buf;
}

// Driver Code
int main(int argc, char* argv[]) {
  int sockfd;
  char *ip_addr, *reverse_hostname;
  struct sockaddr_in addr_con;
  int addrlen = sizeof(addr_con);
  char net_buf[NI_MAXHOST];

  int i = 0;
  for (int i = 1; i < 255; ++i) {
    char ip[80];
    sprintf(ip, "192.168.2.%d", i);

    ip_addr = dns_lookup(ip, &addr_con);
    if (ip_addr == NULL) {
      // printf("\nDNS lookup failed! Could not resolve hostname!\n");
      continue;
    }

    reverse_hostname = reverse_dns_lookup(ip_addr);

    if (reverse_hostname == NULL) {
      // printf("\nDNS lookup failed! Could not resolve hostname!\n");
      continue;
    }
    // printf("\nTrying to connect to '%s' IP: %s\n",ip, ip_addr);
    printf("\nReverse Lookup domain: %s", reverse_hostname);

    printf("\n %s \n", ip);
  }

  return 0;
}

result:

Reverse Lookup domain: router.asus.com 192.168.2.1

Reverse Lookup domain: DESKTOP-CMK0J2S 192.168.2.10

Reverse Lookup domain: User255 192.168.2.14

Wladimir Koroy
  • 123
  • 1
  • 1
-1

Very vague question, there is no single "find all devices" feature to Windows, wireless or even networking in general. You need to scan fer certain services like netbios (139), UPNP, etc. Also, none of this is specific to wireless conenctions.

Deanna
  • 23,876
  • 7
  • 71
  • 156
  • 1
    So how does that tool make it possible? It lists all the devices connected to the same network I am connected. My only guess at this time is to sniff all the netwoerk packets (since it's a router not a smart switch) and read from the packet information about the device that initiated the connection (ip, mac, device name, etc). – ovi Feb 08 '12 at 18:45
  • What tool? I only mentioned methods to do the discovery yourself (which is what you asked for). – Deanna Feb 09 '12 at 08:48
  • It probably uses one of the methods I suggested or similar. – Deanna Feb 09 '12 at 16:12