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

673 lines
19 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>bind()--Set Local Address for 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: -->
<!-- YYMMDD USERID Change description -->
<!-- Unix8 SCRIPT J converted by B2H R4.1 (346) (CMS) by V2KEA304 -->
<!-- at RCHVMW2 on 17 Feb 1999 at 11:05:09 -->
<!-- Edited by Kersten Feb 02 -->
<!-- End Header Records -->
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body>
<!--Java sync-link-->
<script type="text/javascript" language="Javascript" src="../rzahg/synch.js">
</script>
<a name="Top_Of_Page"></a>
<h2>bind()--Set Local Address for Socket</h2>
<div class="box" style="width: 70%;">
<p><br>
&nbsp;&nbsp;BSD 4.3 Syntax<br>
</p>
<pre>
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
int bind(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: 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 bind(int <em>socket_descriptor</em>,
const 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>bind()</em> function is used to associate a local address with a
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 that is to be bound.<br>
<br>
</dd>
<dt><strong>local_address</strong></dt>
<dd>(Input) A pointer to a buffer of type <strong>struct sockaddr</strong> that
contains the local address to which the socket is to be bound. 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>
<dt><strong>address_length</strong></dt>
<dd>(Input) The length of the <em>local_address</em>.</dd>
</dl>
<br>
<h3>Authorities</h3>
<ul>
<li>When the address type of the socket identified by the
<strong>socket_descriptor</strong> is AF_INET, the thread must have retrieve,
insert, delete, and update authority to the port specified by the
<strong>local_address</strong> field. When the thread does not have this level
of authority, an <em>errno</em> of EACCES is returned.<br>
<br>
</li>
<li>When the address type of the socket identified by the
<strong>socket_descriptor</strong> is AF_INET and is running IP over SNA, the
thread must have retrieve, insert, delete, and update authority to the APPC
device. When the thread does not have this level of authority, an
<em>errno</em> of EACCES is returned.</li>
</ul>
<br>
<h3>Return Value</h3>
<p><em>bind()</em> returns an integer. Possible values are:</p>
<ul>
<li>-1 (unsuccessful)</li>
<li>0 (successful)</li>
</ul>
<br>
<h3>Error Conditions</h3>
<p>When a <em>bind()</em> fails, <em>errno</em> can be set to one of the
following:</p>
<table cellpadding="5">
<!-- cols="25 75" -->
<tr>
<td align="left" valign="top"><em>[EACCES]</em></td>
<td align="left" valign="top">Permission denied.
<p>The process does not have the appropriate privileges to bind
<em>local_address</em> to the socket pointed to by <em>socket_descriptor</em>
(for example, if <em>socket_descriptor</em> is a socket with an address family
of <samp>AF_INET</samp>, and the <strong>sockaddr_in</strong> structure
(pointed to by <em>local_address</em>) specified a port that was restricted for
use).</p>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>[EADDRINUSE]</em></td>
<td align="left" valign="top">Address already in use.
<p>This error code indicates one of the following:</p>
<ul>
<li>The <em>socket_descriptor</em> points to a socket with an address family of
<samp>AF_INET</samp>, and the address specified in the
<strong>sockaddr_in</strong> structure (pointed to by <em>local_address</em>)
has already been assigned to another socket.</li>
<li> The
<em>socket_descriptor</em> points to a socket with an address family of
<samp>AF_INET6</samp>, and the address specified in the
<strong>sockaddr_in6</strong> structure (pointed to by <em>local_address</em>)
has already been assigned to another socket.</li>
<li>The <em>socket_descriptor</em> points to a socket with an address family of
<samp>AF_UNIX</samp> or <samp>AF_UNIX_CCSID</samp>, and the address specified
in the <strong>sockaddr_un</strong> or <strong>sockaddr_unc</strong> structure
(pointed to by <em>local_address</em>) has already been assigned to another
socket.<br>
</li>
</ul>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>[EADDRNOTAVAIL]</em></td>
<td align="left" valign="top">Address not available. This error code indicates
one of the following:<br>
<br>
<ul>
<li>The <em>socket_descriptor</em> points to a socket with an address family of
<samp>AF_INET</samp>, and the IP address specified in the
<strong>sockaddr_in</strong> structure (pointed to by <em>local_address</em>)
is not one defined by the local interfaces.</li>
<li> The
<em>socket_descriptor</em> points to a socket with an address family of
<samp>AF_INET6</samp>, and the IP address specified in the
<strong>sockaddr_in6</strong> structure (pointed to by <em>local_address</em>)
is not one defined by the local interfaces.<br>
</li>
</ul>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>[EAFNOSUPPORT]</em></td>
<td align="left" valign="top">The type of socket is not supported in this
protocol family.
<p>The address family specified in the address structure pointed to by
<em>local_address</em> parameter cannot be used with the socket pointed to by
the <em>socket_descriptor</em> parameter.</p>
</td>
</tr>
<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"><img src="delta.gif" alt="Start of change">
<em>[EDESTADDRREQ]</em></td>
<td align="left" valign="top">The <em>local_address</em> parameter is a null pointer.<br>
<br>
<p>This error code is only returned on sockets that use the
<samp>AF_UNIX</samp> or <samp>AF_UNIX_CCSID</samp> address family.</p>
<img src="deltaend.gif" alt="End of change">
</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> parameter.</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 length that is negative
or is not valid for the address family.<br>
<br>
</li>
<li>The socket referenced by <em>socket_descriptor</em> is not a socket of type
SOCK_RAW and is already bound to an address.<br>
<br>
</li>
<li>The local address pointed to by the <em>local_address</em> parameter
specified an address that was not valid.<br>
<br>
</li>
<li>The <em>socket_descriptor</em> points to a socket with an address family of
<samp>AF_UNIX_CCSID</samp>, and the CCSID specified in <em>sunc_qlg</em> in the
<strong>sockaddr_unc</strong> structure (pointed to by <em>local_address</em>)
cannot be converted to the current default CCSID for integrated file system
path names.<br>
<br>
</li>
<li>The <em>socket_descriptor</em> points to a socket with an address family of
<samp>AF_UNIX_CCSID</samp>, and there was an incomplete character or shift
state sequence at the end of <em>sunc_path</em> in the
<strong>sockaddr_unc</strong> structure (pointed to by
<em>local_address</em>).<br>
<br>
</li>
<li>The <em>socket_descriptor</em> points to a socket with an address family of
<samp>AF_UNIX_CCSID</samp>, and the <strong>sockaddr_unc</strong> structure
(pointed to by <em>local_address</em>) was not valid:<br>
<br>
<ul>
<li>The sunc_format was not set to SO_UNC_DEFAULT or SO_UNC_USE_QLG.<br>
<br>
</li>
<li>The sunc_zero was not initialized to zeros.<br>
<br>
</li>
<li>The sunc_format field was set to SO_UNC_USE_QLG and the sunc_qlg structure
was not valid:<br>
<br>
<ul>
<li>The path type was less than 0 or greater than 3.<br>
<br>
</li>
<li>The path length was less than 0 or out of bounds. For example, a
single-byte path name was greater than 126 bytes or a double-byte path name was
greater than 252 bytes.<br>
<br>
</li>
<li>A reserved field was not initialized to zeros.</li>
</ul>
</li>
</ul>
</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>[ELOOP]</em></td>
<td align="left" valign="top">A loop exists in symbolic links encountered
during pathname resolution.
<p>This error code is only returned on sockets that use the
<samp>AF_UNIX</samp> or <samp>AF_UNIX_CCSID</samp> address family.</p>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>[ENAMETOOLONG]</em></td>
<td align="left" valign="top">File name too long.
<p>This error code is only returned on sockets that use the
<samp>AF_UNIX</samp> or <samp>AF_UNIX_CCSID</samp> address family.</p>
</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>[ENOENT]</em></td>
<td align="left" valign="top">No such file or directory.
<p>This error code is only returned on sockets that use the
<samp>AF_UNIX</samp> or <samp>AF_UNIX_CCSID</samp> address family.</p>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>[ENOSYS]</em></td>
<td align="left" valign="top">Function not implemented.
<p>This error code is only returned on sockets that use the
<samp>AF_UNIX</samp> or <samp>AF_UNIX_CCSID</samp> or
<samp>AF_UNIX_CCSID</samp> address family.</p>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>[ENOTDIR]</em></td>
<td align="left" valign="top">Not a directory.
<p>This error code is only returned on sockets that use the
<samp>AF_UNIX</samp> or <samp>AF_UNIX_CCSID</samp> address family.</p>
</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>For sockets that use an address family of <samp>AF_UNIX</samp> or
<samp>AF_UNIX_CCSID</samp>, the following is applicable:<br>
<br>
<ul>
<li>The process must have the following types of permission:
<ul>
<li>Create permission to the directory in which the entry is to be
created.</li>
<li>Search permission along all the components of the path.</li>
</ul>
<p>Also, processes trying to establish a connection with the <em>connect()</em>
must have write access to the entry that is created.</p>
</li>
<li>For <samp>AF_UNIX</samp>, the path name is assumed to be in the default
coded character set identifier (CCSID) currently in effect for the job. For
<samp>AF_UNIX_CCSID</samp>, the path name is assumed to be in the format and
CCSID specified in the <strong>sockaddr_unc</strong> (pointed to by
<em>local_address</em>).<br>
<br>
</li>
<li>When the socket is no longer needed, the caller should remove the file
system entry that was created by the <em>bind()</em> using the
<em>unlink()</em> or Qp0lunlink() system function.</li>
</ul>
<br>
<br>
</li>
<li>For sockets that use an address family of <samp>AF_INET</samp>, the
following is applicable:<br>
<br>
<ul>
<li>The internet address structure <strong>sockaddr_in</strong> requires a
2-byte port number and a 32-bit IP address. You can have the system
automatically select a port number by setting the port number to 0.
<p> The BSD 4.3 structure
is:</p>
<pre>
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
</pre>
<p>The BSD 4.4/UNIX 98 compatible structure is:</p>
<pre>
typedef uchar sa_family_t;
struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
</pre>
<p>The BSD 4.4 <em>sin_len</em> field is the length of the address. The <em>sin_family</em> is the address
family (always AF_INET for TCP and UDP), <em>sin_port</em> is the port number,
and <em>sin_addr</em> is the internet address. The <em>sin_zero</em> field is
reserved and must be hex zeros.</p>
</li>
<li>A wildcard address is provided (<samp>INADDR_ANY</samp> defined in
<strong>&lt;netinet/in.h&gt;</strong>) that allows an application to receive
messages directed to a specified port independent of the IP address that was
specified. If a local IP address is specified, only data received on that IP
address is made available. <samp>INADDR_ANY</samp> must be used to receive data
from multiple local interface definitions.</li>
</ul>
<br>
<br>
</li>
<li> For sockets that use an
address family of <samp>AF_INET6</samp>, the following is applicable:<br>
<br>
<ul>
<li>The internet address structure <strong>sockaddr_in6</strong> requires a
2-byte port number and a 128-bit IP address. You can have the system
automatically select a port number by setting the port number to 0.
<p>The BSD 4.3 structure is:</p>
<pre>
typedef unsigned short sa_family_t;
typedef unsigned short in_port_t;
struct sockaddr_in6 {
sa_family_t sin6_family;
in_port_t sin6_port;
uint32_t sin6_flowinfo;
struct in6_addr sin6_addr;
uint32_t sin6_scope_id;
};
</pre>
<p>The BSD 4.4/UNIX 98 compatible structure is:</p>
<pre>
typedef uchar sa_family_t;
typedef unsigned short in_port_t;
struct sockaddr_in6 {
uint8_t sin6_len;
sa_family_t sin6_family;
in_port_t sin6_port;
uint32_t sin6_flowinfo;
struct in6_addr sin6_addr;
uint32_t sin6_scope_id;
};
</pre>
<p>The BSD 4.4 <em>sin6_len</em> field is the length of the address. The
<em>sin6_family</em> is the address family (AF_INET6 in this case),
<em>sin6_port</em> is the port number, and <em>sin6_addr</em> is the internet
address. The <em>sin6_flowinfo</em> field contains two pieces of information:
the traffic class and the flow label. Note: This field is currently not
supported and should be set to zero for upward compatibility. The
<em>sin6_scope_id</em> field identifies a set of interfaces as appropriate for
the scope of the address carried in the <em>sin6_addr</em> field. Note: This
field is currently not supported and should be set to zero for upward
compatibility.</p>
</li>
<li>A wildcard address is provided that allows an application to receive
messages directed to a specified port independent of the IP address that was
specified. Since the IPv6 address type is a structure (<samp>struct
in6_addr</samp>), a symbolic constant can be used to initialize an IPv6 address
variable, but cannot be used in an assignment. Therfore, the IPv6 wildcard
address is provided in two forms as defined in
<strong>&lt;netinet/in.h&gt;</strong>. The first version is a global variable
named <samp>in6addr_any</samp>. This version is used similarly to the way
applications use the <samp>INADDR_ANY</samp> in IPv4 as defined above and must
be used for structure assignment. The other version is a symbolic constant
named <samp>IN6ADDR_ANY_INIT</samp>. This version may be used to initialize an
<samp>in6_addr</samp> structure. If a local IP address is specified, only data
received on that IP address is made available. The wildcard address must be
used to receive data from multiple local interface definitions.</li>
</ul>
<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>bind()</em> API is mapped to
<em>qso_bind98()</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="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>