su  1.12.11devel
su.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Sofia-SIP package
3  *
4  * Copyright (C) 2005,2006,2007 Nokia Corporation.
5  *
6  * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 
25 #ifndef SU_H
26 
27 #define SU_H
28 
36 /* ---------------------------------------------------------------------- */
37 /* Includes */
38 
39 #ifndef SU_CONFIG_H
40 #include <sofia-sip/su_config.h>
41 #endif
42 #ifndef SU_TYPES_H
43 #include <sofia-sip/su_types.h>
44 #endif
45 #ifndef SU_ERRNO_H
46 #include <sofia-sip/su_errno.h>
47 #endif
48 
49 #include <stdio.h>
50 #include <limits.h>
51 
52 #if SU_HAVE_BSDSOCK /* Unix-compatible includes */
53 #include <errno.h>
54 #include <unistd.h>
55 
56 #include <fcntl.h>
57 #include <sys/types.h>
58 #include <sys/socket.h>
59 #include <sys/ioctl.h>
60 
61 #include <netinet/in.h>
62 #include <arpa/inet.h>
63 #include <netdb.h>
64 #endif
65 
66 #if SU_HAVE_WINSOCK /* Windows includes */
67 # include <winsock2.h>
68 # include <ws2tcpip.h>
69 # if SU_HAVE_IN6
70 # if defined(IPPROTO_IPV6) || (_WIN32_WINNT >= 0x0501)
71 /* case 1: IPv6 defined in winsock2.h/ws2tcpip.h */
72 # else
73 /* case 2: try to use "IPv6 Tech Preview" */
74 # include <tpipv6.h>
75 # endif
76 # endif
77 #endif
78 
79 SOFIA_BEGIN_DECLS
80 
81 /* ---------------------------------------------------------------------- */
82 /* Constant definitions */
83 
84 #if SU_HAVE_BSDSOCK || DOCUMENTATION_ONLY
85 enum {
88 #define INVALID_SOCKET ((su_socket_t)INVALID_SOCKET)
89 
91 #define SOCKET_ERROR SOCKET_ERROR
92 
96 };
97 #if SYMBIAN && !defined(MSG_NOSIGNAL)
98 #define MSG_NOSIGNAL (0)
99 #endif
100 #elif SU_HAVE_WINSOCK
101 enum {
102  su_success = 0,
103  su_failure = 0xffffffffUL
104 };
105 
106 #define MSG_NOSIGNAL (0)
107 
108 #endif
109 
111 #define SU_MAXHOST (1025)
112 
113 #define SU_MAXSERV (25)
114 
116 #define SU_ADDRSIZE (48)
117 
118 #define SU_SERVSIZE (16)
119 
120 #define SU_SUCCESS su_success
121 #define SU_FAILURE su_failure
122 
123 /* ---------------------------------------------------------------------- */
124 /* Type definitions */
125 
127 #if SU_HAVE_BSDSOCK || DOCUMENTATION_ONLY
128 typedef int su_socket_t;
129 #elif SU_HAVE_WINSOCK
130 typedef SOCKET su_socket_t;
131 #endif
132 
133 #if !SU_HAVE_SOCKADDR_STORAGE
134 /*
135  * RFC 2553: protocol-independent placeholder for socket addresses
136  */
137 #define _SS_MAXSIZE 128
138 #define _SS_ALIGNSIZE (sizeof(int64_t))
139 #define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(u_char) * 2)
140 #define _SS_PAD2SIZE (_SS_MAXSIZE - sizeof(u_char) * 2 - \
141  _SS_PAD1SIZE - _SS_ALIGNSIZE)
142 
143 struct sockaddr_storage {
144 #if SU_HAVE_SOCKADDR_SA_LEN
145  unsigned char ss_len; /* address length */
146  unsigned char ss_family; /* address family */
147 #else
148  unsigned short ss_family; /* address family */
149 #endif
150  char __ss_pad1[_SS_PAD1SIZE];
151  int64_t __ss_align; /* force desired structure storage alignment */
152  char __ss_pad2[_SS_PAD2SIZE];
153 };
154 #endif
155 
158 #ifdef DOCUMENTATION_ONLY
162 #else
163  short su_dummy;
164 #if SU_HAVE_SOCKADDR_SA_LEN
165 #define su_len su_sa.sa_len
166 #else
167 #define su_len su_array[0]
168 #endif
169 #define su_family su_sa.sa_family
170 #define su_port su_sin.sin_port
171 #endif
172 
173  char su_array[32];
174  uint16_t su_array16[16];
175  uint32_t su_array32[8];
176  struct sockaddr su_sa;
177  struct sockaddr_in su_sin;
178 #if SU_HAVE_IN6
179  struct sockaddr_in6 su_sin6;
180 #endif
181 #ifdef DOCUMENTATION_ONLY
183 #else
184 #define su_scope_id su_array32[6]
185 #endif
186 };
187 
188 typedef union su_sockaddr_u su_sockaddr_t;
189 
190 #if SU_HAVE_BSDSOCK || DOCUMENTATION_ONLY
191 
201 typedef size_t su_ioveclen_t;
202 
239 typedef struct su_iovec_s {
240  void *siv_base;
242 } su_iovec_t;
243 
250 #define SU_IOVECLEN_MAX SIZE_MAX
251 #endif
252 
253 #if SU_HAVE_WINSOCK
254 typedef u_long su_ioveclen_t;
255 
256 /* This is same as WSABUF */
257 typedef struct su_iovec_s {
258  su_ioveclen_t siv_len;
259  void *siv_base;
260 } su_iovec_t;
261 
262 #define SU_IOVECLEN_MAX ULONG_MAX
263 #endif
264 
265 /* ---------------------------------------------------------------------- */
266 /* Socket compatibility functions */
267 
268 SOFIAPUBFUN int su_init(void);
269 SOFIAPUBFUN void su_deinit(void);
270 
272 SOFIAPUBFUN su_socket_t su_socket(int af, int sock, int proto);
276 SOFIAPUBFUN int su_ioctl(su_socket_t s, int request, ...);
277 
287 SOFIAPUBFUN int su_is_blocking(int errcode);
288 
290 SOFIAPUBFUN int su_setblocking(su_socket_t s, int blocking);
292 SOFIAPUBFUN int su_setreuseaddr(su_socket_t s, int reuse);
297 
300 
303 issize_t su_vsend(su_socket_t, su_iovec_t const iov[], isize_t len, int flags,
304  su_sockaddr_t const *su, socklen_t sulen);
307 issize_t su_vrecv(su_socket_t, su_iovec_t iov[], isize_t len, int flags,
308  su_sockaddr_t *su, socklen_t *sulen);
311 
312 #if SU_HAVE_BSDSOCK
313 #define su_ioctl ioctl
314 /*
315  * Note: before 1.12.2, there was su_isblocking() which did not take argument
316  * and which was missing from WINSOCK
317  */
318 #define su_is_blocking(e) \
319  ((e) == EINPROGRESS || (e) == EAGAIN || (e) == EWOULDBLOCK)
320 #endif
321 
322 #if SU_HAVE_WINSOCK
323 SOFIAPUBFUN int su_inet_pton(int af, char const *src, void *dst);
324 SOFIAPUBFUN const char *su_inet_ntop(int af, void const *src,
325  char *dst, size_t size);
326 SOFIAPUBFUN ssize_t
327  su_send(su_socket_t s, void *buffer, size_t length, int flags),
328  su_sendto(su_socket_t s, void *buffer, size_t length, int flags,
329  su_sockaddr_t const *to, socklen_t tolen),
330  su_recv(su_socket_t s, void *buffer, size_t length, int flags),
331  su_recvfrom(su_socket_t s, void *buffer, size_t length, int flags,
332  su_sockaddr_t *from, socklen_t *fromlen);
333 
334 static __inline
335 uint16_t su_ntohs(uint16_t s)
336 {
337  return (uint16_t)(((s & 255) << 8) | ((s & 0xff00) >> 8));
338 }
339 
340 static __inline
341 uint32_t su_ntohl(uint32_t l)
342 {
343  return ((l & 0xff) << 24) | ((l & 0xff00) << 8)
344  | ((l & 0xff0000) >> 8) | ((l & 0xff000000U) >> 24);
345 }
346 
347 #define ntohs su_ntohs
348 #define htons su_ntohs
349 #define ntohl su_ntohl
350 #define htonl su_ntohl
351 
352 #else
353 #define su_inet_pton inet_pton
354 #define su_inet_ntop inet_ntop
355 #define su_send(s,b,l,f) send((s),(b),(l),(f))
356 #define su_sendto(s,b,l,f,a,L) sendto((s),(b),(l),(f),(void const*)(a),(L))
357 #define su_recv(s,b,l,f) recv((s),(b),(l),(f))
358 #define su_recvfrom(s,b,l,f,a,L) recvfrom((s),(b),(l),(f),(void *)(a),(L))
359 #endif
360 
361 /* ---------------------------------------------------------------------- */
362 /* Other compatibility stuff */
363 
364 #if SU_HAVE_WINSOCK
365 #define getuid() (0x505)
366 #endif
367 
368 #ifndef IPPROTO_SCTP
369 #define IPPROTO_SCTP (132)
370 #endif
371 
372 /* ---------------------------------------------------------------------- */
373 /* Address manipulation macros */
374 
380 #if SU_HAVE_IN6
381 #define SU_ADDR(su) \
382  ((su)->su_family == AF_INET ? (void *)&(su)->su_sin.sin_addr : \
383  ((su)->su_family == AF_INET6 ? (void *)&(su)->su_sin6.sin6_addr : \
384  (void *)&(su)->su_sa.sa_data))
385 #else
386 #define SU_ADDR(su) \
387  ((su)->su_family == AF_INET ? (void *)&(su)->su_sin.sin_addr : \
388  (void *)&(su)->su_sa.sa_data)
389 #endif
390 
396 #if SU_HAVE_IN6
397 #define SU_ADDRLEN(su) \
398  ((su)->su_family == AF_INET \
399  ? (socklen_t)sizeof((su)->su_sin.sin_addr) : \
400  ((su)->su_family == AF_INET6 \
401  ? (socklen_t)sizeof((su)->su_sin6.sin6_addr) \
402  : (socklen_t)sizeof((su)->su_sa.sa_data)))
403 #else
404 #define SU_ADDRLEN(su) \
405  ((su)->su_family == AF_INET \
406  ? (socklen_t)sizeof((su)->su_sin.sin_addr) \
407  : (socklen_t)sizeof((su)->su_sa.sa_data))
408 #endif
409 
411 #if SU_HAVE_IN6
412 #define SU_HAS_INADDR_ANY(su) \
413  ((su)->su_family == AF_INET \
414  ? ((su)->su_sin.sin_addr.s_addr == INADDR_ANY) \
415  : ((su)->su_family == AF_INET6 \
416  ? (memcmp(&(su)->su_sin6.sin6_addr, su_in6addr_any(), \
417  sizeof(*su_in6addr_any())) == 0) : 0))
418 #else
419 #define SU_HAS_INADDR_ANY(su) \
420  ((su)->su_family == AF_INET \
421  ? ((su)->su_sin.sin_addr.s_addr == INADDR_ANY) : 0)
422 #endif
423 
424 #define SU_SOCKADDR_INADDR_ANY(su) SU_HAS_INADDR_ANY(su)
425 
427 #if SU_HAVE_IN6
428 #define SU_SOCKADDR_SIZE(su) \
429  ((socklen_t)((su)->su_family == AF_INET ? sizeof((su)->su_sin) \
430  : ((su)->su_family == AF_INET6 ? sizeof((su)->su_sin6) \
431  : sizeof(*su))))
432 #else
433 #define SU_SOCKADDR_SIZE(su) \
434  ((socklen_t)((su)->su_family == AF_INET ? sizeof((su)->su_sin) \
435  : sizeof(*su)))
436 #endif
437 #define su_sockaddr_size SU_SOCKADDR_SIZE
438 
439 #if SU_HAVE_IN6
440 #if SU_HAVE_BSDSOCK
441 #define su_in6addr_any() (&in6addr_any)
442 #define su_in6addr_loopback() (&in6addr_loopback)
443 #define SU_IN6ADDR_ANY_INIT IN6ADDR_ANY_INIT
444 #define SU_IN6ADDR_LOOPBACK_INIT IN6ADDR_LOOPBACK_INIT
445 #endif
446 #if SU_HAVE_WINSOCK || DOCUMENTATION_ONLY
447 SOFIAPUBVAR const struct in_addr6 *su_in6addr_any(void);
448 SOFIAPUBVAR const struct in_addr6 *su_in6addr_loopback(void);
449 #define SU_IN6ADDR_ANY_INIT { 0 }
450 #define SU_IN6ADDR_LOOPBACK_INIT { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1 }
451 #endif
452 #endif /* SU_HAVE_IN6 */
453 
454 #define SU_IN6_IS_ADDR_V4MAPPED(a) \
455  (((uint32_t const *) (a))[0] == 0 && \
456  ((uint32_t const *) (a))[1] == 0 && \
457  ((uint32_t const *) (a))[2] == htonl(0xffff))
458 
459 #define SU_IN6_IS_ADDR_V4COMPAT(a) \
460  (((uint32_t const *)(a))[0] == 0 && \
461  ((uint32_t const *)(a))[1] == 0 && \
462  ((uint32_t const *)(a))[2] == 0 && \
463  ((uint32_t const *)(a))[3] != htonl(1) && \
464  ((uint32_t const *)(a))[3] != htonl(0))
465 
467  su_sockaddr_t const *b);
469  su_sockaddr_t const *b);
471 
472 #if SU_HAVE_IN6
473 #define SU_CANONIZE_SOCKADDR(su) \
474  ((su)->su_family == AF_INET6 ? su_canonize_sockaddr(su) : (void)0)
475 #else
476 #define SU_CANONIZE_SOCKADDR(su) \
477  ((void)0)
478 #endif
479 
480 SOFIA_END_DECLS
481 
482 #ifndef SU_ADDRINFO_H
483 #include <sofia-sip/su_addrinfo.h>
484 #endif
485 
486 #endif /* !defined(SU_H) */
int su_ioctl(su_socket_t s, int request,...)
Control socket.
Network address and service translation.
uint16_t su_port
Port number.
Definition: su.h:161
Basic integer types for su library.
Return code for an unsuccessful call.
Definition: su.h:95
I/O vector for scatter-gather I/O.
Definition: su.h:239
uint8_t su_len
Length of structure.
Definition: su.h:159
Errno handling.
int su_socket_t
Socket descriptor type.
Definition: su.h:128
Return code for a successful call.
Definition: su.h:93
void * siv_base
Pointer to buffer.
Definition: su.h:240
SU_U16_T uint16_t
16-bit unsigned integer
Definition: su_types.h:91
uint8_t su_family
Address family.
Definition: su.h:160
su_socket_t su_socket(int af, int sock, int proto)
Create an endpoint for communication.
Definition: su.c:84
uint32_t su_scope_id
Scope ID.
Definition: su.h:182
int su_close(su_socket_t s)
Close an socket descriptor.
Definition: su.c:251
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
#define SOFIAPUBFUN
SOFIAPUBFUN declares an exported function.
Definition: su_config.h:66
struct su_iovec_s su_iovec_t
I/O vector for scatter-gather I/O.
int su_setblocking(su_socket_t s, int blocking)
Set/reset blocking option.
Definition: su.c:256
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 library configuration
void su_deinit(void)
Deinitialize socket implementation.
Definition: su.c:243
issize_t su_getmsgsize(su_socket_t s)
Get size of message available in socket.
Definition: su.c:420
int su_is_blocking(int errcode)
Check for in-progress error codes.
#define SOFIAPUBVAR
SOFIAPUBVAR declares an exported variable.
Definition: su_config.h:68
su_ioveclen_t siv_len
Size of buffer.
Definition: su.h:241
int su_init(void)
Initialize socket implementation.
Definition: su.c:217
int su_setreuseaddr(su_socket_t s, int reuse)
Set/reset address reusing option.
Definition: su.c:392
int su_soerror(su_socket_t s)
Get the error code associated with the socket.
Definition: su.c:371
Common socket address structure.
Definition: su.h:157
int su_getlocalip(su_sockaddr_t *sin)
Return local IP address.
Definition: su_localinfo.c:1672
SU_U32_T uint32_t
32-bit unsigned integer
Definition: su_types.h:87
SU_S64_T int64_t
64-bit signed integer
Definition: su_types.h:85
void su_canonize_sockaddr(su_sockaddr_t *su)
Convert mapped/compat address to IPv4 address.
Definition: su.c:659
int su_getsocktype(su_socket_t s)
Get the socket type.
Definition: su.c:381
Invalid socket descriptor, error from socket() or accept()
Definition: su.h:87
SU_U8_T uint8_t
8-bit unsigned integer
Definition: su_types.h:95
int su_cmp_sockaddr(su_sockaddr_t const *a, su_sockaddr_t const *b)
Compare two socket addresses.
Definition: su.c:586
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
Error from other socket calls.
Definition: su.h:90
size_t su_ioveclen_t
Type of siv_len field in su_iovec_t.
Definition: su.h:201

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