Пример #1
0
/*
 * Extract the secret from an auth otr command. The secret can have more than
 * one words so this is more efficient than exploding all args and
 * concatenating them.
 *
 * Return 0 and set secret on success or else return negative value an secret
 * is untouched.
 */
int utils_auth_extract_secret(const char *_data, char **secret)
{
	int ret = -1;
	char *s, *cmd_offset = NULL, *data = NULL;

	if (!_data || !secret) {
		goto error;
	}

	data = strdup(_data);
	if (!data) {
		goto error;
	}

	s = utils_trim_string(data);

	cmd_offset = strchr(s, ' ');
	if (!cmd_offset) {
		goto error;
	}

	s = utils_trim_string(cmd_offset);

	*secret = strdup(s);

	ret = 0;

error:
	free(data);
	return ret;
}
static bool ifconfigurator_get_if_config(struct ifconfigurator *self, const char *iface, struct ifconfig *out_config)
{
    struct ifreq ifr;
    FILE *proc = NULL;

    prepare_if_request(&ifr, iface);

    // Get flags
    if(ioctl(self->ctx->fd, SIOCGIFFLAGS, &ifr) == -1){
        return false;
    }
    out_config->flags = ifr.ifr_flags;

    // Get MAC
    prepare_if_request(&ifr, iface); // Clear, so failed ioctl won't lead to junk value
    ioctl(self->ctx->fd, SIOCGIFHWADDR, &ifr);
    memcpy(&out_config->mac, &ifr.ifr_hwaddr, sizeof(out_config->mac));

    // Get IPv4
    prepare_if_request(&ifr, iface);
    ioctl(self->ctx->fd, SIOCGIFADDR, &ifr);
    memcpy(&out_config->ipv4, &ifr.ifr_addr, sizeof(out_config->ipv4));

    // Get broadcast addr
    prepare_if_request(&ifr, iface);
    ioctl(self->ctx->fd, SIOCGIFBRDADDR, &ifr);
    memcpy(&out_config->ipv4_broadcast, &ifr.ifr_broadaddr, sizeof(out_config->ipv4_broadcast));

    // Get net mask
    prepare_if_request(&ifr, iface);
    ioctl(self->ctx->fd, SIOCGIFNETMASK, &ifr);
    memcpy(&out_config->ipv4_netmask, &ifr.ifr_netmask, sizeof(out_config->ipv4_netmask));

    // Get IPv6
    memset(out_config->ipv6, 0, sizeof(out_config->ipv6));
    if((proc = fopen("/proc/net/if_inet6", "r")) == NULL) {
        fprintf(stderr, "Could not open /proc/net/if_inet6\n");
    } else {
        char line[256] = "\0";
        char *if_in_line;
        while(fgets(line, 256, proc) != NULL) {
            utils_trim_string(line);
            if_in_line = strrchr(line, ' ');
            if (if_in_line == NULL) {
                continue;
            }
            if (strcmp(if_in_line + 1, iface) == 0) {
                int i;
                for (i = 0; i < 16; i++) {
                    out_config->ipv6[i] = utils_uchar_from_hex(line[i * 2]) << 4 | utils_uchar_from_hex(line[i * 2 + 1]);
                }
                break;
            }
        }
        fclose(proc);
    }

    return true;
}
Пример #3
0
/*
 * Set _argv and _argc from the string in _data.
 *
 * On error, argv is untouched argc set to 0.
 */
void utils_explode_args(const char *_data, char ***_argv, int *_argc)
{
	int argc = 0, i = 0, have_arg = 0;
	char **argv = NULL, *c, *data = NULL, *cmd_offset;

	if (!_data || !_argv || !_argc) {
		goto error;
	}

	data = strndup(_data, strlen(_data));
	if (!data) {
		goto error;
	}

	c = utils_trim_string(data);

	/* Ignore first command */
	cmd_offset = strchr(c, ' ');
	if (!cmd_offset) {
		goto error;
	}

	cmd_offset = utils_trim_string(cmd_offset);

	if (cmd_offset && strlen(cmd_offset) > 0) {
		argc++;
		have_arg = 1;
	}

	c = cmd_offset;
	while ((c = strchr(c + 1, ' '))) {
		/* Skip consecutive spaces. */
		if (*(c + 1) == ' ') {
			continue;
		}
		argc++;
		have_arg = 1;
	}

	/* No args, only spaces encountered. */
	if (!have_arg) {
		argc = 0;
		goto error;
	}

	argv = zmalloc(argc * sizeof(char *));
	if (!argv) {
		goto error;
	}

	/* Ignore first command */
	c = strtok(cmd_offset, " ");
	while (c != NULL) {
		argv[i] = strdup(c);
		c = strtok(NULL, " ");
		i++;
	}

	*_argv = argv;

error:
	*_argc = argc;
	free(data);
	return;
}