From ac848de54334e0135cc4f8fb4048689ce652f289 Mon Sep 17 00:00:00 2001 From: darkcoven Date: Mon, 26 Dec 2011 12:00:00 +0100 Subject: [PATCH] Added DENIED error. --- cli/admin.c | 24 +++++++--------- cli/common.c | 8 ++++-- lib/ngadmin.h | 11 +++---- lib/src/lib.h | 2 +- lib/src/network.c | 72 +++++++++++++++++++++++++--------------------- lib/src/ngadmin.c | 1 + wireshark/nsdp.lua | 7 +++-- 7 files changed, 66 insertions(+), 59 deletions(-) diff --git a/cli/admin.c b/cli/admin.c index eddbf83..b23018d 100644 --- a/cli/admin.c +++ b/cli/admin.c @@ -77,14 +77,9 @@ char* my_generator (const char* text, int state) { } - while ( (name=tn->name)!=NULL ) { - ++tn; - - if ( strncmp(name, text, len)==0 ) { + while ( (name=tn++->name)!=NULL ) + if ( strncmp(name, text, len)==0 ) return strdup(name); - } - - } return NULL; @@ -178,6 +173,14 @@ int main (int argc, char **argv) { + tcgetattr(STDIN_FILENO, &orig_term); + current_term=orig_term; + /* + current_term.c_lflag&=~ECHOCTL; + tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term); + */ + + opterr=0; while ( (n=getopt_long(argc, argv, "bfgi:ht:", opts, NULL))!=-1 ) { @@ -253,13 +256,6 @@ int main (int argc, char **argv) { rl_completion_entry_function=my_generator; - tcgetattr(STDIN_FILENO, &orig_term); - current_term=orig_term; - /* - current_term.c_lflag&=~ECHOCTL; - tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term); - */ - signal(SIGTERM, handler); signal(SIGINT, handler); diff --git a/cli/common.c b/cli/common.c index dec8cc5..4a5315c 100644 --- a/cli/common.c +++ b/cli/common.c @@ -4,9 +4,6 @@ - - - void printErrCode (int err) { @@ -14,6 +11,7 @@ void printErrCode (int err) { case ERR_OK: /*printf("ok\n");*/ break; case ERR_NET: printf("network error\n"); break; case ERR_NOTLOG: printf("no switch selected\n"); break; + case ERR_DENIED: printf("access denied\n"); break; case ERR_BADPASS: printf("wrong password\n"); break; case ERR_BADID: printf("bad switch id\n"); break; case ERR_INVARG: printf("invalid argument\n"); break; @@ -55,6 +53,10 @@ const char* prio[]={ }; + + + + int parseBitrate (const char *s) { int i; diff --git a/lib/ngadmin.h b/lib/ngadmin.h index f806a71..81daf25 100644 --- a/lib/ngadmin.h +++ b/lib/ngadmin.h @@ -48,11 +48,12 @@ enum { ERR_OK=0, /**< no error */ ERR_NET=-1, /**< network error */ ERR_NOTLOG=-2, /**< not logged */ - ERR_BADPASS=-3, /**< bad password */ - ERR_BADID=-4, /**< bad switch id */ - ERR_INVARG=-5, /**< invalid argument */ - ERR_TIMEOUT=-6, /**< timeout */ - ERR_NOTIMPL=-7 /**< not implemented */ + ERR_DENIED=-3, /**< access denied */ + ERR_BADPASS=-4, /**< bad password */ + ERR_BADID=-5, /**< bad switch id */ + ERR_INVARG=-6, /**< invalid argument */ + ERR_TIMEOUT=-7, /**< timeout */ + ERR_NOTIMPL=-8 /**< not implemented */ }; diff --git a/lib/src/lib.h b/lib/src/lib.h index 610b80e..93aaae9 100644 --- a/lib/src/lib.h +++ b/lib/src/lib.h @@ -24,7 +24,7 @@ #define ERROR_READONLY 3 #define ERROR_INVALID_VALUE 5 -#define ERROR_INVALID_PASSWORD 7 +#define ERROR_DENIED 7 #define ATTR_PRODUCT 0x0001 #define ATTR_UNK_0002 0x0002 diff --git a/lib/src/network.c b/lib/src/network.c index 4f4b3ff..a7b01d1 100644 --- a/lib/src/network.c +++ b/lib/src/network.c @@ -11,34 +11,14 @@ int startNetwork (struct ngadmin *nga) { int ret; - memset(&nga->local, 0, sizeof(struct sockaddr_in)); - nga->local.sin_family=AF_INET; - nga->local.sin_port=htons(CLIENT_PORT); - - memset(&ifr, 0, sizeof(struct ifreq)); - strncpy(ifr.ifr_name, nga->iface, IFNAMSIZ-1); - + // create socket if ( (nga->sock=socket(AF_INET, SOCK_DGRAM, 0))<0 ) { perror("socket"); return nga->sock; } - /* - // get the interface IP address - if ( (ret=ioctl(nga->sock, SIOCGIFADDR, &ifr))<0 ) { - perror("ioctl(SIOCGIFADDR)"); - close(nga->sock); - return ret; - } - */ - /* - Here we have a problem: when you have multiple interfaces, sending a packet to - 255.255.255.255 may not send it to the interface you want. If you bind() to - the address of the interface, you will not be able to receive broadcasts. - The only solution I have found yet is in this case to use - setsockopt(SO_BINDTODEVICE) but this requires root priviledges. - */ - //local.sin_addr=(*(struct sockaddr_in*)&ifr.ifr_addr).sin_addr; // FIXME + memset(&ifr, 0, sizeof(struct ifreq)); + strncpy(ifr.ifr_name, nga->iface, IFNAMSIZ-1); // get the interface broadcast address if ( (ret=ioctl(nga->sock, SIOCGIFBRDADDR, &ifr))<0 ) { @@ -57,6 +37,10 @@ int startNetwork (struct ngadmin *nga) { memcpy(&nga->localmac, ifr.ifr_hwaddr.sa_data, ETH_ALEN); // bind + memset(&nga->local, 0, sizeof(struct sockaddr_in)); + nga->local.sin_family=AF_INET; + nga->local.sin_port=htons(CLIENT_PORT); + if ( (ret=bind(nga->sock, (struct sockaddr*)&nga->local, sizeof(struct sockaddr_in)))<0 ) { perror("bind"); close(nga->sock); @@ -70,7 +54,7 @@ int startNetwork (struct ngadmin *nga) { return ret; } - // prevent unicast packets from being routed + // prevent unicast packets from being routed by setting the TTL to 1 ret=1; if ( (ret=setsockopt(nga->sock, IPPROTO_IP, IP_TTL, &ret, sizeof(ret)))<0 ) { perror("setsockopt(IP_TTL)"); @@ -145,6 +129,30 @@ int updateTimeout (struct ngadmin *nga) { } +/* +// --------------------------------------------------------- +int connectSwitch (struct ngadmin *nga, struct swi_attr *sa) { + + struct sockaddr_in remote; + + + memset(&remote, 0, sizeof(struct sockaddr_in)); + remote.sin_family=AF_UNSPEC; + remote.sin_port=htons(SWITCH_PORT); + + nga->current=sa; + + if ( sa!=NULL && !nga->keepbroad ) { + remote.sin_family=AF_INET; + remote.sin_addr=sa->nc.ip; + } + + + return connect(nga->sock, (struct sockaddr*)&remote, sizeof(struct sockaddr_in)); + +} +*/ + // ---------------------------------------------------------------- int sendNgPacket (struct ngadmin *nga, char code, const List *attr) { @@ -253,7 +261,7 @@ int recvNgPacket (struct ngadmin *nga, char code, unsigned char *error, unsigned //my_poll(&fds, 1, &rem); FD_ZERO(&fs); FD_SET(nga->sock, &fs); - select(nga->sock+1, &fs, NULL, NULL, &rem); + select(nga->sock+1, &fs, NULL, NULL, &rem); // FIXME: non portable len=recvfrom(nga->sock, buffer, sizeof(buffer), MSG_DONTWAIT, (struct sockaddr*)&remote, &slen); @@ -284,17 +292,15 @@ int recvNgPacket (struct ngadmin *nga, char code, unsigned char *error, unsigned static int checkErrorCode (unsigned char err, unsigned short attr_error) { - if ( err==ERROR_INVALID_PASSWORD && attr_error==ATTR_PASSWORD ) { - return ERR_BADPASS; - } - - if ( err==ERROR_INVALID_VALUE ) { - return ERR_INVARG; + switch ( err ) { + + case ERROR_DENIED: return attr_error==ATTR_PASSWORD ? ERR_BADPASS : ERR_DENIED ; + case ERROR_INVALID_VALUE: return ERR_INVARG; + default: return ERR_OK; + } - return ERR_OK; - } diff --git a/lib/src/ngadmin.c b/lib/src/ngadmin.c index 6473465..7b71922 100644 --- a/lib/src/ngadmin.c +++ b/lib/src/ngadmin.c @@ -216,6 +216,7 @@ int ngadmin_scan (struct ngadmin *nga) { // try to receive any packets until timeout swiList=createEmptyList(); + // FIXME: end after timeout whatever received packet is good or not while ( recvNgPacket(nga, CODE_READ_REP, NULL, NULL, attr)>=0 ) { sa=malloc(sizeof(struct swi_attr)); extractSwitchAttributes(sa, attr); diff --git a/wireshark/nsdp.lua b/wireshark/nsdp.lua index f67f5f1..fccc8b3 100644 --- a/wireshark/nsdp.lua +++ b/wireshark/nsdp.lua @@ -28,7 +28,7 @@ local op_codes={ local error_codes={ [0]="OK", [5]="Invalid Value", - [7]="Invalid Password" + [7]="Access Denied" } @@ -267,11 +267,12 @@ local function dissect_header (buffer, subtree) subtree:add(f_code, buffer(1, 1)):append_text(" ("..(op_codes[buffer(1, 1):uint()] or "unknown")..")") local errcode=buffer(2, 1):uint() + local errattr=buffer(4, 2):uint() subtree:add(f_error, buffer(2, 1)):append_text(" ("..(error_codes[errcode] or "unknown")..")") -- add the erroneous attribute only if an error occurred - if ( errcode~=0 ) then - local atf=attributes[buffer(4, 2):uint()] + if ( errattr~=0 ) then + local atf=attributes[errattr] subtree:add(f_errattr, buffer(4, 2)):append_text(" ("..(atf and atf.name or "unk")..")") end -- 2.39.2