Annotation of embedaddon/strongswan/src/libstrongswan/networking/streams/stream_tcp.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2013 Martin Willi
        !             3:  * Copyright (C) 2013 revosec AG
        !             4:  *
        !             5:  * This program is free software; you can redistribute it and/or modify it
        !             6:  * under the terms of the GNU General Public License as published by the
        !             7:  * Free Software Foundation; either version 2 of the License, or (at your
        !             8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
        !             9:  *
        !            10:  * This program is distributed in the hope that it will be useful, but
        !            11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
        !            13:  * for more details.
        !            14:  */
        !            15: 
        !            16: #include <library.h>
        !            17: #include <errno.h>
        !            18: #include <unistd.h>
        !            19: #include <limits.h>
        !            20: 
        !            21: #include "stream_tcp.h"
        !            22: 
        !            23: /**
        !            24:  * See header.
        !            25:  */
        !            26: int stream_parse_uri_tcp(char *uri, struct sockaddr *addr)
        !            27: {
        !            28:        char *pos, buf[128];
        !            29:        host_t *host;
        !            30:        u_long port;
        !            31:        int len;
        !            32: 
        !            33:        if (!strpfx(uri, "tcp://"))
        !            34:        {
        !            35:                return -1;
        !            36:        }
        !            37:        uri += strlen("tcp://");
        !            38:        pos = strrchr(uri, ':');
        !            39:        if (!pos)
        !            40:        {
        !            41:                return -1;
        !            42:        }
        !            43:        if (*uri == '[' && pos > uri && *(pos - 1) == ']')
        !            44:        {
        !            45:                /* IPv6 URI */
        !            46:                snprintf(buf, sizeof(buf), "%.*s", (int)(pos - uri - 2), uri + 1);
        !            47:        }
        !            48:        else
        !            49:        {
        !            50:                snprintf(buf, sizeof(buf), "%.*s", (int)(pos - uri), uri);
        !            51:        }
        !            52:        port = strtoul(pos + 1, &pos, 10);
        !            53:        if (port == ULONG_MAX || *pos || port > 65535)
        !            54:        {
        !            55:                return -1;
        !            56:        }
        !            57:        host = host_create_from_dns(buf, AF_UNSPEC, port);
        !            58:        if (!host)
        !            59:        {
        !            60:                return -1;
        !            61:        }
        !            62:        len = *host->get_sockaddr_len(host);
        !            63:        memcpy(addr, host->get_sockaddr(host), len);
        !            64:        host->destroy(host);
        !            65:        return len;
        !            66: }
        !            67: 
        !            68: /**
        !            69:  * See header
        !            70:  */
        !            71: stream_t *stream_create_tcp(char *uri)
        !            72: {
        !            73:        union {
        !            74:                struct sockaddr_in in;
        !            75:                struct sockaddr_in6 in6;
        !            76:                struct sockaddr sa;
        !            77:        } addr;
        !            78:        int fd, len;
        !            79: 
        !            80:        len = stream_parse_uri_tcp(uri, &addr.sa);
        !            81:        if (len == -1)
        !            82:        {
        !            83:                DBG1(DBG_NET, "invalid stream URI: '%s'", uri);
        !            84:                return NULL;
        !            85:        }
        !            86:        fd = socket(addr.sa.sa_family, SOCK_STREAM, 0);
        !            87:        if (fd < 0)
        !            88:        {
        !            89:                DBG1(DBG_NET, "opening socket '%s' failed: %s", uri, strerror(errno));
        !            90:                return NULL;
        !            91:        }
        !            92:        if (connect(fd, &addr.sa, len))
        !            93:        {
        !            94:                DBG1(DBG_NET, "connecting to '%s' failed: %s", uri, strerror(errno));
        !            95:                close(fd);
        !            96:                return NULL;
        !            97:        }
        !            98:        return stream_create_from_fd(fd);
        !            99: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>