su.h 13.4 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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
 *
 */

Pekka Pessi's avatar
Pekka Pessi committed
25 26
#ifndef SU_H
/** Defined when <sofia-sip/su.h> has been included. */
Pekka Pessi's avatar
Pekka Pessi committed
27
#define SU_H
Pekka Pessi's avatar
Pekka Pessi committed
28
/**@ingroup su_socket 
Pekka Pessi's avatar
Pekka Pessi committed
29
 * @file sofia-sip/su.h Socket and network address interface
Pekka Pessi's avatar
Pekka Pessi committed
30 31 32 33 34 35 36 37 38 39
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * 
 * @date Created: Thu Mar 18 19:40:51 1999 pessi
 */

/* ---------------------------------------------------------------------- */
/* Includes */

#ifndef SU_CONFIG_H
40
#include "sofia-sip/su_config.h"
Pekka Pessi's avatar
Pekka Pessi committed
41 42
#endif
#ifndef SU_TYPES_H
43
#include "sofia-sip/su_types.h"
Pekka Pessi's avatar
Pekka Pessi committed
44
#endif
Pekka Pessi's avatar
Pekka Pessi committed
45
#ifndef SU_ERRNO_H
46
#include <sofia-sip/su_errno.h>
Pekka Pessi's avatar
Pekka Pessi committed
47
#endif
Pekka Pessi's avatar
Pekka Pessi committed
48 49 50

#include <stdio.h>

51 52
SOFIA_BEGIN_DECLS

Pekka Pessi's avatar
Pekka Pessi committed
53 54 55
#if SU_HAVE_BSDSOCK		/* Unix-compatible includes */
#include <errno.h>
#include <unistd.h>
56
#include <limits.h>
Pekka Pessi's avatar
Pekka Pessi committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

#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>

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

90 91 92 93
#  if defined(IPPROTO_IPV6)
/* IPv6 defined in ws2tcpip.h */
#  elif SU_HAVE_IN6 
#    include <tpipv6.h>		/* From "IPv6 Tech Preview"  */
Pekka Pessi's avatar
Pekka Pessi committed
94
#  else
95
#    error Winsock with IPv6 support required
Pekka Pessi's avatar
Pekka Pessi committed
96
#  endif
Pekka Pessi's avatar
Pekka Pessi committed
97

98 99
#include <limits.h>

Pekka Pessi's avatar
Pekka Pessi committed
100 101 102 103 104 105 106
#endif

/* ---------------------------------------------------------------------- */
/* Constant definitions */

#if SU_HAVE_BSDSOCK || DOCUMENTATION_ONLY
enum {
107
  /** Invalid socket descriptor, error from socket() or accept() */ 
Pekka Pessi's avatar
Pekka Pessi committed
108
  INVALID_SOCKET = -1,
109 110
#define INVALID_SOCKET ((su_socket_t)INVALID_SOCKET)
  /** Error from other socket calls */
Pekka Pessi's avatar
Pekka Pessi committed
111
  SOCKET_ERROR = -1,
112
#define SOCKET_ERROR SOCKET_ERROR
Pekka Pessi's avatar
Pekka Pessi committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  /** Return code for a successful call */
  su_success = 0, 
  /** Return code for an unsuccessful call */
  su_failure = -1
};
#elif SU_HAVE_WINSOCK
enum { 
  su_success = 0, 
  su_failure = 0xffffffffUL 
};

#define MSG_NOSIGNAL (0)

#endif

/**@HI Maximum size of host name. */
#define SU_MAXHOST (1025)
/**@HI Maximum size of service name. */
#define SU_MAXSERV (25)

/**@HI Maximum size of address in text format. */
#define SU_ADDRSIZE (48)
/**@HI Maximum size of port number in text format. */
#define SU_SERVSIZE (16)

#define SU_SUCCESS su_success
#define SU_FAILURE su_failure

/* ---------------------------------------------------------------------- */
/* Type definitions */

