sofia-sip/su.h

Go to the documentation of this file.
00001 /*
00002  * This file is part of the Sofia-SIP package
00003  *
00004  * Copyright (C) 2005 Nokia Corporation.
00005  *
00006  * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation; either version 2.1 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  *
00023  */
00024 
00025 #ifndef SU_H
00026 
00027 #define SU_H
00028 
00036 /* ---------------------------------------------------------------------- */
00037 /* Includes */
00038 
00039 #ifndef SU_CONFIG_H
00040 #include "sofia-sip/su_config.h"
00041 #endif
00042 #ifndef SU_TYPES_H
00043 #include "sofia-sip/su_types.h"
00044 #endif
00045 #ifndef SU_ERRNO_H
00046 #include <sofia-sip/su_errno.h>
00047 #endif
00048 
00049 #include <stdio.h>
00050 #include <limits.h>
00051 
00052 #if SU_HAVE_BSDSOCK             /* Unix-compatible includes */
00053 #include <errno.h>
00054 #include <unistd.h>
00055 
00056 #include <fcntl.h>
00057 #include <sys/types.h>
00058 #include <sys/socket.h>
00059 #include <sys/ioctl.h>
00060 
00061 #include <netinet/in.h>
00062 #include <arpa/inet.h>
00063 #include <netdb.h>
00064 #endif
00065 
00066 #if SU_HAVE_WINSOCK             /* Windows includes */
00067 #  include <winsock2.h>
00068 #  include <ws2tcpip.h>
00069 
00070 #  if defined(IPPROTO_IPV6)
00071 /* IPv6 defined in ws2tcpip.h */
00072 #  elif SU_HAVE_IN6 
00073 #    include <tpipv6.h>         /* From "IPv6 Tech Preview"  */
00074 #  else
00075 #    error Winsock with IPv6 support required
00076 #  endif
00077 
00078 #endif
00079 
00080 SOFIA_BEGIN_DECLS
00081 
00082 /* ---------------------------------------------------------------------- */
00083 /* Constant definitions */
00084 
00085 #if SU_HAVE_BSDSOCK || DOCUMENTATION_ONLY
00086 enum {
00088   INVALID_SOCKET = -1,
00089 #define INVALID_SOCKET ((su_socket_t)INVALID_SOCKET)
00090 
00091   SOCKET_ERROR = -1,
00092 #define SOCKET_ERROR SOCKET_ERROR
00093 
00094   su_success = 0, 
00096   su_failure = -1
00097 };
00098 #elif SU_HAVE_WINSOCK
00099 enum { 
00100   su_success = 0, 
00101   su_failure = 0xffffffffUL 
00102 };
00103 
00104 #define MSG_NOSIGNAL (0)
00105 
00106 #endif
00107 
00109 #define SU_MAXHOST (1025)
00110 
00111 #define SU_MAXSERV (25)
00112 
00114 #define SU_ADDRSIZE (48)
00115 
00116 #define SU_SERVSIZE (16)
00117 
00118 #define SU_SUCCESS su_success
00119 #define SU_FAILURE su_failure
00120 
00121 /* ---------------------------------------------------------------------- */
00122 /* Type definitions */
00123 
00125 #if SU_HAVE_BSDSOCK || DOCUMENTATION_ONLY
00126 typedef int su_socket_t;
00127 #elif SU_HAVE_WINSOCK
00128 typedef SOCKET su_socket_t;
00129 #endif
00130 
00131 #if !SU_HAVE_SOCKADDR_STORAGE
00132 /*
00133  * RFC 2553: protocol-independent placeholder for socket addresses
00134  */
00135 #define _SS_MAXSIZE     128
00136 #define _SS_ALIGNSIZE   (sizeof(int64_t))
00137 #define _SS_PAD1SIZE    (_SS_ALIGNSIZE - sizeof(u_char) * 2)
00138 #define _SS_PAD2SIZE    (_SS_MAXSIZE - sizeof(u_char) * 2 - \
00139                                 _SS_PAD1SIZE - _SS_ALIGNSIZE)
00140 
00141 struct sockaddr_storage {
00142 #if SU_HAVE_SOCKADDR_SA_LEN
00143         unsigned char ss_len;           /* address length */
00144         unsigned char ss_family;        /* address family */
00145 #else
00146         unsigned short ss_family;       /* address family */
00147 #endif
00148         char    __ss_pad1[_SS_PAD1SIZE];
00149         int64_t __ss_align;     /* force desired structure storage alignment */
00150         char    __ss_pad2[_SS_PAD2SIZE];
00151 };
00152 #endif
00153 
00155 union su_sockaddr_u {
00156 #ifdef DOCUMENTATION_ONLY
00157   uint8_t             su_len;         
00158   uint8_t             su_family;      
00159   uint16_t            su_port;        
00160 #else
00161   short               su_dummy;       
00162 #if SU_HAVE_SOCKADDR_SA_LEN
00163 #define               su_len          su_sa.sa_len
00164 #define               su_family       su_sa.sa_family
00165 #else
00166 #define               su_len          su_array[0]
00167   short               su_family;
00168 #endif
00169 #define               su_port         su_sin.sin_port  
00170 #endif
00171 
00172   char                su_array[32];   
00173   uint16_t            su_array16[16]; 
00174   uint32_t            su_array32[8];  
00175   struct sockaddr     su_sa;          
00176   struct sockaddr_in  su_sin;         
00177 #if SU_HAVE_IN6
00178   struct sockaddr_in6 su_sin6;        
00179 #endif
00180 #ifdef DOCUMENTATION_ONLY
00181   uint32_t            su_scope_id;    
00182 #else
00183 #define               su_scope_id     su_array32[6]
00184 #endif
00185 };
00186 
00187 typedef union su_sockaddr_u su_sockaddr_t;
00188 
00189 #if SU_HAVE_BSDSOCK || DOCUMENTATION_ONLY
00190 
00200 typedef size_t su_ioveclen_t;
00201 
00236 typedef struct su_iovec_s {
00237   void  *siv_base;              
00238   su_ioveclen_t siv_len;                
00239 } su_iovec_t;
00240 
00247 #define SU_IOVECLEN_MAX SIZE_MAX
00248 #endif
00249 
00250 #if SU_HAVE_WINSOCK
00251 typedef u_long su_ioveclen_t;
00252 
00253 /* This is same as WSABUF */
00254 typedef struct su_iovec_s {
00255   su_ioveclen_t  siv_len;
00256   void   *siv_base;
00257 } su_iovec_t;
00258 
00259 #define SU_IOVECLEN_MAX ULONG_MAX
00260 #endif
00261 
00262 /* ---------------------------------------------------------------------- */
00263 /* Socket compatibility functions */
00264 
00265 SOFIAPUBFUN int su_init(void);
00266 SOFIAPUBFUN void su_deinit(void);
00267 
00269 SOFIAPUBFUN su_socket_t su_socket(int af, int sock, int proto);
00271 SOFIAPUBFUN int su_close(su_socket_t s);
00273 SOFIAPUBFUN int su_ioctl(su_socket_t s, int request, ...);
00274 
00282 SOFIAPUBFUN int su_is_blocking(int errcode);
00283 
00285 SOFIAPUBFUN int su_setblocking(su_socket_t s, int blocking);
00287 SOFIAPUBFUN int su_setreuseaddr(su_socket_t s, int reuse);
00289 SOFIAPUBFUN int su_soerror(su_socket_t s);
00291 SOFIAPUBFUN issize_t su_getmsgsize(su_socket_t s);
00292 
00294 SOFIAPUBFUN
00295 issize_t su_vsend(su_socket_t, su_iovec_t const iov[], isize_t len, int flags,
00296                   su_sockaddr_t const *su, socklen_t sulen);
00298 SOFIAPUBFUN
00299 issize_t su_vrecv(su_socket_t, su_iovec_t iov[], isize_t len, int flags,
00300                   su_sockaddr_t *su, socklen_t *sulen);
00302 SOFIAPUBFUN int su_getlocalip(su_sockaddr_t *sin);
00303 
00304 #if SU_HAVE_BSDSOCK
00305 #define su_ioctl  ioctl
00306 /*
00307  * Note: before 1.12.2, there was su_isblocking() which did not take argument
00308  * and which was missing from WINSOCK 
00309  */
00310 #define su_is_blocking(e) \
00311   ((e) == EINPROGRESS || (e) == EAGAIN || (e) == EWOULDBLOCK)
00312 #endif
00313 
00314 #if SU_HAVE_WINSOCK
00315 SOFIAPUBFUN int inet_pton(int af, char const *src, void *dst);
00316 SOFIAPUBFUN const char *inet_ntop(int af, void const *src,
00317                                   char *dst, size_t size);
00318 SOFIAPUBFUN ssize_t 
00319   su_send(su_socket_t s, void *buffer, size_t length, int flags),
00320   su_sendto(su_socket_t s, void *buffer, size_t length, int flags,
00321             su_sockaddr_t const *to, socklen_t tolen),
00322   su_recv(su_socket_t s, void *buffer, size_t length, int flags),
00323   su_recvfrom(su_socket_t s, void *buffer, size_t length, int flags,
00324               su_sockaddr_t *from, socklen_t *fromlen);
00325 
00326 static __inline
00327 uint16_t su_ntohs(uint16_t s)
00328 {
00329   return (uint16_t)(((s & 255) << 8) | ((s & 0xff00) >> 8));
00330 }
00331 
00332 static __inline
00333 uint32_t su_ntohl(uint32_t l)
00334 {
00335   return ((l & 0xff) << 24) | ((l & 0xff00) << 8)
00336        | ((l & 0xff0000) >> 8) | ((l & 0xff000000U) >> 24);
00337 }
00338 
00339 #define ntohs su_ntohs
00340 #define htons su_ntohs
00341 #define ntohl su_ntohl
00342 #define htonl su_ntohl
00343 
00344 #else
00345 #define su_send(s,b,l,f) send((s),(b),(l),(f))
00346 #define su_sendto(s,b,l,f,a,L) sendto((s),(b),(l),(f),(void const*)(a),(L))
00347 #define su_recv(s,b,l,f) recv((s),(b),(l),(f))
00348 #define su_recvfrom(s,b,l,f,a,L) recvfrom((s),(b),(l),(f),(void *)(a),(L))
00349 #endif
00350 
00351 /* ---------------------------------------------------------------------- */
00352 /* Other compatibility stuff */
00353 
00354 #if SU_HAVE_WINSOCK
00355 #define getuid() (0x505)
00356 #endif
00357 
00358 #ifndef IPPROTO_SCTP
00359 #define IPPROTO_SCTP (132)
00360 #endif
00361 
00362 /* ---------------------------------------------------------------------- */
00363 /* Address manipulation macros */
00364 
00370 #if SU_HAVE_IN6
00371 #define SU_ADDR(su) \
00372   ((su)->su_family == AF_INET ? (void *)&(su)->su_sin.sin_addr : \
00373   ((su)->su_family == AF_INET6 ? (void *)&(su)->su_sin6.sin6_addr : \
00374   (void *)&(su)->su_sa.sa_data))
00375 #else
00376 #define SU_ADDR(su) \
00377   ((su)->su_family == AF_INET ? (void *)&(su)->su_sin.sin_addr : \
00378   (void *)&(su)->su_sa.sa_data)
00379 #endif
00380 
00386 #if SU_HAVE_IN6
00387 #define SU_ADDRLEN(su)                                  \
00388   ((su)->su_family == AF_INET                           \
00389    ? (socklen_t)sizeof((su)->su_sin.sin_addr) :         \
00390    ((su)->su_family == AF_INET6                         \
00391     ? (socklen_t)sizeof((su)->su_sin6.sin6_addr)        \
00392     : (socklen_t)sizeof((su)->su_sa.sa_data)))
00393 #else
00394 #define SU_ADDRLEN(su)                                  \
00395   ((su)->su_family == AF_INET                           \
00396    ? (socklen_t)sizeof((su)->su_sin.sin_addr)           \
00397    : (socklen_t)sizeof((su)->su_sa.sa_data))
00398 #endif
00399 
00401 #if SU_HAVE_IN6
00402 #define SU_HAS_INADDR_ANY(su) \
00403   ((su)->su_family == AF_INET \
00404    ? ((su)->su_sin.sin_addr.s_addr == INADDR_ANY) \
00405    : ((su)->su_family == AF_INET6 \
00406       ? (memcmp(&(su)->su_sin6.sin6_addr, su_in6addr_any(), \
00407                 sizeof(*su_in6addr_any())) == 0) : 0))
00408 #else
00409 #define SU_HAS_INADDR_ANY(su) \
00410   ((su)->su_family == AF_INET \
00411   ? ((su)->su_sin.sin_addr.s_addr == INADDR_ANY) : 0)
00412 #endif
00413 
00414 #define SU_SOCKADDR_INADDR_ANY(su) SU_HAS_INADDR_ANY(su)
00415 
00417 #if SU_HAVE_IN6
00418 #define SU_SOCKADDR_SIZE(su) \
00419   ((socklen_t)((su)->su_family == AF_INET ? sizeof((su)->su_sin)          \
00420                : ((su)->su_family == AF_INET6 ? sizeof((su)->su_sin6)   \
00421                   : sizeof(*su))))
00422 #else
00423 #define SU_SOCKADDR_SIZE(su) \
00424   ((socklen_t)((su)->su_family == AF_INET ? sizeof((su)->su_sin)        \
00425                : sizeof(*su)))
00426 #endif
00427 #define su_sockaddr_size SU_SOCKADDR_SIZE
00428 
00429 #if SU_HAVE_IN6
00430 #if SU_HAVE_BSDSOCK
00431 #define su_in6addr_any()         (&in6addr_any)
00432 #define su_in6addr_loopback()    (&in6addr_loopback)
00433 #define SU_IN6ADDR_ANY_INIT      IN6ADDR_ANY_INIT
00434 #define SU_IN6ADDR_LOOPBACK_INIT IN6ADDR_LOOPBACK_INIT
00435 #endif
00436 #if SU_HAVE_WINSOCK || DOCUMENTATION_ONLY
00437 SOFIAPUBVAR const struct in_addr6 *su_in6addr_any(void);
00438 SOFIAPUBVAR const struct in_addr6 *su_in6addr_loopback(void);
00439 #define SU_IN6ADDR_ANY_INIT      { 0 }
00440 #define SU_IN6ADDR_LOOPBACK_INIT { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1 }
00441 #endif
00442 #endif /* SU_HAVE_IN6 */
00443 
00444 #define SU_IN6_IS_ADDR_V4MAPPED(a) \
00445   (((uint32_t const *) (a))[0] == 0 &&                        \
00446    ((uint32_t const *) (a))[1] == 0 &&                        \
00447    ((uint32_t const *) (a))[2] == htonl(0xffff))
00448 
00449 #define SU_IN6_IS_ADDR_V4COMPAT(a)                            \
00450   (((uint32_t const *)(a))[0] == 0 &&                         \
00451    ((uint32_t const *)(a))[1] == 0 &&                         \
00452    ((uint32_t const *)(a))[2] == 0 &&                         \
00453    ((uint32_t const *)(a))[3] != htonl(1) &&                  \
00454    ((uint32_t const *)(a))[3] != htonl(0))
00455 
00456 SOFIAPUBFUN int su_cmp_sockaddr(su_sockaddr_t const *a,
00457                                 su_sockaddr_t const *b);
00458 SOFIAPUBFUN int su_match_sockaddr(su_sockaddr_t const *a,
00459                                   su_sockaddr_t const *b);
00460 SOFIAPUBFUN void su_canonize_sockaddr(su_sockaddr_t *su);
00461 
00462 #if SU_HAVE_IN6
00463 #define SU_CANONIZE_SOCKADDR(su) \
00464   ((su)->su_family == AF_INET6 ? su_canonize_sockaddr(su) : (void)0)
00465 #else
00466 #define SU_CANONIZE_SOCKADDR(su) \
00467   ((void)0)
00468 #endif
00469 
00470 SOFIA_END_DECLS
00471 
00472 #ifndef SU_ADDRINFO_H
00473 #include <sofia-sip/su_addrinfo.h>
00474 #endif
00475 
00476 #endif /* !defined(SU_H) */

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