Return to pts_component_manager.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / strongswan / src / libimcv / pts / components |
1.1 misho 1: /* 2: * Copyright (C) 2011-2012 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 "pts/components/pts_component_manager.h" 17: 18: #include <collections/linked_list.h> 19: #include <utils/debug.h> 20: 21: typedef struct private_pts_component_manager_t private_pts_component_manager_t; 22: typedef struct vendor_entry_t vendor_entry_t; 23: typedef struct component_entry_t component_entry_t; 24: 25: #define PTS_QUALIFIER_SIZE 6 26: 27: /** 28: * Vendor-specific namespace information and list of registered components 29: */ 30: struct vendor_entry_t { 31: 32: /** 33: * Vendor ID 34: */ 35: pen_t vendor_id; 36: 37: /** 38: * Vendor-specific Component Functional names 39: */ 40: enum_name_t *comp_func_names; 41: 42: /** 43: * Vendor-specific Qualifier Type names 44: */ 45: enum_name_t *qualifier_type_names; 46: 47: /** 48: * Vendor-specific Qualifier Flag names 49: */ 50: char *qualifier_flag_names; 51: 52: /** 53: * Vendor-specific size of Qualifier Type field 54: */ 55: int qualifier_type_size; 56: 57: /** 58: * List of vendor-specific registered Functional Components 59: */ 60: linked_list_t *components; 61: }; 62: 63: /** 64: * Destroy a vendor_entry_t object 65: */ 66: static void vendor_entry_destroy(vendor_entry_t *entry) 67: { 68: entry->components->destroy_function(entry->components, free); 69: free(entry); 70: } 71: 72: /** 73: * Creation method for a vendor-specific Functional Component 74: */ 75: struct component_entry_t { 76: 77: /** 78: * Vendor-Specific Component Functional Name 79: */ 80: uint32_t name; 81: 82: /** 83: * Functional Component creation method 84: */ 85: pts_component_create_t create; 86: }; 87: 88: /** 89: * Private data of a pts_component_manager_t object. 90: * 91: */ 92: struct private_pts_component_manager_t { 93: 94: /** 95: * Public pts_component_manager_t interface. 96: */ 97: pts_component_manager_t public; 98: 99: /** 100: * List of vendor-specific namespaces and registered components 101: */ 102: linked_list_t *list; 103: }; 104: 105: METHOD(pts_component_manager_t, add_vendor, void, 106: private_pts_component_manager_t *this, pen_t vendor_id, 107: enum_name_t *comp_func_names, int qualifier_type_size, 108: char *qualifier_flag_names, enum_name_t *qualifier_type_names) 109: { 110: vendor_entry_t *entry; 111: 112: entry = malloc_thing(vendor_entry_t); 113: entry->vendor_id = vendor_id; 114: entry->comp_func_names = comp_func_names; 115: entry->qualifier_type_size = qualifier_type_size; 116: entry->qualifier_flag_names = qualifier_flag_names; 117: entry->qualifier_type_names = qualifier_type_names; 118: entry->components = linked_list_create(); 119: 120: this->list->insert_last(this->list, entry); 121: DBG2(DBG_PTS, "added %N functional component namespace", 122: pen_names, vendor_id); 123: } 124: 125: METHOD(pts_component_manager_t, get_comp_func_names, enum_name_t*, 126: private_pts_component_manager_t *this, pen_t vendor_id) 127: { 128: enumerator_t *enumerator; 129: vendor_entry_t *entry; 130: enum_name_t *names = NULL; 131: 132: enumerator = this->list->create_enumerator(this->list); 133: while (enumerator->enumerate(enumerator, &entry)) 134: { 135: if (entry->vendor_id == vendor_id) 136: { 137: names = entry->comp_func_names; 138: break; 139: } 140: } 141: enumerator->destroy(enumerator); 142: 143: return names; 144: } 145: 146: METHOD(pts_component_manager_t, get_qualifier_type_names, enum_name_t*, 147: private_pts_component_manager_t *this, pen_t vendor_id) 148: { 149: enumerator_t *enumerator; 150: vendor_entry_t *entry; 151: enum_name_t *names = NULL; 152: 153: enumerator = this->list->create_enumerator(this->list); 154: while (enumerator->enumerate(enumerator, &entry)) 155: { 156: if (entry->vendor_id == vendor_id) 157: { 158: names = entry->qualifier_type_names; 159: break; 160: } 161: } 162: enumerator->destroy(enumerator); 163: 164: return names; 165: } 166: 167: METHOD(pts_component_manager_t, add_component, void, 168: private_pts_component_manager_t *this, pen_t vendor_id, uint32_t name, 169: pts_component_create_t create) 170: { 171: enumerator_t *enumerator; 172: vendor_entry_t *entry; 173: component_entry_t *component; 174: 175: enumerator = this->list->create_enumerator(this->list); 176: while (enumerator->enumerate(enumerator, &entry)) 177: { 178: if (entry->vendor_id == vendor_id) 179: { 180: component = malloc_thing(component_entry_t); 181: component->name = name; 182: component->create = create; 183: 184: entry->components->insert_last(entry->components, component); 185: DBG2(DBG_PTS, "added %N functional component '%N'", 186: pen_names, vendor_id, 187: get_comp_func_names(this, vendor_id), name); 188: } 189: } 190: enumerator->destroy(enumerator); 191: } 192: 193: METHOD(pts_component_manager_t, remove_vendor, void, 194: private_pts_component_manager_t *this, pen_t vendor_id) 195: { 196: enumerator_t *enumerator; 197: vendor_entry_t *entry; 198: 199: enumerator = this->list->create_enumerator(this->list); 200: while (enumerator->enumerate(enumerator, &entry)) 201: { 202: if (entry->vendor_id == vendor_id) 203: { 204: this->list->remove_at(this->list, enumerator); 205: vendor_entry_destroy(entry); 206: DBG2(DBG_PTS, "removed %N functional component namespace", 207: pen_names, vendor_id); 208: } 209: } 210: enumerator->destroy(enumerator); 211: } 212: 213: METHOD(pts_component_manager_t, get_qualifier, uint8_t, 214: private_pts_component_manager_t *this, pts_comp_func_name_t *name, 215: char *flags) 216: { 217: enumerator_t *enumerator; 218: vendor_entry_t *entry; 219: uint8_t qualifier, size, flag, type = 0; 220: int i; 221: 222: enumerator = this->list->create_enumerator(this->list); 223: while (enumerator->enumerate(enumerator, &entry)) 224: { 225: if (entry->vendor_id == name->get_vendor_id(name)) 226: { 227: qualifier = name->get_qualifier(name); 228: size = entry->qualifier_type_size; 229: 230: /* mask qualifier type field */ 231: type = qualifier & ((1 << size) - 1); 232: 233: /* determine flags */ 234: size = PTS_QUALIFIER_SIZE - size; 235: flag = (1 << (PTS_QUALIFIER_SIZE - 1)); 236: if (flags) 237: { 238: for (i = 0 ; i < size; i++) 239: { 240: flags[i] = (qualifier & flag) ? 241: entry->qualifier_flag_names[i] : '.'; 242: flag >>= 1; 243: } 244: flags[size] = '\0'; 245: } 246: } 247: } 248: enumerator->destroy(enumerator); 249: 250: return type; 251: } 252: 253: METHOD(pts_component_manager_t, create, pts_component_t*, 254: private_pts_component_manager_t *this, 255: pts_comp_func_name_t *name, uint32_t depth, pts_database_t *pts_db) 256: { 257: enumerator_t *enumerator, *e2; 258: vendor_entry_t *entry; 259: component_entry_t *entry2; 260: pts_component_t *component = NULL; 261: 262: enumerator = this->list->create_enumerator(this->list); 263: while (enumerator->enumerate(enumerator, &entry)) 264: { 265: if (entry->vendor_id == name->get_vendor_id(name)) 266: { 267: e2 = entry->components->create_enumerator(entry->components); 268: while (e2->enumerate(e2, &entry2)) 269: { 270: if (entry2->name == name->get_name(name) && entry2->create) 271: { 272: component = entry2->create(depth, pts_db); 273: break; 274: } 275: } 276: e2->destroy(e2); 277: break; 278: } 279: } 280: enumerator->destroy(enumerator); 281: 282: return component; 283: } 284: 285: METHOD(pts_component_manager_t, destroy, void, 286: private_pts_component_manager_t *this) 287: { 288: this->list->destroy_function(this->list, (void *)vendor_entry_destroy); 289: free(this); 290: } 291: 292: /** 293: * See header 294: */ 295: pts_component_manager_t *pts_component_manager_create(void) 296: { 297: private_pts_component_manager_t *this; 298: 299: INIT(this, 300: .public = { 301: .add_vendor = _add_vendor, 302: .add_component = _add_component, 303: .remove_vendor = _remove_vendor, 304: .get_comp_func_names = _get_comp_func_names, 305: .get_qualifier_type_names = _get_qualifier_type_names, 306: .get_qualifier = _get_qualifier, 307: .create = _create, 308: .destroy = _destroy, 309: }, 310: .list = linked_list_create(), 311: ); 312: 313: return &this->public; 314: } 315: