コード例 #1
0
int main()
{
        char ip_address[16];
        printf("Enter the IP address u want to validate\n");
        gets(ip_address);
        //printf("Here\n");
        if(isValidIP(ip_address)){
                printf("valid ipv4 address!\n");
        }
        else{
                printf("invalid ipv4 address!\n");
        }

        return 0;
}
コード例 #2
0
 void restoreIpAddressesDFS(vector<string>& results, string oneSolution, string s, int byte) {
     if (byte == 4) {
         if (s.empty()) {
             results.push_back(oneSolution);
         }
         return;
     }
     for (int i = 1; i <= 3; i++) {
         if (i <= s.length() && isValidIP(s.substr(0, i))) {
             string old_oneSolution = oneSolution;
             oneSolution = oneSolution.empty() ? s.substr(0, i) : oneSolution + "." + s.substr(0, i);
             restoreIpAddressesDFS(results, oneSolution, s.substr(i), byte + 1);
             oneSolution = old_oneSolution;
         }
     }
 }
コード例 #3
0
ファイル: window.cpp プロジェクト: qtdevel/qgateway
//------------------------------------------------------------------------------------------------
void Window::addHost()
{
    bool changed = false;
    QString ip = gwEdit->text();
    if (isValidIP(ip))
    {
        gwList->addItem(ip);
        gwEdit->clear();
        changed = true;
    }
    canSwitch = ((gwList->count() > 1) && ((switchMode != mNoGateway) && (switchMode != mOnlyGateway)));
    gwList->clearSelection();
    selGw.clear();
    if (changed)
        gatewayListChanged();
}
コード例 #4
0
 void rec(string s, int start, int end, vector<string> &res, string curr, int dots) {
     if(dots>4) return;
     if((dots==4 ) && (start==(s.size()))) 
       res.push_back(curr);
     
     int min_end = min(end, start+3);
     for(int i=start;i<=min_end;i++) {
         auto new_curr = curr;
         if(isValidIP(s, start, i)){ 
             new_curr+=(s.substr(start, i-start+1));
             if(dots<3) new_curr+=".";
             rec(s, i+1, end, res, new_curr,dots+1);
         }
        if(s[start]=='0') break;
     }
 }
コード例 #5
0
char * nslookup(char *hostname)
{

    int sz = 128;
    char func_msg[sz];
    static char ipaddr[16];

    if (!globals.use_resolver)
    {
        return hostname;
    }

    if (isValidIP(hostname))
    {
        return hostname;
    }
    struct addrinfo hints, *res;
    struct in_addr addr;
    int err;

    memset(&hints, 0, sizeof (hints));
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_family = AF_INET;

    if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0)
    {
        memset(func_msg, '\0', sz);
        snprintf(func_msg, sz, "GETADDRINFO error for host: %s: %d %s", hostname, err, strerror(errno));
        pthread_mutex_lock(&t_error);
        writeToCustomLog(func_msg);
        pthread_mutex_unlock(&t_error);
        return NULL;
    }

    addr.s_addr = ((struct sockaddr_in *) (res->ai_addr))->sin_addr.s_addr;
    if (res)freeaddrinfo(res);
    snprintf(ipaddr, sizeof (ipaddr), "%s", inet_ntoa(addr));
    return ipaddr;
}
コード例 #6
0
ファイル: arewik.c プロジェクト: arthurtumanyan/arewik
/**
 *
 * @param argc
 * @param argv
 * @return
 */
