Beispiel #1
0
int main (int argc, char **argv)
{
	int s;
#ifdef WIN32
	int err;

	if ((err = InitWinsock()) != 0)
	{
		printf("InitWinsock: Unable to initialize winsock 1.1 (%u)\n", err);
		return 1;
	}
#endif
	if ((argc < 3) || (argc > 4))
	{
		printf("Usage: failover <port> <mainip> [failoverip]\n");
		return 1;
	}

	// connect to the mainip's port
	if ((s = opentcp(argv[2], atoi(argv[1]), 10)) == -1)
	{
		// connection failed, return the error or failoverip
		if (argc == 3)
			printf("%s\n", strserr());
		else
			printf("%s\n", argv[3]);
	}
	else
	{
		closetcp(s);
		// connection succeeded, return the mainip
		printf("%s\n", argv[2]);
	}

	return 0;
}
Beispiel #2
0
int main(int argc, char **argv)
{
    unsigned int ports[3] = {0};
    int sockets[3] = {-1, -1, -1};
    int c;
    while ((c = getopt(argc, argv, "+i:o:e:")) != -1) {
        switch (c) {
        case 'i':
            ports[0] = strtoul(optarg, NULL, 10);
            break;

        case 'o':
            ports[1] = strtoul(optarg, NULL, 10);
            break;

        case 'e':
            ports[2] = strtoul(optarg, NULL, 10);
            break;

        default:
            usage(argv[0]);
        }
    }

    if (optind == argc) {
        fprintf(stderr, "%s: missing program argument\n", argv[0]);
        usage(argv[0]);
    }

    for (int i = 0; i < 3; i++) {
        if (ports[i] != 0) {
            int j;
            for (j = 0; j < i; j++) {
                if (ports[i] == ports[j]) {
                    int s = dup(sockets[j]);
                    if (s < 0) {
                        perror("dup");
                        return 1;
                    }
                    sockets[i] = s;
                    break;
                }
            }

            if (j == i) {
                int s = tcpmode ? opentcp(ports[i]) : openvsock(VMADDR_CID_HOST, ports[i]);
                if (s < 0) {
                    return 1;
                }
                sockets[i] = s;
            }
        }
    }

    for (int i = 0; i < 3; i++) {
        if (sockets[i] >= 0) {
            dup2(sockets[i], i);
            close(sockets[i]);
        }
    }

    execvp(argv[optind], argv + optind);
    fprintf(stderr, "execvp: %s: %s\n", argv[optind], strerror(errno));
    return 1;
}