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

451 lines
12 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<!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>getsockname()--Retrieve Local 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>getsockname()--Retrieve Local Address of Socket</h2>
<div class="box" style="width: 80%;">
<br>
&nbsp;&nbsp;BSD 4.3 Syntax<br>
<pre>
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
int getsockname(int <em>socket_descriptor</em>,
struct sockaddr *<em>local_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: 80%;">
<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 getsockname(int <em>socket_descriptor</em>,
struct sockaddr *<em>local_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>getsockname()</em> function is used to retrieve the local address
associated with the socket.</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 local address is to be
retrieved.<br><br></dd>
<dt><strong>local_address</strong></dt>
<dd>(Output) A pointer to a buffer of type <strong>struct sockaddr</strong> in
which the local address of the socket 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>
</dd>
<dd>
<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>local_address</em> parameter. On return from the call,
the <em>address_length</em> parameter contains the actual length of the local
address.</dd>
</dl>
<br>
<h3>Authorities</h3>
<p>No authorization is required.</p>
<br>
<h3>Return Value</h3>
<p><em>getsockname()</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>getsockname()</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>local_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. This error code indicates
one of the following:<br>
<br>
<ul>
<li>The <em>address_length</em> parameter specifies a negative value.<br>
<br>
</li>
<li>The socket specified by the <em>socket_descriptor</em> parameter is using a
connection-oriented transport service and either the write-side has been shut
down (with a <em>shutdown()</em>) or the connection has been reset.
</li>
</ul>
</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>[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>If the length of the address to be returned exceeds the length of the
<em>local_address</em> parameter, the returned address will be 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>getsockname()</em> always returns the same path
name that was specified on a <em>bind()</em>. If the path name that was
specified is not a fully qualified path name, the output of
<em>getsockname()</em> is meaningful only if your program knows what current
directory was in effect at the time of 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><em>getsockname()</em> produces different results, depending on the address
family or type of the socket:<br>
<br>
<ul>
<li>For address family of <samp>AF_INET</samp>:<br>
<br>
<ul>
<li>If the <em>type</em> is <samp>SOCK_STREAM</samp> or
<samp>SOCK_DGRAM</samp>, <em>getsockname()</em> will return 0 if issued before
the <em>bind()</em>. The socket address that is returned has the IP address and
port number fields set to zeros.<br>
<br>
</li>
<li>If the <em>type</em> is <samp>SOCK_RAW</samp>, <em>getsockname()</em>
returns a -1 if issued before a <em>bind()</em>.<br>
<br>
</li>
<li>If the <em>type</em> is <samp>SOCK_STREAM</samp>, and an <em>Rbind()</em>
has successfully completed, then the address returned is the <em>SOCKS
server</em> address. See <em>Rbind()</em> for more information.</li>
</ul>
<br>
</li>
<li>For address family of
<samp>AF_INET6</samp>:<br>
<br>
<ul>
<li>If the <em>type</em> is <samp>SOCK_STREAM</samp> or
<samp>SOCK_DGRAM</samp>, <em>getsockname()</em> will return 0 if issued before
the <em>bind()</em>. The socket address that is returned has the IP address and
port number fields set to zeros.<br>
<br>
</li>
<li>If the <em>type</em> is <samp>SOCK_RAW</samp>, <em>getsockname()</em>
returns a -1 if issued before a <em>bind()</em>.</li>
</ul>
<br>
</li>
<li>For address family of <samp>AF_UNIX</samp> or <samp>AF_UNIX_CCSID</samp>,
<em>getsockname()</em> returns 0 if issued before a <em>bind()</em>. The
address length is 0. This is always the case for sockets created by
<em>socketpair()</em>.</li>
</ul>
<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>getsockname()</em> API is mapped to
<em>qso_getsockname98()</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="bind.htm">bind()</a>--Set Local Address for Socket<br>
<br>
</li>
<li><a href="connec.htm">connect()</a>--Establish Connection or Destination
Address</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>