int main(int argc, char** argv) {

    if (0 != getuid()) {
        printf("%s", INSUFF_PRIV);
        exit(EXIT_FAILURE);
    }
    start_time = time(NULL);
    char *host = NULL;
    int opt = 0;
    int longIndex = 0;

    memset(globals.routes, 0, ROUTES_MAX_COUNT);
    //
    curl_global_init(CURL_GLOBAL_ALL);
    //
    watchdog_stop_flag = false;
    sheduler_stop_flag = false;
    pbuffer_stop_flag = false;
    rotate_stop_flag = false;
    listen_stop_flag = false;

    /* Initialize arguments before we get to work. */
    char * argument = NULL;

    argument = "/var/run/arewik.pid";
    arguments.pidfile = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.pidfile, argument);

    argument = "/var/log/arewik";
    arguments.logdir = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.logdir, argument);

    argument = "/var/arewik";
    arguments.storagedir = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.storagedir, argument);

    argument = "/var/arewik/buffers";
    arguments.bufferdir = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.bufferdir, argument);

    argument = "/etc/arewik/arewik.conf";
    arguments.configfile = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.configfile, argument);

    argument = "arewik";
    arguments.user = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.user, argument);

    argument = "arewik";
    arguments.group = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.group, argument);

    argument = "/tmp";
    arguments.wdir = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.wdir, argument);

    argument = DEFAULT_LISTEN_IP;
    arguments.listen_host = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(arguments.listen_host, argument);

    arguments.listen_port = DEFAULT_LISTEN_PORT;
    arguments.verbosity = 0;
    arguments.foreground = 0;
    arguments.debuginfo = 0;

    argument = "custom.log";
    globals.custom_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(globals.custom_logfile_name, argument);

    argument = "debug.log";
    globals.debug_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(globals.debug_logfile_name, argument);

    argument = "connections.log";
    globals.connections_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(globals.connections_logfile_name, argument);

    argument = "access.log";
    globals.access_logfile_name = xmalloc(sizeof (char) * strlen(argument) + 1);
    strcpy(globals.access_logfile_name, argument);

    globals.maxcon = 4096; // routes max * 64
    globals.proxymode = false; //todo
    globals.routes_cnt = 1;
    globals.watchdog_interval = WATCHDOG_T;
    globals.ping_interval = 30;
    globals.autoreconfigure = false;
    globals.reconfigure_interval = 60;
    globals.use_resolver = 0;
    globals.use_syslog = 0;
    globals.socktimeout = 10000;
    globals.epolltimeout = EPOLL_RUN_TIMEOUT;
    globals.workers = POSSIBLE_FHNDL;

    sprintf(globals.identline, "%s", ident);
    //
    globals.modules[0].id = 0;
    globals.modules[0].enabled = true;
    globals.modules[0].name = "plain";
    globals.modules[0].process_function = &process_plain;
    globals.modules[0].init_function = NULL;

    globals.modules[1].id = 1;
    globals.modules[1].enabled = false;
    globals.modules[1].name = "riak";
    globals.modules[1].process_function = &process_riak;
    globals.modules[1].init_function = &init_riak_module;
    globals.modules[1].close_function = &close_riak;

    globals.modules[2].id = 2;
    globals.modules[2].enabled = false;
    globals.modules[2].name = "esearch";
    globals.modules[2].process_function = &process_esearch;
    globals.modules[2].init_function = &init_esearch_module;
    globals.modules[2].close_function = &close_esearch;


    globals.modules[3].id = 3;
    globals.modules[3].enabled = false;
    globals.modules[3].name = "webhdfs";
    globals.modules[3].process_function = &process_webhdfs;
    globals.modules[3].init_function = &init_webhdfs_module;
    globals.modules[3].close_function = &close_webhdfs;


    CPRESS_FUNCT[0].type = SNAPPY;
    CPRESS_FUNCT[0].compress_function = &compress_snappy;

    CPRESS_FUNCT[1].type = GZIP;
    CPRESS_FUNCT[1].compress_function = &compress_gzip;


    set_sig_handler();
    static const char *optString = "s:D:B:u:g:p:l:c:H:P:vfhd?";
    opt = getopt_long(argc, argv, optString, longOpts, &longIndex);

    while (opt != -1) {
        switch (opt) {
            case 'p':
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                arguments.pidfile = xrealloc(arguments.pidfile, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.pidfile, optarg);

                break;

            case 'l':
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                arguments.logdir = xrealloc(arguments.pidfile, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.logdir, optarg);
                break;

            case 'c':
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                arguments.configfile = xrealloc(arguments.configfile, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.configfile, optarg);
                break;

            case 'H':
                if (!isValidIP(optarg)) {
                    print_usage();
                }
                if (NULL != (host = nslookup(optarg))) {
                    arguments.listen_host = xrealloc(arguments.listen_host, (sizeof (char) * strlen(optarg) + 1));
                    strcpy(arguments.listen_host, host);
                } else {
                    printf("%s\n", hstrerror(h_errno));
                    exit(EXIT_FAILURE);
                }
                break;
            case 'u':
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                arguments.user = xrealloc(arguments.user, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.user, optarg);
                break;
            case 'g':
                arguments.group = xrealloc(arguments.group, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.group, optarg);
                break;
            case 'D':
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                arguments.wdir = xrealloc(arguments.wdir, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.wdir, optarg);
                break;
            case 's':
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                arguments.storagedir = xrealloc(arguments.storagedir, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.storagedir, optarg);
                break;
            case 'B':
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                arguments.bufferdir = xrealloc(arguments.bufferdir, (sizeof (char) * strlen(optarg) + 1));
                strcpy(arguments.bufferdir, optarg);
                break;
            case 'P':
                arguments.listen_port = atoi(optarg);
                break;
            case 'v':
                arguments.verbosity++;
                break;
            case 'f':
                arguments.foreground++;
                break;
            case 'h':
                print_usage();
                break;
            case 'd':
                arguments.debuginfo++;
                arguments.verbosity++;
                break;
            default:
                if (!is_valid_opt(optarg)) {
                    print_usage();
                }
                break;
        }

        opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
    }

    if (false == DirectoryExists(arguments.storagedir)) {
        printf("Specified storage directory '%s' does not exists\n", arguments.storagedir);
        printf("Creating new one\n");
        if (0 > mkdir(arguments.storagedir, 0755)) {
            printf("Can not create storage directory - %s\n", strerror(errno));
            exit(EXIT_FAILURE);
        } else {
            setRightOwner(arguments.storagedir);
        }
    }
    setRightOwner(arguments.storagedir);

    if (false == DirectoryExists(arguments.bufferdir)) {
        printf("Specified buffer directory '%s' does not exists\n", arguments.bufferdir);
        printf("Creating new one\n");
        if (0 > mkdir(arguments.bufferdir, 0755)) {
            printf("Can not create buffer directory - %s\n", strerror(errno));
            exit(EXIT_FAILURE);
        } else {
            setRightOwner(arguments.bufferdir);
        }
    }
    setRightOwner(arguments.bufferdir);

    if (true == DirectoryExists(arguments.pidfile)) {
        printf("Specified pid file '%s' is a directory\n", arguments.pidfile);
        exit(EXIT_FAILURE);
    }

    if (false == DirectoryExists(arguments.wdir)) {
        printf("Specified working directory '%s' does not exists or not a directory\n", arguments.wdir);
        exit(EXIT_FAILURE);
    }

    if (arguments.foreground == 1) {
        printf("Running in foreground mode...\n");
        setbuf(stdout, 0);
        setbuf(stdin, 0);
        setbuf(stderr, 0);
    }

    if (arguments.verbosity == 1) {
        printf("Verbosity enabled\n");
    }

    if (false == FileExists(arguments.configfile)) {
        printf("Specified file '%s' does not exists\n", arguments.configfile);
        exit(EXIT_FAILURE);
    } else if (true == DirectoryExists(arguments.configfile)) {
        printf("Specified file '%s' is a directory\n", arguments.configfile);
        exit(EXIT_FAILURE);
    } else {
        setRightOwner(arguments.configfile);
        if (true != hasRightOwner(arguments.configfile)) {
            printf("The file '%s' has invalid owner/group\nMust be %s:%s\n", arguments.configfile, arguments.user, arguments.group);
            exit(EXIT_FAILURE);
        }
    }

    if (false == DirectoryExists(arguments.logdir)) {
        printf("Specified log directory '%s' does not exists\n", arguments.logdir);
        printf("Creating new one\n");
        if (0 > mkdir(arguments.logdir, 0755)) {
            printf("Can not create log directory - %s\n", strerror(errno));
            exit(EXIT_FAILURE);
        } else {
            setRightOwner(arguments.logdir);
        }
    }
    setRightOwner(arguments.logdir);

    if (true != hasRightOwner(arguments.logdir)) {
        printf("The file '%s' has invalid owner/group\nMust be %s:%s\n", arguments.logdir, arguments.user, arguments.group);
        exit(EXIT_FAILURE);
    }


    glob = readConfig(arguments.configfile);
    checkPid();

    openCustomLog();
    openDebugLog();
    openConLog();
    openAccessLog();

    if (!(pwd = getpwnam(arguments.user))) {
        snprintf(TMP_MSG, MAXLINE, "No such user: %s. Quitting!", arguments.user);
        writeToCustomLog(TMP_MSG);

        exit(EXIT_FAILURE);
    } else my_uid = pwd->pw_uid;

    if (!(grp = getgrnam(arguments.group))) {
        snprintf(TMP_MSG, MAXLINE, "No such group: %s. Quitting!", arguments.group);
        writeToCustomLog(TMP_MSG);
        exit(EXIT_FAILURE);
    } else my_gid = grp->gr_gid;

    if (arguments.foreground == 0) {

        pid = fork();

        if (pid < 0) {
            snprintf(TMP_MSG, MAXLINE, "%s", "Can not fork! [Invalid PID]");
            writeToCustomLog(TMP_MSG);
            removePid();
            exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
            exit(EXIT_SUCCESS);
        }
        //
        snprintf(TMP_MSG, MAXLINE, "%s", "Forked successfully");
        writeToCustomLog(TMP_MSG);
        /* Change the file mode mask */
        umask(027);
        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
            snprintf(TMP_MSG, MAXLINE, "%s", "Can not create child process");
            writeToCustomLog(TMP_MSG);
            /* Log the failure */
            removePid();
            exit(EXIT_FAILURE);
        }
        snprintf(TMP_MSG, MAXLINE, "%s", "Daemonizing");
        writeToCustomLog(TMP_MSG);

        /* Change the current working directory */
        if ((chdir(arguments.wdir)) < 0) {
            snprintf(TMP_MSG, MAXLINE, "%s", "Can not change current directory");
            writeToCustomLog(TMP_MSG);
            /* Log the failure */
            exit(EXIT_FAILURE);
        }
        snprintf(TMP_MSG, MAXLINE, "%s", "Working directory changed");
        writeToCustomLog(TMP_MSG);
    }
    savePid();

    if ((setgid(my_gid)) < 0) {
        snprintf(TMP_MSG, MAXLINE, "ERROR: setgid(%d) failed: %s", my_gid, strerror(errno));
        writeToCustomLog(TMP_MSG);
        halt();
    }
    snprintf(TMP_MSG, MAXLINE, "Group ID changed to %d", my_gid);
    writeToCustomLog(TMP_MSG);

    if ((setuid(my_uid)) < 0) {
        snprintf(TMP_MSG, MAXLINE, "ERROR: setuid(%d) failed: %s", my_uid, strerror(errno));
        writeToCustomLog(TMP_MSG);
        halt();
    }
    snprintf(TMP_MSG, MAXLINE, "User ID changed to %d", my_gid);
    writeToCustomLog(TMP_MSG);


    if (arguments.foreground == 0) {
        snprintf(TMP_MSG, MAXLINE, "%s", "Closing descriptors & disabling verbosity.\nEnsure you have set right permissions for your log file.\nWatch your log file for further information\n");
        VERBOSE(TMP_MSG);
        arguments.verbosity = 0;
        /* Close out the standard file descriptors */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
    }
    ////////////////////////////////////////////////////////////////////////////////

    //
    init_arrays();

    init_webhdfs_arrays();

    init_active_c_table();
    init_descriptors();
    scan_connections();
    //
    ////////////////////////////////////////////////////////////////////////////////
    spawn_threads();
    do_listen();

    ////////////////////////////////////////////////////////////////////////////////
    /* This never happens */
    return 0;
}
コード例 #7
0
ファイル: window.cpp プロジェクト: qtdevel/qgateway
//------------------------------------------------------------------------------------------------
void Window::gwTextChanged(const QString & text)
{
    addGw->setEnabled(isValidIP(text));
}
コード例 #8
0
ファイル: window.cpp プロジェクト: qtdevel/qgateway
//------------------------------------------------------------------------------------------------
void Window::readSettings()
{
    setInetStatus(imSwitching);

    QSettings settings(iniFileName(), QSettings::IniFormat);
    int val;
    QString key;

    key = "Options/";
    val = settings.value(key + "ConnectionTimeout", QVariant(8)).toInt();
    if (val < connTimeout->minimum())
        val = connTimeout->minimum();
    if (val > connTimeout->maximum())
        val = connTimeout->maximum();
    connTimeout->setValue(val);
    val = settings.value(key + "PriorityTimeout", QVariant(10)).toInt();
    if (val < prioTimeout->minimum())
        val = prioTimeout->minimum();
    if (val > prioTimeout->maximum())
        val = prioTimeout->maximum();
    prioTimeout->setValue(val);
    val = settings.value(key + "PingTimeout", QVariant(800)).toInt();
    if (val < pingTimeout->minimum())
        val = pingTimeout->minimum();
    if (val > pingTimeout->maximum())
        val = pingTimeout->maximum();
    pingTimeout->setValue(val);

    bool bval;
    bval = settings.value(key + "RunOnStartup", QVariant(false)).toBool();
    runOnStartup->setChecked(bval);

    bval = settings.value(key + "ShowBalloons", QVariant(true)).toBool();
    showBalloons->setChecked(bval);

    bval = settings.value(key + "AskOnQuit", QVariant(true)).toBool();
    askOnQuit->setChecked(bval);

    bval = settings.value(key + "WriteGatewayOnExit", QVariant(true)).toBool();
    writeGwOnExit->setChecked(bval);

    indexGateway = -1;

    canSwitch = (gwList->count() > 1);

    pingHost = settings.value(key + "PingHost", QVariant("8.8.8.8")).toString();
    if (pingHost.isEmpty() || !isValidIP(pingHost))
        pingHost = GOOGLE_DNS;
    pingHostEdit->setText(pingHost);

    key = "Gateways/";
    QStringList defaultGateways;
    defaultGateways << "172.16.10.1" << "172.16.10.5";

    //  avoid redundant calling slot
    disconnect(switchType, SIGNAL(currentIndexChanged(int)),
               this, SLOT(switchTypeChanged(int)));
    //  remove gateways from combobox
    int c = switchType->count();
    for (int i = 4; i < c; i++)
        switchType->removeItem(4);
    switchType->setCurrentIndex(-1);
    //  connect again
    connect(switchType, SIGNAL(currentIndexChanged(int)),
            this, SLOT(switchTypeChanged(int)));

    QStringList gateways = settings.value(key + "GatewayList", QVariant(defaultGateways)).toStringList();

    gwList->addItems(gateways);
    switchType->addItems(gateways);
    gwListChanged = false;

    key = "MainWindow/";
    resize((const QSize &)settings.value(key + "Size", QVariant(QSize(550, 300))));

    key = "Options/";
    val = settings.value(key + "SwitchType", QVariant(0)).toInt();
    if (val < 0)
        val = 0;
    if (val >= switchType->count())
        val = 0;
    switchType->setCurrentIndex(val);
}
コード例 #9
0
ファイル: window.cpp プロジェクト: qtdevel/qgateway
//------------------------------------------------------------------------------------------------
void Window::pingHostEdited(const QString & ipAddress)
{
    if (isValidIP(ipAddress))
        pingHost = ipAddress;
    settingsChanged();
}