net-snmp  5.4.1
snmp_service.c
00001 #include <net-snmp/net-snmp-config.h>
00002 
00003 #include <stdlib.h>
00004 #include <string.h>
00005 
00006 #include <net-snmp/net-snmp-includes.h>
00007 #include <net-snmp/library/snmp_transport.h>
00008 
00009 struct netsnmp_lookup_domain {
00010     char* application;
00011     char* userDomain;
00012     char* domain;
00013     struct netsnmp_lookup_domain* next;
00014 };
00015 
00016 static struct netsnmp_lookup_domain* domains = NULL;
00017 
00018 int
00019 netsnmp_register_default_domain(const char* application, const char* domain)
00020 {
00021     struct netsnmp_lookup_domain *run = domains, *prev = NULL;
00022     int res = 0;
00023 
00024     while (run != NULL && strcmp(run->application, application) < 0) {
00025         prev = run;
00026         run = run->next;
00027     }
00028     if (run && strcmp(run->application, application) == 0) {
00029       if (run->domain != NULL) {
00030           free (run->domain);
00031           run->domain=NULL;
00032           res = 1;
00033       }
00034     } else {
00035         run = SNMP_MALLOC_STRUCT(netsnmp_lookup_domain);
00036         run->application = strdup(application);
00037         run->userDomain = NULL;
00038         if (prev) {
00039             run->next = prev->next;
00040             prev->next = run;
00041         } else {
00042             run->next = domains;
00043             domains = run;
00044         }
00045     }
00046     if (domain) {
00047         run->domain = strdup(domain);
00048     } else if (run->userDomain == NULL) {
00049         if (prev)
00050             prev->next = run->next;
00051         else
00052             domains = run->next;
00053         free(run->application);
00054         free(run);
00055     }
00056     return res;
00057 }
00058 
00059 void
00060 netsnmp_clear_default_domain(void)
00061 {
00062     while (domains) {
00063         struct netsnmp_lookup_domain *tmp = domains;
00064         domains = domains->next;
00065         free(tmp->application);
00066         free(tmp->userDomain);
00067         free(tmp->domain);
00068         free(tmp);
00069     }
00070 }
00071 
00072 static void
00073 netsnmp_register_user_domain(const char* token, char* cptr)
00074 {
00075     struct netsnmp_lookup_domain *run = domains, *prev = NULL;
00076     size_t len = strlen(cptr) + 1;
00077     char* application = (char*)malloc(len);
00078     char* domain = (char*)malloc(len);
00079 
00080     {
00081         char* cp = copy_nword(cptr, application, len);
00082         cp = copy_nword(cp, domain, len);
00083         if (cp)
00084             config_pwarn("Trailing junk found");
00085     }
00086 
00087     while (run != NULL && strcmp(run->application, application) < 0) {
00088         prev = run;
00089         run = run->next;
00090     }
00091     if (run && strcmp(run->application, application) == 0) {
00092         if (run->userDomain != NULL) {
00093             config_perror("Default transport already registered for this "
00094                           "application");
00095             goto done;
00096         }
00097     } else {
00098         run = SNMP_MALLOC_STRUCT(netsnmp_lookup_domain);
00099         run->application = strdup(application);
00100         run->domain = NULL;
00101         if (prev) {
00102             run->next = prev->next;
00103             prev->next = run;
00104         } else {
00105             run->next = domains;
00106             domains = run;
00107         }
00108     }
00109     run->userDomain = strdup(domain);
00110  done:
00111     free(domain);
00112     free(application);
00113 }
00114 
00115 static void
00116 netsnmp_clear_user_domain(void)
00117 {
00118     struct netsnmp_lookup_domain *run = domains, *prev = NULL;
00119 
00120     while (run) {
00121         if (run->userDomain != NULL) {
00122             free(run->userDomain);
00123             run->userDomain = NULL;
00124         }
00125         if (run->domain == NULL) {
00126             struct netsnmp_lookup_domain *tmp = run;
00127             if (prev)
00128                 run = prev->next = run->next;
00129             else
00130                 run = domains = run->next;
00131             free(tmp->application);
00132             free(tmp);
00133         } else {
00134             prev = run;
00135             run = run->next;
00136         }
00137     }
00138 }
00139 
00140 const char*
00141 netsnmp_lookup_default_domain(const char* application)
00142 {
00143     const char *res;
00144 
00145     if (application == NULL)
00146         res = NULL;
00147     else {
00148         struct netsnmp_lookup_domain *run = domains;
00149 
00150         while (run && strcmp(run->application, application) < 0)
00151             run = run->next;
00152         if (run && strcmp(run->application, application) == 0)
00153             if (run->userDomain)
00154                 res = run->userDomain;
00155             else
00156                 res = run->domain;
00157         else
00158             res = NULL;
00159     }
00160     DEBUGMSGTL(("defaults",
00161                 "netsnmp_lookup_default_domain(\"%s\") -> \"%s\"\n",
00162                 application ? application : "[NIL]",
00163                 res ? res : "[NIL]"));
00164     return res;
00165 }
00166 
00167 struct netsnmp_lookup_target {
00168     char* application;
00169     char* domain;
00170     char* userTarget;
00171     char* target;
00172     struct netsnmp_lookup_target* next;
00173 };
00174 
00175 static struct netsnmp_lookup_target* targets = NULL;
00176 
00177 int
00178 netsnmp_register_default_target(const char* application, const char* domain,
00179                                 const char* target)
00180 {
00181     struct netsnmp_lookup_target *run = targets, *prev = NULL;
00182     int i, res = 0;
00183     while (run && ((i = strcmp(run->application, application)) < 0 ||
00184                    (i == 0 && strcmp(run->domain, domain) < 0))) {
00185         prev = run;
00186         run = run->next;
00187     }
00188     if (run && i == 0 && strcmp(run->domain, domain) == 0) {
00189       if (run->target != NULL) {
00190             free(run->target);
00191             run->target = NULL;
00192             res = 1;
00193       }
00194     } else {
00195         run = SNMP_MALLOC_STRUCT(netsnmp_lookup_target);
00196         run->application = strdup(application);
00197         run->domain = strdup(domain);
00198         run->userTarget = NULL;
00199         if (prev) {
00200             run->next = prev->next;
00201             prev->next = run;
00202         } else {
00203             run->next = targets;
00204             targets = run;
00205         }
00206     }
00207     if (target) {
00208         run->target = strdup(target);
00209     } else if (run->userTarget == NULL) {
00210         if (prev)
00211             prev->next = run->next;
00212         else
00213             targets = run->next;
00214         free(run->domain);
00215         free(run->application);
00216         free(run);
00217     }
00218     return res;
00219 }
00220 
00221 void
00222 netsnmp_clear_default_target(void)
00223 {
00224     while (targets) {
00225         struct netsnmp_lookup_target *tmp = targets;
00226         targets = targets->next;
00227         free(tmp->application);
00228         free(tmp->domain);
00229         free(tmp->userTarget);
00230         free(tmp->target);
00231         free(tmp);
00232     }
00233 }
00234 
00235 static void
00236 netsnmp_register_user_target(const char* token, char* cptr)
00237 {
00238     struct netsnmp_lookup_target *run = targets, *prev = NULL;
00239     size_t len = strlen(cptr) + 1;
00240     char* application = (char*)malloc(len);
00241     char* domain = (char*)malloc(len);
00242     char* target = (char*)malloc(len);
00243     int i;
00244 
00245     {
00246         char* cp = copy_nword(cptr, application, len);
00247         cp = copy_nword(cp, domain, len);
00248         cp = copy_nword(cp, target, len);
00249         if (cp)
00250             config_pwarn("Trailing junk found");
00251     }
00252 
00253     while (run && ((i = strcmp(run->application, application)) < 0 ||
00254                    (i == 0 && strcmp(run->domain, domain) < 0))) {
00255         prev = run;
00256         run = run->next;
00257     }
00258     if (run && i == 0 && strcmp(run->domain, domain) == 0) {
00259         if (run->userTarget != NULL) {
00260             config_perror("Default target already registered for this "
00261                           "application-domain combination");
00262             goto done;
00263         }
00264     } else {
00265         run = SNMP_MALLOC_STRUCT(netsnmp_lookup_target);
00266         run->application = strdup(application);
00267         run->domain = strdup(domain);
00268         run->target = NULL;
00269         if (prev) {
00270             run->next = prev->next;
00271             prev->next = run;
00272         } else {
00273             run->next = targets;
00274             targets = run;
00275         }
00276     }
00277     run->userTarget = strdup(target);
00278  done:
00279     free(target);
00280     free(domain);
00281     free(application);
00282 }
00283 
00284 static void
00285 netsnmp_clear_user_target(void)
00286 {
00287     struct netsnmp_lookup_target *run = targets, *prev = NULL;
00288 
00289     while (run) {
00290         if (run->userTarget != NULL) {
00291             free(run->userTarget);
00292             run->userTarget = NULL;
00293         }
00294         if (run->target == NULL) {
00295             struct netsnmp_lookup_target *tmp = run;
00296             if (prev)
00297                 run = prev->next = run->next;
00298             else
00299                 run = targets = run->next;
00300             free(tmp->application);
00301             free(tmp->domain);
00302             free(tmp);
00303         } else {
00304             prev = run;
00305             run = run->next;
00306         }
00307     }
00308 }
00309 
00310 const char*
00311 netsnmp_lookup_default_target(const char* application, const char* domain)
00312 {
00313     int i;
00314     struct netsnmp_lookup_target *run = targets;
00315     const char *res;
00316 
00317     if (application == NULL || domain == NULL)
00318         res = NULL;
00319     else {
00320         while (run && ((i = strcmp(run->application, application)) < 0 ||
00321                        (i == 0 && strcmp(run->domain, domain) < 0)))
00322             run = run->next;
00323         if (run && i == 0 && strcmp(run->domain, domain) == 0)
00324             if (run->userTarget != NULL)
00325                 res = run->userTarget;
00326             else
00327                 res = run->target;
00328         else
00329             res = NULL;
00330     }
00331     DEBUGMSGTL(("defaults",
00332                 "netsnmp_lookup_default_target(\"%s\", \"%s\") -> \"%s\"\n",
00333                 application ? application : "[NIL]",
00334                 domain ? domain : "[NIL]",
00335                 res ? res : "[NIL]"));
00336     return res;
00337 }
00338 
00339 void
00340 netsnmp_register_service_handlers(void)
00341 {
00342     register_config_handler("snmp:", "defDomain",
00343                             netsnmp_register_user_domain,
00344                             netsnmp_clear_user_domain,
00345                             "application domain");
00346     register_config_handler("snmp:", "defTarget",
00347                             netsnmp_register_user_target,
00348                             netsnmp_clear_user_target,
00349                             "application domain target");
00350 }