/** Socket descriptor type. */
#if SU_HAVE_BSDSOCK || DOCUMENTATION_ONLY
typedef int su_socket_t;
#elif SU_HAVE_WINSOCK
typedef SOCKET su_socket_t;
#endif

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
#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

Pekka Pessi's avatar
Pekka Pessi committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
/** Common socket address structure. */
union su_sockaddr_u {
#ifdef DOCUMENTATION_ONLY
  uint8_t             su_len;         /**< Length of structure */
  uint8_t             su_family;      /**< Address family. */
  uint16_t            su_port;        /**< Port number. */
#else
  short               su_dummy;	      /**< Dummy member to initialize */
#if SU_HAVE_SOCKADDR_SA_LEN
#define               su_len          su_sa.sa_len
#define               su_family       su_sa.sa_family
#else
#define               su_len          su_array[0]
  short               su_family;
#endif
#define               su_port         su_sin.sin_port  
#endif

  char                su_array[32];   /**< Presented as chars */
  uint16_t            su_array16[16]; /**< Presented as 16-bit ints */
  uint32_t            su_array32[8];  /**< Presented as 32-bit ints */
  struct sockaddr     su_sa;          /**< Address in struct sockaddr format */
  struct sockaddr_in  su_sin;         /**< Address in IPv4 format */
#if SU_HAVE_IN6
  struct sockaddr_in6 su_sin6;        /**< Address in IPv6 format */
#endif
#ifdef DOCUMENTATION_ONLY
  uint32_t            su_scope_id;    /**< 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
210 211 212 213 214 215 216 217 218 219 220 221
/**Type of @a siv_len field in #su_iovec_t.
 *
 * The @a siv_len field in #su_iovec_t has different types in with POSIX
 * (size_t) and WINSOCK2 (u_long). Truncate the iovec element size to
 * #SU_IOVECLEN_MAX, if needed, and cast using #su_ioveclen_t.
 *
 * @sa #su_iovec_t, #SU_IOVECLEN_MAX
 *
 * @since New in @VERSION_1_12_2.
 */
typedef size_t su_ioveclen_t;

222 223 224
/** I/O vector for scatter-gather I/O.
 *
 * This is the I/O vector element used with su_vsend() and su_vrecv(). It is
225 226 227 228 229 230 231 232 233
 * defined like struct iovec with POSIX sockets:
 * @code
 * struct iovec {
 *    void *iov_base;	// Pointer to data.
 *    size_t iov_len;	// Length of data.
 * };
 * @endcode
 *
 * When using WINSOCK sockets it is defined as
234
 * <a href="http://msdn.microsoft.com/library/en-us/winsock/winsock/wsabuf_2.asp">
235 236 237 238 239 240 241
 * WSABUF</a>:
 * @code
 * typedef struct __WSABUF {
 *   u_long len;
 *   char FAR* buf;
 * } WSABUF, *LPWSABUF;
 * @endcode
242 243 244 245
 *
 * @note Ordering of the fields is reversed on Windows. Do not initialize this
 * structure with static initializer, but assign both fields separately.
 *
246 247 248
 * For historical reasons, the structure is known as #msg_iovec_t in @msg
 * module.
 *
249 250 251
 * @sa #su_iovec_t, #su_ioveclen_t, SU_IOVECLEN_MAX, su_vsend(), su_vrecv(), 
 * #msg_iovec_t, msg_iovec(), msg_recv_iovec(), 
 * @c struct @c iovec defined in <sys/uio.h>, writev(2), readv(2),
252
 * sendmsg(), recvmsg(),
253 254
 * <a href="http://msdn.microsoft.com/library/en-us/winsock/winsock/wsabuf_2.asp">
 * WSABUF of WinSock2</a>
Pekka Pessi's avatar
Pekka Pessi committed
255
 */
256
typedef struct su_iovec_s {
Pekka Pessi's avatar
Pekka Pessi committed
257
  void  *siv_base;		/**< Pointer to buffer. */
258
  su_ioveclen_t siv_len;		/**< Size of buffer.  */
259 260 261 262 263 264 265 266
} su_iovec_t;

/** Maximum size of buffer in a single su_iovec_t element. 
 * @sa #su_ioveclen_t, #su_iovec_t
 *
 * @since New in @VERSION_1_12_2.
 * @HIDE
 */
267
#define SU_IOVECLEN_MAX SIZE_MAX
Pekka Pessi's avatar
Pekka Pessi committed
268 269 270
#endif

#if SU_HAVE_WINSOCK
271 272
typedef u_long su_ioveclen_t;

273
/* This is same as WSABUF */
274
typedef struct su_iovec_s {
275
  su_ioveclen_t  siv_len;
276
  void   *siv_base;
277
} su_iovec_t;
278

279
#define SU_IOVECLEN_MAX ULONG_MAX
Pekka Pessi's avatar
Pekka Pessi committed
280 281 282 283 284
#endif

/* ---------------------------------------------------------------------- */
/* Socket compatibility functions */

285 286
SOFIAPUBFUN int su_init(void);
SOFIAPUBFUN void su_deinit(void);
Pekka Pessi's avatar
Pekka Pessi committed
287 288

/** Create an endpoint for communication. */
289
SOFIAPUBFUN su_socket_t su_socket(int af, int sock, int proto);
Pekka Pessi's avatar
Pekka Pessi committed
290
/** Close an socket descriptor. */
291
SOFIAPUBFUN int su_close(su_socket_t s);
Pekka Pessi's avatar
Pekka Pessi committed
292
/** Control socket. */
293
SOFIAPUBFUN int su_ioctl(su_socket_t s, int request, ...);
Pekka Pessi's avatar
Pekka Pessi committed
294

295 296 297
/** Checks if the @a errcode indicates that the socket call failed because
 * it would have blocked.
 *
298 299 300
 * Defined as macro with POSIX sockets.
 *
 * @since New in @VERSION_1_12_2.
301 302 303
 */
SOFIAPUBFUN int su_is_blocking(int errcode);

Pekka Pessi's avatar
Pekka Pessi committed
304
/** Set/reset blocking option. */ 
305
SOFIAPUBFUN int su_setblocking(su_socket_t s, int blocking);
Pekka Pessi's avatar
Pekka Pessi committed
306
/** Set/reset address reusing option. */
307
SOFIAPUBFUN int su_setreuseaddr(su_socket_t s, int reuse);
Pekka Pessi's avatar
Pekka Pessi committed
308
/** Get the error code associated with the socket. */
309
SOFIAPUBFUN int su_soerror(su_socket_t s);
Pekka Pessi's avatar
Pekka Pessi committed
310
/** Get size of message available in socket. */
311
SOFIAPUBFUN issize_t su_getmsgsize(su_socket_t s);
Pekka Pessi's avatar
Pekka Pessi committed
312 313

/** Scatter-gather send. */
314
SOFIAPUBFUN
315 316
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);
Pekka Pessi's avatar
Pekka Pessi committed
317
/** Scatter-gather receive. */
318
SOFIAPUBFUN
319 320
issize_t su_vrecv(su_socket_t, su_iovec_t iov[], isize_t len, int flags,
		  su_sockaddr_t *su, socklen_t *sulen);
