Ejemplo n.º 1
0
int		validate_mod(char *choped)
{
    int i;

    i = 0;
    while (choped[i])
    {
        if (!is_double_mod(choped, &i) && is_mod(choped[i]) && \
                ft_strchr(SPECIFIERS, choped[i + 1]) == NULL)
            return (-1);
        else if (is_double_mod(choped, &i) && \
                 ft_strchr(SPECIFIERS, choped[i + 2]) == NULL)
            return (-1);
        i++;
    }
    return (1);
}
Ejemplo n.º 2
0
int main(int argc, char **argv)
{
	/* pipe descriptors */
	int pd_rd[2], pd_wr[2];
	pid_t pid;
	char *pppd = STD_PPPD_PATH;
	char *log_path = STD_LOG_PATH;
	char *ip = NULL;
	char *mod_name = NULL;
	int mod_idx;
	int i;
	int need_arg = 0;
	int mod_argc = 0;
	char **mod_argv = NULL;

	prog_name = argv[0];

	/* parsing arguments */
	if (argc < 2)
		help(argv[0]);
	for (i = 1; i < argc; i++) {
		if (need_arg) {
			switch (need_arg) {
			case ARG_MODULE:
				mod_name = argv[i];
				break;
			case ARG_LOG_PATH:
				log_path = argv[i];
				break;
			default:
				fprintf(stderr, "there is internal problem with parsing arguments\n");
				return 1;
			}
			need_arg = 0;
			continue;
		}
		if (argv[i][0] != '-') {
			/* <local_ip>:<remote_ip> */
			ip = argv[i];
			continue;
		}
		if (!strcmp("-h", argv[i]) || !strcmp("--help", argv[i]))
			/* print help and exit */
			help(argv[0]);
		if (!strcmp("-l", argv[i]) || !strcmp("--list", argv[i]))
			/* print list of available modules and exit */
			list_mod();
		if (!strcmp("-L", argv[i])) {
			/* specify file for logging */
			need_arg = ARG_LOG_PATH;
			continue;
		}
		if (!strcmp("-m", argv[i])) {
			/* choose module */
			need_arg = ARG_MODULE;
			continue;
		}
		if (!strcmp("-q", argv[i]) || !strcmp("--quiet", argv[i])) {
			/* quiet mode */
			quiet = 1;
			continue;
		}
		if (!strcmp("--", argv[i])) {
			/* TODO: set pointer to i+1, the rest options are for pppd */
			mod_argv = argv + i;
			mod_argc = argc - i;
			break;
		}
		/* unrecognized options may be addressed for the module */
	}
	if (need_arg) {
		fprintf(stderr, "incomplete arguments, see %s --help\n", argv[0]);
		return 2;
	}
	/* check whether required arguments are set */
	if (!mod_name) {
		fprintf(stderr, "you must choose a module, see %s --help\n", argv[0]);
		return 2;
	}

	/* redirect logs to a file */
	redirect_logs(log_path);

	/* check whether module name is correct */
	if ((mod_idx = is_mod(mod_name)) < 0) {
		fprintf(stderr, "there isn't such a module: %s\n", mod_name);
		return 2;
	}

	/* create pipes for communication with pppd */
	if (pipe(pd_rd) < 0)
		err_exit("pipe");
	if (pipe(pd_wr) < 0)
		err_exit("pipe");

	/* exec pppd */
	if ((pid = fork()) < 0)
		err_exit("fork");
	if (!pid) {
		if (dup2(pd_rd[1], 1) < 0)
			err_exit("dup2");
		if (dup2(pd_wr[0], 0) < 0)
			err_exit("dup2");
		close(pd_rd[0]);
		close(pd_rd[1]);
		close(pd_wr[0]);
		close(pd_wr[1]);
		execl(pppd, pppd, "nodetach", "noauth", "notty", "passive", ip, NULL);
		err_exit("execl");
	}

	close(pd_rd[1]);
	close(pd_wr[0]);

	/* run appropriate module's function */
	return mod_tbl[mod_idx].func(mod_argc, mod_argv, pd_rd[0], pd_wr[1]);
}