public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* FW: Combining winpcap packet wait with poll/select in cygwin
@ 2007-12-14  1:28 S A
  2007-12-14 15:03 ` Brian Ford
  0 siblings, 1 reply; 4+ messages in thread
From: S A @ 2007-12-14  1:28 UTC (permalink / raw)
  To: cygwin



 
  Hi all
 
  (I've tried this question on the winpcap-users list already with no luck).
 
  Summary: Has anyone tried to have events (POLL_IN/POLL_OUT) on regular
           fd descriptors combined with winpcap packet receive handles incorporated
           into the same sleep event (i.e. poll/select)?
 
   The Linux program I'm trying to port to cygwin, waits for a user to establish a telnet connection
   on a local port number (i.e. localhost). The user can then choose to send/receive raw packets
   (of any ethernet type - hence the need for raw sockets via winpcap) on any of the network devices
   on the machine.
 
   My dilemma is how to make the poll/select call i use (to wait for an incoming telnet connection
   and further interaction with the user) and combine it with using pcap_next_ex() to receive
   raw packets.
 
   I know very little of windows but I understand both winpcap & cygwin poll (winsup/cygwin/select.cc
   right?) use a call "WaitForMultipleObjects ()" to sleep and wait for new receive events. I've seen
   some of the code in select.cc but is there an easy way to translate the fd's into the HANDLE w4 array
   so I can somehow combine it with the winpcap HANDLE)? Or anybody have any experience doing this?
 
 
   My only other options (none of which I like) are:
 
   1. Use threads. Multiple processes won't work because the telnet program and the raw socket
      program would have to interact with each other - and wouldn't that mean more localhost connections
      unless I use some kind of proc or ioctl interface?
 
   2.  Use winpcap to also receive IP packets and thus handle the telnet protocol in my program.
       However this is unnnecessarily complex.
 
   3. Use a maximum wait time on both the poll() call and the pcap_next_ex() call and alternate
      checking between the two. I'm afraid my program will get sluggish and also eat up a lot of CPU cycles.
 
   Any recommendations on what to do or more code to look at are greatly welcomed. Thanks!

_________________________________________________________________
i’m is proud to present Cause Effect, a series about real people making a difference.
http://im.live.com/Messenger/IM/MTV/?source=text_Cause_Effect

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: FW: Combining winpcap packet wait with poll/select in cygwin
  2007-12-14  1:28 FW: Combining winpcap packet wait with poll/select in cygwin S A
@ 2007-12-14 15:03 ` Brian Ford
  0 siblings, 0 replies; 4+ messages in thread
From: Brian Ford @ 2007-12-14 15:03 UTC (permalink / raw)
  To: cygwin

On Thu, 13 Dec 2007, S A wrote:

> Summary: Has anyone tried to have events (POLL_IN/POLL_OUT) on regular
>          fd descriptors combined with winpcap packet receive handles
>          incorporated into the same sleep event (i.e. poll/select)?

No, I haven't tried because it looked too fragile if it even works, and
Cygwin's poll/select (or the preamble to read/recv; I'm not sure yet) was
causing significant packet reception jitter in my real time application.

> I know very little of windows but I understand both winpcap & cygwin
> poll (winsup/cygwin/select.cc right?) use a call "WaitForMultipleObjects
> ()" to sleep and wait for new receive events. I've seen some of the code
> in select.cc but is there an easy way to translate the fd's into the
> HANDLE w4 array so I can somehow combine it with the winpcap HANDLE)?

You could try get_osfhandle, but I'd suggest going the other way around by
using Cygwin's cygwin_attach_handle_to_fd.  I don't know if it will work
for you, but this might get you an fd that you can use in poll/select for
the winpcap handle.

> My only other options (none of which I like) are:
>
> 1. Use threads.

I went this route and it worked out well for me, but my application was
already threaded, and it didn't have this kind of interaction.

