From: advielsack@gmail.com Date: Sun, 8 Jan 2012 13:47:13 +0000 (+0000) Subject: added sys [name|password|restart] and vlan enable X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=11ae7c1958d87f75fb840c6556fa99abf85e6607;p=gsconf added sys [name|password|restart] and vlan enable git-svn-id: http://gsconf.googlecode.com/svn/trunk@6 11042eb7-4a36-49e9-8ab2-01d26d512705 --- diff --git a/Makefile b/Makefile index 9e9fcf5..c548187 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=gcc CFLAGS=-I. DEPS = -OBJ = socket.o main.o gs105e.o shell.o shell_ip.o shell_vlan.o shell_port.o +OBJ = socket.o main.o gs105e.o shell.o shell_ip.o shell_vlan.o shell_sys.o shell_port.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/gs105e.c b/gs105e.c index bcd9008..a5b9aa6 100644 --- a/gs105e.c +++ b/gs105e.c @@ -90,7 +90,7 @@ void makeHeader(unsigned int queryType) { gs105e_queryData[1] = queryType % 256; memcpy(&gs105e_queryData[8], myMac, 6); - if (memcpy(settings.mac, "\x00\x00\x00\x00\x00\x00", 6)) + if (memcmp(settings.mac, "\x00\x00\x00\x00\x00\x00", 6)) memcpy(&gs105e_queryData[14], settings.mac, 6); memcpy(&gs105e_queryData[24], "\x4e\x53\x44\x50", 4); //Magic!! :-O @@ -226,6 +226,9 @@ void gs105e_interpret_slice(unsigned int ID, char * data, int len) { case ACT_DHCP:; settings.dhcp = data[1] & 0x03; break; + case GS_VLANSETTING:; + settings.vlanType = data[0]; + break; } } @@ -304,7 +307,12 @@ int gs105e_actRecv(void) { } int gs105e_act() { gs105e_query(); - return gs105e_actRecv(); + int n = gs105e_actRecv(); + if (n < 0){ + gs105e_query(); + n = gs105e_actRecv(); + } + return n; } @@ -351,7 +359,7 @@ int gs105e_restart(int vlanId) { int gs105e_mirrorPorts(int outputPort, int mirrorMask) { makeHeader(QR_EXEC); - char data[3] = {outputPort, 0, mirrorMask}; + char data[3] = {(char)outputPort, 0, (char) mirrorMask}; addActData(ACT_PORTMIRROR, 3, data); return gs105e_act(); @@ -373,6 +381,14 @@ int gs105e_setName(char * data) { return gs105e_act(); } +int gs105e_setPassword(char * data) { + makeHeader(QR_EXEC); + + + addActData(GS_NEWPASSWORD, strlen(data), data); + return gs105e_act(); +} + int gs105e_dhcpSettings(int action) { makeHeader(QR_EXEC); char data[1] = {(char)action}; @@ -381,6 +397,14 @@ int gs105e_dhcpSettings(int action) { return gs105e_act(); } +int gs105e_vlanEnable(void) { + makeHeader(QR_EXEC); + char data[1] = {4}; + + addActData(GS_VLANSETTING, 1, data); + return gs105e_act(); +} + void addDiscoveredDevice(int id) { struct gs105e_discovered * ddev = gs105e_devs; @@ -469,6 +493,7 @@ void gs105e_queryAll(void) { addQuery(ACT_DHCP); addQuery(GS_PORTSTATUS); addQuery(GS_VLAN); + addQuery(GS_VLANSETTING); gs105e_query (); gs105e_receive(); diff --git a/gs105e.h b/gs105e.h index e89828d..f62e5e7 100644 --- a/gs105e.h +++ b/gs105e.h @@ -21,6 +21,8 @@ #define GS_PORTDIAG 0x1c00 #define ACT_DHCP 0x000b +#define GS_VLANSETTING 0x2000 + #define ACT_ADDVLAN 0x2800 #define ACT_DELVLAN 0x2c00 @@ -85,6 +87,7 @@ struct gs105e_settings { char dhcp; struct vlan * vlans; + char vlanType; @@ -115,6 +118,7 @@ struct gs105e_discovered * gs105e_devs ; struct gs105e_settings settings; void debug(void); +int gs105e_setPassword(char * data); void gs105e_init(void) ; void makeHeader(unsigned int queryType); diff --git a/shell.c b/shell.c index eceb152..da91eb9 100644 --- a/shell.c +++ b/shell.c @@ -77,10 +77,17 @@ void shell_set(char ** argv, int elem) { } } +char * copyString(char * data) { + char * ret = (char * )malloc(strlen(data) + 1); + memcpy(ret, data, strlen(data) + 1); + ret[strlen(data)] = 0; + return ret; +} + void password(void) { if (settings.password == NULL) { printf("\033[91mWarning: As the protocol of the switch wants it that way, all configuration packets are send as broadcasts, meaning everyone on this net can sniff your password!\033[0m\n"); - settings.password = getpass("Password: "); + settings.password = copyString(getpass("Password: ")); } } @@ -209,6 +216,10 @@ int shell (void) { shell_port(argv, elem); } + if (strncmp(argv[0], "sys", 3) == 0) { + shell_sys(argv, elem); + } + diff --git a/shell.h b/shell.h index 743062e..111487b 100644 --- a/shell.h +++ b/shell.h @@ -11,12 +11,13 @@ #include void shell_ip(char ** argv, int elem); - +char * copyString(char * data); int shell(void); void password(void) ; void printError(int errCode); void printIp(char * data); void shell_port(char ** argv, int elem); +void shell_sys(char ** argv, int elem); #endif diff --git a/shell_port.c b/shell_port.c index 4e69179..d940029 100644 --- a/shell_port.c +++ b/shell_port.c @@ -2,7 +2,7 @@ void shell_port(char ** argv, int elem) { - int n, i; + int n, i, m, o; if (elem == 1) { @@ -48,5 +48,47 @@ void shell_port(char ** argv, int elem) { else printf("\033[92mCable is fine!\033[0m\n", settings.portStatistics[n].errorDist); } + } else if (strncmp(argv[1], "mirror", 6) == 0) { + + if (elem == 3) { + if (strncmp(argv[2], "off", 3) == 0) { + password(); + printError(gs105e_mirrorPorts(0, 0)); + return; + + } + } + + if (elem < 4) { + printf("port mirror output input [input.....]\n"); + return; + } + + n = atoi(argv[2]); + + if (n < 1 | n > 5) { + printf("Invalid Port %i\n", n); + return; + } + + o = 0; + + for (m = 3; m < elem; m++) { + i = atoi(argv[m]); + if (i < 1 | i > 5) { + printf("Invalid Port %i\n", i); + return; + } + + if (i == n) { + printf("Output can't be input port!\n"); + return; + } + + o |= (0x80 >> (i - 1)); + } + password(); + printError(gs105e_mirrorPorts(n, o)); + } } diff --git a/shell_sys.c b/shell_sys.c new file mode 100644 index 0000000..c72b8f5 --- /dev/null +++ b/shell_sys.c @@ -0,0 +1,52 @@ +#include "shell.h" + + + +void shell_sys(char ** argv, int elem) { + int n, i, m, o; + + if (elem == 1) { + printf("\n"); + return; + } + + if (strncmp(argv[1], "show", 4) == 0) { + printf("Name: \t\t%s\n", settings.name); + printf("Model: \t\t%s\n", settings.model); + printf("SW-Version:\t %s\n", settings.swVersion); + }else if (strncmp(argv[1], "name", 4) == 0) { + if (elem != 3) { + printf("Please provide a name\n"); + return; + } + + password(); + printError(gs105e_setName(argv[2])); + + }else if (strncmp(argv[1], "password", 4) == 0) { + + password(); + + char * newPwd = copyString(getpass("New Password: ")); + if (strlen(newPwd) == 0) { + printf("Password musst be at least one character!\n"); + return; + } + char * newPwdConf = copyString(getpass("New Password: ")); + printf("%p %p\n", settings.password, newPwd); + if (strncmp(newPwd, newPwdConf, strlen(newPwd)) == 0 && strlen(newPwd) == strlen(newPwdConf)) { + int e = gs105e_setPassword(newPwd); + printError(e); + if (e != 0) + return; + settings.password = newPwd; + }else { + printf("Passwords do not match!\n"); + } + }else if (strncmp(argv[1], "restart", 4) == 0) { + password(); + printError(gs105e_restart()); + } + + +} diff --git a/shell_vlan.c b/shell_vlan.c index 83f004a..80dfcb1 100644 --- a/shell_vlan.c +++ b/shell_vlan.c @@ -8,6 +8,18 @@ void shell_vlan(char ** argv, int elem) { return; } + + if (strncmp(argv[1], "enable", 6) == 0) { + password(); + printError(gs105e_vlanEnable()); + return; + } + + if (settings.vlanType != 4) { + printf("Type vlan enable once to configure vlan\n"); + return; + } + if (strncmp(argv[1], "show", 4) == 0) { struct vlan * vl = settings.vlans;