Sponsored Content
Top Forums Programming How to get IP Address of machine? Post 42248 by DreamWarrior on Friday 24th of October 2003 11:16:36 AM
Old 10-24-2003
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.
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Changing Machine IP address

Hopefully someone can help with this. I need the command to change the IP address on a machine (7 Replies)
Discussion started by: kkinnon
7 Replies

2. UNIX for Dummies Questions & Answers

Restricting access to a machine by IP Address

I have a need to allow only certain IP addresses to access a machine running solaris 9. I am not sure how this can be accomplished. Thanks in advance for your help. Patch (2 Replies)
Discussion started by: patch
2 Replies

3. IP Networking

how to retrieve IP address of a machine

hi is there is any command that retrieves the IP address of a machine. Also is there any function in c that does the same. thankx (5 Replies)
Discussion started by: mridula
5 Replies

4. Shell Programming and Scripting

How to get Windows machine's IP address from Unix?

I am using windows Xp. From windows I would connect to my IBM AIX unix machine using telnet client. Is there any command available to view the IP address of Windows machine from Unix? (Note that ifconfig will give unix mahcine's IP address currently logged in.) (3 Replies)
Discussion started by: mvictorvijayan
3 Replies

5. UNIX for Advanced & Expert Users

IP address of machine

Hi i want to know the Ip address of the machine from where i logged into the unix server and made some changes to a file. Can I know the last changes made to a unix file ? (3 Replies)
Discussion started by: harneet2004us
3 Replies

6. UNIX for Dummies Questions & Answers

IP address of Unix machine

Can any one please tell me how to find the IP address of the Unix machine we are in? (9 Replies)
Discussion started by: thoothukudiking
9 Replies

7. Solaris

How to get the IP address / Host name of client machine

Hi How to get the IP address / Host name of a particular user connected to Unix Server. For example: If used 'DevUser1' is connected to Unix server. I need to find out from which PC this connection has been made. How can this be achieved? Thanks (6 Replies)
Discussion started by: MVL
6 Replies

8. UNIX for Dummies Questions & Answers

Static IP address for solaris 10 virtual machine

Hi All I am having a solaris 10 virtual machine on vista (using vmware 7) laptop. Now i want to access virtual machine from vista using putty. Problem is that i insalled the solaris machine as dhcp. and whenever i connect to internet or reboot my system the IP address of solaris... (1 Reply)
Discussion started by: ankurk
1 Replies

9. Solaris

i want to set ip address to a virtual machine

hi all i want to set ip address to a vitrual machine i am using following command. but it is not ifconfig -a command output. what is wrong i dont know bash# ifconfig interfacename plumb bash# ifconfig interfacename auto-dhcp Please use code tags next time for your code and data. (4 Replies)
Discussion started by: nikhil kasar
4 Replies

10. Programming

I.p address of machine

i m writing a program which finds the i.p address of the machine. but it just prints out the first three character of the ifconfig output but i want to just print my i.p address lik 10.0.0.222 which is in second line after inet addr: code : #include<iostream> #include<cstdlib> using... (1 Reply)
Discussion started by: console
1 Replies
getpeername(2)							System Calls Manual						    getpeername(2)

NAME
getpeername - Gets the name of the peer socket SYNOPSIS
#include <sys/socket.h> int getpeername ( int socket, struct sockaddr *address, socklen_t *address_len ); [XNS4.0] The definition of the getpeername() function in XNS4.0 uses a size_t data type instead of a socklen_t data type as specified in XNS5.0 (the previous definition). [Tru64 UNIX] The following definition of the getpeername() function does not conform to current standards and is supported only for back- ward compatibility (see standards(5)): int getpeername ( int socket, struct sockaddr *address, int *address_len ); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: getpeername(): XNS5.0 Refer to the standards(5) reference page for more information about industry standards and associated tags. PARAMETERS
Specifies the descriptor number of a connected socket. Points to a sockaddr structure, the format of which is determined by the domain and by the behavior requested for the socket. The sockaddr structure is an overlay for a sockaddr_in or sockaddr_un structure, depending on which of the supported address families is active. [Tru64 UNIX] If the compile-time option _SOCKADDR_LEN is defined before the sys/socket.h header file is included, the sockaddr structure takes 4.4BSD behavior, with a field for specifying the length of the socket address. Otherwise, the default 4.3BSD sock- addr structure is used, with the length of the socket address assumed to be 14 bytes or less. If _SOCKADDR_LEN is defined, the 4.3BSD sockaddr structure is defined with the name osockaddr. Specifies the length of the sockaddr structure pointed to by the address parameter. DESCRIPTION
The getpeername() function retrieves the name of the peer socket connected to the specified socket. If the actual length of the address is greater than the length of the sockaddr structure, the address is truncated. If the protocol permits connections by unbound clients and the peer is unbound, the value pointed to by address is unspecified. A process created by another process can inherit open sockets, but may need to identify the addresses of the sockets it has inherited. The getpeername() function allows a process to retrieve the address of the peer socket at the remote end of the socket connection. NOTES
The getpeername() function operates only on connected sockets. A process can use the getsockname() function to retrieve the local address of a socket. [Tru64 UNIX] When compiled in the X/Open UNIX environment or the POSIX.1g socket environment, calls to the getpeername() function are internally renamed by prepending _E to the function name. When you are debugging a module that includes the getpeername() function and for which _XOPEN_SOURCE_EXTENDED or _POSIX_PII_SOCKET has been defined, use _Egetpeername to refer to the getpeername() call. See standards(5) for further information. RETURN VALUES
Upon successful completion, a value of 0 (zero) is returned and the address parameter holds the address of the peer socket. If the get- peername() function fails, a value of -1 is returned and errno is set to indicate the error. ERRORS
If the getpeername() function fails, errno may be set to one of the following values: The socket parameter is not valid. The address or address_len parameter is not in a readable OR writable part of the user address space. The socket has been shut down. Insufficient resources were available in the system to complete the call. The available STREAMS resources were insufficient for the operation to com- plete. The socket is not connected. The socket parameter refers to a file, not a socket. The operation is not supported for the socket protocol. RELATED INFORMATION
Functions: accept(2), bind(2), getsockname(2), socket(2). Standards: standards(5). delim off getpeername(2)
All times are GMT -4. The time now is 02:53 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy