Ejemplo n.º 1
0
static bool
parse_argument( struct listener_info *listener_info, int argc, char *argv[] ) {
  int c;

  while ( ( c = getopt_long( argc, argv, short_options, long_options, NULL ) ) != -1 ) {
    switch ( c ) {
      case 'p':
        listener_info->listen_port = strtoport( optarg );
        if ( listener_info->listen_port == 0 ) {
          return false;
        }
        break;
      case 's':
        xfree( ( void * ) ( uintptr_t ) listener_info->switch_daemon );
        listener_info->switch_daemon = xstrdup( optarg );
        break;
      default:
        usage();
        exit( EXIT_SUCCESS );
        return false;
        break;
    }
  }

  listener_info->switch_daemon_argc = argc - optind;
  listener_info->switch_daemon_argv = &argv[ optind ];

  return true;
}
Ejemplo n.º 2
0
int main(int argc, char** argv) {
	// create server
	int servsock;
	struct sockaddr_in servaddr;
	unsigned short port = argc >= 2 ? strtoport(argv[PORTNUM]) : DEFPORT;
	createserver(&servsock, &servaddr, port);

	printf("Server running on port %u.\n", port);
	// server will begin accepting clients again after each game ends
	for (EVER) {
		puts("\nWaiting for players to connect...");

		// wait for players to connect, then assign appropriate values
		int sockarr[NUMPLAYERS];
		struct sockaddr_in addrarr[NUMPLAYERS];
		waitforplayers(servsock, sockarr, addrarr);

		// parent will close sockets and wait for other players to connect
		// child will serve the current game and then terminate
		pid_t pid = fork();
		if (pid < 0) {
			exitwerror("fork", EXIT_ERRNO);
		} else if (pid > 0) {
			int i;
			for (i = 0; i < NUMPLAYERS; ++i) close(sockarr[i]);
			continue;
		}

		// notify players of their player numbers
		sendplayernums(sockarr);

		// inital direction values
		char dirbuffer[SC_STDSIZE];
		dirbuffer[PLAYER_1] = RIGHT;
		dirbuffer[PLAYER_2] = LEFT;

		// keep recieving and sending until a collision signal is recieved
		int collided = -1;
		while (collided == -1) {
			sendvars(sockarr, dirbuffer);				// send directions
			collided = recvclientsignal(dirbuffer, sockarr);	// recieve client information coming back to server socket
		}

		// determine winner
		char winner;
		switch (collided) {
			case PLAYER_1: winner = PLAYER_2; break;
			case PLAYER_2: winner = PLAYER_1; break;
			default: winner = 3;
		}

		// game over, notify clients
		endclients(sockarr, winner);
		break;
	}

	return EXIT_SUCCESS;
}
Ejemplo n.º 3
0
/*
 * cmd->arg3 is count of the destination
 * cmd->arg1 is the type, random 0, round-robin 1, sticky 2
 */
