Ejemplo n.º 1
0
int DispatchInit(const char *file_cfg)
{
    FILE *fp;
    char module_dir[CFG_LINE_MAX_SIZE];
    char buffer[CFG_LINE_MAX_SIZE];
    char bufcpy[CFG_LINE_MAX_SIZE];
    char module_path[CFG_LINE_MAX_SIZE];
    char module_name[CFG_LINE_MAX_SIZE];
    char mask[CFG_LINE_MAX_SIZE];
    char manip_name[CFG_LINE_MAX_SIZE];
    char manip_host[CFG_LINE_MAX_SIZE];
    char manip_bin[CFG_LINE_MAX_SIZE];
    unsigned int manip_port;
    char *param;
    unsigned short logm;
    int res, nl, val, i;
    pthread_t pid;
    
    /* default */
    parallel = FALSE;
    pei_ins = 0;
    pei_pend = 0;
    manip = NULL;
    manip_num = 0;

    /* find directory location of module from config file */
    fp = fopen(file_cfg, "r");
    if (fp == NULL) {
        LogPrintf(LV_ERROR, "Config file can't be opened");
        return -1;
    }
    
    /* copy path */
    config_path = xmalloc(strlen(file_cfg) + 1);
    strcpy(config_path, file_cfg);

    /* modules */
    module_dir[0] = '\0';
    module_name[0] = '\0';
    manip_bin[0] = '\0';
    manip_host[0] = '\0';
    nl = 0;
    while (fgets(buffer, CFG_LINE_MAX_SIZE, fp) != NULL) {
        nl++;
        /* check all line */
        if (strlen(buffer)+1 == CFG_LINE_MAX_SIZE) {
            LogPrintf(LV_ERROR, "Config file line more length to %d characters", CFG_LINE_MAX_SIZE);
            return -1;
        }
        /* check if line is a comment */
        if (!CfgParIsComment(buffer)) {
            /* modules directory */
            param = strstr(buffer, CFG_PAR_MODULES_DIR);
            if (param != NULL) {
                if (module_dir[0] != '\0') {
                    LogPrintf(LV_ERROR, "Config param error: param '%s' defined two times", CFG_PAR_MODULES_DIR);
                    return -1;
                }
                res = sscanf(param, CFG_PAR_MODULES_DIR"=%s %s", module_dir, bufcpy);
                if (res > 0) {
                    if (res == 2 && !CfgParIsComment(bufcpy)) {
                        LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, bufcpy);
                        return -1;
                    }
                }
            }
            /* dispatcher module name */
            param = strstr(buffer, CFG_PAR_DISPATCH"=");
            if (param != NULL) {
                if (module_name[0] != '\0') {
                    LogPrintf(LV_ERROR, "Config param error: param '%s' defined two times", CFG_PAR_DISPATCH);
                    return -1;
                }
                res = sscanf(param, CFG_PAR_DISPATCH"=%s %s", module_name, bufcpy);
                if (res > 0) {
                    if (res == 2 && !CfgParIsComment(bufcpy)) {
                        /* log mask */
                        res = strncmp(bufcpy, CFG_PAR_MODULE_LOG, strlen(CFG_PAR_MODULE_LOG));
                        if (res != 0) {
                            LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, bufcpy);
                            return -1;
                        }
                        param = strstr(param, CFG_PAR_MODULE_LOG);
                        res = sscanf(param, CFG_PAR_MODULE_LOG"=%s %s", mask, bufcpy);
                        logm = LV_BASE;
                        if (res > 0) {
                            if (res == 2 && !CfgParIsComment(bufcpy)) {
                                LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, bufcpy);
                                return -1;
                            }
                            logm |= CfgParLogMask(mask, nl);
                        }
                        else {
                            LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, buffer);
                            return -1;
                        }
                        /* set mask */
                        LogSetMask(LOG_COMPONENT, logm);
                    }
                }
            }

            /* parallel o serial insert */
            param = strstr(buffer, CFG_PAR_DISPATCH_PARAL);
            if (param != NULL) {
                res = sscanf(param, CFG_PAR_DISPATCH_PARAL"=%i %s", &val, bufcpy);
                if (res > 0) {
                    if (res == 2 && !CfgParIsComment(bufcpy)) {
                        LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, bufcpy);
                        return -1;
                    }
                    else {
                        if (val == 1)
                            parallel = TRUE;
                        else
                            parallel = FALSE;
                    }
                }
            }

            /* manipulator connection info */
            param = strstr(buffer, CFG_PAR_DISPATCH_MANIP_NAME"=");
            if (param != NULL) {
                res = sscanf(param, CFG_PAR_DISPATCH_MANIP_NAME"=%s %s", manip_name, bufcpy);
                if (res > 0) {
                    if (res == 2 && !CfgParIsComment(bufcpy)) {
                        /* manipulator host */
                        res = strncmp(bufcpy, CFG_PAR_DISPATCH_MANIP_HOST, strlen(CFG_PAR_DISPATCH_MANIP_HOST));
                        if (res != 0) {
                            param = strstr(param, CFG_PAR_DISPATCH_MANIP_BIN);
                            if (param != NULL) {
                                res = sscanf(param, CFG_PAR_DISPATCH_MANIP_BIN"=%s %s", manip_bin, bufcpy);
                                if (res > 0) {
                                    /* inset manip in table */
                                    manip = xrealloc(manip, sizeof(manip_con)*(manip_num + 1));
                                    memset(&(manip[manip_num]), 0, sizeof(manip_con));
                                    strcpy(manip[manip_num].name, manip_name);
                                    strcpy(manip[manip_num].host, manip_host);
                                    strcpy(manip[manip_num].bin, manip_bin);
                                    manip[manip_num].port = 0;
                                    manip[manip_num].sock = -1;
                                    manip[manip_num].wait = FALSE;
                                    manip[manip_num].peil = NULL;
                                    manip[manip_num].peilast = NULL;
                                    /* check pei of protocol */
                                    manip[manip_num].pid = ProtId(manip[manip_num].name);
                                    manip[manip_num].mux = xmalloc(sizeof(pthread_mutex_t));
                                    pthread_mutex_init(manip[manip_num].mux, NULL);
                                    if (manip[manip_num].pid == -1) {
                                        LogPrintf(LV_WARNING, "Protocol Manipulator %s haven't PEI", manip[manip_num].name);
                                    }
                                    manip_num++;
                                }
                            }
                            else {
                                LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, bufcpy);
                                return -1;
                            }
                        }
                        else {
                            param = strstr(param, CFG_PAR_DISPATCH_MANIP_HOST);
                            res = sscanf(param, CFG_PAR_DISPATCH_MANIP_HOST"=%s %s", manip_host, bufcpy);
                            if (res > 0) {
                                if (res == 2 && !CfgParIsComment(bufcpy)) {
                                    res = strncmp(bufcpy, CFG_PAR_DISPATCH_MANIP_PORT, strlen(CFG_PAR_DISPATCH_MANIP_PORT));
                                    if (res != 0) {
                                        LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, bufcpy);
                                        return -1;
                                    }
                                    param = strstr(param, CFG_PAR_DISPATCH_MANIP_PORT);
                                    res = sscanf(param, CFG_PAR_DISPATCH_MANIP_PORT"=%d %s", &manip_port, bufcpy);
                                    if (res > 0) {
                                        if (res == 2 && !CfgParIsComment(bufcpy)) {
                                            LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, buffer);
                                            return -1;
                                        }
                                        /* inset manip in table */
                                        manip = xrealloc(manip, sizeof(manip_con)*(manip_num + 1));
                                        memset(&(manip[manip_num]), 0, sizeof(manip_con));
                                        strcpy(manip[manip_num].name, manip_name);
                                        strcpy(manip[manip_num].host, manip_host);
                                        manip[manip_num].bin[0] = '\0';
                                        manip[manip_num].port = manip_port;
                                        manip[manip_num].sock = -1;
                                        manip[manip_num].wait = FALSE;
                                        manip[manip_num].peil = NULL;
                                        manip[manip_num].peilast = NULL;
                                        /* check pei of protocol */
                                        manip[manip_num].pid = ProtId(manip[manip_num].name);
                                        manip[manip_num].mux = xmalloc(sizeof(pthread_mutex_t));
                                        pthread_mutex_init(manip[manip_num].mux, NULL);
                                        if (manip[manip_num].pid == -1) {
                                            LogPrintf(LV_WARNING, "Protocol Manipulator %s haven't PEI", manip[manip_num].name);
                                        }
                                        manip_num++;
                                    }
                                    else {
                                        LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, buffer);
                                        return -1;
                                    }
                                }
                                else {
                                    LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, buffer);
                                    return -1;
                                }
                            }
                            else {
                                LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, buffer);
                                return -1;
                            }
                        }
                    }
                    else {
                        LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, buffer);
                        return -1;
                    }
                }
            }
        }
    }
    fclose(fp);

    /* check name */
    if (module_name[0] == '\0') {
        LogPrintf(LV_WARNING, "The dispatch module isn't defined, will be used 'none' dispatch module");
        printf("The dispatch module isn't defined, will be used 'none' dispatch module\n");

        /* default dispatcher */
        strcpy(module_name, "disp_none.so");
    }

    /* module path */
    sprintf(module_path, "%s/%s", module_dir, module_name);

    /* open module */
    handle = dlopen(module_path, RTLD_LAZY);
    if (handle == NULL) {
        printf("Can't load dispatch module %s\n", dlerror());
        return -1;
    }
    
    /* inizilizations of all software that can be used in dispatcer modules */
    /* gearth initialization */
    if (GearthInit(file_cfg) == -1) {
        return -1;
    }
    /* end inizilizations of all software that can be used in dispatcer modules */

    /* attach functions */
    DispInit = dlsym(handle, DISP_INIT_FUN);
    if (DispInit == NULL) {
        printf("Dispatch module don't contain function %s\n", DISP_INIT_FUN);
        return -1;
    }

    DispEnd = dlsym(handle, DISP_END_FUN);
    if (DispEnd == NULL) {
        printf("Dispatch module don't contain function %s\n", DISP_END_FUN);
        return -1;
    }

    DispInsPei = dlsym(handle, DISP_INDPEI_FUN);
    if (DispInsPei == NULL) {
        printf("Dispatch module don't contain function %s\n", DISP_INDPEI_FUN);
        return -1;
    }

    /* initialize dispatcher module */
    if (DispInit(file_cfg) == -1) {
        printf("Dispatch module initialization error\n");
        return -1;
    }
    
    if (DispManipInit() == -1) {
        printf("Dispatch to manipulator initialization error\n");
        return -1;
    }

    /* parallel or serial */
    pthread_mutex_init(&plist_mux, NULL);
    if (parallel == FALSE) {
        /* in this case single dissector that generate and insert one PEI call 'directly' the
           insert function of dispatcher module, otherwise a thread is the middelware from dissectors
           and dispatch function module */
        pthread_cond_init(&plist_cond, NULL);
        plist = NULL;
        plist_end = NULL;
        res = pthread_create(&pid, NULL, DispatchAgent, NULL);
        if (res != 0) {
            printf("Dispatch Agent setup failed");
            LogPrintf(LV_ERROR, "Dispatch Agent setup failed");
            return -1;
        }
        pthread_detach(pid);
    }

    /* manipeagtor info */
    for (i=0; i!=manip_num; i++) {
        LogPrintf(LV_START, "Manipulator ---> %s host:%s port:%d", manip[i].name, manip[i].host, manip[i].port);
    }

    return 0;
}
Ejemplo n.º 2
0
int CapInit(const char *file_cfg, const char *cap)
{
    FILE *fp;
    char module_dir[CFG_LINE_MAX_SIZE];
    char buffer[CFG_LINE_MAX_SIZE];
    char bufcpy[CFG_LINE_MAX_SIZE];
    char mask[CFG_LINE_MAX_SIZE];
    char module_path[CFG_LINE_MAX_SIZE];
    char *param;
    int res, nl;
    unsigned short logm;

    /* find directory location of module from config file */
    fp = fopen(file_cfg, "r");
    if (fp == NULL) {
        LogPrintf(LV_ERROR, "Config file can't be opened");
        return -1;
    }
    module_dir[0] = '\0';
    nl = 0;
    while (fgets(buffer, CFG_LINE_MAX_SIZE, fp) != NULL) {
        nl++;
        /* check all line */
        if (strlen(buffer)+1 == CFG_LINE_MAX_SIZE) {
            LogPrintf(LV_ERROR, "Config file line more length to %d characters", CFG_LINE_MAX_SIZE);
            return -1;
        }
        /* check if line is a comment */
        if (!CfgParIsComment(buffer)) {
            /* modules directory */
            param = strstr(buffer, CFG_PAR_MODULES_DIR);
            if (param != NULL) {
                if (module_dir[0] != '\0') {
                    LogPrintf(LV_ERROR, "Config param error: param '%s' defined two times", CFG_PAR_MODULES_DIR);
                    return -1;
                }
                res = sscanf(param, CFG_PAR_MODULES_DIR"=%s %s", module_dir, bufcpy);
                if (res > 0) {
                    if (res == 2 && !CfgParIsComment(bufcpy)) {
                        LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, bufcpy);
                        return -1;
                    }
                }
            }
            /* log mask */
            param = strstr(buffer, CFG_PAR_CAPTURE_LOG"=");
            if (param != NULL) {
                res = sscanf(param, CFG_PAR_CAPTURE_LOG"=%s %s", mask, bufcpy);
                logm = LV_BASE;
                if (res > 0) {
                    if (res == 2 && !CfgParIsComment(bufcpy)) {
                        LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, bufcpy);
                        return -1;
                    }
                    logm |= CfgParLogMask(mask, nl);
                }
                else {
                    LogPrintf(LV_ERROR, "Config param error in line %d. Unknow param: %s", nl, buffer);
                    return -1;
                }
                /* set mask */
                LogSetMask(LOG_COMPONENT, logm);
            }
        }
    }
    fclose(fp);

    /* module path */
    sprintf(module_path, "%s/cap_%s.so", module_dir, cap);

    /* open module */
    handle = dlopen(module_path, RTLD_NOW);
    if (handle == NULL) {
        printf("Can't load capture module %s\n", dlerror());
        return -1;
    }
    
    /* attach funztions */
    CaptOptions = dlsym(handle, CAPT_OPTIONS_FUN);
    if (CapOptions == NULL) {
        printf("Capture module don't contain function %s\n", CAPT_OPTIONS_FUN);
        return -1;
    }

    CaptOptionsHelp = dlsym(handle, CAPT_OPTIONS_HELP_FUN);
    if (CapOptionsHelp == NULL) {
        printf("Capture module don't contain function %s\n", CAPT_OPTIONS_HELP_FUN);
        return -1;
    }

    CaptMain = dlsym(handle, CAPT_MAIN_FUN);
    if (CapMain == NULL) {
        printf("Capture module don't contain function %s\n", CAPT_MAIN_FUN);
        return -1;
    }


    CaptSource = dlsym(handle, CAPT_SOURCE_FUN);
    
    return 0;
}