00001
00002
00003
00004
00005 #include <stdio.h>
00006 #include <errno.h>
00007 #include <string.h>
00008 #include <dlfcn.h>
00009 #include "chtbl.h"
00010
00011 #define PRIVATE_CODE_ONLY
00012 #include "config.h"
00013 #undef PRIVATE_CODE_ONLY
00014
00015
00016
00017
00018 #define CONFIG_H_ERROR_SIZE 2048
00019
00020
00021
00022
00023 static char last_error[CONFIG_H_ERROR_SIZE];
00024
00025
00026
00027
00028
00029
00030
00031 char* conf_get_last_error() { return last_error; }
00032
00033
00034
00035
00036 static int allochtab = 0;
00037
00038
00039
00040
00041
00042
00043 int config_hash_function (const void *key)
00044 {
00045 struct user_data *data;
00046 int index, i;
00047
00048 data = (struct user_data*)key;
00049 index = 0;
00050
00051 for (i=0; i<strlen(data->tag); i++) { index *= (int)(data->tag[i]); }
00052
00053 if (index < 0) { index *= - 1; }
00054
00055 return index;
00056 }
00057
00058
00059
00060
00061
00062
00063 void config_free_user_data (void *d)
00064 {
00065 struct user_data *data;
00066
00067 data = (struct user_data*)d;
00068 free (data->tag);
00069 free (data->value);
00070 free (data);
00071
00072 return;
00073 }
00074
00075
00076
00077
00078
00079
00080 int config_compare_user_data (const void *d1, const void *d2)
00081 {
00082 struct user_data *data1, *data2;
00083
00084 data1 = (struct user_data*)d1;
00085 data2 = (struct user_data*)d2;
00086
00087 return !strcmp(data1->tag, data2->tag);
00088 }
00089
00090
00091
00092
00093 CHTbl htab;
00094
00095
00096
00097
00098
00099 struct modem_conf_struct modem_configuration_list[MODEM_CONF_MAX_ENTRY];
00100
00101
00102
00103
00104
00105 int nb_modem;
00106
00107
00108
00109
00110
00111
00112 int get_number_of_modem_conf() { return nb_modem; }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 char* dummy_mac_processor (char* addr)
00123 {
00124 static char mac[128];
00125
00126 memset((void*)mac, 0, 128);
00127 strncpy (mac, addr, 127);
00128 return mac;
00129 }
00130
00131
00132
00133
00134 extern int configlex();
00135
00136
00137
00138
00139 extern FILE *configin;
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 int parse_configuration_file (char *path, int *err)
00172 {
00173 int rc, i;
00174
00175 *err = 0;
00176
00177 memset((void*)last_error, 0, CONFIG_H_ERROR_SIZE);
00178
00179 memset ((void*)&modem_configuration_list, 0, sizeof(struct modem_conf_struct)*MODEM_CONF_MAX_ENTRY);
00180
00181 modem_configuration_list[0].id = 0;
00182 strncpy (modem_configuration_list[0].identifier, "XXXXXX", MODEM_CONF_IDENTIFIER_SIZE-1);
00183 strncpy (modem_configuration_list[0].tag_name, "UNKNOWN", MODEM_CONF_TAG_NAME_SIZE-1);
00184 strncpy (modem_configuration_list[0].autoconf, "no", MODEM_CONF_AUTOCONF_SIZE-1);
00185 strncpy (modem_configuration_list[0].action, "dummy", MODEM_CONF_ACTION_SIZE-1);
00186 modem_configuration_list[0].dll = NULL;
00187 modem_configuration_list[0].calculate_mac_address = dummy_mac_processor;
00188
00189 nb_modem = 1;
00190
00191
00192
00193
00194
00195 if (
00196 chtbl_init (
00197 &htab,
00198 10,
00199 config_hash_function,
00200 config_compare_user_data,
00201 config_free_user_data
00202 ) == -1
00203 )
00204 { return PARSE_CONFIGURATION_NO_MEM; }
00205
00206 allochtab = 1;
00207
00208
00209
00210
00211
00212 configin = fopen (path, "r");
00213 if (configin == NULL) { *err = errno; return PARSE_CONFIGURATION_FILE_NOT_FOUND; }
00214
00215 rc = configlex();
00216 switch (rc)
00217 {
00218 case CONF_PARSER_SYNTAX_ERROR: free_all_configuration_data();
00219 fclose (configin);
00220 return PARSE_CONFIGURATION_SYNTAX_ERROR;
00221 case CONF_PARSER_DUPLICATED_TAG: free_all_configuration_data();
00222 fclose (configin);
00223 return PARSE_CONFIGURATION_DUPLICATED_TAG;
00224 case CONF_PARSER_NO_MEM: fclose (configin);
00225 return PARSE_CONFIGURATION_NO_MEM;
00226 case CONF_PARSER_IDENTIFIER_TOO_LONG: free_all_configuration_data();
00227 fclose (configin);
00228 return PARSE_CONFIGURATION_MODEM_CONF_NOT_VALID;
00229 case CONF_PARSER_TAG_NAME_TOO_LONG: free_all_configuration_data();
00230 fclose (configin);
00231 return PARSE_CONFIGURATION_MODEM_CONF_NOT_VALID;
00232 case CONF_PARSER_AUTOCONF_TOO_LONG: free_all_configuration_data();
00233 fclose (configin);
00234 return PARSE_CONFIGURATION_MODEM_CONF_NOT_VALID;
00235 case CONF_PARSER_ACTION_TOO_LONG: free_all_configuration_data();
00236 fclose (configin);
00237 return PARSE_CONFIGURATION_MODEM_CONF_NOT_VALID;
00238 case CONF_PARSER_TOO_MANY_ENTRIES: free_all_configuration_data();
00239 fclose (configin);
00240 return PARSE_CONFIGURATION_MODEM_CONF_NOT_VALID;
00241 }
00242
00243 fclose (configin);
00244
00245
00246
00247
00248
00249 for (i=1; i<get_number_of_modem_conf(); i++)
00250 {
00251 if (strcasecmp(modem_configuration_list[i].action, "dummy") == 0)
00252 { modem_configuration_list[i].calculate_mac_address = dummy_mac_processor; }
00253 else {
00254 modem_configuration_list[i].dll = dlopen (modem_configuration_list[i].action, RTLD_LAZY);
00255 if (modem_configuration_list[i].dll == NULL)
00256 {
00257 if (dlerror() != NULL) { strncpy (last_error, dlerror(), CONFIG_H_ERROR_SIZE-1); }
00258 free_all_configuration_data();
00259 return PARSE_CONFIGURATION_DLL_OPEN_ERROR;
00260 }
00261
00262 modem_configuration_list[i].calculate_mac_address = dlsym (modem_configuration_list[i].dll, "calculate_mac");
00263 if (dlerror() != NULL) {
00264 strncpy (last_error, dlerror(), CONFIG_H_ERROR_SIZE-1);
00265 free_all_configuration_data();
00266 return PARSE_CONFIGURATION_DLL_INVALID;
00267 }
00268 }
00269
00270 }
00271
00272 return PARSE_CONFIGURATION_OK;
00273 }
00274
00275
00276
00277
00278
00279
00280
00281 int modem_conf_get_id (char *mac)
00282 {
00283 int i;
00284
00285 for (i=0; i<get_number_of_modem_conf(); i++)
00286 { if (strncmp(modem_configuration_list[i].identifier, mac, 6) == 0) { return i; } }
00287
00288 return 0;
00289 }
00290
00291
00292
00293
00294
00295
00296
00297 char* modem_conf_get_tag_name (int id) { return modem_configuration_list[id].tag_name; }
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309 int modem_conf_is_autoconf (int id)
00310 {
00311 if (strcasecmp (modem_configuration_list[id].autoconf, "yes") == 0) { return 1; }
00312 return 0;
00313 }
00314
00315
00316
00317
00318
00319
00320
00321 void* modem_conf_get_function (int id)
00322 {
00323 return modem_configuration_list[id].calculate_mac_address;
00324 }
00325
00326
00327
00328
00329
00330 void free_all_configuration_data() { if (allochtab == 1) { chtbl_destroy (&htab); allochtab = 0; } }
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 int get_value (char *tag, char **value)
00344 {
00345 struct user_data *ud, sud;
00346 int cr, retval;
00347
00348
00349
00350
00351
00352 *value = NULL;
00353 retval = 0;
00354
00355
00356
00357
00358
00359 sud.tag = tag;
00360 sud.value = NULL;
00361
00362 ud = &sud;
00363
00364
00365
00366
00367
00368 cr = chtbl_lookup (&htab, (void**)&ud);
00369
00370 if (cr == 0) { retval = 1; *value = ud->value; }
00371
00372 return retval;
00373 }
00374
00375
00376
00377
00378
00379
00380
00381
00382