media/mtransport/third_party/nICEr/src/net/transport_addr_reg.c
author Nils Ohlmeier [:drno] <drno@ohlmeier.org>
Wed, 27 Jul 2016 17:13:43 -0700
changeset 393607 5d5c2e088c6e0dffcba9d8692d4e1a878b24ae44
parent 275336 dfb822545e3446e8d01bb3fb25e4313350c72c0e
child 610070 e1dad7f1e7f194a6828e567191bc5da449930509
child 770825 f98674f05a92a146c7cd10ae788b5959bfe622fa
permissions -rw-r--r--
Bug 1289984: fix Windows compiler warnings. r=bwc MozReview-Commit-ID: EWSJZdxPzQk

/*
Copyright (c) 2007, Adobe Systems, Incorporated
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

* Neither the name of Adobe Systems, Network Resonance nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/



static char *RCSSTRING __UNUSED__="$Id: transport_addr_reg.c,v 1.2 2008/04/28 17:59:03 ekr Exp $";

#include <csi_platform.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <sys/types.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <strings.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#include <assert.h>
#include "nr_api.h"
#include "util.h"
#include "transport_addr.h"
#include "transport_addr_reg.h"

#ifndef INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN 46 /* Value used by linux/BSD */
#endif

int
nr_reg_get_transport_addr(NR_registry prefix, int keep, nr_transport_addr *addr)
{
    int r,_status;
    unsigned int count;
    char *address = 0;
    UINT2 port = 0;
    char *ifname = 0;
    char *protocol = 0;
    int p;

    if ((r=NR_reg_get_child_count(prefix, &count)))
        ABORT(r);

    if (count == 0)
        ABORT(R_NOT_FOUND);

    if ((r=NR_reg_alloc2_string(prefix, "address", &address))) {
        if (r != R_NOT_FOUND)
            ABORT(r);
        address = 0;
    }

    if ((r=NR_reg_alloc2_string(prefix, "ifname", &ifname))) {
        if (r != R_NOT_FOUND)
            ABORT(r);
        ifname = 0;
    }

    if ((r=NR_reg_get2_uint2(prefix, "port", &port))) {
        if (r != R_NOT_FOUND)
            ABORT(r);
        port = 0;
    }

    if ((r=NR_reg_alloc2_string(prefix, "protocol", &protocol))) {
        if (r != R_NOT_FOUND)
            ABORT(r);
        p = IPPROTO_UDP;

        protocol = 0;
    }
    else {
        if (!strcasecmp("tcp", protocol))
            p = IPPROTO_TCP;
        else if (!strcasecmp("udp", protocol))
            p = IPPROTO_UDP;
        else
            ABORT(R_BAD_DATA);
    }

    if (!keep) memset(addr, 0, sizeof(*addr));

    if ((r=nr_str_port_to_transport_addr(address?address:"0.0.0.0", port, p, addr)))
        ABORT(r);

    if (ifname)
        strlcpy(addr->ifname, ifname, sizeof(addr->ifname));

    _status=0;
  abort:
    RFREE(protocol);
    RFREE(ifname);
    RFREE(address);
    return(_status);
}

int
nr_reg_set_transport_addr(NR_registry prefix, int keep, nr_transport_addr *addr)
{
    int r,_status;

    if (! keep) {
        if ((r=NR_reg_del(prefix)))
            ABORT(r);
    }

    switch (addr->ip_version) {
    case NR_IPV4:
        if (!nr_transport_addr_is_wildcard(addr)) {
            if ((r=NR_reg_set2_string(prefix, "address", inet_ntoa(addr->u.addr4.sin_addr))))
                ABORT(r);
        }

        if (addr->u.addr4.sin_port != 0) {
            if ((r=NR_reg_set2_uint2(prefix, "port", ntohs(addr->u.addr4.sin_port))))
                ABORT(r);
        }
        break;

    case NR_IPV6:
        if (!nr_transport_addr_is_wildcard(addr)) {
          char address[INET6_ADDRSTRLEN];
          if(!inet_ntop(AF_INET6, &addr->u.addr6.sin6_addr,address,sizeof(address))) {
            ABORT(R_BAD_DATA);
          }

          if ((r=NR_reg_set2_string(prefix, "address", address))) {
            ABORT(r);
          }
        }

        if (addr->u.addr6.sin6_port != 0) {
            if ((r=NR_reg_set2_uint2(prefix, "port", ntohs(addr->u.addr6.sin6_port))))
                ABORT(r);
        }
        break;
    default:
        ABORT(R_INTERNAL);
        break;
    }

    /* We abort if neither NR_IPV4 or NR_IPV6 above */
    switch (addr->protocol) {
      case IPPROTO_TCP:
        if ((r=NR_reg_set2_string(prefix, "protocol", "tcp")))
          ABORT(r);
        break;
      case IPPROTO_UDP:
        if ((r=NR_reg_set2_string(prefix, "protocol", "udp")))
          ABORT(r);
        break;
      default:
        UNIMPLEMENTED;
        break;
    }

    if (strlen(addr->ifname) > 0) {
      if ((r=NR_reg_set2_string(prefix, "ifname", addr->ifname)))
        ABORT(r);
    }

    _status=0;
  abort:
    if (_status)
        NR_reg_del(prefix);
    return _status;
}

int
nr_reg_get_transport_addr2(NR_registry prefix, char *name, int keep, nr_transport_addr *addr)
{
    int r, _status;
    NR_registry registry;

    if ((r=NR_reg_make_registry(prefix, name, registry)))
        ABORT(r);

    if ((r=nr_reg_get_transport_addr(registry, keep, addr)))
        ABORT(r);

    _status = 0;
abort:
    return _status;
}

int
nr_reg_set_transport_addr2(NR_registry prefix, char *name, int keep, nr_transport_addr *addr)
{
    int r, _status;
    NR_registry registry;

    if ((r=NR_reg_make_registry(prefix, name, registry)))
        ABORT(r);

    if ((r=nr_reg_set_transport_addr(registry, keep, addr)))
        ABORT(r);

    _status = 0;
abort:
    return _status;
}