portbind.c 5.4 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * 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
/**@internal @file portbind.c
 * @brief Bind a socket to an UDP/TCP port and return the port number
27
 *
Pekka Pessi's avatar
Pekka Pessi committed
28
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
29
 *
Pekka Pessi's avatar
Pekka Pessi committed
30 31 32 33 34 35 36 37 38 39 40 41 42
 * @date Created: Thu Mar 25 12:12:25 2004 ppessi
 */

#include "config.h"

char const name[] = "portbind";
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>

43
#if HAVE_SYS_SOCKET_T
Pekka Pessi's avatar
Pekka Pessi committed
44
#include <sys/socket.h>
45 46 47
#endif

#if HAVE_NETINET_IN_H
Pekka Pessi's avatar
Pekka Pessi committed
48
#include <netinet/in.h>
49 50 51
#endif

#if HAVE_NETDB_H
Pekka Pessi's avatar
Pekka Pessi committed
52
#include <netdb.h>
53
#endif
Pekka Pessi's avatar
Pekka Pessi committed
54

55
#include "sofia-sip/su.h"
56

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 90 91 92
#if !defined(IPPROTO_SCTP)
#define IPPROTO_SCTP (132)
#endif

static
char const helptext[] =
  "usage: portbind OPTIONS [port]\n"
  "  where OPTIONS are\n"
  "    [-t] bind to TCP\n"
  "    [-u] bind to UDP\n"
  "    [-s] bind to SCTP\n"
#if HAVE_SIN6
  "    [-6] bind to IPv6 only\n"
  "    [-4] bind to IPv4 only\n"
  "  By default, portbind binds to UDP and TCP on IPv6 and IPv4.\n"
#else
  "  By default, portbind binds to UDP and TCP.\n"
#endif
;

void usage(int returncode)
{
  fprintf(returncode ? stderr : stdout, helptext);
  exit(returncode);
}

int main(int argc, char *argv[])
{
  char *o_port = "0", *o_protocol = NULL;
  int o_tcp = 0, o_udp = 0, o_sctp = 0;
  char *names[5] = { NULL };
  int protos[5] = { 0 };
  int types[5] = { 0 };
  int n, N = 0;
  unsigned long portno;
  unsigned short port;
93 94
  int af;
  su_socket_t s;
Pekka Pessi's avatar
Pekka Pessi committed
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
  socklen_t salen;
  struct sockaddr_storage ss[1];
  struct sockaddr *sa = (void *)ss;
  struct sockaddr_in *sin = (void *)ss;
#if HAVE_SIN6
  int o_ip6 = 0, o_ip4 = 0;
  struct sockaddr_in6 *sin6 = (void *)ss;
#endif

  for (argv++; *argv && **argv == '-';) {
    char *opt = *argv++ + 1;
    if (strcmp(opt, "-") == 0)
      break;
    else if (strcmp(opt, "P") == 0 && *argv)
      o_protocol = *argv++;
    else if (strcmp(opt, "u") == 0)
      o_udp = 1;
    else if (strcmp(opt, "t") == 0)
      o_tcp = 1;
    else if (strcmp(opt, "s") == 0)
      o_sctp = 1;
    else if (strcmp(opt, "h") == 0)
      usage(0);
    else if (strcmp(opt, "-help") == 0)
      usage(0);
#if HAVE_SIN6
    else if (strcmp(opt, "6") == 0)
      o_ip6 = AF_INET6;
    else if (strcmp(opt, "4") == 0)
      o_ip4 = 0;
#endif
126
    else
Pekka Pessi's avatar
Pekka Pessi committed
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
      usage(1);
  }

  if (argv[0]) {
    char *s = NULL;

    portno = strtoul(argv[0], &s, 10);

    if (portno != (portno & 65535) || s == argv[0] || *s) {
      fprintf(stderr, "%s: invalid port %s\n", name, argv[0]);
      exit(1);
    }
  } else {
    portno = 0;
  }

#if HAVE_SIN6
  if (o_ip6)
    af = AF_INET6;
  else if (o_ip4)
    af = AF_INET;
  else
    af = AF_INET6;
#else
  af = AF_INET;
#endif

  memset(ss, 0, sizeof ss);

  if (!o_tcp || !o_udp || !o_sctp || !o_protocol)
    o_tcp = o_udp = 1;

  if (o_protocol) {
    struct protoent *pent = getprotobyname(o_protocol);

    if (!pent) {
      fprintf(stderr, "%s: %s\n", o_protocol, "unknown protocol");
      exit(1);
    }
    names[N] = pent->p_name, protos[N] = pent->p_proto, types[N++] = SOCK_RAW;
  }

169
  if (o_tcp)
Pekka Pessi's avatar
Pekka Pessi committed
170 171 172 173 174 175 176 177 178 179 180
    names[N] = "TCP", protos[N] = IPPROTO_TCP, types[N++] = SOCK_STREAM;
  if (o_udp)
    names[N] = "UDP", protos[N] = IPPROTO_UDP, types[N++] = SOCK_DGRAM;
  if (o_sctp)
    names[N] = "SCTP", protos[N] = IPPROTO_SCTP, types[N++] = SOCK_SEQPACKET;

  assert(N != 0);

  port = portno;

  for (n = 0; n < N;) {
181
    s = su_socket(sa->sa_family = af, types[n], protos[n]);
Pekka Pessi's avatar
Pekka Pessi committed
182 183

#if HAVE_SIN6
184
    if (s == INVALID_SOCKET && af == AF_INET6 && !o_ip6)
185
      s = su_socket(sa->sa_family = af = AF_INET, types[n], protos[n]);
Pekka Pessi's avatar
Pekka Pessi committed
186 187
#endif

188
    if (s == INVALID_SOCKET) {
189
      fprintf(stderr, "%s: socket(AF_INET%s, 0, %s): %s\n",
Pekka Pessi's avatar
Pekka Pessi committed
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
	      name, af == AF_INET ? "" : "6", names[n], strerror(errno));
      exit(1);
    }

#if HAVE_SIN6
    if (af == AF_INET6 && !o_ip6 && !o_ip4)
      o_ip6 = o_ip4;
    if (af == AF_INET6)
      salen = sizeof *sin6;
    else
#endif
      salen = sizeof *sin;

    sin->sin_port = htons(port);

    if (bind(s, sa, salen) == -1) {
      if (errno == EADDRINUSE) {
	if (++port == 0)
	  port = 1024;
	if (port != portno) {
	  close(s);
	  n = 0;
	  continue;
	}
      }
215

Pekka Pessi's avatar
Pekka Pessi committed
216 217 218 219 220 221 222 223
      fprintf(stderr, "%s: bind(%s): %s\n", name, o_port, strerror(errno));
      exit(1);
    }

    if (portno == 0) {
      struct sockaddr_storage ss[1];
      struct sockaddr *sa = (void *)ss;
      struct sockaddr_in *sin = (void *)ss;
224

Pekka Pessi's avatar
Pekka Pessi committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
      salen = sizeof *ss;
      if (getsockname(s, sa, &salen) == -1) {
	fprintf(stderr, "%s: getsockname(): %s\n", name, strerror(errno));
	exit(1);
      }

      portno = port = ntohs(sin->sin_port);
    }

    close(s);

    n++;
  }

  printf("%u\n", ntohs(sin->sin_port));

  return 0;
}