Esempio n. 1
0
int 
do_resolve()
{
    int ip1, ip2, ip3, ip4;
    int prt, myprt;
    int doagain;
    char *result;
    const char *ptr;
    char buf[1024];
    long fullip;

    ip1 = ip2 = ip3 = ip4 = prt = myprt = -1;
    do {
        doagain = 0;
        *buf = '\0';
	result = fgets(buf, sizeof(buf), stdin);
	if (!result) {
	    if (errno == EAGAIN) {
		doagain = 1;
		sleep(1);
	    } else {
		if (feof(stdin)) erreturn;
	        perror("fgets");
	        erreturn;
	    }
        }
    } while (doagain || !strcmp(buf,"\n"));

    sscanf(buf, "%d.%d.%d.%d(%d)%d", &ip1, &ip2, &ip3, &ip4, &prt, &myprt);
    if (ip1 < 0 || ip2 < 0 || ip3 < 0 || ip4 < 0 || prt < 0) erreturn;
    if (ip1>255 || ip2>255 || ip3>255 || ip4>255 || prt>65535) erreturn;
    if (myprt > 65535 || myprt < 0) erreturn;

    fullip = (ip1 << 24) | (ip2 << 16) | (ip3 << 8) | ip4;
    fullip = htonl(fullip);
    ptr = addrout(fullip, prt, myprt);
    printf("%d.%d.%d.%d(%d):%s\n",
            ip1, ip2, ip3, ip4, prt, ptr
    );
    fflush(stdout);
    return 1;
}
Esempio n. 2
0
static int do_resolve(void) {
    int ip1, ip2, ip3, ip4;
    int prt, myprt;
    int doagain;
    char *result;
    const char *ptr;
    char buf[1024];
    char outbuf[1024];
    char *bufptr = NULL;
    long fullip;

#ifdef USE_IPV6
    struct in6_addr fullip_v6;
#endif

    for (;;) {
	ip1 = ip2 = ip3 = ip4 = prt = myprt = -1;
	do {
	    doagain = 0;
	    *buf = '\0';

	    /* lock input here. */
	    if (pthread_mutex_lock(&input_mutex)) {
		return 0;
	    }
	    if (shutdown_was_requested) {
		/* unlock input here. */
		pthread_mutex_unlock(&input_mutex);
		return 0;
	    }

	    result = fgets(buf, sizeof(buf), stdin);

            /* detect if QUIT was requested before letting
               another thread start reading */
	    if (!result) {
		if (errno == EAGAIN) {
		    doagain = 1;
		} else {
		    if (!feof(stdin)) {
                        perror("fgets");
                    }
		    shutdown_was_requested = 1;
		}
	    } else if (!strncmp("QUIT", buf, 4)) {
                shutdown_was_requested = 1;
                fclose(stdin);
            }

	    /* unlock input here. */
	    pthread_mutex_unlock(&input_mutex);

	    if (shutdown_was_requested) {
		return 0;
	    } else if (doagain) {
                sleep(1);
            }
	} while (doagain || !strcmp(buf, "\n"));

	bufptr = NULL;
#ifdef USE_IPV6
	bufptr = strchr(buf, ':');
	if (bufptr) {
	    /* Is an IPv6 addr. */
	    bufptr = strchr(buf, '(');
	    if (!bufptr) {
		continue;
	    }
	    sscanf(bufptr, "(%d)%d", &prt, &myprt);
	    *bufptr = '\0';
	    if (myprt > 65535 || myprt < 0) {
		continue;
	    }
	    if (inet_pton(AF_INET6, buf, &fullip_v6) <= 0) {
		continue;
	    }
	    ptr = addrout_v6(&fullip_v6, prt, myprt);
	    snprintf(outbuf, sizeof(outbuf), "%s(%d)|%s", buf, prt, ptr);
	}
#endif
	if (!bufptr) {
	    /* Is an IPv4 addr. */
	    sscanf(buf, "%d.%d.%d.%d(%d)%d", &ip1, &ip2, &ip3, &ip4, &prt, &myprt);
	    if (ip1 < 0 || ip2 < 0 || ip3 < 0 || ip4 < 0 || prt < 0) {
		continue;
	    }
	    if (ip1 > 255 || ip2 > 255 || ip3 > 255 || ip4 > 255 || prt > 65535) {
		continue;
	    }
	    if (myprt > 65535 || myprt < 0) {
		continue;
	    }

	    fullip = (ip1 << 24) | (ip2 << 16) | (ip3 << 8) | ip4;
	    fullip = htonl(fullip);

	    ptr = addrout(fullip, prt, myprt);
	    snprintf(outbuf, sizeof(outbuf), "%d.%d.%d.%d(%d)|%s", ip1, ip2, ip3, ip4, prt,
		     ptr);
	}

	/* lock output here. */
	if (pthread_mutex_lock(&output_mutex)) {
	    return 0;
	}

	fprintf(stdout, "%s\n", outbuf);
	fflush(stdout);

	/* unlock output here. */
	pthread_mutex_unlock(&output_mutex);
    }
}