From 84795a4c9a792a8ab63f71174fef707a4776a078 Mon Sep 17 00:00:00 2001 From: "advielsack@gmail.com" Date: Sat, 7 Jan 2012 01:24:39 +0000 Subject: [PATCH] Initial Commit git-svn-id: http://gsconf.googlecode.com/svn/trunk@2 11042eb7-4a36-49e9-8ab2-01d26d512705 --- Makefile | 14 ++ gs105e.c | 466 +++++++++++++++++++++++++++++++++++++++++++++++++++ gs105e.h | 129 ++++++++++++++ main.c | 61 +++++++ packet.c | 21 +++ packet.h | 13 ++ shell.c | 196 ++++++++++++++++++++++ shell.h | 22 +++ shell_ip.c | 40 +++++ shell_port.c | 21 +++ shell_vlan.c | 139 +++++++++++++++ socket.c | 115 +++++++++++++ socket.h | 6 + 13 files changed, 1243 insertions(+) create mode 100644 Makefile create mode 100644 gs105e.c create mode 100644 gs105e.h create mode 100644 main.c create mode 100644 packet.c create mode 100644 packet.h create mode 100644 shell.c create mode 100644 shell.h create mode 100644 shell_ip.c create mode 100644 shell_port.c create mode 100644 shell_vlan.c create mode 100644 socket.c create mode 100644 socket.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9e9fcf5 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +CC=gcc +CFLAGS=-I. +DEPS = +OBJ = socket.o main.o gs105e.o shell.o shell_ip.o shell_vlan.o shell_port.o + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +gsconfig: $(OBJ) + gcc -o $@ $^ $(CFLAGS) + +clean: + rm *.o -rf + rm gsconfig -rf diff --git a/gs105e.c b/gs105e.c new file mode 100644 index 0000000..ccee5d7 --- /dev/null +++ b/gs105e.c @@ -0,0 +1,466 @@ + +#include "gs105e.h" +#include "socket.h" +#include +#include +#include + +unsigned char * gs105e_queryData; +unsigned int gsDataLen; + +struct vlan * getVlanSetting(unsigned int vlanId) { + struct vlan * _vlan = settings.vlans; + + while (_vlan != NULL) { + if (_vlan->id == vlanId) + return _vlan; + _vlan = _vlan->next; + } + + return NULL; +} + +void addVlanSetting(unsigned int vlanId, unsigned char tagged, unsigned char member) { + struct vlan * old = settings.vlans; + struct vlan * new = (struct vlan *) malloc (sizeof( struct vlan) ); + new->id = vlanId; + new->tag = tagged; + new->members = member; + new->next = old; + settings.vlans = new; +} + +void hexDump (char * data, int len) { + int n; + for (n = 0; n < len; n++) { + if (n % 16 == 0) + printf("\n %06X |",n); + printf(" %02X", (int)data[n] & 0xFF); + } + printf("\n"); +} +void addData(char * data, int len) { + gsDataLen += len; + gs105e_queryData = (char * )realloc(gs105e_queryData, sizeof(char) * gsDataLen); + memcpy(&gs105e_queryData[gsDataLen - len], data, len); +} +void addQuery(int qid) { + char queryData[] = {qid / 256, qid % 256, 0, 0}; + addData(queryData, 4); +} + +void addActData(int actId, int len, char * data) { + + char actData[] = {actId / 256, actId % 256, len / 256, len % 256}; + addData(actData, 4); + + addData(data, len); +} +void gs105e_init(void) { + packetId = 0; + gs105e_devs = NULL; + gs105e_queryData = NULL; + gsDataLen = 0; + settings.vlans = NULL; +} + + + + + +char newPacketId() { + packetId ++; + packetId %= 256; + return (char)packetId; +} + +void makeHeader(unsigned int queryType) { + int n; + + + + gs105e_queryData = (unsigned char * )realloc(gs105e_queryData, sizeof(char) * 32); + gsDataLen = 32; + + + for (n = 0; n < 32; n++) + gs105e_queryData[n] = 0; + + gs105e_queryData[0] = queryType / 256; + gs105e_queryData[1] = queryType % 256; + + memcpy(&gs105e_queryData[8], myMac, 6); + if (memcpy(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 + if (settings.password != NULL && queryType == QR_EXEC) { + + addActData(GS_PASSWORD, strlen(settings.password), settings.password); + } + + + +} + + +void debug(void) { + hexDump(gs105e_queryData, gsDataLen); +} + +void emptyBuffer(void) { + char tmp[2500]; + while (recvBroadcast(tmp) > 0 ) { + ;; + } +} + +void gs105e_query (void) { + emptyBuffer(); + gs105e_queryData[23] = newPacketId(); + + if (gs105e_queryData[gsDataLen - 4] != 0xFF | gs105e_queryData[gsDataLen - 3] != 0xFF | gs105e_queryData[gsDataLen - 2] != 0x00 | gs105e_queryData[gsDataLen - 1] != 0x00) + addData("\xFF\xFF\x00\x00", 4); + sendBroadcast(gs105e_queryData, gsDataLen); + +} + +unsigned long toLong(char * data) { + int n; + unsigned long a; + for (n = 0; n < 8; n++) { + a <<= 8; + a |= ((unsigned long)data[n]) & 0xFF; + } + return a; +} + +unsigned int toUInt4(char * data) { + int n; + unsigned int a; + for (n = 0; n < 4; n++) { + a <<= 8; + a |= ((unsigned int )data[n]) & 0xFF; + } + return a; +} + +unsigned int toUInt(char * data) { + int n; + unsigned int a; + for (n = 0; n < 2; n++) { + a <<= 8; + a |= ((unsigned int )data[n]) & 0xFF; + } + return a; +} + +void gs105e_interpret_slice(unsigned int ID, char * data, int len) { + int mLen , tmp; + struct portStatistic *p; + switch (ID) { + case GS_MODEL:; + + mLen = (strstr(data, " ") - data) + 1; + settings.model = realloc(settings.model, sizeof(char) * mLen); + memcpy(settings.model, data, mLen); + break; + case GS_NAME:; + mLen= (strchr(data, 0x00) - data) + 1; + settings.name = realloc(settings.name, sizeof(char) * mLen); + memcpy(settings.name, data, mLen); + break; + case GS_MAC:; + memcpy(settings.mac, data, 6); + break; + case GS_IP:; + memcpy(settings.ip, data, 4); + break; + case GS_SUBNETMSK:; + memcpy(settings.subnetmask, data, 4); + break; + case GS_GATEWAY:; + memcpy(settings.gateway, data, 4); + break; + case GS_FIRMWAREVER:; + settings.swVersion = realloc(settings.swVersion, sizeof(char) * len); + memcpy(settings.swVersion, data, len); + break; + case GS_PACKETSTATISTICS:; + tmp = (unsigned int)data[0] - 1; + if (tmp > 5) + return; + p = &settings.portStatistics[tmp]; + p->portId = tmp + 1; + p->bytesIn = toLong(&data[1]); + p->bytesOut = toLong(&data[9]); + p->errors = toLong(&data[41]); + break; + case GS_VLAN:; + unsigned int vlanId = toUInt(data); + unsigned char tagged = (unsigned char) data[3]; + unsigned char memberOf = (unsigned char) data[2]; + struct vlan * _vlan = getVlanSetting(vlanId); + + if (_vlan == NULL) { + addVlanSetting(vlanId, tagged, memberOf); + }else { + _vlan->members = memberOf; + _vlan->tag = tagged; + + } + + + break; + case GS_PORTSTATUS:; + unsigned int portId = (unsigned int) data[0]; + unsigned char state = (unsigned char) data[1]; + p = &settings.portStatistics[portId - 1]; + p->state = state; + break; + case GS_PORTDIAG:; + p = &settings.portStatistics[data[0]]; + p->cableError = toUInt4(&data[1]); + p->errorDist = toUInt4(&data[5]); + break; + case ACT_DHCP:; + settings.dhcp = data[1] & 0x03; + break; + } +} + +int gs105e__receive(void) { + char data[2500]; + int len = recvBroadcast(data); + int n; + + unsigned int id; + unsigned int slLen; + + +// if (len > 0) +// hexDump(data, len); + if (len == -1) { +// printf("Received No or Invalid Packet\n"); + return -1; + } + if (memcmp(&data[8], myMac, 6) | data[0] != 0x01 | (!(data[1] == 0x02 | data[1] == 0x04))) { + return -1; + } + + for (n = 32; n < len; ) { + id = (data[n] * 256) + data[n + 1]; + slLen = (data[n+2] * 256) + data[n + 3]; + + gs105e_interpret_slice(id, &data[n + 4], slLen); + n += slLen + 4; + } + return 0; + + +} + +int gs105e_receive(void) { + if (gs105e__receive() == -1) { + gs105e_query(); + return gs105e_receive(); + } + return 0; +} + + +int gs105e_actRecv(void) { + char data[2500]; + int len = recvBroadcast_tout(data); + int n; + + unsigned int id; + unsigned int slLen; + + +// if (len > 0) +// hexDump(data, len); + if (len == -1) { +// printf("Received No or Invalid Packet\n"); + return -1; + } + if (memcmp(&data[8], myMac, 6) | data[0] != 0x01 | (!(data[1] == 0x02 | data[1] == 0x04))) { + gs105e_actRecv(); + return -1; + } + + if (data[2] == 0) { + return data[2]; + } +} +int gs105e_act() { + gs105e_query(); + return gs105e_actRecv(); +} + + +int gs105e_addVlan(int vlanId) { + makeHeader(QR_EXEC); + + char data[4] = {vlanId / 256, vlanId % 256, 0, 0}; + + addActData(ACT_ADDVLAN, 4, data); + + return gs105e_act(); + +} + +int gs105e_setVlanMembers(unsigned int vlanId, unsigned int members, unsigned int tagged) { + char data[4] = {0, vlanId & 0xFF, (char)members & 0xFF, (char)tagged & 0xFF}; + makeHeader(QR_EXEC); + addActData(GS_VLAN, 4, data); + return gs105e_act(); + +} + + +int gs105e_delVlan(int vlanId) { + makeHeader(QR_EXEC); + + char data[2] = {vlanId / 256, vlanId % 256}; + + addActData(ACT_DELVLAN, 2, data); + + return gs105e_act(); + +} +int gs105e_restart(int vlanId) { + makeHeader(QR_EXEC); + + char data[1] = {1}; + + addActData(ACT_RESTART, 1, data); + + return gs105e_act(); + +} + +int gs105e_mirrorPorts(int outputPort, int mirrorMask) { + makeHeader(QR_EXEC); + char data[3] = {outputPort, 0, mirrorMask}; + + addActData(ACT_PORTMIRROR, 3, data); + return gs105e_act(); +} + +int gs105e_cableDiagnostics(int port) { + makeHeader(QR_EXEC); + char data[1] = {port}; + + addActData(ACT_PORTMIRROR, 3, data); + return gs105e_act(); +} + +int gs105e_setName(char * data) { + makeHeader(QR_EXEC); + + + addActData(GS_NAME, strlen(data), data); + return gs105e_act(); +} + +int gs105e_dhcpSettings(int action) { + makeHeader(QR_EXEC); + char data[1] = {(char)action}; + + addActData(ACT_DHCP, 1, data); + return gs105e_act(); +} + +void addDiscoveredDevice(int id) { + + struct gs105e_discovered * ddev = gs105e_devs; + + if (ddev == NULL) { + ddev = (struct gs105e_discovered *)malloc(sizeof(struct gs105e_discovered)); + gs105e_devs = ddev; + }else { + while (ddev != NULL) { + if (memcmp(ddev->mac, settings.mac, 6)) + continue; + ddev = ddev->next; + } + } + + if (ddev == NULL) { + struct gs105e_discovered * ddev = gs105e_devs; + gs105e_devs = (struct gs105e_discovered *)malloc(sizeof(struct gs105e_discovered)); + gs105e_devs->next = ddev; + ddev = gs105e_devs; + } + ddev->name = settings.name; + memcpy(ddev->mac, settings.mac, 6); + memcpy(ddev->ip, settings.ip, 4); + memcpy(ddev->gateway, settings.gateway, 4); + ddev->model = settings.model; + ddev->id = id; + +} + + +int gs105e_discover(void) { + int n = 0; + struct gs105e_discovered * ddev = gs105e_devs; + struct gs105e_discovered * ddev_T; + while (ddev != NULL) { + ddev_T = ddev; + ddev = ddev->next; + free(ddev_T); + } + gs105e_devs = NULL; + + makeHeader(QR_REQ); + addQuery(GS_MODEL); + addQuery(GS_NAME); + addQuery(GS_MAC); + addQuery(GS_IP); + addQuery(GS_SUBNETMSK); + addQuery(GS_GATEWAY); + + gs105e_query (); + gs105e_query (); + while (gs105e__receive() == 0) { + addDiscoveredDevice(n + 1); + n++; + } + return n; + +} + + +void gs105e_queryAll(void) { + + + + if (settings.vlans != NULL) { + struct vlan * vl = settings.vlans; + struct vlan * vl_T; + while (vl != NULL) { + vl_T = vl; + vl = vl->next; + free(vl_T); + } + settings.vlans = NULL; + } + + makeHeader(QR_REQ); + addQuery(GS_MODEL); + addQuery(GS_NAME); + addQuery(GS_MAC); + addQuery(GS_IP); + addQuery(GS_SUBNETMSK); + addQuery(GS_GATEWAY); + addQuery(GS_PACKETSTATISTICS); + addQuery(GS_FIRMWAREVER); + addQuery(ACT_DHCP); + addQuery(GS_PORTSTATUS); + addQuery(GS_VLAN); + + gs105e_query (); + gs105e_receive(); +} diff --git a/gs105e.h b/gs105e.h new file mode 100644 index 0000000..e89828d --- /dev/null +++ b/gs105e.h @@ -0,0 +1,129 @@ +#ifndef GS105E_H +#define GS105E_H +#include + +#define GS_MODEL 0x0001 +#define GS_NAME 0x0003 +#define GS_MAC 0x0004 +#define GS_IP 0x0006 +#define GS_SUBNETMSK 0x0007 +#define GS_GATEWAY 0x0008 + +#define GS_NEWPASSWORD 0x0009 +#define GS_PASSWORD 0x000a + +#define GS_FIRMWAREVER 0x000d + +#define GS_PACKETSTATISTICS 0x1000 + +#define GS_VLAN 0x2800 +#define GS_PORTSTATUS 0x0c00 +#define GS_PORTDIAG 0x1c00 +#define ACT_DHCP 0x000b + + +#define ACT_ADDVLAN 0x2800 +#define ACT_DELVLAN 0x2c00 +#define ACT_RESTART 0x0013 +#define ACT_PORTMIRROR 0x5c00 + + + +#define QR_REQ 0x0101 +#define QR_ANSWER 0x0102 +#define QR_EXEC 0x0103 +#define QR_EXECUTED 0x0104 + +#define MSK_PORT1 0x80 +#define MSK_PORT2 0x40 +#define MSK_PORT3 0x20 +#define MSK_PORT4 0x10 +#define MSK_PORT5 0x08 + + +#define DHCP_ON 0x01 +#define DHCP_OFF 0x00 +#define DHCP_RENEW 0x02 + +int packetId; + +struct vlan { + int id; + char members; + char tag; + struct vlan * next; +}; + +struct portSniffing { + char ports; + char output; +}; + +struct portStatistic { + char portId; + unsigned long bytesIn; + unsigned long bytesOut; + unsigned long errors; + char state; + char cableError; + char errorDist; +}; + +struct gs105e_settings { + + char * password; + char * model; + + char mac[6]; + char ip[4]; + char subnetmask[4]; + char gateway[4]; + + char * name; + char * swVersion; + + char dhcp; + + struct vlan * vlans; + + + + struct portStatistic portStatistics[5]; + + struct portSniffing portMonitor; + + +}; + + +struct gs105e_discovered { + int id; + char * model; + + char mac[6]; + char ip[4]; + char subnetmask[4]; + char gateway[4]; + + char * name; + struct gs105e_discovered * next; +}; + + +struct gs105e_discovered * gs105e_devs ; + +struct gs105e_settings settings; + +void debug(void); + +void gs105e_init(void) ; +void makeHeader(unsigned int queryType); + +void gs105e_query (void); + +int gs105e_addVlan(int vlanId) ; +int gs105e_delVlan(int vlanId) ; +int gs105e_discover(void); + +int gs105e_setName(char * data); +#endif diff --git a/main.c b/main.c new file mode 100644 index 0000000..60f2ba4 --- /dev/null +++ b/main.c @@ -0,0 +1,61 @@ +#include +#include "socket.h" +#include "packet.h" +#include "gs105e.h" +#include +#include +#include "shell.h" +#include +int shellHandler = 0; + +/*void shellHandlers[] = { + + +}*/ + + +int main () { + int n; + + init_socket(); + + myMac = get_mac("eth0"); + + gs105e_init(); + + shell(); + /* + printf("Model: %s\n", settings.model); + struct vlan * vl = settings.vlans; + + if (vl == NULL) { + printf("NO Vlans\n"); + } else + printf("Vlans\n"); + + while (vl != NULL) { + printf("%u\t", vl->id); + for (n = 0; n < 5; n++) + printf("%c\t", vl->members & (0x80 >> n) ? (vl->tag & (0x80 >> n) ? 'T' : 'U') : '-', (unsigned int) vl->members); + printf("\n"); + vl = vl->next; + } + + for (n = 0; n < 5; n++) { + printf("Port %u\t%s\t\t%lu Bytes in\t\t%lu Bytes out\n", n + 1, + (settings.portStatistics[n].state == 0x05 ? "1000 Mbit/s" : (settings.portStatistics[n].state == 0x04 ? "100 Mbit/s" : (settings.portStatistics[n].state == 0x03 ? "10 Mbit/s": (settings.portStatistics[n].state == 0x00 ? "no connection": "???")))), settings.portStatistics[n].bytesIn, settings.portStatistics[n].bytesOut); + } + + + + //settings.password = "password"; + + //gs105e_mirrorPorts(3, MSK_PORT1|MSK_PORT4); + + */ +} + + + + + diff --git a/packet.c b/packet.c new file mode 100644 index 0000000..c9328f1 --- /dev/null +++ b/packet.c @@ -0,0 +1,21 @@ +#include "packet.h" + +unsigned short toShort(char * data) { + return (((unsigned short) data[0]) << 8) | ((unsigned short) data[1]); +} + +struct packet genPacket(char * data, int length) { + + struct packet pck; + //(*((unsigned long*)&dns_server_ip[0])) + + + pck.packetId = toShort(&data[0]); + pck.packetLen = toShort(&data[2]); + + + + pck.data = 0; + + return pck; +} diff --git a/packet.h b/packet.h new file mode 100644 index 0000000..c11e5db --- /dev/null +++ b/packet.h @@ -0,0 +1,13 @@ + + +struct packet genPacket(char * data, int length); + +struct packet { + unsigned short packetId; + unsigned short packetLen; + char myMac[6]; + + char devMac[6]; + + unsigned char * data; +}; diff --git a/shell.c b/shell.c new file mode 100644 index 0000000..284acc0 --- /dev/null +++ b/shell.c @@ -0,0 +1,196 @@ +#include "shell.h" + + +unsigned int countTokens(char * data, char * deli) { + unsigned int n = 0; + char * lp; + + char * da = malloc(sizeof(char) * strlen(data)); + strcpy(da, data); + + lp = strtok(da, deli); + while (lp != NULL) { + lp = strtok(NULL, deli); + n++; + } + + return n; +} + +unsigned int split(char *** _data, char * text, char * deli) { + + + + unsigned int elements = countTokens(text, deli); + + *_data = malloc( sizeof(char *) * (elements + 1)); + + char ** data = (char **)*_data; + + char * lp = strtok(text, deli); + + unsigned int n = 0; + + data[elements - 1] = NULL; + + while (lp != NULL) { + data[n] = (char*)malloc( sizeof(char *) * (strlen(lp) + 1)); + strcpy(data[n], lp); + data[n][strlen(lp)] = 0; + lp = strtok(NULL, deli); + n++; + } + return elements; +} + +void printIp(char * data) { + printf("\033[92m%u.%u.%u.%u\033[0m\n",data[0]&0xff, data[1]&0xff, data[2]&0xff, data[3]&0xff); +} +void printError(int errCode) { + switch (errCode) { + case 0x00:; + printf("\033[92mSuccess!\033[0m\n"); + gs105e_queryAll(); + break; + case -1:; + printf("\033[91mSwitch does not answer!\033[0m\n"); + break; + case 0x07:; + printf("\033[91mWrong Password, try again!\033[0m\n"); + settings.password = NULL; + break; + default:; + printf("\033[91mUups, unknown Error!?\033[0m\n"); + break; + } +} + +void shell_set(char ** argv, int elem) { + + int n; + if (strncmp(argv[1], "name", 4) == 0) { + if (elem != 3) { + printf("set name [name]\n"); + return; + } + printError(gs105e_setName(argv[2])); + } +} + +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: "); + } +} + + + + +int shell (void) { + + char cmd[300]; + int elem = 0; + char **argv; + int n; + + struct gs105e_discovered * ddev; + + char * dev = "nodev"; + while (1) { + printf("\033[96mgs (\033[93m%s\033[96m)# \033[0m", dev); + cmd[0] = 0; + fflush(0); + if (fgets(cmd, 300, stdin) == NULL) { //STRG + D + printf("\nExiting ...\n"); + return 0; + + } + + + + cmd[strlen(cmd) - 1] = 0; + + //Check for an empty command line + if (strlen(cmd) == 0) { + continue; + } + + + //Check for the exit Command + if (strncmp(cmd, "exit", 4) == 0 && strlen(cmd) == 4) + return 0; + + elem = split(&argv, cmd, " "); + + + + if (strncmp(argv[0], "discover", 8) == 0) { + n = gs105e_discover(); + printf("Discovered \033[92m%i\033[0m devices\n", n); + + ddev = gs105e_devs; + + printf("ID\tName\t\tMAC\t\tIP\n"); + + while (ddev != NULL) { + printf("%i\t%s\t\t%X:%X:%X:%X:%X:%X\t%u.%u.%u.%u\n", ddev->id, ddev->name, ddev->mac[0]&0xff, ddev->mac[1]&0xff, ddev->mac[2]&0xff, ddev->mac[3]&0xff, ddev->mac[4]&0xff, ddev->mac[5]&0xff, ddev->ip[0]&0xff, ddev->ip[1]&0xff, ddev->ip[2]&0xff, ddev->ip[3]&0xff); + ddev = ddev->next; + } + + if (n == 1) { + printf("only one switch, selecting 1\n"); + + memcpy(settings.mac, gs105e_devs->mac, 6); + gs105e_queryAll(); + dev = gs105e_devs->name; + } + } + + + + if (strncmp(argv[0], "select", 6) == 0 && elem == 2) { + n = atoi(argv[1]); + if (n == 0){ + printf("Please select a valid ID\n"); + continue; + } + ddev = gs105e_devs; + while (ddev != NULL) { + if (n == ddev->id) + break; + ddev = ddev->next; + } + + if (ddev == NULL){ + printf("Please select a valid ID\n"); + continue; + } + + memcpy(settings.mac, ddev->mac, 6); + gs105e_queryAll(); + dev = ddev->name; + + } + + if (strncmp(dev, "nodev", 6) == 0){ + printf("Discover and select device first!\n"); + continue; + } + + if (strncmp(argv[0], "ip", 2) == 0) { + shell_ip(argv, elem); + } + if (strncmp(argv[0], "vlan", 4) == 0) { + shell_vlan(argv, elem); + } + if (strncmp(argv[0], "port", 4) == 0) { + shell_port(argv, elem); + } + + + + + } +} + diff --git a/shell.h b/shell.h new file mode 100644 index 0000000..743062e --- /dev/null +++ b/shell.h @@ -0,0 +1,22 @@ +#ifndef SHELL_H +#define SHELL_H + +#include +#include "socket.h" + +#include "gs105e.h" +#include +#include + +#include + +void shell_ip(char ** argv, int elem); + + +int shell(void); +void password(void) ; +void printError(int errCode); +void printIp(char * data); +void shell_port(char ** argv, int elem); + +#endif diff --git a/shell_ip.c b/shell_ip.c new file mode 100644 index 0000000..df50cdc --- /dev/null +++ b/shell_ip.c @@ -0,0 +1,40 @@ +#include "shell.h" +void shell_ip(char ** argv, int elem) { + + int n; + if (elem == 1) { + printf(" ip [show|dhcp|set] \n"); + return; + } + + if (strncmp(argv[1], "show", 2) == 0) { + + printf("IP:\t\t"); + printIp(settings.ip); + printf("DHCP:\t\t%s\n", settings.dhcp == 0x01 ? "ON":"OFF"); + printf("Subnetmask:\t"); + printIp(settings.subnetmask); + printf("Gateway:\t"); + printIp(settings.gateway); + printf("\n"); + return; + } + if (strncmp(argv[1], "dhcp", 4) == 0) { + if (elem == 2) { + printf("ip dhcp [on|off|renew]\n"); + printf(" on: Turn on DHCP\n"); + printf(" off: Turn off DHCP, current IP will be taken\n"); + printf(" renew: renew IP-Address via DHCP (turns on DHCP too)\n"); + }else { + password(); + if (strncmp(argv[2], "on", 2) == 0) { + printError(gs105e_dhcpSettings(DHCP_ON)); + } else if (strncmp(argv[2], "off", 3) == 0) { + printError(gs105e_dhcpSettings(DHCP_OFF)); + } else if (strncmp(argv[2], "renew", 5) == 0) { + printError(gs105e_dhcpSettings(DHCP_RENEW)); + } + + } + } +} diff --git a/shell_port.c b/shell_port.c new file mode 100644 index 0000000..258d966 --- /dev/null +++ b/shell_port.c @@ -0,0 +1,21 @@ +#include "shell.h" + +void shell_port(char ** argv, int elem) { + + int n; + + if (elem == 1) { + + return; + } + if (strncmp(argv[1], "show", 4) == 0) { + printf("Port\tSpeed\t\tBytes In\t\tBytes Out\n"); + for (n = 0; n< 5; n++) { + printf("%i\t%s\t% 8lu\t\t% 8lu\n", n, + (settings.portStatistics[n].state == 0x05 ? "1000 Mbit/s" : (settings.portStatistics[n].state == 0x04 ? "100 Mbit/s" : (settings.portStatistics[n].state == 0x03 ? "10 Mbit/s": (settings.portStatistics[n].state == 0x00 ? "No Cable": "???")))), + settings.portStatistics[n].bytesIn, + settings.portStatistics[n].bytesOut + ); + } + } +} diff --git a/shell_vlan.c b/shell_vlan.c new file mode 100644 index 0000000..83f004a --- /dev/null +++ b/shell_vlan.c @@ -0,0 +1,139 @@ +#include "shell.h" + +void shell_vlan(char ** argv, int elem) { + + int n, m, o; + + if (elem == 1) { + + return; + } + if (strncmp(argv[1], "show", 4) == 0) { + struct vlan * vl = settings.vlans; + + if (vl == NULL) { + printf("NO Vlans\n"); + return; + } else + printf("Vlans\n"); + + while (vl != NULL) { + printf("%u\t", vl->id); + for (n = 0; n < 5; n++) + printf("%s\t", vl->members & (0x80 >> n) ? (vl->tag & (0x80 >> n) ? "\033[93mT\033[0m" : "\033[92mU\033[0m") : "\033[91m-\033[0m", (unsigned int) vl->members); + printf("\n"); + vl = vl->next; + } + + }else if (strncmp(argv[1], "add", 3) == 0) { + if (elem != 3) { + printf("You need to provide a vlan ID to add!\n"); + return; + } + n = atoi(argv[2]); + if (n <= 0) { + printf("Invalid id!\n"); + return; + } + password(); + printError(gs105e_addVlan(n)); + }else if (strncmp(argv[1], "del", 3) == 0) { + + if (elem != 3) { + printf("You need to provide a vlan ID to add!\n"); + return; + } + n = atoi(argv[2]); + + struct vlan * vl = settings.vlans; + + while (vl != NULL) { + if (vl->id == n) + break; + vl = vl->next; + } + if (vl == NULL) + n = 0; + + if (n <= 0) { + printf("Invalid id!\n"); + return; + } + password(); + printError(gs105e_delVlan(n)); + }else if (strncmp(argv[1], "assign", 6) == 0) { + if (elem != 5) { + printf("vlan assign [vlan ID] [port ID] [T|U]\n"); + return; + } + int n = atoi(argv[2]); + struct vlan * vl = settings.vlans; + + while (vl != NULL) { + if (vl->id == n) + break; + vl = vl->next; + } + if (vl == NULL) + n = 0; + + if (n <= 0) { + printf("Invalid Vlan ID\n"); + return; + } + + m = atoi(argv[3]); + + if (m < 1 | m > 5) { + printf("Invalid Port ID\n"); + return; + } + + m = (0x80 >> ((m & 0xFF) - 1)) | (int)vl->members; + + if (strncmp(argv[4], "T", 1) == 0) { + o = (int)vl->tag | m; + }else if (strncmp(argv[4], "U", 1) == 0) { + o = (int)vl->tag &(~((char)m)); + } + password(); + printError(gs105e_setVlanMembers(n, m, o)); + + }else if (strncmp(argv[1], "revoke", 6) == 0) { + if (elem != 4) { + printf("vlan revoke [vlan ID] [port ID]\n"); + return; + } + int n = atoi(argv[2]); + struct vlan * vl = settings.vlans; + + while (vl != NULL) { + if (vl->id == n) + break; + vl = vl->next; + } + if (vl == NULL) + n = 0; + + if (n <= 0) { + printf("Invalid Vlan ID\n"); + return; + } + + m = atoi(argv[3]); + + if (m < 1 | m > 5) { + printf("Invalid Port ID\n"); + return; + } + + m = (~(0x80 >> ((m & 0xFF) - 1))) & (int)vl->members; + + + o = (int)vl->tag &(~((char)m)); + + password(); + printError(gs105e_setVlanMembers(n, m, o)); + + } +} diff --git a/socket.c b/socket.c new file mode 100644 index 0000000..b8a46ec --- /dev/null +++ b/socket.c @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "socket.h" +#include +int sock; + + +struct sockaddr_in sockAddr; + +unsigned char * get_mac(const char *intf) { + unsigned char * mac = (unsigned char *) malloc(sizeof(unsigned char *) * 6); + struct ifreq iface; + unsigned char i; + + int tmpSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + + + //How to get the MAC of the Interface: http://www.geekpage.jp/en/programming/linux-network/get-macaddr.php + iface.ifr_addr.sa_family = AF_INET; + strncpy(iface.ifr_name, intf, IFNAMSIZ-1); + ioctl(tmpSock, SIOCGIFHWADDR, &iface); + close(tmpSock); + + for (i = 0; i < 6; i++) + mac[i] = (unsigned char)iface.ifr_hwaddr.sa_data[i]; + + return mac; +} + +void init_socket() { + sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + + if (sock == -1) { + printf("Could not create Socket\n"); + exit(1); + } + + + + memset(&sockAddr, 0, sizeof(sockAddr)); + sockAddr.sin_family = AF_INET; + sockAddr.sin_addr.s_addr = htonl(INADDR_ANY); + sockAddr.sin_port = htons(63321); + + if (bind(sock, (struct sockaddr *) &sockAddr, sizeof(sockAddr)) < 0) { + printf("Could not bind to socket\n"); + exit(1); + } + + +} + + +int sendBroadcast(char * data, unsigned int len) { + + int bcp = 1; + if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &bcp, sizeof(bcp)) < 0) { + printf("Could not broadcast data\n"); + exit(1); + } + + struct sockaddr_in bcAddr; + + bcAddr.sin_family = AF_INET; + bcAddr.sin_addr.s_addr = inet_addr("255.255.255.255"); + bcAddr.sin_port = htons(63322); + + return sendto(sock, data, len, 0, (struct sockaddr *) &bcAddr, sizeof(bcAddr)); + + +} + +int recvBroadcast(char * data) { + + struct sockaddr_in sa_in; + int sa_len = sizeof(sa_in); + struct timeval tout; + tout.tv_sec = 0; + tout.tv_usec = 500000; + fd_set fds; + FD_ZERO(&fds); + FD_SET(sock, &fds); + int s = select(sock + 1, &fds, NULL, NULL, &tout); + if (s <= 0) + return -1; + return recvfrom(sock, data, PACKET_BUFFER, 0,(struct sockaddr * )&sa_in, &sa_len); + +} + + +int recvBroadcast_tout(char * data) { + + struct sockaddr_in sa_in; + int sa_len = sizeof(sa_in); + struct timeval tout; + tout.tv_sec = 5; + tout.tv_usec = 0; + fd_set fds; + FD_ZERO(&fds); + FD_SET(sock, &fds); + int s = select(sock + 1, &fds, NULL, NULL, &tout); + if (s <= 0) + return -1; + return recvfrom(sock, data, PACKET_BUFFER, 0,(struct sockaddr * )&sa_in, &sa_len); + +} + + diff --git a/socket.h b/socket.h new file mode 100644 index 0000000..cdbccea --- /dev/null +++ b/socket.h @@ -0,0 +1,6 @@ +void init_socket(); +unsigned char * get_mac(const char *intf); + +unsigned char * myMac; + +#define PACKET_BUFFER 2500 -- 2.39.5