summaryrefslogtreecommitdiffstats
path: root/bcmdhd/bcmdhd_net_iface/bcmdhd_net_iface.c
blob: b724fdbd8cdd82f387deb1268455cce4b2fc471d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/types.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <cutils/log.h>
#include <netutils/ifc.h>
#include <private/android_filesystem_config.h>

#define INTERFACE_MAX_BUFFER_SIZE 4096

typedef struct android_wifi_priv_cmd {
	char *buf;
	int used_len;
	int total_len;
} android_wifi_priv_cmd;

static struct net_if_snd_cmd_state {
	int sock;
	char cmd[INTERFACE_MAX_BUFFER_SIZE];
	char ibuf[INTERFACE_MAX_BUFFER_SIZE];
} state;

int net_iface_send_command_init(void) {
	state.sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (state.sock < 0) {
		return -ENOTCONN;
	} else {
		return 0;
	}
}

/*
 * Arguments:
 *	  argv[2] - wlan interface
 *	  argv[3] - command
 *	  argv[4] - argument
 */
int net_iface_send_command(int argc, char *argv[], char **rbuf) {
	int ret, i;
	size_t bc = 0;
	struct ifreq ifr;
	android_wifi_priv_cmd priv_cmd;

	if (argc < 3) {
		return -EINVAL;
	}
	for (i = 3; i < argc; i++) {
		bc += snprintf(&state.cmd[bc], sizeof(state.cmd) - bc, "%s ", argv[i]);
		if (bc >= sizeof(state.cmd)) {
			return -ENOBUFS;
		}
	}
	state.cmd[bc] = '\0';

	memset(&ifr, 0, sizeof(ifr));
	strncpy(ifr.ifr_name, argv[2], IFNAMSIZ);
	strncpy(state.ibuf, state.cmd, INTERFACE_MAX_BUFFER_SIZE);

	priv_cmd.buf = state.ibuf;
	priv_cmd.used_len = INTERFACE_MAX_BUFFER_SIZE;
	priv_cmd.total_len = INTERFACE_MAX_BUFFER_SIZE;

	ifr.ifr_data = &priv_cmd;

	if ((ret = ioctl(state.sock, SIOCDEVPRIVATE + 1, &ifr)) < 0) {
		return ret;
	}

	if (rbuf)
		*rbuf = state.ibuf;
	else
		ret = -EFAULT;

	return ret;
}