The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
IP address of machine harneet2004us UNIX for Advanced & Expert Users 3 04-16-2008 04:54 PM
Getting Unix machine IP address asutoshch Shell Programming and Scripting 6 04-30-2006 08:14 AM
how to retrieve IP address of a machine mridula IP Networking 5 11-02-2005 09:21 AM
Restricting access to a machine by IP Address patch UNIX for Dummies Questions & Answers 2 10-20-2003 02:46 PM
Changing Machine IP address kkinnon UNIX for Advanced & Expert Users 7 08-27-2002 02:17 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-24-2003
DreamWarrior DreamWarrior is offline
Registered User
  
 

Join Date: Oct 2003
Posts: 70
I didn't think that inet_ntoa would work because I didn't have an in_addr structure to pass it, I only had a struct sockaddr_in, and I didn't see an in_addr structure residing within that. So I didn't want to sit and guess what part (if any) of that structure to pass it, or try and find something to extract that information. However the man page for what I ended up using (inet_ntop) does say that is equivalent to inet_ntoa, although it takes entirely different arguments.

Anyway, I found code (in the Steven's book) to obtain my desired results. I knew we had a copy laying around here somewhere, I just couldn't find it...but you're right it is the God of network books.

So (drumroll) for those curious, this is what I ended up doing. Keep in mind that I needed to go from an integer socket descriptor to an IP, so this code may not be exactly what you need.

Just a quick note, the saddr and slen variables work fine with the type I gave them for HP-UX (and Redhat - while it gives compile warnings it works)). However, some OSes want saddr to be of type struct sockaddr *, and some want slen to be of type socklen_t. And while that may work, sockaddr doesn't have the internet sin_addr field required...so some typecasting may need to be done to shut those compilers up.

Also, this isn't the exact code I used, but it does the job and presents the basics without all the garbage I put around it for my particular needs.

Code:

int getIP(int sfd, char *buf, size_t bufSize, int getMine)
{
   int  rVal;
   struct sockaddr_in saddr;
   int slen;

   /* get saddr depending on what caller wants */

   if (getMine)
   {
      rVal = getsockname(sfd, &saddr, &slen);
   }
   else
   {
      rVal = getpeername(sfd, &saddr, &slen);
   }

   /* insure saddr was filled in */

   if (rVal < 0)
   {
      /* it wasn't */

      rVal = 0;
   }
   else
   {
      /* it was - convert address in sin_addr to dotted IP */

      rVal = (inet_ntop (AF_INET, &saddr.sin_addr, buf, bufSize) == NULL) ? 0 : 1;
   }

   return rVal;
}
Also, if you wanted to know port information, you could get it with:

port = ntohs(saddr.sin_port);

Provided, of course, that saddr was filled in.

getMine allows you to get information regarding the local socket connection, otherwise (by passing it 0) you get information regarding the connection at the other end (the peer).

And that's it...hope this helps those curious.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0