su.c 13.8 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 26 27 28 29 30 31 32 33 34
 * 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
 *
 */

/**@ingroup su_socket
 * @CFILE su.c OS-independent socket functions
 * 
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * 
 * @date Created: Thu Mar 18 19:40:51 1999 pessi
 */

#include "config.h" 

35 36 37 38 39 40 41
#if HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif

#include <sofia-sip/su.h>
#include <sofia-sip/su_log.h>
#include <sofia-sip/su_alloc.h>
Pekka Pessi's avatar
Pekka Pessi committed
42

43 44 45 46 47 48
#include <stdio.h>
#include <string.h>
#if HAVE_SIGNAL
#include <signal.h>
#endif

Pekka Pessi's avatar
Pekka Pessi committed
49 50 51 52
#if !SU_HAVE_BSDSOCK && !SU_HAVE_WINSOCK
#error Bad configuration
#endif

53 54 55 56 57
#ifndef FD_CLOEXEC
#define FD_CLOEXEC (1)
#endif

int su_socket_close_on_exec = 0;
58
int su_socket_blocking = 0;
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
#if HAVE_OPEN_C && HAVE_NET_IF_H
#include <net/if.h>
#endif

#if SU_HAVE_BSDSOCK && HAVE_OPEN_C
char su_global_ap_name[IFNAMSIZ];
extern int su_get_local_ip_addr(su_sockaddr_t *su);
#endif

/** Create a socket endpoint for communication.
 *
 * @param af addressing family 
 * @param socktype socket type 
 * @param proto protocol number specific to the addressing family
 *
 * The newly created socket is nonblocking unless global variable
 * su_socket_blocking is set to true. 
 *
 * Also, the newly created socket is closed on exec() if global variable
 * su_socket_close_on_exec is set to true. Note that a multithreaded program
 * can fork() and exec() before the close-on-exec flag is set.
 *
 * @return A valid socket descriptor or INVALID_SOCKET (-1) upon an error.
 */
84
su_socket_t su_socket(int af, int socktype, int proto)
85
{
86 87 88 89 90 91 92 93
#if HAVE_OPEN_C
  struct ifconf ifc;
  int numifs = 64;
  char *buffer;
  struct ifreq ifr;
  int const su_xtra = 0;
#endif

94
  su_socket_t s = socket(af, socktype, proto);
95

96
  if (s != INVALID_SOCKET) {
Pekka Pessi's avatar
Pekka Pessi committed
97
#if SU_HAVE_BSDSOCK
98 99
    if (su_socket_close_on_exec)
      fcntl(s, F_SETFD, FD_CLOEXEC); /* Close on exec */
Pekka Pessi's avatar
Pekka Pessi committed
100
#endif
101 102
    if (!su_socket_blocking)	/* All sockets are born blocking */
      su_setblocking(s, 0);
103
  }
104 105 106 107 108 109 110 111 112

#if HAVE_OPEN_C
  /* Use AP we have raised up */
  memset(&ifr, 0, sizeof(struct ifreq));
  strncpy(ifr.ifr_name, (char const *) su_global_ap_name, IFNAMSIZ);

  /* Assign socket to an already active access point (interface) */
  ioctl(s, SIOCSIFNAME, &ifr);
  ioctl(s, SIOCIFSTART, &ifr);	
113
#endif
114

115
  return s;
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
#if HAVE_OPEN_C
#include <errno.h>
su_sockaddr_t su_ap[1];
int ifindex;
void *aConnection;
extern void *su_localinfo_ap_set(su_sockaddr_t *su, int *index);
extern int su_localinfo_ap_deinit(void *aconn);
#define NUMIFS 64
  
int su_localinfo_ap_name_to_index(int ap_index)
{
  struct ifconf ifc;

  struct ifreq *ifr, *ifr_next;
  int error = EFAULT;
  char *buffer, buf[NUMIFS * sizeof(struct ifreq)];
  su_socket_t s;

  s= socket(AF_INET, SOCK_STREAM, 0);
  if (s < 0)
    return -1;
  
  ifc.ifc_len = NUMIFS * sizeof (struct ifreq);

  memset(buf, 0, ifc.ifc_len);
  ifc.ifc_buf = buf;
  if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
    return error;
  }

  buffer = ifc.ifc_buf + ifc.ifc_len;

  for (ifr = ifc.ifc_req;
       (void *)ifr < (void *)buffer;
       ifr = ifr_next) {
    struct ifreq ifreq[1];
    int scope, if_index, flags = 0, gni_flags = 0;
    char *if_name;
    su_sockaddr_t su2[1];

#if SA_LEN
    if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
      ifr_next = (struct ifreq *)
	(ifr->ifr_addr.sa_len + (char *)(&ifr->ifr_addr));
    else
#else
      ifr_next = ifr + 1;
#endif

    if_name = ifr->ifr_name;

#if defined(SIOCGIFINDEX)
    ifreq[0] = *ifr;
    if (ioctl(s, SIOCGIFINDEX, ifreq) < 0) {
return -1;
    }
#if HAVE_IFR_INDEX
    if_index = ifreq->ifr_index;
#elif HAVE_IFR_IFINDEX
    if_index = ifreq->ifr_ifindex;
#else
#error Unknown index field in struct ifreq
#endif

    if (ap_index == if_index) 
    {
      strncpy(su_global_ap_name, (const char *) if_name, sizeof(su_global_ap_name));
      error = 0;
    };
  
#else
#error su_localinfo() cannot map interface name to number
#endif

  }

  close(s);
  return error;
}
#endif