Pekka Pessi's avatar
Pekka Pessi committed
321
/** Return local IP address */
322
SOFIAPUBFUN int su_getlocalip(su_sockaddr_t *sin);
Pekka Pessi's avatar
Pekka Pessi committed
323

324
#include <sofia-sip/su_addrinfo.h>
Pekka Pessi's avatar
Pekka Pessi committed
325 326 327

#if SU_HAVE_BSDSOCK
#define su_ioctl  ioctl
328 329 330 331 332 333
/*
 * 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)
Pekka Pessi's avatar
Pekka Pessi committed
334 335 336
#endif

#if SU_HAVE_WINSOCK
337 338 339
SOFIAPUBFUN int inet_pton(int af, char const *src, void *dst);
SOFIAPUBFUN const char *inet_ntop(int af, void const *src,
				  char *dst, size_t size);
340 341 342 343
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),
Michael Jerris's avatar
Michael Jerris committed
344 345
  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,
346
	      su_sockaddr_t *from, socklen_t *fromlen);
347 348 349 350 351
#else
#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))
Pekka Pessi's avatar
Pekka Pessi committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
#endif

/* ---------------------------------------------------------------------- */
/* Other compatibility stuff */

#if SU_HAVE_WINSOCK
#define getuid() (0x505)
#endif

