su.h 9.82 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 25
 * 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 /** Defined when su.h has been included. */
Pekka Pessi's avatar
Pekka Pessi committed
26
#define SU_H
Pekka Pessi's avatar
Pekka Pessi committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/**@ingroup su_socket 
 * @file su.h Socket and network address interface
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * 
 * @date Created: Thu Mar 18 19:40:51 1999 pessi
 */

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

#ifndef SU_CONFIG_H
#include "su_config.h"
#endif
#ifndef SU_TYPES_H
#include "su_types.h"
#endif

#include <stdio.h>

#if SU_HAVE_BSDSOCK		/* Unix-compatible includes */
#include <errno.h>
#include <unistd.h>
50
#include <limits.h>
Pekka Pessi's avatar
Pekka Pessi committed
51 52 53 54 55 56 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

#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

84 85 86 87
#  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
88
#  else
89
#    error Winsock with IPv6 support required
Pekka Pessi's avatar
Pekka Pessi committed
90
#  endif
Pekka Pessi's avatar
Pekka Pessi committed
91

92 93
#include <limits.h>

Pekka Pessi's avatar
Pekka Pessi committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
#endif

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

#if SU_HAVE_BSDSOCK || DOCUMENTATION_ONLY
enum {
  /** Invalid socket descriptor */ 
  INVALID_SOCKET = -1,
  /** Error from su_socket() call */
  SOCKET_ERROR = -1,

  /** 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)

#define EAFNOSUPPORT WSAEAFNOSUPPORT
#define ECONNREFUSED WSAECONNREFUSED
#define EADDRINUSE   WSAEADDRINUSE
#define EWOULDBLOCK  WSAEWOULDBLOCK
#define EINPROGRESS  WSAEINPROGRESS
#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

/** 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
/** IO vector for su_vsend() and su_vrecv(). 
 * @note Ordering of the fields is reversed on Windows.
 */
struct su_iovec_s {
  void  *siv_base;		/**< Pointer to buffer. */
  size_t siv_len;		/**< Size of buffer.  */
};
#endif

#if SU_HAVE_WINSOCK
struct su_iovec_s {
  long  siv_len;
  void *siv_base;
};
#endif

/** I/O vector for scatter-gather I/O. */
typedef struct su_iovec_s   su_iovec_t;

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

int su_init(void);
void su_deinit(void);

/** Create an endpoint for communication. */
su_socket_t su_socket(int af, int sock, int proto);
/** Close an socket descriptor. */
int su_close(su_socket_t s);
/** Control socket. */
int su_ioctl(su_socket_t s, int request, ...);

/** Checks if the previous call failed because it would have blocked. */
int su_isblocking(void);
/** Set/reset blocking option. */ 
int su_setblocking(su_socket_t s, int blocking);
/** Set/reset address reusing option. */
int su_setreuseaddr(su_socket_t s, int reuse);
/** Get the error code associated with the socket. */
int su_soerror(su_socket_t s);
/** Get size of message available in socket. */
int su_getmsgsize(su_socket_t s);

/** Print the latest socket error message to stderr */
void su_perror(char const *s);
/** Print the socket error message to stderr. */
void su_perror2(char const *s, int errcode);
/** Return string describing socket error code. */
char const *su_strerror(int e);
/** The latest socket error. */
int su_errno(void);
/** Set the socket error. */
int su_seterrno(int);

/** Scatter-gather send. */
int su_vsend(su_socket_t s, su_iovec_t const iov[], int iovlen, int flags, 
             su_sockaddr_t const *su, socklen_t sulen);
/** Scatter-gather receive. */
int su_vrecv(su_socket_t s, su_iovec_t iov[], int iovlen, int flags, 
             su_sockaddr_t *su, socklen_t *sulen);
/** Return local IP address */
int su_getlocalip(su_sockaddr_t *sin);

248
#include <su_addrinfo.h>
Pekka Pessi's avatar
Pekka Pessi committed
249 250 251 252 253 254 255 256 257 258 259 260 261

#if SU_HAVE_BSDSOCK
#define su_socket socket
#define su_close  close
#define su_ioctl  ioctl
#define su_isblocking() (su_errno() == EAGAIN || su_errno() == EWOULDBLOCK)
#define su_errno() errno
#define su_seterrno(n) ((errno = (n)), -1)
#define su_perror  perror
#define su_strerror(e) strerror(e)
#endif

#if SU_HAVE_WINSOCK
262 263
int inet_pton(int af, char const *src, void *dst);
const char *inet_ntop(int af, void const *src, char *dst, size_t size);
Pekka Pessi's avatar
Pekka Pessi committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
#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

/**@HI Test if su_sockaddr_t is INADDR_ANY or IN6ADDR_ANY. */
#if SU_HAVE_IN6
298
#define SU_HAS_INADDR_ANY(su) \
Pekka Pessi's avatar
Pekka Pessi committed
299 300 301 302 303 304
  ((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
305
#define SU_HAS_INADDR_ANY(su) \
Pekka Pessi's avatar
Pekka Pessi committed
306 307 308 309
  ((su)->su_family == AF_INET \
  ? ((su)->su_sin.sin_addr.s_addr == INADDR_ANY) : 0)
#endif

310 311
#define SU_SOCKADDR_INADDR_ANY(su) SU_HAS_INADDR_ANY(su)

Pekka Pessi's avatar
Pekka Pessi committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
/**@HI Calculate correct size of su_sockaddr_t structure. */ 
#if SU_HAVE_IN6
#define SU_SOCKADDR_SIZE(su) \
  ((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) \
  ((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
extern const struct in_addr6 *su_in6addr_any(void);
extern 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 */

int su_cmp_sockaddr(su_sockaddr_t const *a, su_sockaddr_t const *b); 
int su_match_sockaddr(su_sockaddr_t const *a, su_sockaddr_t const *b);
void su_canonize_sockaddr(su_sockaddr_t *su);

#define SU_CANONIZE_SOCKADDR(su) \
  ((su)->su_family == AF_INET6 ? su_canonize_sockaddr(su) : (void)0)

#endif /* !defined(SU_H) */