Annotation of embedaddon/strongswan/src/libcharon/tests/utils/mock_net.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2018 Tobias Brunner
                      3:  * HSR Hochschule fuer Technik Rapperswil
                      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 "mock_net.h"
                     17: 
                     18: #include <daemon.h>
                     19: 
                     20: #include <assert.h>
                     21: 
                     22: typedef struct private_kernel_net_t private_kernel_net_t;
                     23: 
                     24: /**
                     25:  * Private data
                     26:  */
                     27: struct private_kernel_net_t {
                     28: 
                     29:        /**
                     30:         * Public interface
                     31:         */
                     32:        kernel_net_t public;
                     33: 
                     34:        /**
                     35:         * Local IP address
                     36:         */
                     37:        host_t *host;
                     38: };
                     39: 
                     40: /**
                     41:  * Global instance
                     42:  */
                     43: static private_kernel_net_t *instance;
                     44: 
                     45: METHOD(kernel_net_t, get_source_addr, host_t*,
                     46:        private_kernel_net_t *this, host_t *dest, host_t *src)
                     47: {
                     48:        return this->host->clone(this->host);
                     49: }
                     50: 
                     51: METHOD(kernel_net_t, get_nexthop, host_t*,
                     52:        private_kernel_net_t *this, host_t *dest, int prefix, host_t *src,
                     53:        char **iface)
                     54: {
                     55:        if (iface)
                     56:        {
                     57:                *iface = strdup("lo");
                     58:        }
                     59:        return this->host->clone(this->host);
                     60: }
                     61: 
                     62: METHOD(kernel_net_t, get_interface, bool,
                     63:        private_kernel_net_t *this, host_t *host, char **name)
                     64: {
                     65:        if (host->ip_equals(host, this->host))
                     66:        {
                     67:                if (name)
                     68:                {
                     69:                        *name = strdup("lo");
                     70:                }
                     71:                return TRUE;
                     72:        }
                     73:        return FALSE;
                     74: }
                     75: 
                     76: METHOD(kernel_net_t, create_address_enumerator, enumerator_t*,
                     77:        private_kernel_net_t *this, kernel_address_type_t which)
                     78: {
                     79:        return enumerator_create_single(this->host, NULL);
                     80: }
                     81: 
                     82: METHOD(kernel_net_t, destroy, void,
                     83:        private_kernel_net_t *this)
                     84: {
                     85:        this->host->destroy(this->host);
                     86:        free(this);
                     87: }
                     88: 
                     89: /*
                     90:  * Described in header
                     91:  */
                     92: kernel_net_t *mock_net_create()
                     93: {
                     94:        private_kernel_net_t *this;
                     95: 
                     96:        INIT(this,
                     97:                .public = {
                     98:                        .get_source_addr = _get_source_addr,
                     99:                        .get_nexthop = _get_nexthop,
                    100:                        .get_interface = _get_interface,
                    101:                        .create_address_enumerator = _create_address_enumerator,
                    102:                        .create_local_subnet_enumerator = (void*)enumerator_create_empty,
                    103:                        .add_ip = (void*)return_failed,
                    104:                        .del_ip = (void*)return_failed,
                    105:                        .add_route = (void*)return_failed,
                    106:                        .del_route = (void*)return_failed,
                    107:                        .destroy = _destroy,
                    108:                },
                    109:                .host = host_create_from_string("127.0.0.1", 500),
                    110:        );
                    111: 
                    112:        instance = this;
                    113: 
                    114:        return &this->public;
                    115: }

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