1 //----------------------------------------------------------------------- 2 // This software is part of the OpenBeOS distribution and is covered 3 // by the OpenBeOS license. 4 // 5 // Copyright (c) 2003-2004 Waldemar Kornewald, Waldemar.Kornewald@web.de 6 //----------------------------------------------------------------------- 7 8 #include <KPPPDefs.h> 9 #include <driver_settings.h> 10 11 #include "settings_tools.h" 12 13 #include <cstring> 14 #include <malloc.h> 15 16 17 static const char *sSkipInterfaceParameters[] = { 18 PPP_ASK_BEFORE_DIALING_KEY, 19 PPP_DISONNECT_AFTER_IDLE_SINCE_KEY, 20 PPP_DIAL_RETRIES_LIMIT_KEY, 21 PPP_DIAL_RETRY_DELAY_KEY, 22 PPP_DIAL_ON_DEMAND_KEY, 23 PPP_AUTO_REDIAL_KEY, 24 PPP_REDIAL_DELAY_KEY, 25 NULL 26 }; 27 28 29 driver_settings* 30 dup_driver_settings(const driver_settings *dup) 31 { 32 if(!dup) 33 return NULL; // we got a NULL pointer, so return nothing 34 35 driver_settings *ret = new_driver_settings(); 36 37 ret->parameter_count = dup->parameter_count; 38 39 if(ret->parameter_count > 0) { 40 ret->parameters = (driver_parameter*) 41 malloc(ret->parameter_count * sizeof(driver_parameter)); 42 memset(ret->parameters, 0, ret->parameter_count * sizeof(driver_parameter)); 43 } else 44 ret->parameters = NULL; 45 46 for(int32 index = 0; index < ret->parameter_count; index++) 47 copy_driver_parameter(&dup->parameters[index], &ret->parameters[index]); 48 49 return ret; 50 } 51 52 53 void 54 free_driver_settings(driver_settings *settings) 55 { 56 if(!settings) 57 return; 58 59 for(int32 index = 0; index < settings->parameter_count; index++) 60 free_driver_parameter_fields(&settings->parameters[index]); 61 62 free(settings->parameters); 63 free(settings); 64 } 65 66 67 void 68 free_driver_parameter(driver_parameter *parameter) 69 { 70 free_driver_parameter_fields(parameter); 71 free(parameter); 72 } 73 74 75 void 76 free_driver_parameter_fields(driver_parameter *parameter) 77 { 78 free(parameter->name); 79 80 for(int32 index = 0; index < parameter->value_count; index++) 81 free(parameter->values[index]); 82 83 free(parameter->values); 84 85 for(int32 index = 0; index < parameter->parameter_count; index++) 86 free_driver_parameter_fields(¶meter->parameters[index]); 87 88 free(parameter->parameters); 89 } 90 91 92 driver_settings* 93 new_driver_settings() 94 { 95 driver_settings *settings = (driver_settings*) malloc(sizeof(driver_settings)); 96 memset(settings, 0, sizeof(driver_settings)); 97 98 return settings; 99 } 100 101 102 driver_parameter* 103 new_driver_parameter(const char *name) 104 { 105 driver_parameter *parameter = (driver_parameter*) malloc(sizeof(driver_parameter)); 106 memset(parameter, 0, sizeof(driver_parameter)); 107 108 set_driver_parameter_name(name, parameter); 109 110 return parameter; 111 } 112 113 114 bool 115 copy_driver_parameter(const driver_parameter *from, driver_parameter *to) 116 { 117 if(!from || !to) 118 return false; 119 120 free_driver_parameter_fields(to); 121 122 if(from->name) 123 to->name = strdup(from->name); 124 else 125 to->name = NULL; 126 127 to->value_count = from->value_count; 128 if(from->value_count > 0) 129 to->values = (char**) malloc(from->value_count * sizeof(char*)); 130 else 131 to->values = NULL; 132 133 for(int32 index = 0; index < to->value_count; index++) 134 to->values[index] = strdup(from->values[index]); 135 136 to->parameter_count = from->parameter_count; 137 138 if(to->parameter_count > 0) { 139 to->parameters = 140 (driver_parameter*) malloc(to->parameter_count * sizeof(driver_parameter)); 141 memset(to->parameters, 0, to->parameter_count * sizeof(driver_parameter)); 142 } else 143 to->parameters = NULL; 144 145 for(int32 index = 0; index < to->parameter_count; index++) 146 copy_driver_parameter(&from->parameters[index], &to->parameters[index]); 147 148 return true; 149 } 150 151 152 bool 153 set_driver_parameter_name(const char *name, driver_parameter *parameter) 154 { 155 if(!parameter) 156 return false; 157 158 free(parameter->name); 159 160 if(name) 161 parameter->name = strdup(name); 162 else 163 parameter->name = NULL; 164 165 return true; 166 } 167 168 169 bool 170 add_driver_parameter_value(const char *value, driver_parameter *to) 171 { 172 if(!value || !to) 173 return false; 174 175 int32 oldCount = to->value_count; 176 char **old = to->values; 177 178 to->values = (char**) malloc((oldCount + 1) * sizeof(char*)); 179 180 if(!to->values) { 181 to->values = old; 182 return false; 183 } 184 185 to->value_count++; 186 memcpy(to->values, old, oldCount * sizeof(char*)); 187 to->values[oldCount] = strdup(value); 188 189 return true; 190 } 191 192 193 bool 194 add_driver_parameter(driver_parameter *add, driver_settings *to) 195 { 196 if(!add || !to) 197 return false; 198 199 int32 oldCount = to->parameter_count; 200 driver_parameter *old = to->parameters; 201 202 to->parameters = 203 (driver_parameter*) malloc((oldCount + 1) * sizeof(driver_parameter)); 204 205 if(!to->parameters) { 206 to->parameters = old; 207 return false; 208 } 209 210 to->parameter_count++; 211 memcpy(to->parameters, old, oldCount * sizeof(driver_parameter)); 212 memcpy(to->parameters + oldCount, add, sizeof(driver_parameter)); 213 214 return true; 215 } 216 217 218 bool 219 equal_driver_settings(const driver_settings *lhs, const driver_settings *rhs) 220 { 221 if(!lhs && !rhs) 222 return true; 223 else if(!lhs || !rhs) 224 return false; 225 226 if(lhs->parameter_count != rhs->parameter_count) 227 return false; 228 229 for(int32 index = 0; index < lhs->parameter_count; index++) { 230 if(!equal_driver_parameters(&lhs->parameters[index], &rhs->parameters[index])) 231 return false; 232 } 233 234 return true; 235 } 236 237 238 bool 239 equal_driver_parameters(const driver_parameter *lhs, const driver_parameter *rhs) 240 { 241 if(!lhs && !rhs) 242 return true; 243 else if(!lhs || !rhs) 244 return false; 245 246 if(lhs->name && rhs->name) { 247 if(strcmp(lhs->name, rhs->name)) 248 return false; 249 } else if(lhs->name != rhs->name) 250 return false; 251 252 if(lhs->value_count != rhs->value_count 253 || lhs->parameter_count != rhs->parameter_count) 254 return false; 255 256 for(int32 index = 0; index < lhs->value_count; index++) { 257 if(strcmp(lhs->values[index], rhs->values[index])) 258 return false; 259 } 260 261 for(int32 index = 0; index < lhs->parameter_count; index++) { 262 if(!equal_driver_parameters(&lhs->parameters[index], &rhs->parameters[index])) 263 return false; 264 } 265 266 return true; 267 } 268 269 270 bool 271 skip_interface_parameter(const driver_parameter *parameter) 272 { 273 if(!parameter || !parameter->name) 274 return false; 275 276 for(int32 index = 0; sSkipInterfaceParameters[index]; index++) 277 if(!strcasecmp(parameter->name, sSkipInterfaceParameters[index])) 278 return true; 279 280 return false; 281 } 282 283 284 bool 285 equal_interface_settings(const driver_settings *lhs, const driver_settings *rhs) 286 { 287 if(!lhs && !rhs) 288 return true; 289 else if(!lhs || !rhs) 290 return false; 291 292 int32 lhsIndex = 0, rhsIndex = 0; 293 for(; lhsIndex < lhs->parameter_count; lhsIndex++) { 294 if(skip_interface_parameter(&lhs->parameters[lhsIndex])) 295 continue; 296 297 for(; rhsIndex < rhs->parameter_count; rhsIndex++) 298 if(!skip_interface_parameter(&rhs->parameters[rhsIndex])) 299 break; 300 301 if(rhsIndex >= rhs->parameter_count) 302 return false; 303 304 if(!equal_driver_parameters(&lhs->parameters[lhsIndex], 305 &rhs->parameters[rhsIndex])) 306 return false; 307 } 308 309 for(; rhsIndex < rhs->parameter_count; rhsIndex++) 310 if(!skip_interface_parameter(&rhs->parameters[rhsIndex])) 311 return false; 312 313 return true; 314 } 315 316 317 ppp_side 318 get_side_string_value(const char *sideString, ppp_side unknownValue) 319 { 320 if(!sideString) 321 return unknownValue; 322 323 if(!strcasecmp(sideString, "local")) 324 return PPP_LOCAL_SIDE; 325 if(!strcasecmp(sideString, "peer")) 326 return PPP_PEER_SIDE; 327 if(!strcasecmp(sideString, "none") 328 || !strcasecmp(sideString, "no")) 329 return PPP_NO_SIDE; 330 if(!strcasecmp(sideString, "both")) 331 return PPP_BOTH_SIDES; 332 333 // no correct value has been found => return default value 334 return unknownValue; 335 } 336 337 338 bool 339 get_boolean_value(const char *string, bool unknownValue) 340 { 341 if(!string) 342 return unknownValue; 343 344 if (!strcmp(string, "1") 345 || !strcasecmp(string, "true") 346 || !strcasecmp(string, "yes") 347 || !strcasecmp(string, "on") 348 || !strcasecmp(string, "enable") 349 || !strcasecmp(string, "enabled")) 350 return true; 351 352 if (!strcmp(string, "0") 353 || !strcasecmp(string, "false") 354 || !strcasecmp(string, "no") 355 || !strcasecmp(string, "off") 356 || !strcasecmp(string, "disable") 357 || !strcasecmp(string, "disabled")) 358 return false; 359 360 // no correct value has been found => return default value 361 return unknownValue; 362 } 363 364 365 const driver_parameter* 366 get_parameter_with_name(const char *name, const driver_settings *settings) 367 { 368 if(!name || !settings) 369 return NULL; 370 371 for(int32 index = 0; index < settings->parameter_count; index++) 372 if(!strcasecmp(settings->parameters[index].name, name)) 373 return &settings->parameters[index]; 374 375 return NULL; 376 } 377 378 379 const char* 380 get_settings_value(const char *name, const driver_settings *settings) 381 { 382 const driver_parameter *parameter = get_parameter_with_name(name, settings); 383 384 if(parameter && parameter->value_count > 0 && parameter->values) 385 return parameter->values[0]; 386 387 return NULL; 388 } 389