#if SU_HAVE_BSDSOCK || DOCUMENTATION_ONLY
/** Initialize socket implementation.
 *
 * Before using any sofia-sip-ua functions, the application should call
 * su_init() in order to initialize run-time environment including sockets. 
 * This function may prepare plugins if there are any. 
 *
 * @par POSIX Implementation
 * The su_init() initializes debugging logs and ignores the SIGPIPE signal.
 *
 * @par Windows Implementation
 * The function su_init() initializes Winsock2 library on Windows.
 *
 * @par Symbian Implementation
 * The function su_init() prompts user to select an access point (interface
 * towards Internet) and uses the activated access point for the socket
 * operations.
 */
Pekka Pessi's avatar
Pekka Pessi committed
217 218
int su_init(void)
{
219 220 221 222 223
#if HAVE_OPEN_C
  char apname[60];
  su_socket_t s;
#endif

224 225
  su_home_threadsafe(NULL);

226
#if HAVE_SIGPIPE
Pekka Pessi's avatar
Pekka Pessi committed
227
  signal(SIGPIPE, SIG_IGN);	/* we want to get EPIPE instead */
228
#endif
Pekka Pessi's avatar
Pekka Pessi committed
229

230 231 232 233 234 235
#if HAVE_OPEN_C
  /* This code takes care of enabling an access point (interface) */
  aConnection = su_localinfo_ap_set(su_ap, &ifindex);
  su_localinfo_ap_name_to_index(ifindex);
#endif

Pekka Pessi's avatar
Pekka Pessi committed
236 237 238 239 240 241
  su_log_init(su_log_default);
  su_log_init(su_log_global);

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
242
/** Deinitialize socket implementation. */
Pekka Pessi's avatar
Pekka Pessi committed
243 244
void su_deinit(void)
{
245
#if HAVE_OPEN_C
246
	su_localinfo_ap_deinit(aConnection);
247
#endif
Pekka Pessi's avatar
Pekka Pessi committed
248 249
}

250 251 252 253 254 255
/** Close an socket descriptor. */
int su_close(su_socket_t s)
{
  return close(s);
}

256
int su_setblocking(su_socket_t s, int blocking)
Pekka Pessi's avatar
Pekka Pessi committed
257
{
258
  int mode = fcntl(s, F_GETFL, 0);
Pekka Pessi's avatar
Pekka Pessi committed
259 260 261 262 263 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

  if (mode < 0)
     return -1;

  if (blocking) 
    mode &= ~(O_NDELAY | O_NONBLOCK);
  else
    mode |= O_NDELAY | O_NONBLOCK;

  return fcntl(s, F_SETFL, mode);
}
#endif

#if SU_HAVE_WINSOCK
int su_init(void)
{
  WORD	wVersionRequested;
  WSADATA	wsaData;

  wVersionRequested = MAKEWORD(2, 0);

  if (WSAStartup(wVersionRequested, &wsaData) !=0) {
    return -1;
  }

  su_log_init(su_log_default);

  su_log_init(su_log_global);

  return 0;
}

void su_deinit(void)
{
  WSACleanup();
}

Pekka Pessi's avatar
Pekka Pessi committed
296
/** Close a socket descriptor. */
Pekka Pessi's avatar
Pekka Pessi committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
int su_close(su_socket_t s)
{
  return closesocket(s);
}

/** Control socket. */
int su_ioctl(su_socket_t s, int request, ...)
{
  int retval;
  void *argp;
  va_list va;
  va_start(va, request);
  argp = va_arg(va, void *);
  retval = ioctlsocket(s, request, argp);
  va_end(va);
  return retval;
}

315 316 317 318 319
int su_is_blocking(int errcode)
{
  return errcode == EAGAIN || errcode == EWOULDBLOCK || errcode == EINPROGRESS;
}

Pekka Pessi's avatar
Pekka Pessi committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
int su_setblocking(su_socket_t s, int blocking)
{
  unsigned long nonBlock = !blocking;
  
  return ioctlsocket(s, FIONBIO, &nonBlock);
}


#endif /* SU_HAVE_WINSOCK */

int su_soerror(su_socket_t s)
{
  int error = 0;
  socklen_t errorlen = sizeof(error);

  getsockopt(s, SOL_SOCKET, SO_ERROR, (void *)&error, &errorlen);

  return error;
}

Pekka Pessi's avatar
Pekka Pessi committed
340 341 342 343 344 345 346 347 348 349 350
int su_getsocktype(su_socket_t s)
{
  int socktype = 0;
  socklen_t intlen = sizeof(socktype);

  if (getsockopt(s, SOL_SOCKET, SO_TYPE, (void *)&socktype, &intlen) < 0)
    return -1;

  return socktype;
}

Pekka Pessi's avatar
Pekka Pessi committed
351 352 353
int su_setreuseaddr(su_socket_t s, int reuse)
{
  return setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 
354
		    (void *)&reuse, (socklen_t)sizeof(reuse));
Pekka Pessi's avatar
Pekka Pessi committed
355 356 357 358 359 360 361
}


