Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

[OT]: How to do fileno() and Win32::APIFile::FdGetOsFHandle() in C

by syphilis (Archbishop)
on Dec 14, 2006 at 06:19 UTC ( [id://589755]=perlquestion: print w/replies, xml ) Need Help??

syphilis has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
On Win32, I have some perl code as follows:
$fd = fileno($sock); $fd = Win32API::File::FdGetOsFHandle($fd);
I would like to do the same thing in C. I already have the C equivalent of $sock ... I just need to do the fileno() and FdGetOsFHandle() bits, but I haven't yet found out how to do that. If someone knows, or has some hints on where I should be looking, that would be great.

In the meantime I'll keep digging through the perl source ... the needle is somewhere in that haystack ...

(This is just an attempt to determine whether a problem with Net-SSH2-0.09 lies with the perl implementation, or with the C library it uses. I have a simple perl script that demonstrates the problem - all I need to do now is to run the same program as a C app and see if the same problem persists. So the question is definitely OT, but the overall aim is not :-)

Cheers,
Rob
  • Comment on [OT]: How to do fileno() and Win32::APIFile::FdGetOsFHandle() in C
  • Download Code

Replies are listed 'Best First'.
Re: [OT]: How to do fileno() and Win32::APIFile::FdGetOsFHandle() in C
by BrowserUk (Patriarch) on Dec 14, 2006 at 06:53 UTC
    I already have the C equivalent of $sock ...

    What do you mean by that? What is it that you have and where did it come from?

    My probably incomplete understanding of this is that unlike *nix, where sockets masquarade as files (File*), WS2 sockets are natively completely different beasts and cannot be used directly with the vast majority of APIs that expect a native file handle, and certainly not be used with CRT calls that expect a FILE*.

    Perl works around this difference in platform semantics by (in the terms of the comments) "faking it". See the routine _alloc_osfhnd() in win32.c(~2200) for some info. Basically, they open a native handle to NUL, and build a fake CRT FILE* around it and use that to allow *nix-style semantics to operate on WS2 handles.

    Depending what you have and where you got it from, you might have to emulate that in order to achieve your goal.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: [OT]: How to do fileno() and Win32::APIFile::FdGetOsFHandle() in C
by ikegami (Patriarch) on Dec 14, 2006 at 07:02 UTC

    I already have the C equivalent of $sock ... I just need to do the fileno() and FdGetOsFHandle() bits

    If you already have the C equivalent of $sock, you don't need fileno+FdGetOsFHandle since they return the C equivalent of $sock.

      If you already have the C equivalent of $sock, you don't need fileno+FdGetOsFHandle since they return the C equivalent of $sock

      Dammit ... you're right ... though there's something in your convoluted reasoning that troubles me :-)

      My "lhSocket" variable (of type "SOCKET") is already in the form it needs to be. I don't need to do anything to it at all !!

      Maaan ... C is so easy ... why do I bother with perl ;-)

      Thanks ikegami, BrowserUK.

      Cheers,
      Rob
Re: [OT]: How to do fileno() and Win32::APIFile::FdGetOsFHandle() in C
by Steve_p (Priest) on Dec 14, 2006 at 19:24 UTC

    To see how fileno() in Perl works, take a quick look at the pp_fileno() function in pp_sys.c in the Perl source code. Assuming your filehandle isn't tied, the steps basically are:

    1. Get the IO from the Perl filehandle passed in (or use the IO if that's what you got)
    2. Get the FILE pointer from the IO
    3. Pass the FILE pointer to fileno() to get your file descriptor.

    Now, to get at Win32API::File::FdGetOsFHandle(), grep around the win32/ subdirectory for your answers. I'm afraid my Win32 skills aren't good enough to help you there.


    Test your modules with bleadperl!

      rsync -avz rsync://public.activestate.com/perl-current/ .
      ./Configure -des -Dusedevel -Dprefix=/path/to/test/perl
      make test
      make install
    

    Now, please test you modules! If you have test failures that don't happen with Perl 5.8.8, send a simplified test case to

    perlbug at perl.org

      FdGetOsFHandle is simply ( system call MS C lib function ) _get_osfhandle.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://589755]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-23 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found