Re: Linux sockets and IP

new topic     » goto parent     » topic index » view thread      » older message » newer message

On Fri, 03 Nov 2000, Kayhlan wrote:
>> I was wondering if anyone can help me....I've been using a large chunk of
>> Pete's
>> webclient/server program.
>> 
>> The thing is, I am trying to get the actual dot notated ip address for
>> incoming
>> socket connections. I know that using the C function accept should store or
>> return the ip address but I have no clue how to get at it.
>> 
>> Any help would be greatly appreciated and  um...helpful!
>> 
>> Thanks,
>> 
>> Kayhlan

Hey there Kayhlan,

 Right now im working on my own Linux socket wrapper so i can probably help
you out with your problem. (ive said that before to no avail) 

So if i understand what you are saying, you want to get the "0.0.0.0" (dot note
address) from a client after you make the call to accept?

Well here is how i did it in my socket wrapper, leftsock as a section of
leftlib. (and it works too!)

-- my Eu code to wrap accept
-- requires mixedlib.e by Bernie Ryan

global
function left_accept ( int sd )
  int status
  pointer sockaddr_ptr
  sequence return_seq
   
  sockaddr_ptr = allocate( 16 )
  
  status = c_func( accept_, { sd, sockaddr_ptr, sizeof( sockaddr_ptr ) } )
  
  if( status > 0 ) then  
    AssociatePtr( sockaddr_ptr, get_sockaddr_in_seq( ), HIGH )
                        
    return_seq = { { Gets( sockaddr_ptr, "sin_family" ), 
                     Gets( sockaddr_ptr, "sin_port" ),
                     Gets( sockaddr_ptr, "s_addr" ) }, status }
        
    free( sockaddr_ptr )
    return return_seq
  else
    free( sockaddr_ptr )
    return -1
  end if
  
end function

if you pass an atom (or pointer from mixedlib) as the second argument to
"accept" then that atom will contain the address of memory where the start of
all the data you want is located after the call is made. Now, i used a slightly
fancy of method of getting at this data by using mixedlib.e but it can be had
this was as well:

-- UNTESTED CODE (sorry jiri, i dont have a lot of time and i just wanna give
--                what might be a little help)

atom ptr_mem
atom sin_family, sin_port, s_addr

-- assume that the call is made to "accept" with second arg as ptr_mem

sin_family = peek( ptr_mem ) + peek( ptr_mem + 1 ) * 256
sin_port   = peek( ptr_mem + 2 ) + peek( ptr_mem + 3 ) * 256 
s_addr     = peek4u( ptr_mem + 4 )

-- i stole peek( ptr_mem ) + peek( ptr_mem + 1 ) * 256 from Bernies peek2

After this,  you should have the address of the client, s_addr, in network byte
order, NOT dot notation, to get the dot notation you have to run that value
through "inet_aton"

If you need any help on getting "inet_ntoa" working or if my help was really of
no help, just mail the list again and tell me where i f*ct up :)

I hope that can start you on your way. If nothing seems to work after giving
it a few swings, you can always drop me a line about my socket wrapper library
that is almost done.

-- 
evil, corruption and bad taste
^[cense]

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu