ibm-information-center/dist/eclipse/plugins/i5OS.ic.apis_5.4.0.1/gpeern.htm

411 lines
11 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
<title>getpeername()--Retrieve Destination Address of Socket</title>
<!-- Begin Header Records ========================================== -->
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<!-- Change History: -->
<!-- Unix8 SCRIPT J converted by B2H R4.1 (346) (CMS) by V2KEA304 -->
<!-- at RCHVMW2 on 17 Feb 1999 at 11:05:09 -->
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body>
<!--End Header Records --><!-- Edited by Kersten Feb 02 -->
<!-- Java sync-link -->
<script type="text/javascript" language="Javascript" src="../rzahg/synch.js">
</script>
<a name="Top_Of_Page"></a>
<h2>getpeername()--Retrieve Destination Address of Socket</h2>
<div class="box" style="width: 70%;">
<br>
&nbsp;&nbsp;BSD 4.3 Syntax<br>
<pre>
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
int getpeername(int <em>socket_descriptor</em>,
struct sockaddr *<em>destination_address</em>,
int *<em>address_length</em>)
</pre>
<br>
&nbsp;&nbsp;Service Program Name: QSOSRV1<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Default Public Authority: *USE<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Threadsafe: Yes<br>
<!-- iddvc RMBR -->
<br>
</div><br>
<div class="box" style="width: 70%;">
<br>
&nbsp;&nbsp;<a href="_xopen_source.htm">UNIX 98 Compatible Syntax</a><br>
<pre>
#define _XOPEN_SOURCE 520
#include &lt;sys/socket.h&gt;
int getpeername(int <em>socket_descriptor</em>,
struct sockaddr *<em>destination_address</em>,
socklen_t *<em>address_length</em>)
</pre>
<br>
&nbsp;&nbsp;Service Program Name: QSOSRV1<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Default Public Authority: *USE<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Threadsafe: Yes<br>
<!-- iddvc RMBR -->
<br>
</div>
<br>
<p>The <em>getpeername()</em> function is used to retrieve the destination
address to which the socket is connected.</p>
<p> There are two versions of
the API, as shown above. The base i5/OS API uses BSD 4.3 structures and
syntax. The other uses syntax and structures compatible with the UNIX 98
programming interface specifications. You can select the UNIX 98 compatible
interface with the <a href="_xopen_source.htm">_XOPEN_SOURCE</a> macro.</p>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong>socket_descriptor</strong></dt>
<dd>(Input) The descriptor of the socket for which the destination address is
to be retrieved.<br>
<br>
</dd>
<dt><strong>destination_address</strong></dt>
<dd>(Output) A pointer to a buffer of type <strong>struct sockaddr</strong> in
which the destination address to which the socket connects is stored. The
structure <strong>sockaddr</strong> is defined in
<strong>&lt;sys/socket.h&gt;</strong>.
<p> The BSD 4.3 structure
is:</p>
<pre>
struct sockaddr {
u_short sa_family;
char sa_data[14];
};
</pre>
<p>The BSD 4.4/UNIX 98 compatible structure is:</p>
<pre>
typedef uchar sa_family_t;
struct sockaddr {
uint8_t sa_len;
sa_family_t sa_family;
char sa_data[14];
};
</pre>
<p>The BSD 4.4 <em>sa_len</em> field is the length of the address. The <em>sa_family</em> field identifies
the address family to which the address belongs, and <em>sa_data</em> is the
address whose format is dependent on the address family.</p>
<p> <strong>Note:</strong> See
the usage notes about using different address families with
<strong>sockaddr_storage</strong>.</p>
</dd>
<dt><strong>address_length</strong></dt>
<dd>(I/O) This parameter is a value-result field. The caller passes a pointer
to the length of the <em>destination_address</em> parameter. On return from the
call, the <em>address_length</em> parameter contains the actual length of the
destination address.</dd>
</dl>
<br>
<h3>Authorities</h3>
<p>No authorization is required.</p>
<br>
<h3>Return Value</h3>
<p><em>getpeername()</em> returns an integer. Possible values are: </p>
<ul>
<li>-1 (unsuccessful)<br>
<br>
</li>
<li>0 (successful)
</li>
</ul>
<br>
<h3>Error Conditions</h3>
<p>When <em>getpeername()</em> fails, <em>errno</em> can be set to one of the
following:</p>
<table cellpadding="5">
<!-- cols="15 85" -->
<tr>
<td align="left" valign="top"><em>[EBADF]</em></td>
<td align="left" valign="top">Descriptor not valid.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>[EFAULT]</em></td>
<td align="left" valign="top">Bad address.
<p>The system detected an address which was not valid while attempting to
access the <em>destination_address</em> or <em>address_length</em>
parameters.</p>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>[EINVAL]</em></td>
<td align="left" valign="top">Parameter not valid.
<p>The <em>address_length</em> parameter specifies a negative value.</p>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>[EIO]</em></td>
<td align="left" valign="top">Input/output error.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>[ENOBUFS]</em></td>
<td align="left" valign="top">There is not enough buffer space for the
requested operation.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>[ENOTCONN]</em></td>
<td align="left" valign="top">Requested operation requires a connection.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>[ENOTSOCK]</em></td>
<td align="left" valign="top">The specified descriptor does not reference a
socket.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>[EUNKNOWN]</em></td>
<td align="left" valign="top">Unknown system state.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>[EUNATCH]</em></td>
<td align="left" valign="top">The protocol required to support the specified
address family is not available at this time.</td>
</tr>
</table>
<br>
<br>
<h3>Error Messages</h3>
<table width="100%" cellpadding="5">
<!-- cols="15 85" -->
<tr>
<th align="left" valign="top">Message ID</th>
<th align="left" valign="top">Error Message Text</th>
</tr>
<tr>
<td width="15%" valign="top">CPE3418 E</td>
<td width="85%" valign="top">Possible APAR condition or hardware failure.</td>
</tr>
<tr>
<td align="left" valign="top">CPF9872 E</td>
<td align="left" valign="top">Program or service program &amp;1 in library
&amp;2 ended. Reason code &amp;3.</td>
</tr>
<tr>
<td align="left" valign="top">CPFA081 E</td>
<td align="left" valign="top">Unable to set return value or error code.</td>
</tr>
</table>
<br>
<br>
<h3>Usage Notes</h3>
<ol>
<li><em>getpeername()</em> fails if issued against a socket for which a
<em>connect()</em> has not been done.<br>
<br>
</li>
<li>For connection oriented sockets, <em>getpeername()</em> fails if both the
write side and the read side have been closed through the use of one or more
previous <em>shutdown()</em> functions.<br>
<br>
</li>
<li>If the length of the address to be returned exceeds the length of the
<em>destination_address</em> parameter, the returned address is truncated.<br>
<br>
</li>
<li>The structure
<strong>sockaddr</strong> is a generic structure used for any address family
but it is only 16 bytes long. The actual address returned for some address
families may be much larger. You should declare storage for the address with
the structure <strong>sockaddr_storage</strong>. This structure is large enough
and aligned for any protocol-specific structure. It may then be cast as
<strong>sockaddr</strong> structure for use on the APIs. The <em>ss_family</em>
field of the <strong>sockaddr_storage</strong> will always align with the
family field of any protocol-specific structure.
<p>The BSD 4.3 structure is:</p>
<pre>
#define _SS_MAXSIZE 304
#define _SS_ALIGNSIZE (sizeof (char*))
#define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(sa_family_t))
#define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof(sa_family_t)+
_SS_PAD1SIZE + _SS_ALIGNSIZE))
struct sockaddr_storage {
sa_family_t ss_family;
char _ss_pad1[_SS_PAD1SIZE];
char* _ss_align;
char _ss_pad2[_SS_PAD2SIZE];
};
</pre>
<p>The BSD 4.4/UNIX 98 compatible structure is:</p>
<pre>
#define _SS_MAXSIZE 304
#define _SS_ALIGNSIZE (sizeof (char*))
#define _SS_PAD1SIZE (_SS_ALIGNSIZE - (sizeof(uint8_t) + sizeof(sa_family_t)))
#define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof(uint8_t) + sizeof(sa_family_t)+
_SS_PAD1SIZE + _SS_ALIGNSIZE))
struct sockaddr_storage {
uint8_t ss_len;
sa_family_t ss_family;
char _ss_pad1[_SS_PAD1SIZE];
char* _ss_align;
char _ss_pad2[_SS_PAD2SIZE];
};
</pre>
</li>
<li>When used with an address family of <samp>AF_UNIX</samp> or
<samp>AF_UNIX_CCSID</samp>, <em>getpeername()</em> always returns the same path
name that was specified on the <em>bind()</em> in the peer program. If the path
name specified by the peer program was not a fully qualified path name, the
output of <em>getpeername()</em> is meaningful only if your program knows what
current directory was in effect for the peer program when it issued the
<em>bind()</em>. For <samp>AF_UNIX</samp>, the path name is returned in the
default coded character set identifier (CCSID) currently in effect for the job.
For <samp>AF_UNIX_CCSID</samp>, the output structure sockaddr_unc defines the
format and CCSID of the returned path name.<br>
<br>
</li>
<li>When you develop in C-based
languages and an application is compiled with the _XOPEN_SOURCE macro defined
to the value 520 or greater, the <em>getpeername()</em> API is mapped to
<em>qso_getpeername98()</em>.</li>
</ol>
<br>
<h3>Related Information</h3>
<ul>
<li><a href=
"_xopen_source.htm">_XOPEN_SOURCE</a>--Using _XOPEN_SOURCE for the UNIX 98
compatible interface<br>
<br>
</li>
<li><a href="accept.htm">accept()</a>--Wait for Connection Request and Make
Connection<br>
<br>
</li>
<li><a href="bind.htm">bind()</a>--Set Local Address for Socket<br>
<br>
</li>
<li><a href="connec.htm">connect()</a>--Establish Connection or Destination
Address<br>
<br>
</li>
<li><a href="gsockn.htm">getsockname()</a>--Retrieve Local Address of
Socket</li>
</ul>
<br>
<hr>
API introduced: V3R1
<hr>
<center>
<table cellpadding="2" cellspacing="2">
<tr align="center">
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href=
"unix.htm">UNIX-Type APIs</a> | <a href="aplist.htm">APIs by category</a></td>
</tr>
</table></center>
</body>
</html>