diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ecosboard.c | 58 | ||||
-rw-r--r-- | src/helper/ioutil.c | 121 | ||||
-rw-r--r-- | src/server/telnet_server.c | 2 |
3 files changed, 122 insertions, 59 deletions
diff --git a/src/ecosboard.c b/src/ecosboard.c index d2c11b35..019eb0e8 100644 --- a/src/ecosboard.c +++ b/src/ecosboard.c @@ -419,60 +419,6 @@ static int zylinjtag_Jim_Command_reboot(Jim_Interp *interp, int argc, return JIM_OK; } -static int zylinjtag_Jim_Command_mac(Jim_Interp *interp, int argc, - Jim_Obj * const *argv) -{ - - Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0); - - Jim_AppendString(httpstate.jim_interp, tclOutput, hwaddr, strlen(hwaddr)); - - Jim_SetResult(interp, tclOutput); - - return JIM_OK; -} - -static int zylinjtag_Jim_Command_ip(Jim_Interp *interp, int argc, - Jim_Obj * const *argv) -{ - Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0); - - struct ifaddrs *ifa = NULL, *ifp = NULL; - - if (getifaddrs(&ifp) < 0) - { - return JIM_ERR; - } - - for (ifa = ifp; ifa; ifa = ifa->ifa_next) - { - char ip[200]; - socklen_t salen; - - if (ifa->ifa_addr->sa_family == AF_INET) - salen = sizeof(struct sockaddr_in); - else if (ifa->ifa_addr->sa_family == AF_INET6) - salen = sizeof(struct sockaddr_in6); - else - continue; - - if (getnameinfo(ifa->ifa_addr, salen, ip, sizeof(ip), NULL, 0, - NI_NUMERICHOST) < 0) - { - continue; - } - - Jim_AppendString(httpstate.jim_interp, tclOutput, ip, strlen(ip)); - break; - - } - - freeifaddrs(ifp); - - Jim_SetResult(interp, tclOutput); - - return JIM_OK; -} extern Jim_Interp *interp; @@ -506,10 +452,6 @@ static void zylinjtag_startNetwork() zylinjtag_Jim_Command_reboot, NULL, NULL); Jim_CreateCommand(httpstate.jim_interp, "threads", zylinjtag_Jim_Command_threads, NULL, NULL); - Jim_CreateCommand(httpstate.jim_interp, "mac", zylinjtag_Jim_Command_mac, - NULL, NULL); - Jim_CreateCommand(httpstate.jim_interp, "ip", zylinjtag_Jim_Command_ip, - NULL, NULL); Jim_CreateCommand(httpstate.jim_interp, "format_jffs2", zylinjtag_Jim_Command_format_jffs2, NULL, NULL); diff --git a/src/helper/ioutil.c b/src/helper/ioutil.c index 101df2d9..80e43d4e 100644 --- a/src/helper/ioutil.c +++ b/src/helper/ioutil.c @@ -63,6 +63,8 @@ #include <string.h> +#include <ifaddrs.h> + #include <unistd.h> #include <stdio.h> @@ -557,6 +559,120 @@ zylinjtag_Jim_Command_poke(Jim_Interp *interp, return JIM_OK; } + +/* not so pretty code to fish out ip number*/ +static int zylinjtag_Jim_Command_ip(Jim_Interp *interp, int argc, + Jim_Obj * const *argv) +{ + Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0); + + struct ifaddrs *ifa = NULL, *ifp = NULL; + + if (getifaddrs(&ifp) < 0) + { + return JIM_ERR; + } + + for (ifa = ifp; ifa; ifa = ifa->ifa_next) + { + char ip[200]; + socklen_t salen; + + if (ifa->ifa_addr->sa_family == AF_INET) + salen = sizeof(struct sockaddr_in); + else if (ifa->ifa_addr->sa_family == AF_INET6) + salen = sizeof(struct sockaddr_in6); + else + continue; + + if (getnameinfo(ifa->ifa_addr, salen, ip, sizeof(ip), NULL, 0, + NI_NUMERICHOST) < 0) + { + continue; + } + + Jim_AppendString(interp, tclOutput, ip, strlen(ip)); + break; + + } + + freeifaddrs(ifp); + + Jim_SetResult(interp, tclOutput); + + return JIM_OK; +} + + +/* not so pretty code to fish out eth0 mac address */ +static int zylinjtag_Jim_Command_mac(Jim_Interp *interp, int argc, + Jim_Obj * const *argv) +{ + + + struct ifreq *ifr, *ifend; + struct ifreq ifreq; + struct ifconf ifc; + struct ifreq ifs[5]; + int SockFD; + + SockFD = socket(AF_INET, SOCK_DGRAM, 0); + if (SockFD < 0) + { + return JIM_ERR; + } + + ifc.ifc_len = sizeof(ifs); + ifc.ifc_req = ifs; + if (ioctl(SockFD, SIOCGIFCONF, &ifc) < 0) + { + close(SockFD); + return JIM_ERR; + } + + ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq)); + for (ifr = ifc.ifc_req; ifr < ifend; ifr++) + { + //if (ifr->ifr_addr.sa_family == AF_INET) + { + if (strcmp("eth0", ifr->ifr_name)!=0) + continue; + strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name)); + if (ioctl(SockFD, SIOCGIFHWADDR, &ifreq) < 0) + { + close(SockFD); + return JIM_ERR; + } + + close(SockFD); + + + Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0); + + char buffer[256]; + sprintf(buffer, "%02x-%02x-%02x-%02x-%02x-%02x", + ifreq.ifr_hwaddr.sa_data[0]&0xff, + ifreq.ifr_hwaddr.sa_data[1]&0xff, + ifreq.ifr_hwaddr.sa_data[2]&0xff, + ifreq.ifr_hwaddr.sa_data[3]&0xff, + ifreq.ifr_hwaddr.sa_data[4]&0xff, + ifreq.ifr_hwaddr.sa_data[5]&0xff); + + Jim_AppendString(interp, tclOutput, buffer, strlen(buffer)); + + Jim_SetResult(interp, tclOutput); + + return JIM_OK; + } + } + close(SockFD); + + return JIM_ERR; + +} + + + int ioutil_init(struct command_context_s *cmd_ctx) { register_command(cmd_ctx, NULL, "rm", handle_rm_command, COMMAND_ANY, @@ -583,6 +699,11 @@ int ioutil_init(struct command_context_s *cmd_ctx) Jim_CreateCommand(interp, "poke", zylinjtag_Jim_Command_poke, NULL, NULL); Jim_CreateCommand(interp, "ls", zylinjtag_Jim_Command_ls, NULL, NULL); + Jim_CreateCommand(interp, "mac", zylinjtag_Jim_Command_mac, + NULL, NULL); + + Jim_CreateCommand(interp, "ip", zylinjtag_Jim_Command_ip, + NULL, NULL); return ERROR_OK; } diff --git a/src/server/telnet_server.c b/src/server/telnet_server.c index dc555214..95dfce16 100644 --- a/src/server/telnet_server.c +++ b/src/server/telnet_server.c @@ -626,7 +626,7 @@ int telnet_register_commands(command_context_t *command_context) COMMAND_EXEC, "exit telnet session"); register_command(command_context, NULL, "telnet_port", handle_telnet_port_command, - COMMAND_CONFIG, "port on which to listen for incoming telnet connections"); + COMMAND_ANY, "port on which to listen for incoming telnet connections"); return ERROR_OK; } |