> 2. Use winpcap to also receive IP packets and thus handle the telnet
> protocol in my program. However this is unnnecessarily complex.

Why did you choose this strange localhost telnet IPC to begin with?
Couldn't it just be a library?  I guess it might be because of privilege
issues?

> Any recommendations on what to do or more code to look at are greatly
> welcomed. Thanks!

I would appreciate you letting us know how it turns out.

-- 
Brian Ford
Lead Realtime Software Engineer
VITAL - Visual Simulation Systems
FlightSafety International
the best safety device in any aircraft is a well-trained crew...

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: FW: Combining winpcap packet wait with poll/select in cygwin
  2007-12-21  2:08 ` S A
@ 2007-12-21  2:10   ` Christopher Faylor
  0 siblings, 0 replies; 4+ messages in thread
From: Christopher Faylor @ 2007-12-21  2:10 UTC (permalink / raw)
  To: cygwin

On Thu, Dec 20, 2007 at 04:04:11PM -0800, S A wrote:
>Hi Brian 
>
>Finally got a change to try this (sorry it tooks so long). It looks like it does NOT work.
>
>The fd returned from cygwin_attach_handle_to_fd() always returns
>from the poll call with a POLLIN event even though no packets
>are flowing on the network (and hence there should be no pcap
>packets received).
>
>The code I used is attached below. I tried both blocking and non blocking
>fcntl calls. But the poll awakes immediately all the time. If I try
>to do a read of the fd, read returns a -1.
>
>I also found another thread:
>http://cygwin.com/ml/cygwin/2006-10/msg00338.html
>
>I didn't know what exactly to pass as the name (i.e. the first argument
>to cygwin_attach_handle_to_fd()) but I tried NULL, "", "/dev/fd").
>
>Maybe I need to pass the right type of device for a windows socket. Just can't
>find what it is. I've looked at all the fhandler_base classes & derivatives
>but can't seem to locate the "name" field. But I don't know C++ that well.
>
>I'd be happy to try any other suggestions. Thanks.
>
>main (int argc, char *argv[])
>{
>  pcap_t *adhandle;
>  int pcap_fd;
>  struct pollfd pcap_pfd;
>  int i, ret;
>
>  [initialization for winpcap adhandle]
>
>  pcap_fd = cygwin_attach_handle_to_fd("/dev/fd", -1, pcap_getevent(adhandle),
>                                       O_BINARY, GENERIC_READ);
>
>  fcntl(pcap_fd, F_SETFL, O_NONBLOCK);
>
>  pcap_pfd.fd = pcap_fd;
>  pcap_pfd.events =  POLLIN;
>  pcap_pfd.revents = 0;

I doubt that a random handle will be selectable.  Cygwin needs to
know specifically what type of device is attached to a handle in
order to emulate select or poll and a raw windows handle is not
apt to work well.

In other words, it's unlikely that you can successfully use the result
of cygwin_attach_handle_to_fd in a select or poll function.

cgf

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* FW: Combining winpcap packet wait with poll/select in cygwin
       [not found] <BAY128-W147E0BFF22B2B5B0D232D3D25D0@phx.gbl>
@ 2007-12-21  2:08 ` S A
  2007-12-21  2:10   ` Christopher Faylor
  0 siblings, 1 reply; 4+ messages in thread
From: S A @ 2007-12-21  2:08 UTC (permalink / raw)
  To: cygwin, brian.ford


Hi Brian 

Finally got a change to try this (sorry it tooks so long). It looks like it does NOT work.

The fd returned from cygwin_attach_handle_to_fd() always returns
from the poll call with a POLLIN event even though no packets
are flowing on the network (and hence there should be no pcap
packets received).

The code I used is attached below. I tried both blocking and non blocking
fcntl calls. But the poll awakes immediately all the time. If I try
to do a read of the fd, read returns a -1.

I also found another thread:
http://cygwin.com/ml/cygwin/2006-10/msg00338.html

I didn't know what exactly to pass as the name (i.e. the first argument
to cygwin_attach_handle_to_fd()) but I tried NULL, "", "/dev/fd").

