[Win32utils-devel] Unraveling a protoent struct

Park Heesob phasis at gmail.com
Sat Jun 7 22:15:30 EDT 2008


Hi,
----- Original Message ----- 
From: "Daniel Berger" <djberg96 at gmail.com>
To: "Development and ideas for win32utils projects" 
<win32utils-devel at rubyforge.org>
Sent: Sunday, June 08, 2008 1:24 AM
Subject: [Win32utils-devel] Unraveling a protoent struct


> I'm spacing - how do I unravel a protent struct pointer?
>
> require 'win32/api'
> include Win32
>
> getprotobyname = API.new('getprotobyname', 'P', 'P', 'ws2_32')
>
> ptr = getprotobyname.call('tcp')
>
> puts "Name: " + # ???
> puts "Aliases: " + # ???
> puts "Number: " + # ???
>
Here is a little dirty but working code:

require 'win32/api'
require "windows/msvcrt/buffer"
require 'windows/msvcrt/string'
include Win32
include Windows::MSVCRT::Buffer
include Windows::MSVCRT::String

protent = "\0" * 12
name_buf = "\0" * 256
alias_ptr = "\0" * 256
getprotobyname = API.new('getprotobyname', 'P', 'L', 'ws2_32')
ptr = getprotobyname.call('tcp')
memcpy(protent,ptr,12)
strcpy(name_buf,protent[0,4].unpack('L').first)
memcpy(alias_ptr,protent[4,4].unpack('L').first,4)
aliases = []
alias_ptr.unpack("L*").each do |name|
  break if name==0
  alias_buf = "\0"*256
  strcpy(alias_buf,name)
  aliases << alias_buf.strip
end

puts "Name: #{name_buf.strip}"
puts "Aliases: #{aliases}"
puts "Number: #{protent[8,4].unpack('L').first}"


Regards,

Park Heesob 




More information about the win32utils-devel mailing list