#if HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif

362 363 364 365 366 367 368 369
#if SU_HAVE_WINSOCK
issize_t su_getmsgsize(su_socket_t s)
{
  unsigned long n = (unsigned long)-1;
  if (ioctlsocket(s, FIONREAD, &n) == -1)
    return -1;
  return (issize_t)n;
}
370 371 372 373 374 375 376 377
#elif HAVE_OPEN_C
issize_t su_getmsgsize(su_socket_t s)
{
  int n = -1;
  if (su_ioctl(s, E32IONREAD, &n) == -1)
    return -1;
  return (issize_t)n;
}
378 379
#else
issize_t su_getmsgsize(su_socket_t s)
Pekka Pessi's avatar
Pekka Pessi committed
380 381 382 383
{
  int n = -1;
  if (su_ioctl(s, FIONREAD, &n) == -1)
    return -1;
384
  return (issize_t)n;
Pekka Pessi's avatar
Pekka Pessi committed
385
}
386
#endif
Pekka Pessi's avatar
Pekka Pessi committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

#if SU_HAVE_WINSOCK && SU_HAVE_IN6
/** Return a pointer to the in6addr_any. */
struct in_addr6 const *su_in6addr_any(void)
{
  static const struct in_addr6 a = SU_IN6ADDR_ANY_INIT;
  return &a;
}

/** Return a pointer to IPv6 loopback address */
struct in_addr6 const *su_in6addr_loopback(void)
{
  static const struct in_addr6 a = SU_IN6ADDR_LOOPBACK_INIT;
  return &a;
}
#endif

Pekka Pessi's avatar
Pekka Pessi committed
404
#if SU_HAVE_WINSOCK || DOCUMENTATION_ONLY
Pekka Pessi's avatar
Pekka Pessi committed
405

Pekka Pessi's avatar
Pekka Pessi committed
406
/** Call send() with POSIX-compatible signature */
407 408 409 410 411 412 413
ssize_t su_send(su_socket_t s, void *buffer, size_t length, int flags)
{
  if (length > INT_MAX)
    length = INT_MAX;
  return (ssize_t)send(s, buffer, (int)length, flags);
}

Pekka Pessi's avatar
Pekka Pessi committed
414
/** Call sendto() with POSIX-compatible signature */
415 416 417 418 419 420
ssize_t su_sendto(su_socket_t s, void *buffer, size_t length, int flags,
		   su_sockaddr_t const *to, socklen_t tolen)
{
  if (length > INT_MAX)
    length = INT_MAX;
  return (ssize_t)sendto(s, buffer, (int)length, flags,
421
			 &to->su_sa, (int) tolen);
422 423
}

