diff --git a/zwickroll.c b/zwickroll.c index 8973e13..93a07c9 100644 --- a/zwickroll.c +++ b/zwickroll.c @@ -12,15 +12,58 @@ * * Created on: December, 4, 2013 * Author: koennecke + * + * Revised to identify out-of-band messages and to pass them on to a separate + * handler script + * + * Mark Koennecke, December 2014 */ #include #include #include +#include +#include +#include +#include -/*----------------------------------------------------------- - * I am abusing the echo field as a flag if we are reading - * an echo char or not ------------------------------------------------------------*/ +static char *outofBandHandler = NULL; +static int outofBandList = -1; +/*----------------------------------------------------------------------------*/ +static int outofBandHandled(Ascon *a) +{ + int status; + char testString[80]; + char command[1024]; + char *toTest; + + if(outofBandList < 0){ + return 0; + } + + status = LLDnodePtr2First(outofBandList); + toTest = GetCharArray(a->rdBuffer); + /* printf("outofBandHandler processing: %s\n", toTest); */ + while(status != 0){ + LLDstringData(outofBandList, testString); + if(strstr(toTest, testString) != NULL){ + strcpy(command,outofBandHandler); + strncat(command," \"",sizeof(command)); + strncat(command,GetCharArray(a->rdBuffer),sizeof(command)); + strncat(command,"\"",sizeof(command)); + /* printf("outofBandHandler found match with %s\n", testString); */ + status = Tcl_Eval(InterpGetTcl(pServ->pSics), command); + if(status != TCL_OK){ + traceIO("zwickroll","Tcl Error %s while processing out-of-band", Tcl_GetStringResult(InterpGetTcl(pServ->pSics))); + /* printf("Tcl Error %s while processing out-of-band\n", Tcl_GetStringResult(InterpGetTcl(pServ->pSics))); */ + } + DynStringClear(a->rdBuffer); + return 1; + } + status = LLDnodePtr2Next(outofBandList); + } + return 0; +} +/*----------------------------------------------------------------------------*/ static int ZwickrollHandler(Ascon *a) { int ret, l; @@ -39,11 +82,44 @@ static int ZwickrollHandler(Ascon *a) } break; case AsconReading: + ret = AsconStdHandler(a); + if(a->state == AsconReadDone){ + if(outofBandHandled(a)){ + a->state = AsconReading; + } + } + return ret; default: return AsconStdHandler(a); } return 1; } +/*------------------------------------------------------------------------*/ +static int ZwickRollInit(Ascon *a, SConnection *con, int argc, char *argv[]) +{ + char token[80], *pPtr; + + assert(argc>1); + a->hostport = strdup(argv[1]); + a->sendTerminator = strdup("\r\n"); + a->replyTerminator = strdup("\n"); + a->timeout = 2.; + + if(argc > 3){ + outofBandHandler = strdup(argv[3]); + outofBandList = LLDstringCreate(); + pPtr = argv[2]; + while(pPtr != NULL){ + pPtr = stptok(pPtr,token,sizeof(token),":"); + if(pPtr != NULL){ + LLDstringAdd(outofBandList,token); + } + } + } + + return 1; +} + /*------------------------------------------------------------------------*/ void AddZwickrollProtocoll() { @@ -51,7 +127,7 @@ void AddZwickrollProtocoll() prot = calloc(sizeof(AsconProtocol), 1); prot->name = strdup("zwickroll"); - prot->init = AsconStdInit; + prot->init = ZwickRollInit; prot->handler = ZwickrollHandler; AsconInsertProtocol(prot); }