Loading...
Searching...
No Matches
su.h
Go to the documentation of this file.
/*
* This file is part of the Sofia-SIP package
*
* Copyright (C) 2005,2006,2007 Nokia Corporation.
*
* Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#ifndef SU_H
#define SU_H
/* ---------------------------------------------------------------------- */
/* Includes */
#ifndef SU_CONFIG_H
#include <sofia-sip/su_config.h>
#endif
#ifndef SU_TYPES_H
#include <sofia-sip/su_types.h>
#endif
#ifndef SU_ERRNO_H
#include <sofia-sip/su_errno.h>
#endif
#include <stdio.h>
#include <limits.h>
#if SU_HAVE_BSDSOCK /* Unix-compatible includes */
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif
#if SU_HAVE_WINSOCK /* Windows includes */
# include <winsock2.h>
# include <ws2tcpip.h>
# if SU_HAVE_IN6
# if defined(IPPROTO_IPV6) || (_WIN32_WINNT >= 0x0501)
/* case 1: IPv6 defined in winsock2.h/ws2tcpip.h */
# else
/* case 2: try to use "IPv6 Tech Preview" */
# include <tpipv6.h>
# endif
# endif
#endif
SOFIA_BEGIN_DECLS
/* ---------------------------------------------------------------------- */
/* Constant definitions */
#if SU_HAVE_BSDSOCK || DOCUMENTATION_ONLY
enum {
INVALID_SOCKET = -1,
#define INVALID_SOCKET ((su_socket_t)INVALID_SOCKET)
SOCKET_ERROR = -1,
#define SOCKET_ERROR SOCKET_ERROR
su_success = 0,
su_failure = -1
};
#if SYMBIAN && !defined(MSG_NOSIGNAL)
#define MSG_NOSIGNAL (0)
#endif
#elif SU_HAVE_WINSOCK
enum {
su_success = 0,
su_failure = 0xffffffffUL
};
#define MSG_NOSIGNAL (0)
#endif
#define SU_MAXHOST (1025)
#define SU_MAXSERV (25)
#define SU_ADDRSIZE (48)
#define SU_SERVSIZE (16)
#define SU_SUCCESS su_success
#define SU_FAILURE su_failure
/* ---------------------------------------------------------------------- */
/* Type definitions */
#if SU_HAVE_BSDSOCK || DOCUMENTATION_ONLY
typedef int su_socket_t;
#elif SU_HAVE_WINSOCK
typedef SOCKET su_socket_t;
#endif
#if !SU_HAVE_SOCKADDR_STORAGE
/*
* RFC 2553: protocol-independent placeholder for socket addresses
*/
#define _SS_MAXSIZE 128
#define _SS_ALIGNSIZE (sizeof(int64_t))
#define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(u_char) * 2)
#define _SS_PAD2SIZE (_SS_MAXSIZE - sizeof(u_char) * 2 - \
_SS_PAD1SIZE - _SS_ALIGNSIZE)
struct sockaddr_storage {
#if SU_HAVE_SOCKADDR_SA_LEN
unsigned char ss_len; /* address length */
unsigned char ss_family; /* address family */
#else
unsigned short ss_family; /* address family */
#endif
char __ss_pad1[_SS_PAD1SIZE];
int64_t __ss_align; /* force desired structure storage alignment */
char __ss_pad2[_SS_PAD2SIZE];
};
#endif
union su_sockaddr_u {
#ifdef DOCUMENTATION_ONLY
uint8_t su_len;
uint8_t su_family;
uint16_t su_port;
#else
short su_dummy;
#if SU_HAVE_SOCKADDR_SA_LEN
#define su_len su_sa.sa_len
#else
#define su_len su_array[0]
#endif
#define su_family su_sa.sa_family
#define su_port su_sin.sin_port
#endif
char su_array[32];
uint16_t su_array16[16];
uint32_t su_array32[8];
struct sockaddr su_sa;
struct sockaddr_in su_sin;
#if SU_HAVE_IN6
struct sockaddr_in6 su_sin6;
#endif
#ifdef DOCUMENTATION_ONLY
uint32_t su_scope_id;
#else
#define su_scope_id su_array32[6]
#endif
};
typedef union su_sockaddr_u su_sockaddr_t;
#if SU_HAVE_BSDSOCK || DOCUMENTATION_ONLY
typedef size_t su_ioveclen_t;
typedef struct su_iovec_s {
void *siv_base;
su_ioveclen_t siv_len;
} su_iovec_t;
#define SU_IOVECLEN_MAX SIZE_MAX
#endif
#if SU_HAVE_WINSOCK
typedef u_long su_ioveclen_t;
/* This is same as WSABUF */
typedef struct su_iovec_s {
su_ioveclen_t siv_len;
void *siv_base;
} su_iovec_t;
#define SU_IOVECLEN_MAX ULONG_MAX
#endif
/* ---------------------------------------------------------------------- */
/* Socket compatibility functions */
SOFIAPUBFUN int su_init(void);
SOFIAPUBFUN void su_deinit(void);
SOFIAPUBFUN su_socket_t su_socket(int af, int sock, int proto);
SOFIAPUBFUN int su_close(su_socket_t s);
SOFIAPUBFUN int su_ioctl(su_socket_t s, int request, ...);
SOFIAPUBFUN int su_is_blocking(int errcode);
SOFIAPUBFUN int su_setblocking(su_socket_t s, int blocking);
SOFIAPUBFUN int su_setreuseaddr(su_socket_t s, int reuse);
SOFIAPUBFUN int su_soerror(su_socket_t s);
SOFIAPUBFUN int su_getsocktype(su_socket_t s);
SOFIAPUBFUN issize_t su_getmsgsize(su_socket_t s);
SOFIAPUBFUN
issize_t su_vsend(su_socket_t, su_iovec_t const iov[], isize_t len, int flags,
su_sockaddr_t const *su, socklen_t sulen);
SOFIAPUBFUN
issize_t su_vrecv(su_socket_t, su_iovec_t iov[], isize_t len, int flags,
su_sockaddr_t *su, socklen_t *sulen);
SOFIAPUBFUN int su_getlocalip(su_sockaddr_t *sin);
#if SU_HAVE_BSDSOCK
#define su_ioctl ioctl
/*
* Note: before 1.12.2, there was su_isblocking() which did not take argument
* and which was missing from WINSOCK
*/
#define su_is_blocking(e) \
((e) == EINPROGRESS || (e) == EAGAIN || (e) == EWOULDBLOCK)
#endif
#if SU_HAVE_WINSOCK
SOFIAPUBFUN int su_inet_pton(int af, char const *src, void *dst);
SOFIAPUBFUN const char *su_inet_ntop(int af, void const *src,
char *dst, size_t size);
SOFIAPUBFUN ssize_t
su_send(su_socket_t s, void *buffer, size_t length, int flags),
su_sendto(su_socket_t s, void *buffer, size_t length, int flags,
su_sockaddr_t const *to, socklen_t tolen),
su_recv(su_socket_t s, void *buffer, size_t length, int flags),
su_recvfrom(su_socket_t s, void *buffer, size_t length, int flags,
su_sockaddr_t *from, socklen_t *fromlen);
static __inline
uint16_t su_ntohs(uint16_t s)
{
return (uint16_t)(((s & 255) << 8) | ((s & 0xff00) >> 8));
}
static __inline
uint32_t su_ntohl(uint32_t l)
{
return ((l & 0xff) << 24) | ((l & 0xff00) << 8)
| ((l & 0xff0000) >> 8) | ((l & 0xff000000U) >> 24);
}
#define ntohs su_ntohs
#define htons su_ntohs
#define ntohl su_ntohl
#define htonl su_ntohl
#else
#define su_inet_pton inet_pton
#define su_inet_ntop inet_ntop
#define su_send(s,b,l,f) send((s),(b),(l),(f))
#define su_sendto(s,b,l,f,a,L) sendto((s),(b),(l),(f),(void const*)(a),(L))
#define su_recv(s,b,l,f) recv((s),(b),(l),(f))
#define su_recvfrom(s,b,l,f,a,L) recvfrom((s),(b),(l),(f),(void *)(a),(L))
#endif
/* ---------------------------------------------------------------------- */
/* Other compatibility stuff */
#if SU_HAVE_WINSOCK
#define getuid() (0x505)
#endif
#ifndef IPPROTO_SCTP
#define IPPROTO_SCTP (132)
#endif
/* ---------------------------------------------------------------------- */
/* Address manipulation macros */
#if SU_HAVE_IN6
#define SU_ADDR(su) \
((su)->su_family == AF_INET ? (void *)&(su)->su_sin.sin_addr : \
((su)->su_family == AF_INET6 ? (void *)&(su)->su_sin6.sin6_addr : \
(void *)&(su)->su_sa.sa_data))
#else
#define SU_ADDR(su) \
((su)->su_family == AF_INET ? (void *)&(su)->su_sin.sin_addr : \
(void *)&(su)->su_sa.sa_data)
#endif
#if SU_HAVE_IN6
#define SU_ADDRLEN(su) \
((su)->su_family == AF_INET \
? (socklen_t)sizeof((su)->su_sin.sin_addr) : \
((su)->su_family == AF_INET6 \
? (socklen_t)sizeof((su)->su_sin6.sin6_addr) \
: (socklen_t)sizeof((su)->su_sa.sa_data)))
#else
#define SU_ADDRLEN(su) \
((su)->su_family == AF_INET \
? (socklen_t)sizeof((su)->su_sin.sin_addr) \
: (socklen_t)sizeof((su)->su_sa.sa_data))
#endif
#if SU_HAVE_IN6
#define SU_HAS_INADDR_ANY(su) \
((su)->su_family == AF_INET \
? ((su)->su_sin.sin_addr.s_addr == INADDR_ANY) \
: ((su)->su_family == AF_INET6 \
? (memcmp(&(su)->su_sin6.sin6_addr, su_in6addr_any(), \
sizeof(*su_in6addr_any())) == 0) : 0))
#else
#define SU_HAS_INADDR_ANY(su) \
((su)->su_family == AF_INET \
? ((su)->su_sin.sin_addr.s_addr == INADDR_ANY) : 0)
#endif
#define SU_SOCKADDR_INADDR_ANY(su) SU_HAS_INADDR_ANY(su)
#if SU_HAVE_IN6
#define SU_SOCKADDR_SIZE(su) \
((socklen_t)((su)->su_family == AF_INET ? sizeof((su)->su_sin) \
: ((su)->su_family == AF_INET6 ? sizeof((su)->su_sin6) \
: sizeof(*su))))
#else
#define SU_SOCKADDR_SIZE(su) \
((socklen_t)((su)->su_family == AF_INET ? sizeof((su)->su_sin) \
: sizeof(*su)))
#endif
#define su_sockaddr_size SU_SOCKADDR_SIZE
#if SU_HAVE_IN6
#if SU_HAVE_BSDSOCK
#define su_in6addr_any() (&in6addr_any)
#define su_in6addr_loopback() (&in6addr_loopback)
#define SU_IN6ADDR_ANY_INIT IN6ADDR_ANY_INIT
#define SU_IN6ADDR_LOOPBACK_INIT IN6ADDR_LOOPBACK_INIT
#endif
#if SU_HAVE_WINSOCK || DOCUMENTATION_ONLY
SOFIAPUBVAR const struct in_addr6 *su_in6addr_any(void);
SOFIAPUBVAR const struct in_addr6 *su_in6addr_loopback(void);
#define SU_IN6ADDR_ANY_INIT { 0 }
#define SU_IN6ADDR_LOOPBACK_INIT { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1 }
#endif
#endif /* SU_HAVE_IN6 */
#define SU_IN6_IS_ADDR_V4MAPPED(a) \
(((uint32_t const *) (a))[0] == 0 && \
((uint32_t const *) (a))[1] == 0 && \
((uint32_t const *) (a))[2] == htonl(0xffff))
#define SU_IN6_IS_ADDR_V4COMPAT(a) \
(((uint32_t const *)(a))[0] == 0 && \
((uint32_t const *)(a))[1] == 0 && \
((uint32_t const *)(a))[2] == 0 && \
((uint32_t const *)(a))[3] != htonl(1) && \
((uint32_t const *)(a))[3] != htonl(0))
SOFIAPUBFUN int su_cmp_sockaddr(su_sockaddr_t const *a,
su_sockaddr_t const *b);
SOFIAPUBFUN int su_match_sockaddr(su_sockaddr_t const *a,
su_sockaddr_t const *b);
SOFIAPUBFUN void su_canonize_sockaddr(su_sockaddr_t *su);
#if SU_HAVE_IN6
#define SU_CANONIZE_SOCKADDR(su) \
((su)->su_family == AF_INET6 ? su_canonize_sockaddr(su) : (void)0)
#else
#define SU_CANONIZE_SOCKADDR(su) \
((void)0)
#endif
SOFIA_END_DECLS
#ifndef SU_ADDRINFO_H
#include <sofia-sip/su_addrinfo.h>
#endif
#endif /* !defined(SU_H) */
su_iovec_s
I/O vector for scatter-gather I/O.
Definition su.h:239
su_iovec_s::siv_base
void * siv_base
Pointer to buffer.
Definition su.h:240
su_iovec_s::siv_len
su_ioveclen_t siv_len
Size of buffer.
Definition su.h:241
su_canonize_sockaddr
void su_canonize_sockaddr(su_sockaddr_t *su)
Convert mapped/compat address to IPv4 address.
Definition su.c:659
su_ioveclen_t
size_t su_ioveclen_t
Type of siv_len field in su_iovec_t.
Definition su.h:201
su_success
@ su_success
Return code for a successful call.
Definition su.h:93
su_failure
@ su_failure
Return code for an unsuccessful call.
Definition su.h:95
su_deinit
void su_deinit(void)
Deinitialize socket implementation.
Definition su.c:243
su_socket_t
int su_socket_t
Socket descriptor type.
Definition su.h:128
su_close
int su_close(su_socket_t s)
Close an socket descriptor.
Definition su.c:251
su_setblocking
int su_setblocking(su_socket_t s, int blocking)
Set/reset blocking option.
Definition su.c:256
su_setreuseaddr
int su_setreuseaddr(su_socket_t s, int reuse)
Set/reset address reusing option.
Definition su.c:392
su_match_sockaddr
int su_match_sockaddr(su_sockaddr_t const *a, su_sockaddr_t const *b)
Check if socket address b match with a.
Definition su.c:622
su_ioctl
int su_ioctl(su_socket_t s, int request,...)
Control socket.
su_iovec_t
struct su_iovec_s su_iovec_t
I/O vector for scatter-gather I/O.
su_cmp_sockaddr
int su_cmp_sockaddr(su_sockaddr_t const *a, su_sockaddr_t const *b)
Compare two socket addresses.
Definition su.c:586
su_getsocktype
int su_getsocktype(su_socket_t s)
Get the socket type.
Definition su.c:381
su_vsend
issize_t su_vsend(su_socket_t, su_iovec_t const iov[], isize_t len, int flags, su_sockaddr_t const *su, socklen_t sulen)
Scatter-gather send.
Definition su.c:496
su_getmsgsize
issize_t su_getmsgsize(su_socket_t s)
Get size of message available in socket.
Definition su.c:420
su_soerror
int su_soerror(su_socket_t s)
Get the error code associated with the socket.
Definition su.c:371
su_socket
su_socket_t su_socket(int af, int sock, int proto)
Create an endpoint for communication.
Definition su.c:84
su_vrecv
issize_t su_vrecv(su_socket_t, su_iovec_t iov[], isize_t len, int flags, su_sockaddr_t *su, socklen_t *sulen)
Scatter-gather receive.
Definition su.c:520
su_is_blocking
int su_is_blocking(int errcode)
Check for in-progress error codes.
su_init
int su_init(void)
Initialize socket implementation.
Definition su.c:217
su_getlocalip
int su_getlocalip(su_sockaddr_t *sin)
Return local IP address.
Definition su_localinfo.c:1672
su_addrinfo.h
Network address and service translation.
su_config.h
su library configuration
SOFIAPUBFUN
#define SOFIAPUBFUN
SOFIAPUBFUN declares an exported function.
Definition su_config.h:66
SOFIAPUBVAR
#define SOFIAPUBVAR
SOFIAPUBVAR declares an exported variable.
Definition su_config.h:68
su_errno.h
Errno handling.
su_types.h
Basic integer types for su library.
uint32_t
SU_U32_T uint32_t
32-bit unsigned integer
Definition su_types.h:87
uint16_t
SU_U16_T uint16_t
16-bit unsigned integer
Definition su_types.h:91
int64_t
SU_S64_T int64_t
64-bit signed integer
Definition su_types.h:85
uint8_t
SU_U8_T uint8_t
8-bit unsigned integer
Definition su_types.h:95
su_sockaddr_u
Common socket address structure.
Definition su.h:157
su_sockaddr_u::su_sin
struct sockaddr_in su_sin
Address in IPv4 format.
Definition su.h:177
su_sockaddr_u::su_array
char su_array[32]
Presented as chars.
Definition su.h:173
su_sockaddr_u::su_sa
struct sockaddr su_sa
Address in struct sockaddr format.
Definition su.h:176
su_sockaddr_u::su_port
uint16_t su_port
Port number.
Definition su.h:161
su_sockaddr_u::su_family
uint8_t su_family
Address family.
Definition su.h:160
su_sockaddr_u::su_array16
uint16_t su_array16[16]
Presented as 16-bit ints.
Definition su.h:174
su_sockaddr_u::su_len
uint8_t su_len
Length of structure.
Definition su.h:159
su_sockaddr_u::su_array32
uint32_t su_array32[8]
Presented as 32-bit ints.
Definition su.h:175
su_sockaddr_u::su_scope_id
uint32_t su_scope_id
Scope ID.
Definition su.h:182

Sofia-SIP 1.12.11devel - Copyright (C) 2006 Nokia Corporation. All rights reserved. Licensed under the terms of the GNU Lesser General Public License.