#ifndef IPPROTO_SCTP
#define IPPROTO_SCTP (132)
#endif

/* ---------------------------------------------------------------------- */
/* Address manipulation macros */

/**@HI Get pointer to address field.
 *
 * The macro SU_ADDR() returns pointer to the address field (sin_data,
 * sin_addr or sin_addr6, depending on the address family).
 */
#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

Pekka Pessi's avatar
Pekka Pessi committed
384 385 386 387 388 389
/**@HI Get length of address field.
 *
 * The macro SU_ADDRLEN() returns length of the address field (sin_data,
 * sin_addr or sin_addr6, depending on the address family).
 */
#if SU_HAVE_IN6
390 391 392 393 394 395
#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)))
Pekka Pessi's avatar
Pekka Pessi committed
396
#else
397 398 399 400
#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))
Pekka Pessi's avatar
Pekka Pessi committed
401 402
#endif

Pekka Pessi's avatar
Pekka Pessi committed
403 404
/**@HI Test if su_sockaddr_t is INADDR_ANY or IN6ADDR_ANY. */
#if SU_HAVE_IN6
405
#define SU_HAS_INADDR_ANY(su) \
Pekka Pessi's avatar
Pekka Pessi committed
406 407 408 409 410 411
  ((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
412
#define SU_HAS_INADDR_ANY(su) \
Pekka Pessi's avatar
Pekka Pessi committed
413 414 415 416
  ((su)->su_family == AF_INET \
  ? ((su)->su_sin.sin_addr.s_addr == INADDR_ANY) : 0)
#endif

417 418
#define SU_SOCKADDR_INADDR_ANY(su) SU_HAS_INADDR_ANY(su)

Pekka Pessi's avatar
Pekka Pessi committed
419 420 421
/**@HI Calculate correct size of su_sockaddr_t structure. */ 
#if SU_HAVE_IN6
#define SU_SOCKADDR_SIZE(su) \
422 423 424
  ((socklen_t)((su)->su_family == AF_INET ? sizeof((su)->su_sin)	  \
	       : ((su)->su_family == AF_INET6 ? sizeof((su)->su_sin6)	\
		  : sizeof(*su))))
Pekka Pessi's avatar
Pekka Pessi committed
425 426
#else
#define SU_SOCKADDR_SIZE(su) \
427 428
  ((socklen_t)((su)->su_family == AF_INET ? sizeof((su)->su_sin)	\
	       : sizeof(*su)))
Pekka Pessi's avatar
Pekka Pessi committed
429 430 431 432 433 434 435 436 437 438 439
#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
Pekka Pessi's avatar
Pekka Pessi committed
440 441
SOFIAPUBVAR const struct in_addr6 *su_in6addr_any(void);
SOFIAPUBVAR const struct in_addr6 *su_in6addr_loopback(void);
Pekka Pessi's avatar
Pekka Pessi committed
442 443 444 445 446
#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 */

447 448 449 450 451
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);
Pekka Pessi's avatar
Pekka Pessi committed
452

453
#if SU_HAVE_IN6
Pekka Pessi's avatar
Pekka Pessi committed
454 455
#define SU_CANONIZE_SOCKADDR(su) \
  ((su)->su_family == AF_INET6 ? su_canonize_sockaddr(su) : (void)0)
456 457 458 459 460
#else
#define SU_CANONIZE_SOCKADDR(su) \
  ((void)0)
#endif

461
SOFIA_END_DECLS
Pekka Pessi's avatar
Pekka Pessi committed
462 463

#endif /* !defined(SU_H) */