diff options
Diffstat (limited to 'qtctl.c')
-rw-r--r-- | qtctl.c | 104 |
1 files changed, 99 insertions, 5 deletions
@@ -12,11 +12,13 @@ static void usage() { - fputs("Usage: qtctl show [dev NAME]\n" + fputs("Usage: qtctl show [ dev NAME ]\n" " qtctl add [ dev NAME ] [ mode { ethernet | ip } ]\n" " [ local_address ADDRESS ] [ local_port PORT ]\n" " [ remote_address ADDRESS ] [ remote_port PORT ]\n" - " [ remote_float ]\n" + " [ remote_float { on | off } ]\n" + " qtctl change dev NAME [ remote_address ADDRESS ] [ remote_port PORT ]\n" + " [ remote_float { on | off } ]\n" " qtctl del dev NAME\n", stderr); exit(2); @@ -105,7 +107,15 @@ static void add(int argc, char *argv[], struct nl_handle *sock, int family) nla_put_u16(msg, QUICKTUN_A_REMOTE_PORT, htons((short)tmp_ul)); } else if (!strcmp(argv[p], "remote_float")) { - nla_put_flag(msg, QUICKTUN_A_REMOTE_FLOAT); + if ((++p) >= argc) + usage(); + + if (!strcmp(argv[p], "on")) + nla_put_u8(msg, QUICKTUN_A_REMOTE_FLOAT, 1); + else if (!strcmp(argv[p], "off")) + nla_put_u8(msg, QUICKTUN_A_REMOTE_FLOAT, 0); + else + usage(); } else usage(); @@ -151,6 +161,8 @@ static void del(int argc, char *argv[], struct nl_handle *sock, int family) has_dev = 1; } + else + usage(); } if (!has_dev) @@ -166,6 +178,86 @@ static void del(int argc, char *argv[], struct nl_handle *sock, int family) nl_recvmsgs(sock, cb); } +static void change(int argc, char *argv[], struct nl_handle *sock, int family) +{ + struct nl_msg *msg; + struct nl_cb *cb; + + int p = 0; + + char *tmp_charp; + struct in_addr tmp_addr; + unsigned long tmp_ul; + + int has_dev = 0; + + + msg = nlmsg_alloc(); + genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_ECHO, QUICKTUN_CMD_CHANGE_DEVICE, 1); + + while ((++p) < argc) { + if (!strcmp(argv[p], "dev")) { + if ((++p) >= argc) + usage(); + + if (has_dev) + usage(); + + tmp_charp = strndup(argv[p], IFNAMSIZ - 1); + nla_put_string(msg, QUICKTUN_A_IFNAME, tmp_charp); + free(tmp_charp); + + has_dev = 1; + } + else if (!strcmp(argv[p], "remote_address")) { + if ((++p) >= argc) + usage(); + + if (!(inet_aton(argv[p], &tmp_addr))) + usage(); + + nla_put_u32(msg, QUICKTUN_A_REMOTE_ADDRESS, tmp_addr.s_addr); + } + else if (!strcmp(argv[p], "remote_port")) { + if ((++p) >= argc) + usage(); + + tmp_ul = strtoul(argv[p], &tmp_charp, 10); + + if (*tmp_charp) + usage(); + + nla_put_u16(msg, QUICKTUN_A_REMOTE_PORT, htons((short)tmp_ul)); + } + else if (!strcmp(argv[p], "remote_float")) { + if ((++p) >= argc) + usage(); + + if (!strcmp(argv[p], "on")) + nla_put_u8(msg, QUICKTUN_A_REMOTE_FLOAT, 1); + else if (!strcmp(argv[p], "off")) + nla_put_u8(msg, QUICKTUN_A_REMOTE_FLOAT, 0); + else + usage(); + } + else + usage(); + } + + if (!has_dev) + usage(); + + + nl_send_auto_complete(sock, msg); + + nlmsg_free(msg); + + cb = nl_cb_alloc(NL_CB_DEFAULT); + nl_cb_err(cb, NL_CB_CUSTOM, print_error, NULL); + + nl_recvmsgs(sock, cb); +} + static int show_callback(struct nl_msg *msg, void *arg) { struct nlmsghdr *nlh = nlmsg_hdr(msg); @@ -189,7 +281,7 @@ static int show_callback(struct nl_msg *msg, void *arg) nla_parse_nested(nested, QUICKTUN_A_MAX, pos, NULL); if(!nested[QUICKTUN_A_IFNAME] || !nested[QUICKTUN_A_MODE] || !nested[QUICKTUN_A_LOCAL_ADDRESS] || !nested[QUICKTUN_A_LOCAL_PORT] - || !nested[QUICKTUN_A_REMOTE_ADDRESS] || !nested[QUICKTUN_A_REMOTE_PORT]) + || !nested[QUICKTUN_A_REMOTE_ADDRESS] || !nested[QUICKTUN_A_REMOTE_PORT] || !nested[QUICKTUN_A_REMOTE_FLOAT]) continue; printf("%s: %s ", nla_get_string(nested[QUICKTUN_A_IFNAME]), (nla_get_u16(nested[QUICKTUN_A_MODE]) == QUICKTUN_MODE_ETHERNET) ? "ethernet" : (nla_get_u16(nested[QUICKTUN_A_MODE]) == QUICKTUN_MODE_IP) ? "ip" : "unknown" ); @@ -200,7 +292,7 @@ static int show_callback(struct nl_msg *msg, void *arg) addr.s_addr = nla_get_u32(nested[QUICKTUN_A_REMOTE_ADDRESS]); printf("remote %s:%u ", inet_ntoa(addr), ntohs(nla_get_u16(nested[QUICKTUN_A_REMOTE_PORT]))); - printf("remote_float %s\n", nla_get_flag(nested[QUICKTUN_A_REMOTE_FLOAT]) ? "on" : "off"); + printf("remote_float %s\n", nla_get_u8(nested[QUICKTUN_A_REMOTE_FLOAT]) ? "on" : "off"); } return NL_STOP; @@ -267,6 +359,8 @@ int main(int argc, char *argv[]) add(argc-1, argv+1, sock, family); else if (!strcmp(argv[1], "del")) del(argc-1, argv+1, sock, family); + else if (!strcmp(argv[1], "change")) + change(argc-1, argv+1, sock, family); else if (!strcmp(argv[1], "show")) show(argc-1, argv+1, sock, family); else |