tags:

views:

42

answers:

2

Is it possible to read a Network Adapter similar to a Serial Port? I know that Serial Ports can be read with CreateFile WINAPI Function. Is there a similar way to read raw bytes from a Network Adapter?

I am aware of the WiFi/Network Functions but the WiFi Examples are fairly sparse.

+1  A: 

If you want to capture raw packets you need a support driver like WinPCAP to do that.

Axel Gneiting
Unfortunately my network card does not support WinPCAP.
Shiftbit
Are you using wireless LAN? Ethernet should always be supported.
Axel Gneiting
I have not tried Ethernet, but that is good to know.
Shiftbit
+1  A: 

You can pass the SOCK_RAW flag when you create the socket using WSASocket() (or socket(), as your tastes run). This is described in further detail under TCP/IP Raw Sockets on MSDN.

From that page --

Once an application creates a socket of type SOCK_RAW, this socket may be used to send and receive data. All packets sent or received on a socket of type SOCK_RAW are treated as datagrams on an unconnected socket.

Of note, Microsoft crippled their raw sockets implementation after Windows XP SP2; the details are described on the MSDN page in the section Limitations on Raw Sockets:

  • TCP data cannot be sent over raw sockets.
  • UDP datagrams with an invalid source address cannot be sent over raw sockets.
  • A call to the bind function with a raw socket is not allowed.

If these limitations are too restrictive, you can fall back to the previously recommended winpcap library.

J.J.