Socket Win32 IO
jose maria
scasjos at mixmail.com
Wed Jul 9 08:42:07 EDT 2003
Peter Hansen <peter at engcorp.com> wrote in message news:<3F0AC6B8.A94C0435 at engcorp.com>...
> jose maria wrote:
> >
> > Hello Peter tanks for you attenion and time
> >
> > Yes really I forget put in the message one parameter in the function
> > I put the traceback and all code I hope that this help you. Thousands
> > of pardons for my bad English
> >
> > Traceback:
> >
> > ActivePython 2.2.1 Build 222 (ActiveState Corp.) based on
> > Python 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)] on wi
> > Type "help", "copyright", "credits" or "license" for more informatio
> > >>> from socket import *
> > >>> import win32api
> > >>> import win32file
> > >>> Ip=getprotobyname("ip")
> > >>> SIO_RCVALL=0x98000001
> > >>> ip=('xxx.xxx.xxx.xxx',0)
> > >>> Sock=socket(AF_INET,SOCK_RAW,Ip) #Raw Socket
> > >>> Sock.bind(ip) # Bind Socket to ip
> > >>> fh=Sock.fileno() # Get file handle
> > >>> test=win32file.DeviceIoControl(fh,SIO_RCVALL,"", 0,None) # The
> > function
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in ?
> > pywintypes.api_error: (1, 'DeviceIoControl', 'Incorrect function.')
>
> I still get a different result (the second of the two that I posted
> before) even if I add that "None" argument, not what you show above.
> Are you certain you are cutting and pasting *exactly* what you typed?
> I note you have 'xxx.xxx.xxx.xxx' above, so presumably you have
> edited the transcript manually in at least one way... any others?
>
> I'm not likely to be able to solve the problem, since I have no idea
> what DeviceIoControl is for, nor what you're trying to accomplish.
> I just thought I'd report that I do not get the results you are
> getting when I try what you show you are trying.
>
> -Peter
Hi Peter thanks again for your time
I feel it if my message is not correct I will try to explain more in
detail my objective and try to explain my code in more detail
My objective is write a simple sniffer for Windows 2000/XP without
have to resort external code (DLL)
I evaluate several options
1) Use pylibpcap: problem: Not with himself that work in my machine
2) Write a external DLL in C++ and then expose to python: problem I
have little experience in C++
3) Imagination (¿why not?) : Reading several code from sniffers in
win32 I finded similarities in all parts of the code
the similarities are that all use constant 0x98000001 in
SocketIOControl this especial mode permit to read all ip packets
that arrive in network adapter
I put you a simple example in C++
############# This is the file
PacketCapture.h##########################
#include <winsock2.h>
#include <stdio.h>
#define SIO_RCVALL 0x98000001
#define MAX_IP_SIZE 65535
class CPacketCapture
{
public:
int GetPacket(WSABUF *wsabuf);
int Initialize(int AdapterNr);
SOCKET sock;
WSADATA wsd;
CPacketCapture();
virtual ~CPacketCapture();
};
###########################################################################
############# NetAdapter.h##########################
#include <winsock2.h>
class CNetAdapter
{
public:
int GetAdapterList(SOCKET_ADDRESS_LIST* slist);
int GetAdapter(SOCKET s, SOCKADDR_IN *ifx, int num);
CNetAdapter();
virtual ~CNetAdapter();
};
############################################################################
############### This is the file
PacketCapture.cpp##########################
#include "PacketCapture.h"
#include "NetAdapter.h"
CPacketCapture::CPacketCapture()
{
}
CPacketCapture::~CPacketCapture()
{
}
int CPacketCapture::Initialize(int AdapterNr)
{
CNetAdapter *netadapter=new CNetAdapter;
SOCKADDR_IN if0;
unsigned int optval;
DWORD dwBytesRet;
sock=WSASocket(AF_INET,SOCK_RAW,IPPROTO_IP,NULL,0,WSA_FLAG_OVERLAPPED);
if(sock==INVALID_SOCKET) {
MessageBox(NULL,"Creation of Socket(SOCK_RAW/IPPROTO_IP)
failed","Alert!",MB_OK);
return -1;
}
if(netadapter->GetAdapter(sock,&if0,AdapterNr)!=0) {
MessageBox(NULL,"Unable to obtain selected network
adapter!","Alert!",MB_OK);
return -1;
}
if0.sin_family = AF_INET;
if0.sin_port = htons(0);
if(bind(sock,(SOCKADDR *)&if0,sizeof(if0))==SOCKET_ERROR) {
MessageBox(NULL,"Bind call failed!","Alert!",MB_OK);
return -1;
}
optval=1;
if(WSAIoctl(sock,SIO_RCVALL,&optval,sizeof(optval),NULL,0,&dwBytesRet,NULL,NULL)==SOCKET_ERROR){
MessageBox(NULL,"WSAIoCtl(SIO_RCVALL) failed","Alert!",MB_OK);
return -1;
}
delete netadapter;
return 0;
}
int CPacketCapture::GetPacket(WSABUF *wbuf)
{
DWORD dwBytesRet=0,dwFlags=0;
if(SOCKET_ERROR==WSARecv(sock,wbuf,1,&dwBytesRet,&dwFlags,NULL,NULL)){
char buf[200];
sprintf(buf,"WSARecv failed. Code %d",WSAGetLastError(),sock);
MessageBox(NULL,buf,"Alert!",MB_OK);
}
wbuf->len=dwBytesRet;
return 0;
}
######################################################################################################################
I try to emulate this code in python (if its posible that that I don`t
know it) reading doc for socket module I not found
a method, property that permit to take control for IO in the socket
(perhaps there is it but I´don't know I´m newbie)
After to give him returned to the problem I founded in win32file
module a function that permit control for devices
this function is win32fileDeviceIoControl that have this arguments:
string = DeviceIoControl(hFile, dwIoControlCode , data , readSize ,
ol )
Call DeviceIoControl
Parameters
hFile : int
Handle to the file
dwIoControlCode : int
IOControl Code to use.
data : string
The data to write.
readSize : int
Size of the buffer to create for the read.
ol=None : PyOVERLAPPED
An overlapped structure
My trick is (if it's posible) play with this function for to take
control in the sockect.
My code
## import the necessary modules
from socket import * # for create socket
import win32file # for manage IO of the socket( in my theory )
Ip=getprotobyname("ip") # for raw socket
SIO_RCVALL=0x98000001 # constant for IO to recived all ip packtes
ip=('XXX.XXX.XXX.XXX',0) # ip that I want read for after bind to the
socket
#pardom for to omit this detail
# where XXXX.XXXX.XXX.XXX is the ip from one
network
# adapter I hope that this clarify
# your doubts :) no be a transcript error
there was
# my ip sorry
Sock=socket(AF_INET,SOCK_RAW,Ip) # I create the raw socket no stream
socket or
# other kind only raw
Sock.bind(ip) # Bind socket to the IP
fh=Sock.fileno() # Get the file handle
test=win32file.DeviceIoControl(fh,SIO_RCVALL,"", 0,None) # The
function to
# manage IO
End Code
More details sorry for my mistake I´m using Windows 2000 professional
SP4 and ActiveState Python 2.2.1
This is my traceback
ActivePython 2.2.1 Build 222 (ActiveState Corp.) based on
Python 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)] on win
Type "help", "copyright", "credits" or "license" for more information
>>> from socket import *
>>> import win32file
>>> Ip=getprotobyname("ip")
>>> SIO_RCVALL=0x98000001
>>> ip=('10.134.202.22',0)
>>> Sock=socket(AF_INET,SOCK_RAW,Ip)
>>> Sock.bind(ip)
>>> fh=Sock.fileno()
>>> fh
892
>>> test=win32file.DeviceIoControl(fh,SIO_RCVALL,"", 0,None)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
pywintypes.api_error: (1, 'DeviceIoControl', 'Incorrect function.')
>>>
End of traceback
I try to emulate yours errors but only I obtained the first error that
you posted not the second and I don´t know how try to emulate
your second error and I don´t know why I obtained this error
pywintypes.api_error: (1, 'DeviceIoControl', 'Incorrect function.')
Thousands Thanks Peter for your time and patience (pardon for my bad
English)
sincerely Jose Maria
More information about the Python-list
mailing list