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)
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
case ACT_DHCP:;
settings.dhcp = data[1] & 0x03;
break;
+ case GS_VLANSETTING:;
+ settings.vlanType = data[0];
+ break;
}
}
}
int gs105e_act() {
gs105e_query();
- return gs105e_actRecv();
+ int n = gs105e_actRecv();
+ if (n < 0){
+ gs105e_query();
+ n = gs105e_actRecv();
+ }
+ return n;
}
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();
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};
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;
addQuery(ACT_DHCP);
addQuery(GS_PORTSTATUS);
addQuery(GS_VLAN);
+ addQuery(GS_VLANSETTING);
gs105e_query ();
gs105e_receive();
#define GS_PORTDIAG 0x1c00
#define ACT_DHCP 0x000b
+#define GS_VLANSETTING 0x2000
+
#define ACT_ADDVLAN 0x2800
#define ACT_DELVLAN 0x2c00
char dhcp;
struct vlan * vlans;
+ char vlanType;
struct gs105e_settings settings;
void debug(void);
+int gs105e_setPassword(char * data);
void gs105e_init(void) ;
void makeHeader(unsigned int queryType);
}
}
+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: "));
}
}
shell_port(argv, elem);
}
+ if (strncmp(argv[0], "sys", 3) == 0) {
+ shell_sys(argv, elem);
+ }
+
#include <pwd.h>
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
void shell_port(char ** argv, int elem) {
- int n, i;
+ int n, i, m, o;
if (elem == 1) {
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));
+
}
}
--- /dev/null
+#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());
+ }
+
+
+}
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;