Pekka Pessi's avatar
Pekka Pessi committed
424
/** Call recv() with POSIX-compatible signature */
Michael Jerris's avatar
Michael Jerris committed
425
ssize_t su_recv(su_socket_t s, void *buffer, size_t length, int flags)
426 427 428 429 430 431 432
{
  if (length > INT_MAX)
    length = INT_MAX;

  return (ssize_t)recv(s, buffer, (int)length, flags);
}

Pekka Pessi's avatar
Pekka Pessi committed
433
/** Call recvfrom() with POSIX-compatible signature */
Michael Jerris's avatar
Michael Jerris committed
434
ssize_t su_recvfrom(su_socket_t s, void *buffer, size_t length, int flags,
435
		    su_sockaddr_t *from, socklen_t *fromlen)
436 437 438 439 440 441 442 443 444 445
{
  int retval, ilen;

  if (fromlen)
    ilen = *fromlen;

  if (length > INT_MAX)
    length = INT_MAX;

  retval = recvfrom(s, buffer, (int)length, flags, 
446
		    &from->su_sa, fromlen ? &ilen : NULL);
447 448 449 450 451 452 453

  if (fromlen)
    *fromlen = ilen;

  return (ssize_t)retval;
}

Pekka Pessi's avatar
Pekka Pessi committed
454
/** Scatter/gather send */
455 456 457
issize_t su_vsend(su_socket_t s,
		  su_iovec_t const iov[], isize_t iovlen, int flags,
		  su_sockaddr_t const *su, socklen_t sulen)
Pekka Pessi's avatar
Pekka Pessi committed
458
{
459
  int ret;
Michael Jerris's avatar
Michael Jerris committed
460
  DWORD bytes_sent = (DWORD)su_failure;
461
  
462 463 464 465 466 467 468 469 470
  ret = WSASendTo(s,
		  (LPWSABUF)iov,
		  (DWORD)iovlen,
		  &bytes_sent,
		  flags,
		  &su->su_sa,
		  sulen,
		  NULL,
		  NULL);
Pekka Pessi's avatar
Pekka Pessi committed
471
  if (ret < 0)
472
    return (issize_t)ret;
Pekka Pessi's avatar
Pekka Pessi committed
473
  else
474
    return (issize_t)bytes_sent;
Pekka Pessi's avatar
Pekka Pessi committed
475 476 477 478
}


/** Scatter/gather recv */
479 480
issize_t su_vrecv(su_socket_t s, su_iovec_t iov[], isize_t iovlen, int flags,
		  su_sockaddr_t *su, socklen_t *sulen)
Pekka Pessi's avatar
Pekka Pessi committed
481
{
482
  int ret;
Michael Jerris's avatar
Michael Jerris committed
483
  DWORD bytes_recv = (DWORD)su_failure;
484
  DWORD dflags = flags;
485
  int fromlen = sulen ? *sulen : 0;
Pekka Pessi's avatar
Pekka Pessi committed
486 487

  ret =  WSARecvFrom(s,
488
		     (LPWSABUF)iov,
489
		     (DWORD)iovlen,
Pekka Pessi's avatar
Pekka Pessi committed
490
		     &bytes_recv,
491
		     &dflags,
Pekka Pessi's avatar
Pekka Pessi committed
492
		     &su->su_sa,
493
		     sulen ? &fromlen : NULL,
Pekka Pessi's avatar
Pekka Pessi committed
494 495
		     NULL,
		     NULL);
496 497 498

  if (sulen) *sulen = fromlen;

Pekka Pessi's avatar
Pekka Pessi committed
499
  if (ret < 0)
500
    return (issize_t)ret;
Pekka Pessi's avatar
Pekka Pessi committed
501
  else
502
    return (issize_t)bytes_recv;
Pekka Pessi's avatar
Pekka Pessi committed
503 504 505 506 507
}


#else

508 509 510
issize_t su_vsend(su_socket_t s,
		  su_iovec_t const iov[], isize_t iovlen, int flags,
		  su_sockaddr_t const *su, socklen_t sulen)
