Annotation of embedaddon/strongswan/src/libimcv/generic/generic_attr_string.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2013-2015 Andreas Steffen
        !             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 "generic_attr_string.h"
        !            17: 
        !            18: #include <imcv.h>
        !            19: #include <pen/pen.h>
        !            20: #include <utils/debug.h>
        !            21: 
        !            22: typedef struct private_generic_attr_string_t private_generic_attr_string_t;
        !            23: 
        !            24: /**
        !            25:  * Private data of an generic_attr_string_t object.
        !            26:  */
        !            27: struct private_generic_attr_string_t {
        !            28: 
        !            29:        /**
        !            30:         * Public members of generic_attr_string_t
        !            31:         */
        !            32:        generic_attr_string_t public;
        !            33: 
        !            34:        /**
        !            35:         * Vendor-specific attribute type
        !            36:         */
        !            37:        pen_type_t type;
        !            38: 
        !            39:        /**
        !            40:         * Length of attribute value
        !            41:         */
        !            42:        size_t length;
        !            43: 
        !            44:        /**
        !            45:         * Attribute value or segment
        !            46:         */
        !            47:        chunk_t value;
        !            48: 
        !            49:        /**
        !            50:         * Noskip flag
        !            51:         */
        !            52:        bool noskip_flag;
        !            53: 
        !            54:        /**
        !            55:         * Reference count
        !            56:         */
        !            57:        refcount_t ref;
        !            58: };
        !            59: 
        !            60: METHOD(pa_tnc_attr_t, get_type, pen_type_t,
        !            61:        private_generic_attr_string_t *this)
        !            62: {
        !            63:        return this->type;
        !            64: }
        !            65: 
        !            66: METHOD(pa_tnc_attr_t, get_value, chunk_t,
        !            67:        private_generic_attr_string_t *this)
        !            68: {
        !            69:        return this->value;
        !            70: }
        !            71: 
        !            72: METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
        !            73:        private_generic_attr_string_t *this)
        !            74: {
        !            75:        return this->noskip_flag;
        !            76: }
        !            77: 
        !            78: METHOD(pa_tnc_attr_t, set_noskip_flag,void,
        !            79:        private_generic_attr_string_t *this, bool noskip)
        !            80: {
        !            81:        this->noskip_flag = noskip;
        !            82: }
        !            83: 
        !            84: METHOD(pa_tnc_attr_t, build, void,
        !            85:        private_generic_attr_string_t *this)
        !            86: {
        !            87:        return;
        !            88: }
        !            89: 
        !            90: METHOD(pa_tnc_attr_t, process, status_t,
        !            91:        private_generic_attr_string_t *this, uint32_t *offset)
        !            92: {
        !            93:        enum_name_t *pa_attr_names;
        !            94:        u_char *pos;
        !            95:        *offset = 0;
        !            96: 
        !            97:        if (this->value.len < this->length)
        !            98:        {
        !            99:                return NEED_MORE;
        !           100:        }
        !           101:        pa_attr_names = imcv_pa_tnc_attributes->get_names(imcv_pa_tnc_attributes,
        !           102:                                                                                                          this->type.vendor_id);
        !           103:        if (this->value.len > this->length)
        !           104:        {
        !           105:                DBG1(DBG_TNC, "inconsistent length of %N/%N string attribute",
        !           106:                         pen_names, this->type.vendor_id, pa_attr_names, this->type.type);
        !           107:                return FAILED;
        !           108:        }
        !           109: 
        !           110:        pos = memchr(this->value.ptr, '\0', this->value.len);
        !           111:        if (pos)
        !           112:        {
        !           113:                DBG1(DBG_TNC, "nul termination in %N/%N string attribute",
        !           114:                         pen_names, this->type.vendor_id, pa_attr_names, this->type.type);
        !           115:                *offset = pos - this->value.ptr;
        !           116:                return FAILED;
        !           117:        }
        !           118: 
        !           119:        return SUCCESS;
        !           120: }
        !           121: 
        !           122: METHOD(pa_tnc_attr_t, add_segment, void,
        !           123:        private_generic_attr_string_t *this, chunk_t segment)
        !           124: {
        !           125:        this->value = chunk_cat("mc", this->value, segment);
        !           126: }
        !           127: 
        !           128: METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
        !           129:        private_generic_attr_string_t *this)
        !           130: {
        !           131:        ref_get(&this->ref);
        !           132:        return &this->public.pa_tnc_attribute;
        !           133: }
        !           134: 
        !           135: METHOD(pa_tnc_attr_t, destroy, void,
        !           136:        private_generic_attr_string_t *this)
        !           137: {
        !           138:        if (ref_put(&this->ref))
        !           139:        {
        !           140:                free(this->value.ptr);
        !           141:                free(this);
        !           142:        }
        !           143: }
        !           144: 
        !           145: /**
        !           146:  * Described in header.
        !           147:  */
        !           148: pa_tnc_attr_t *generic_attr_string_create_from_data(size_t length,
        !           149:                                                                        chunk_t value, pen_type_t type)
        !           150: {
        !           151:        private_generic_attr_string_t *this;
        !           152: 
        !           153:        INIT(this,
        !           154:                .public = {
        !           155:                        .pa_tnc_attribute = {
        !           156:                                .get_type = _get_type,
        !           157:                                .get_value = _get_value,
        !           158:                                .get_noskip_flag = _get_noskip_flag,
        !           159:                                .set_noskip_flag = _set_noskip_flag,
        !           160:                                .build = _build,
        !           161:                                .process = _process,
        !           162:                                .add_segment = _add_segment,
        !           163:                                .get_ref = _get_ref,
        !           164:                                .destroy = _destroy,
        !           165:                        },
        !           166:                },
        !           167:                .type = type,
        !           168:                .length = length,
        !           169:                .value = chunk_clone(value),
        !           170:                .ref = 1,
        !           171:        );
        !           172: 
        !           173:        return &this->public.pa_tnc_attribute;
        !           174: }
        !           175: 
        !           176: /**
        !           177:  * Described in header.
        !           178:  */
        !           179: pa_tnc_attr_t *generic_attr_string_create(chunk_t value, pen_type_t type)
        !           180: {
        !           181:        return generic_attr_string_create_from_data(value.len, value, type);
        !           182: }
        !           183: 

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