void
parse_forward(ipfw_insn **cmd, int *ac, char **av[])
{
	ipfw_insn_sa *p = (ipfw_insn_sa *)(*cmd);
	struct sockaddr_in *sa;
	char *tok, *end = '\0';
	char *str;
	int count, port;

	(*cmd)->opcode = O_BASIC_FORWARD;
	NEXT_ARG1;
	/*
	 * multiple forward destinations are seperated by colon
	 * ip address and port are seperated by comma
	 * e.g. 192.168.1.1:80,192.168.1.2:8080
	 *      192.168.1.1,192.168.1.2 or keep the port the same
	 */
	tok = strtok(**av, ",");
	sa = &p->sa;
	count = 0;
	while (tok != NULL) {
		sa->sin_len = sizeof(struct sockaddr_in);
		sa->sin_family = AF_INET;
		sa->sin_port = 0;
		str = strchr(tok,':');
		if (str != NULL) {
			*(str++) = '\0';
			port = strtoport(str, &end, 0, 0);
			sa->sin_port = (u_short)port;
		}
		lookup_host(tok, &(sa->sin_addr));
		tok = strtok (NULL, ",");
		sa++;
		count++;
	}
	(*cmd)->arg3 = count;
	if (count == 0) {
		errx(EX_DATAERR, "forward `%s' not recognizable", **av);
	}
	NEXT_ARG1;
	if (count > 1) {
		if (strcmp(**av, "round-robin") == 0) {
			NEXT_ARG1;
			(*cmd)->arg1 = 1;
		} else if (strcmp(**av, "sticky") == 0) {
			NEXT_ARG1;
			(*cmd)->arg1 = 2;
		} else {
			/* random */
			(*cmd)->arg1 = 0;
		}
	}
	(*cmd)->len = LEN_OF_IPFWINSN + count * sizeof(struct sockaddr_in);
}
Ejemplo n.º 4
0
static int
parse_options (int argc, char *argv[], bool has_connection)
{
        uint16_t port = GLUSTER_DEFAULT_PORT;
        int ret = -1;
        int opt = 0;
        int option_index = 0;
        struct xlator_option *option;

        // Reset getopt since other utilities may have called it already.
        optind = 0;
        while (true) {
                opt = getopt_long (argc, argv, "fro:p:", long_options,
                                   &option_index);

                if (opt == -1) {
                        break;
                }

                switch (opt) {
                        case 'd':
                                state->debug = true;
                                break;
                        case 'f':
                                state->force = true;
                                break;
                        case 'o':
                                option = parse_xlator_option (optarg);
                                if (option == NULL) {
                                        error (0, errno, "%s", optarg);
                                        goto err;
                                }

                                if (append_xlator_option (&state->xlator_options, option) == -1) {
                                        error (0, errno, "append_xlator_option: %s", optarg);
                                        goto err;
                                }

                                break;
                        case 'p':
                                port = strtoport (optarg);
                                if (port == 0) {
                                        goto out;
                                }

                                break;
                        case 'r':
                                state->directory = true;
                                break;
                        case 'v':
                                printf ("%s (%s) %s\n%s\n%s\n%s\n",
                                        program_invocation_name,
                                        PACKAGE_NAME,
                                        PACKAGE_VERSION,
                                        COPYRIGHT,
                                        LICENSE,
                                        AUTHORS);
                                ret = -2;
                                goto out;
                        case 'x':
                                usage ();
                                ret = -2;
                                goto out;
                        default:
                                goto err;
                }
        }

        if ((argc - option_index) < 2) {
                error (0, 0, "missing operand");
                goto err;
        } else {
                // state->url is free'd in do_rm()
                state->url = strdup (argv[argc - 1]);
                if (state->url == NULL) {
                        error (0, errno, "strdup");
                        goto out;
                }

                // state->gluster_url is free'd in do_rm()
                if (has_connection) {
                        state->gluster_url = gluster_url_init ();
                        if (state->gluster_url == NULL) {
                                error (0, errno, "gluster_url_init");
                                goto out;
                        }

                        state->gluster_url->path = strdup (argv[argc - 1]);
                        if (state->gluster_url->path == NULL) {
                                error (0, errno, "strdup");
                                goto out;
                        }

                        ret = 0;
                        goto out;
                }

                ret = gluster_parse_url (argv[argc - 1], &(state->gluster_url));
                if (ret == -1) {
                        error (0, EINVAL, "%s", state->url);
                        goto err;
                }

                state->gluster_url->port = port;
        }

        goto out;

err:
        error (0, 0, "Try --help for more information.");
out:
        return ret;
}
Ejemplo n.º 5
0
static int
parse_options (int argc, char *argv[], bool has_connection)
{
        uint16_t port = GLUSTER_DEFAULT_PORT;
        int ret = -1;
        int opt = 0;
        int option_index = 0;
        struct xlator_option *option;

        // Reset getopt since other utilities may have called it already.
        optind = 0;
        while (true) {
                opt = getopt_long (argc, argv, "do:p:rvx", long_options,
                                   &option_index);

                if (opt == -1) {
                        break;
                }

                switch (opt) {
                        case 'd':
                                state->debug = true;
                                break;
                        case 'o':
                                option = parse_xlator_option (optarg);
                                if (option == NULL) {
                                        error (0, errno, "%s", optarg);
                                        goto err;
                                }

                                if (append_xlator_option (&state->xlator_options, option) == -1) {
                                        error (0, errno, "append_xlator_option: %s", optarg);
                                        goto err;
                                }

                                break;
                        case 'p':
                                port = strtoport (optarg);
                                if (port == 0) {
                                        goto err;
                                }

                                break;
                        case 'r':
                                state->parents = true;
                                break;
                        case 'v':
                                printf ("%s (%s) %s\n%s\n%s\n%s\n",
                                        program_invocation_name,
                                        PACKAGE_NAME,
                                        PACKAGE_VERSION,
                                        COPYRIGHT,
                                        LICENSE,
                                        AUTHORS);
                                ret = -2;
                                goto out;
                        case 'x':
                                usage ();
                                ret = -2;
                                goto out;
                        default:
                                goto err;
                }
        }

        if ((argc - option_index) < 2) {
                error (0, 0, "missing operand");
                goto err;
        } else {
                state->url = strdup (argv[argc - 1]);
                if (state->url == NULL) {
                        error (0, errno, "strdup");
                        goto out;
                }

                if (has_connection) {
                        state->gluster_url = gluster_url_init ();
                        if (state->gluster_url == NULL) {
                                error (0, errno, "gluster_url_init");
                                goto out;
                        }

                        state->gluster_url->path = strdup (argv[argc - 1]);
                        if (state->gluster_url->path == NULL) {
                                error (0, errno, "strdup");
                                goto out;
                        }

                        ret = 0;
                        goto out;
                }

                ret = gluster_parse_url (argv[argc - 1], &(state->gluster_url));
                if (ret == -1) {
                        error (0, EINVAL, "%s", state->url);
                        goto err;
                }

                state->gluster_url->port = port;

                // gluster_create_path will not create
                // the last directory in a path if that path does not
                // include a trailing slash ('/'), so add one if it does not
                // exist.
                char *path = state->gluster_url->path;
                if (path[strlen (path) - 1] != '/') {
                        strncat (path, "/", strlen (path) + 2);
                }
        }

        goto out;

err:
        error (0, 0, "Try --help for more information.");
out:
        return ret;
}
int
cli_connect (struct cli_context *ctx)
{
        int argc = ctx->argc;
        char **argv = ctx->argv;
        int ret;
        int opt;
        int option_index;
        struct gluster_url *url = NULL;
        glfs_t *fs = NULL;
        uint16_t port = GLUSTER_DEFAULT_PORT;
        struct xlator_option *xlator_options = NULL;
        struct xlator_option *option;

        while ((opt = getopt_long (argc, argv, "o:p:", connect_options, &option_index)) != -1) {
                switch (opt) {
                        case 'o':
                                option = parse_xlator_option (optarg);
                                if (option == NULL) {
                                        error (0, errno, "%s", optarg);
                                        goto err;
                                }

                                if (append_xlator_option (&xlator_options, option) == -1) {
                                        error (0, errno, "append_xlator_option: %s", optarg);
                                        goto err;
                                }

                                break;
                        case 'p':
                                port = strtoport (optarg);
                                if (port == 0) {
                                        goto err;
                                }

                                break;
                        default:
                                goto err;
                }
        }

        ret = gluster_parse_url (argv[argc - 1], &url);
        if (ret == -1) {
                printf ("Usage: %s [OPTION]... URL\n"
                        "Connect to a Gluster volume for this session.\n\n"
                        "  -o, --xlator-option=OPTION   specify a translator option for the\n"
                        "                               connection. Multiple options are supported\n"
                        "                               and take the form xlator.key=value.\n"
                        "  -p, --port=PORT              specify the port on which to connect\n",
                        argv[0]);
                goto err;
        }

        url->port = port;

        ret = gluster_getfs (&fs, url);
        if (ret == -1) {
                error (0, errno, "failed to connect to %s/%s", url->host, url->volume);
                goto err;
        }

        ret = apply_xlator_options (fs, &xlator_options);
        if (ret == -1) {
                error (0, errno, "failed to apply translator options");
                goto err;
        }

        ret = cli_disconnect (ctx);
        if (ret == -1) {
                error (0, 0, "failed to terminate previous connection");
                goto err;
        }

        ctx->fs = fs;
        ctx->url = url;

        // TODO(craigcabrey): Look into using asprintf here.
        // 5 is the length of the string format: (%s/%s)
        size_t length = strlen (ctx->url->host) + strlen (ctx->url->volume) + 5;

        ctx->conn_str = malloc (length);
        if (ctx->conn_str == NULL) {
                error (0, errno, "malloc");
                handle_quit (ctx);
        }

        snprintf (ctx->conn_str,
                        length,
                        "(%s/%s)",
                        ctx->url->host,
                        ctx->url->volume);

        goto out;

err:
        ret = -1;
        gluster_url_free (url);
out:
        return ret;
}
Ejemplo n.º 7
0
int main(int c, char **v)
{
	struct sockaddr_in server_addr, from;
	socklen_t fromlen;
	fd_set rfds;
	struct tunnel_packet *tp;
	char buf[MTU];
	unsigned int buflen;
	int ret;
	int tun_fd;
	int server_fd;

	tp = (struct tunnel_packet *)buf;

	if(c < 4) {
		fprintf(stderr, "TCP/UDP/ICMP Tunnel over UDP\n"
			"%s <hostname> <port> <passphrase>\n", v[0]);
		return 0;
	}

	struct hostent* host_info = gethostbyname(v[1]);
	if (host_info == NULL) {
		fprintf(stderr, "%s: Invalid hostname or IPv4 address\n", v[1]);
		return 1;
	}

	memset(&server_addr, 0, sizeof(server_addr));
	memset(&from, 0, sizeof(from));
	memcpy(&server_addr.sin_addr, host_info->h_addr, host_info->h_length);
	if(strtoport(v[2], &server_addr.sin_port) == 0) {
		fprintf(stderr, "%s: Invalid port\n", v[0]);
		return 1;
	}
	server_addr.sin_family = AF_INET;
	tun_fd = tun_create();
	server_fd = socket_create(0);

	/* Sending authentication */
	tp->type = CONTROL_PACKET;
	tp->cmd = AUTH_CMD;
	strcpy(tp->data, PASSPHRASE);
	socket_put_packet(server_fd, &server_addr, sizeof(server_addr), buf, sizeof(struct tunnel_packet) + strlen(PASSPHRASE));

	/* Waiting acknowledge */
	fromlen = sizeof(struct sockaddr_in);
	buflen = socket_get_packet(server_fd, &from, &fromlen, buf, sizeof(buf));
	if(tp->type == CONTROL_PACKET) {
		if(tp->cmd != OK_CMD) {
			puts("Password is incorret");
			return 1;
		}
	} else {
		puts("Unexpected packet was received.");
		return 1;
	}

#ifdef __linux__
	exec_script("linux_client.sh", v[1]);
#else
	exec_script("osx_client.sh", v[1]);
#endif
	puts("+ Auth is OK.\n+ UDP Tunnel is running.");
	FD_ZERO(&rfds);
	while(1) {
		FD_SET(server_fd, &rfds);
		FD_SET(tun_fd, &rfds);
		ret = select(server_fd+1, &rfds, NULL, NULL, NULL);

		if(ret == -1)
			break;

		if(FD_ISSET(tun_fd, &rfds)) {
			buflen = tun_get_packet(tun_fd, tp->data, sizeof(buf)-sizeof(struct tunnel_packet));
			tp->type = TRAFFIC_PACKET;
			tp->cmd = 0;
			socket_put_packet(server_fd, &server_addr, sizeof(server_addr), buf, buflen + sizeof(struct tunnel_packet));
		}

		if(FD_ISSET(server_fd, &rfds)) {
			buflen = socket_get_packet(server_fd, &from, &fromlen, buf, sizeof(buf));
			if(server_addr.sin_addr.s_addr == from.sin_addr.s_addr && server_addr.sin_port == from.sin_port)
				tun_put_packet(tun_fd, tp->data, buflen-sizeof(struct tunnel_packet));
		}
	}

	return 1;
}
Ejemplo n.º 8
0
/* The main method. */
int main(int argc, char * argv[]) {
	int i;
	int port = 80;
	struct in_addr address = {0};

	/* Parse the arguments: */
	for(i = 1; i < argc; i++) {
		if(strcmp(argv[i], "--port") == 0) {
			i++;

			if(i == argc) {
				warn("Expected port number after --port option. Continuing with port 80.");
				break;
			}

			port = strtoport(argv[i]);

			if(!port) {
				warn("Invalid port number specified. Continuing with port 80.");
				port = 80;
			}
		} else if(strcmp(argv[i], "--address") == 0) {
			i++;

			if(i == argc) {
				warn("Expected address after --address option. Continuing with address 0.0.0.0.");
				break;
			}

			if(inet_pton(AF_INET, argv[i], &address) != 1) {
				warn("Invalid address specified. Continuing with address 0.0.0.0.");
			}
		} else {
			printf("Unrecognized option %s.\n", argv[i]);
		}
	}

	char str_address[16];
	struct sockaddr_in serv_addr;

	/* Create the socket: */
	s = socket(AF_INET, SOCK_STREAM, 0);

	if(s < 0) {
		warn("Error opening socket.");
		return 1;
	}

	/* Listen on the socket: */
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(port);
	serv_addr.sin_addr = address;

	errno = 0;

	if(bind(s, (struct sockaddr *) &serv_addr, sizeof serv_addr) < 0) {
		if(errno == EADDRINUSE)
			warn("Could not bind to port; already in use.");
		else
			warn("Could not bind to port due to an unknown error.");

		return 1;
	}

	listen(s, 5);

	success("Xeric 0.1.0 has started!");

	inet_ntop(AF_INET, &serv_addr.sin_addr, str_address, 15);
	printf("Listening at %s:%d.\n", str_address, port);

	/* Enable the Ctrl+C SIGINT handler so that the socket can be closed */
	signal(SIGINT, interrupted);

	while(keepRunning) {
		accept_client();
	}

	if(s >= 0) {
		close(s);
	}

	return 0;
}
Ejemplo n.º 9
0
void
parse_options (int argc, char *argv[])
{
        uint16_t port = GLUSTER_DEFAULT_PORT;
        int ret = -1;
        int opt = 0;
        int option_index = 0;
        struct xlator_option *option;

        while (true) {
                opt = getopt_long (argc, argv, "adfo:p:r", long_options,
                                   &option_index);

                if (opt == -1) {
                        break;
                }

                switch (opt) {
                        case 'a':
                                state->append = true;
                                break;
                        case 'd':
                                state->debug = true;
                                break;
                        case 'f':
                                state->overwrite = true;
                                break;
                        case 'o':
                                option = parse_xlator_option (optarg);
                                if (option == NULL) {
                                        error (0, errno, "%s", optarg);
                                        goto err;
                                }

                                if (append_xlator_option (&state->xlator_options, option) == -1) {
                                        error (EXIT_FAILURE, errno, "append_xlator_option: %s", optarg);
                                }

                                break;
                        case 'p':
                                port = strtoport (optarg);
                                if (port == 0) {
                                        exit (EXIT_FAILURE);
                                }

                                break;
                        case 'r':
                                state->parents = true;
                                break;
                        case 'v':
                                printf ("%s (%s) %s\n%s\n%s\n%s\n",
                                        program_invocation_name,
                                        PACKAGE_NAME,
                                        PACKAGE_VERSION,
                                        COPYRIGHT,
                                        LICENSE,
                                        AUTHORS);
                                exit (EXIT_SUCCESS);
                        case 'x':
                                usage (EXIT_SUCCESS);
                        default:
                                goto err;
                }
        }

        if ((argc - option_index) < 2) {
                error (0, 0, "missing operand");
                goto err;
        } else {
                state->url = strdup (argv[argc - 1]);
                if (state->url == NULL) {
                        error (0, errno, "strdup");
                        goto out;
                }

                ret = gluster_parse_url (argv[argc - 1], &(state->gluster_url));
                if (ret == -1) {
                        error (0, EINVAL, "%s", state->url);
                        goto err;
                }

                state->gluster_url->port = port;
        }

        goto out;

err:
        error (EXIT_FAILURE, 0, "Try --help for more information.");
out:
        return;
}