Pekka Pessi's avatar
Pekka Pessi committed
511 512 513
{
  struct msghdr hdr[1] = {{0}};

514
  hdr->msg_name = (void *)su;
Pekka Pessi's avatar
Pekka Pessi committed
515 516 517 518 519 520 521
  hdr->msg_namelen = sulen;
  hdr->msg_iov = (struct iovec *)iov;
  hdr->msg_iovlen = iovlen;

  return sendmsg(s, hdr, flags);
}

522 523
issize_t su_vrecv(su_socket_t s, su_iovec_t iov[], isize_t iovlen, int flags,
		  su_sockaddr_t *su, socklen_t *sulen)
Pekka Pessi's avatar
Pekka Pessi committed
524 525
{
  struct msghdr hdr[1] = {{0}};
526
  issize_t retval;
Pekka Pessi's avatar
Pekka Pessi committed
527

528
  hdr->msg_name = (void *)su;
Pekka Pessi's avatar
Pekka Pessi committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
  if (su && sulen)
    hdr->msg_namelen = *sulen;
  hdr->msg_iov = (struct iovec *)iov;
  hdr->msg_iovlen = iovlen;

  retval = recvmsg(s, hdr, flags);

  if (su && sulen)
    *sulen = hdr->msg_namelen;

  return retval;
}

#endif

/** Compare two socket addresses */
int su_cmp_sockaddr(su_sockaddr_t const *a, su_sockaddr_t const *b)
{
  int rv;

  /* Check that a and b are non-NULL */
  if ((rv = (a != NULL) - (b != NULL)) || a == NULL /* && b == NULL */)
    return rv;

  if ((rv = a->su_family - b->su_family))
    return rv;
  
  if (a->su_family == AF_INET)
    rv = memcmp(&a->su_sin.sin_addr, &b->su_sin.sin_addr, 
		sizeof(struct in_addr));
#if SU_HAVE_IN6
  else if (a->su_family == AF_INET6)
    rv = memcmp(&a->su_sin6.sin6_addr, &b->su_sin6.sin6_addr, 
		sizeof(struct in6_addr));
#endif
  else
    rv = memcmp(a, b, sizeof(struct sockaddr));

  if (rv)
    return rv;
  
  return a->su_port - b->su_port;
}

/** Check if socket address b match with a.
 *
 * The function su_match_sockaddr() returns true if the socket address @a b
 * matches with the socket address @a a. This happens if either all the
 * interesting fields are identical: address family, port number, address,
 * and scope ID (in case of IPv6) or that the @a a contains a wildcard
 * (zero) in their place.
 */
int su_match_sockaddr(su_sockaddr_t const *a, su_sockaddr_t const *b)
{
  /* Check that a and b are non-NULL */
  if (a == NULL)
    return 1;
  if (b == NULL)
    return 0;

  if (a->su_family != 0 && a->su_family != b->su_family)
    return 0;

  if (a->su_family == 0 || SU_SOCKADDR_INADDR_ANY(a))
    ;
  else if (a->su_family == AF_INET) {
    if (memcmp(&a->su_sin.sin_addr, &b->su_sin.sin_addr, 
	       sizeof(struct in_addr)))
      return 0;
  }
#if SU_HAVE_IN6
  else if (a->su_family == AF_INET6) {
    if (a->su_scope_id != 0 && a->su_scope_id != b->su_scope_id)
      return 0;
    if (memcmp(&a->su_sin6.sin6_addr, &b->su_sin6.sin6_addr, 
	       sizeof(struct in6_addr)))
      return 0;
  }
#endif
  else if (memcmp(a, b, sizeof(struct sockaddr)))
    return 0;

  if (a->su_port == 0)
    return 1;
  
  return a->su_port == b->su_port;
}

/** Convert mapped/compat address to IPv4 address */
void su_canonize_sockaddr(su_sockaddr_t *su)
{
Pekka Pessi's avatar
Pekka Pessi committed
620
#if SU_HAVE_IN6
Pekka Pessi's avatar
Pekka Pessi committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634
  if (su->su_family != AF_INET6)
    return;

  if (!IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr) &&
      !IN6_IS_ADDR_V4COMPAT(&su->su_sin6.sin6_addr))
    return;
  
  su->su_family = AF_INET;
  su->su_array32[1] = su->su_array32[5];
  su->su_array32[2] = 0; 
  su->su_array32[3] = 0;
#endif
}