Maybe I need to pass the right type of device for a windows socket. Just can't
find what it is. I've looked at all the fhandler_base classes & derivatives
but can't seem to locate the "name" field. But I don't know C++ that well.

I'd be happy to try any other suggestions. Thanks.

main (int argc, char *argv[])
{
  pcap_t *adhandle;
  int pcap_fd;
  struct pollfd pcap_pfd;
  int i, ret;

  [initialization for winpcap adhandle]

  pcap_fd = cygwin_attach_handle_to_fd("/dev/fd", -1, pcap_getevent(adhandle),
                                       O_BINARY, GENERIC_READ);

  fcntl(pcap_fd, F_SETFL, O_NONBLOCK);

  pcap_pfd.fd = pcap_fd;
  pcap_pfd.events =  POLLIN;
  pcap_pfd.revents = 0;

  while (1) {
      pcap_pfd.fd = pcap_fd;
      pcap_pfd.events =  POLLIN;
      pcap_pfd.revents = 0;

      ret = poll(&pcap_pfd, 1, -1);
      if (ret> 0) {
          if (pcap_pfd.revents) { fprintf(stdout, "fd %d is awake on events %x\n", pcap_pfd.fd, pcap_pfd.revents); }
      else if (ret == 0) {
          printf("poll timeout!\n");
      } else if (ret < 0) {
          printf("poll errno=%d, %s\n", errno, strerror(errno));
      }
  }
}


=========================================================================

> Summary: Has anyone tried to have events (POLL_IN/POLL_OUT) on regular
>          fd descriptors combined with winpcap packet receive handles
>          incorporated into the same sleep event (i.e. poll/select)?

No, I haven't tried because it looked too fragile if it even works, and
Cygwin's poll/select (or the preamble to read/recv; I'm not sure yet) was
causing significant packet reception jitter in my real time application.

> I know very little of windows but I understand both winpcap & cygwin
> poll (winsup/cygwin/select.cc right?) use a call "WaitForMultipleObjects
> ()" to sleep and wait for new receive events. I've seen some of the code
> in select.cc but is there an easy way to translate the fd's into the
> HANDLE w4 array so I can somehow combine it with the winpcap HANDLE)?

You could try get_osfhandle, but I'd suggest going the other way around by
using Cygwin's cygwin_attach_handle_to_fd.  I don't know if it will work
for you, but this might get you an fd that you can use in poll/select for
the winpcap handle.

> My only other options (none of which I like) are:
>
> 1. Use threads.

I went this route and it worked out well for me, but my application was
already threaded, and it didn't have this kind of interaction.

> 2. Use winpcap to also receive IP packets and thus handle the telnet
> protocol in my program. However this is unnnecessarily complex.

Why did you choose this strange localhost telnet IPC to begin with?
Couldn't it just be a library?  I guess it might be because of privilege
issues?

> Any recommendations on what to do or more code to look at are greatly
> welcomed. Thanks!

I would appreciate you letting us know how it turns out.

--
Brian Ford
Lead Realtime Software Engineer
VITAL - Visual Simulation Systems
FlightSafety International
the best safety device in any aircraft is a well-trained crew...

==============================================================================

________________________________
Get the power of Windows + Web with the new Windows Live. Get it now!

_________________________________________________________________
i’m is proud to present Cause Effect, a series about real people making a difference.
http://im.live.com/Messenger/IM/MTV/?source=text_Cause_Effect

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-12-21  0:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-14  1:28 FW: Combining winpcap packet wait with poll/select in cygwin S A
2007-12-14 15:03 ` Brian Ford
     [not found] <BAY128-W147E0BFF22B2B5B0D232D3D25D0@phx.gbl>
2007-12-21  2:08 ` S A
2007-12-21  2:10   ` Christopher Faylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).