diff --git a/SCinter.c b/SCinter.c index 283580f0..76f5c938 100644 --- a/SCinter.c +++ b/SCinter.c @@ -334,14 +334,14 @@ int InterpExecute(SicsInterp * self, SConnection * pCon, char *pText) self->eOut = eValue; Tcl_ResetResult((Tcl_Interp *) self->pTcl); MacroPush(pCon); - pCon->conStatus = 0; + SCSetConStatus(pCon,0); old = StatisticsBegin(pCommand->stat); iRet = pCommand->OFunc(pCon, self, pCommand->pData, argc, argv); StatisticsEnd(old); /* If a task is registered with the dev exec then conStatus is HWBusy */ - if (pCon->conStatus != HWBusy) { + if (SCGetConStatus(pCon) != HWBusy) { /*comCon = SCGetContext(pCon); */ - if (0 != strcmp("contextdo", pCon->deviceID)) + if (0 != strcmp("contextdo", SCGetDeviceID(pCon))) SCWrite(pCon, "", eFinish); } MacroPop(); diff --git a/anticollider.c b/anticollider.c index 5e144766..582a71e2 100644 --- a/anticollider.c +++ b/anticollider.c @@ -60,7 +60,7 @@ static int ReplacementCheckStatus(void *pData, SConnection * pCon) myCollider->isDirty = 0; StartDevice(pServ->pExecutor, "anticollider", myCollider->pDes, myCollider, - pCon,pCon->runLevel, 77.77); + pCon,SCGetRunLevel(pCon), 77.77); return HWIdle; } else { return HWIdle; diff --git a/conman.c b/conman.c index f68e8497..dadb375d 100644 --- a/conman.c +++ b/conman.c @@ -45,6 +45,11 @@ substantially revised for asynchronous I/O Mark Koennecke, January 2009 + + Removed old cruft including SCStore. Added accessor functions to make the + connection object more private + + Mark Koennecke, October 2016 -----------------------------------------------------------------------------*/ #include "fortify.h" #include @@ -115,14 +120,6 @@ static long lastIdent = 0; /*------------- sending connection (prevent double write when listening) ----*/ static SConnection *sendingConnection = NULL; static int sendingSockHandle = 0; -/*------------- storing connection and context for later use ----*/ -struct SCStore { - SConnection *pCon; - long ident; - int inMacro; - long macroStack; - commandContext cc; -}; /* * This will return the value of a SICS int variable called "sicsdebug" @@ -168,11 +165,6 @@ static void FreeConnection(SConnection * pCon) free(pCon); } -/*------------------------------------------------------------------------*/ -void KillFreeConnections() -{ -} - /*---------------------------------------------------------------------------*/ SConnection *GetSendingConnection(void) { @@ -550,47 +542,6 @@ static int HasNL(char *buffer) #define TXT 0 #define LF 1 -int TelnetWrite(mkChannel * pSock, char *pBuffer) -{ - char *pStart = NULL, *pPtr; - int iCount, iState; - int iRet = 1; - - pStart = pBuffer; - pPtr = pStart; - iState = TXT; - iCount = 0; - while (*pPtr != '\0') { - switch (iState) { - case TXT: - if ((*pPtr == '\r') || (*pPtr == '\n')) { - iState = LF; - iRet = NETWrite(pSock, pStart, iCount); - iRet = NETWrite(pSock, "\r\n", 2); - iCount = 0; - } else { - iCount++; - } - break; - case LF: - if ((*pPtr != '\r') && (*pPtr != '\n')) { - pStart = pPtr; - iCount = 1; - iState = TXT; - } else { - /* do nothing */ - } - break; - } - pPtr++; - } - if (iCount > 0) { - iRet = NETWrite(pSock, pStart, iCount); - iRet = NETWrite(pSock, "\r\n", 2); - } - return iRet; -} - /*-----------------------------------------------------------*/ int TelnetWriteANET(int sockHandle, char *pBuffer) { @@ -2331,117 +2282,6 @@ int SCActive(SConnection * self) } return 0; } - -/*--------------------------------------------------------------------------*/ -SCStore *SCSave(SConnection * pCon, SCStore * oldStore) -{ - commandContext cc; - - if (oldStore == NULL) { - oldStore = calloc(1, sizeof(*oldStore)); - assert(oldStore); - } - oldStore->pCon = pCon; - if (pCon) { - oldStore->ident = pCon->ident; - oldStore->inMacro = pCon->iMacro; - oldStore->cc = SCGetContext(pCon); - oldStore->macroStack = 0; - } - return oldStore; -} - -/*--------------------------------------------------------------------------*/ -SConnection *SCLoad(SCStore * conStore) -{ - SConnection *pCon = NULL; - commandContext old; - - if (conStore) { - pCon = conStore->pCon; - } - if (pCon) { - if (conStore->ident != pCon->ident) { - conStore->pCon = NULL; /* connection is dead */ - pCon = NULL; - } - } - if (pCon) { - return pCon; - } - return pServ->dummyCon; -} - -/*--------------------------------------------------------------------------*/ -SConnection *SCStorePush(SCStore * conStore) -{ - SConnection *pCon; - - pCon = SCLoad(conStore); - /* push macro flag on stack */ - conStore->macroStack <<= 1; - conStore->macroStack |= (pCon->iMacro != 0); - SCsetMacro(pCon, conStore->inMacro); - SCPushContext2(pCon, conStore->cc); - return pCon; -} - -/*--------------------------------------------------------------------------*/ -void SCStorePop(SCStore * conStore) -{ - SConnection *pCon; - - pCon = SCLoad(conStore); - SCPopContext(pCon); - /* pop macro flag from stack - SCsetMacro(pCon,conStore->macroStack); - */ - SCsetMacro(pCon, (conStore->macroStack & 1)); - conStore->macroStack >>= 1; -} - -/*--------------------------------------------------------------------------*/ -int SCStoreConnected(SCStore * conStore) -{ - return (conStore && - conStore->pCon && conStore->pCon->ident == conStore->ident); -} - -/*--------------------------------------------------------------------------*/ -void SCStoreFree(SCStore * conStore) -{ - free(conStore); -} - -/* --------------------------------------------------------------------------*/ -long SCTagContext(SConnection * self, char *tagName) -{ - commandContext a; - if (NULL == self) - return -1; - - a = SCGetContext(self); - strlcpy(a.deviceID, tagName, SCDEVIDLEN); - /* - SCGetContext will already have advanced the stack pointer to the - last position - */ - LLDnodeDataTo(self->contextStack, &a); - return 1; -} - -/* --------------------------------------------------------------------------*/ -long SCAdvanceContext(SConnection * self, char *tagName) -{ - if (NULL == self) - return -1; - self->iCmdCtr++; - if (999999 < self->iCmdCtr) { - self->iCmdCtr = 0; - } - return SCPushContext(self, self->iCmdCtr, tagName); -} - /*------------------------------------------------------------------------*/ int SCVerifyConnection(SConnection * self) { @@ -2501,3 +2341,171 @@ int SCPopContext(SConnection * pCon) } return 1; } +/*--------------------------------------------------------*/ +int SCGetRunLevel(SConnection *pCon) +{ + if (!VerifyConnection(pCon)) { + return 0; + } + return pCon->runLevel; +} +/*--------------------------------------------------------*/ +long SCGetIdent(SConnection *pCon) +{ + if (!VerifyConnection(pCon)) { + return 0; + } + return pCon->ident; +} +/*--------------------------------------------------------*/ +int SCGetSicsError(SConnection *pCon) +{ + if (!VerifyConnection(pCon)) { + return 0; + } + return pCon->sicsError; +} +/*--------------------------------------------------------*/ +int SCGetTransID(SConnection *pCon) +{ + if (!VerifyConnection(pCon)) { + return 0; + } + return pCon->transID; +} +/*--------------------------------------------------------*/ +int SCGetProtocolID(SConnection *pCon) +{ + if (!VerifyConnection(pCon)) { + return 0; + } + return pCon->iProtocolID; +} +/*--------------------------------------------------------*/ +int SCGetSockHandle(SConnection *pCon) +{ + if (!VerifyConnection(pCon)) { + return 0; + } + return pCon->sockHandle; +} +/*--------------------------------------------------------*/ +int SCGetConStatus(SConnection *pCon) +{ + if (!VerifyConnection(pCon)) { + return 0; + } + return pCon->conStatus; +} +/*--------------------------------------------------------*/ +char *SCGetDeviceID(SConnection *pCon) +{ + if (!VerifyConnection(pCon)) { + return 0; + } + return pCon->deviceID; +} +/*-------------------------------------------------------*/ +int SCGetEnd(SConnection *pCon) +{ + if (!VerifyConnection(pCon)) { + return 0; + } + return pCon->iEnd; +} +/*-------------------------------------------------------*/ +void SCSetConStatus(SConnection *pCon, int conStatus) +{ + if (!VerifyConnection(pCon)) { + return; + } + pCon->conStatus = conStatus; +} +/*-------------------------------------------------------*/ +void SCSetEventType(SConnection *pCon, int eventType) +{ + if (!VerifyConnection(pCon)) { + return; + } + pCon->conEventType = eventType; +} +/*-------------------------------------------------------*/ +void SCSetSicsError(SConnection *pCon, int sicsError) +{ + if (!VerifyConnection(pCon)) { + return; + } + pCon->sicsError = sicsError; +} +/*-------------------------------------------------------*/ +void SCSetProtocolID(SConnection *pCon, int id) +{ + if (!VerifyConnection(pCon)) { + return; + } + pCon->iProtocolID = id; +} +/*--------------------------------------------------------*/ +void SCCostaLock(SConnection *pCon) +{ + if (!VerifyConnection(pCon)) { + return; + } + CostaLock(pCon->pStack); +} +/*---------------------------------------------------------*/ +void SCCostaUnLock(SConnection *pCon){ + if (!VerifyConnection(pCon)) { + return; + } + CostaUnlock(pCon->pStack); +} +/*---------------------------------------------------------*/ +int SCCostaLocked(SConnection *pCon) +{ + if (!VerifyConnection(pCon)) { + return 0; + } + return CostaLocked(pCon->pStack); +} +/*----------------------------------------------------------*/ +int SCCostaTop(SConnection *pCon, char *command) +{ + if (!VerifyConnection(pCon)) { + return 0; + } + return CostaTop(pCon->pStack, command); +} +/*----------------------------------------------------------*/ +void SCSetGrab(SConnection *pCon, int iGrab) +{ + if (!VerifyConnection(pCon)) { + return; + } + pCon->iGrab = iGrab; +} +/*------------------------------------------------------------*/ +void SCSetEnd(SConnection *pCon, int val) +{ + if (!VerifyConnection(pCon)) { + return; + } + pCon->iEnd = val; +} +/*------------------------------------------------------------*/ +void SCSetTelnet(SConnection *pCon, int val) +{ + if (!VerifyConnection(pCon)) { + return; + } + pCon->iTelnet = val; +} +/*------------------------------------------------------------*/ +void SCClose(SConnection *pCon) +{ + if (!VerifyConnection(pCon)) { + return; + } + ANETclose(pCon->sockHandle); + pCon->iEnd = 1; +} diff --git a/conman.h b/conman.h index 89cf400a..2b37cc49 100644 --- a/conman.h +++ b/conman.h @@ -85,6 +85,7 @@ SConnection *SCfindMaster(SConnection * pCon); int SCisConnected(SConnection * pCon); int VerifyConnection(SConnection * pCon); int SCVerifyConnection(SConnection * self); +void SCClose(SConnection *pCon); /*------------------------------- tasking --------------------------------*/ int SCTaskFunction(void *pCon); void SCSignalFunction(void *pCon, int iSignal, void *pSigData); @@ -139,6 +140,23 @@ int SCMatchRights(SConnection * pCon, int iCode); int SCGetOutClass(SConnection * self); int SCGetGrab(SConnection * pCon); int SCActive(SConnection * pCon); +int SCGetRunLevel(SConnection *pCon); +long SCGetIdent(SConnection *pCon); +int SCGetSicsError(SConnection *pCon); +int SCGetTransID(SConnection *pCon); +int SCGetProtocolID(SConnection *pCon); +int SCGetSockHandle(SConnection *pCon); +int SCGetConStatus(SConnection *pCon); +char *SCGetDeviceID(SConnection *pCon); +int SCGetEnd(SConnection *pCon); +/************************* connection parameter change **********************/ +void SCSetConStatus(SConnection *pCon, int conStatus); +void SCSetEventType(SConnection *pCon, int eventType); +void SCSetSicsError(SConnection *pCon, int sicsError); +void SCSetProtocolID(SConnection *pCon, int proID); +void SCSetGrab(SConnection *pCon, int iGrab); +void SCSetEnd(SConnection *pCon, int val); +void SCSetTelnet(SConnection *pCon, int val); /* **************************** Invocation ******************************** */ int SCInvoke(SConnection * self, SicsInterp * pInter, char *pCommand); @@ -165,28 +183,6 @@ int SCPushContext(SConnection * pCon, int ID, char *deviceID); int SCPushContext2(SConnection * pCon, commandContext cc); int SCPopContext(SConnection * pCon); commandContext SCGetContext(SConnection * pCon); -/******************************** Store ************************************/ -typedef struct SCStore SCStore; - -SCStore *SCSave(SConnection * pCon, SCStore * oldStore); -/* save a connection and its context for later use. */ - -SConnection *SCLoad(SCStore * conStore); -/* check con and return SConnection if still valid or a dummy connection otherwise. */ - -SConnection *SCStorePush(SCStore * conStore); -/* load connection and push stored context. Must be paired with an SCStorePop command */ - -void SCStorePop(SCStore * conStore); -/* pop context */ - -int SCStoreConnected(SCStore * conStore); -/* check if a stored connection is not closed */ - -void SCStoreFree(SCStore * conStore); -/* free an SCStore */ - -void KillFreeConnections(void); /************************* CallBack *********************************** */ int SCRegister(SConnection * pCon, SicsInterp * pSics, void *pInter, long lID); @@ -202,4 +198,10 @@ int SCUnregisterID(SConnection * pCon, long ID); * returns -1 if no ID can be found. */ long SCgetCallbackID(SConnection * pCon, void *pData); +/*************************** command stack handling **********************/ +void SCCostaLock(SConnection *pCon); +void SCCostaUnLock(SConnection *pCon); +int SCCostaLocked(SConnection *pCon); +int SCCostaTop(SConnection *pCon, char *command); + #endif diff --git a/devexec.c b/devexec.c index dcea95e0..261cb8a1 100644 --- a/devexec.c +++ b/devexec.c @@ -256,7 +256,7 @@ int StartDevice(pExeList self, char *name, pObjectDescriptor pDes, /* may we? */ if (self->pOwner != NULL) { - if (pCon->ident != self->pOwner->ident) { + if (SCGetIdent(pCon) != SCGetIdent(self->pOwner)) { /* this hack helps on rita2, when using the sendsics script which opens a client for every command */ if (overwriteOwner < 0) { @@ -329,7 +329,7 @@ int StartDevice(pExeList self, char *name, pObjectDescriptor pDes, DevExecSignal, NULL, self,TASK_PRIO_HIGH); } - pCon->conStatus = HWBusy; + SCSetConStatus(pCon,HWBusy); return 1; } else { snprintf(pBueffel,131, "ERROR: cannot start device %s", name); diff --git a/evcontroller.c b/evcontroller.c index e5542976..9713cc8d 100644 --- a/evcontroller.c +++ b/evcontroller.c @@ -835,7 +835,7 @@ int EVCDrive(pEVControl self, SConnection * pCon, float fVal) /* start executing */ iRet = StartDevice(GetExecutor(), self->pName, self->pDes, - self, pCon, pCon->runLevel, fVal); + self, pCon, SCGetRunLevel(pCon), fVal); if (!iRet) { snprintf(pBueffel,255, "ERROR: Failure to start %s", self->pName); SCWrite(pCon, pBueffel, eError); @@ -1022,7 +1022,7 @@ static int EVCallBack(int iEvent, void *pEventData, void *pUserData) } if (iEvent == VALUECHANGE && pCon != NULL) { - pCon->conEventType = POSITION; + SCSetEventType(pCon,POSITION); SCWrite(pCon, pBuf, eEvent); return 1; } diff --git a/exebuf.c b/exebuf.c index aed2278d..a1e663a0 100644 --- a/exebuf.c +++ b/exebuf.c @@ -304,7 +304,7 @@ int exeBufProcess(pExeBuf self, SicsInterp * pSics, Log(INFO,"com","%s:batch:%s", ConID(pCon), cmd); status = Tcl_Eval(pTcl, cmd); if (status != TCL_OK) { - if (pCon->sicsError == 0) { + if (SCGetSicsError(pCon) == 0) { /* Tcl Error */ @@ -314,7 +314,7 @@ int exeBufProcess(pExeBuf self, SicsInterp * pSics, } } else { SCWrite(pCon, pTcl->result, eError); - pCon->sicsError = 0; + SCSetSicsError(pCon,0); } SCWrite(pCon, "ERROR: above error was in block:", eError); SCWrite(pCon, cmd, eError); diff --git a/interface.c b/interface.c index 84942f76..c0a7a129 100644 --- a/interface.c +++ b/interface.c @@ -255,8 +255,8 @@ static int DriveTaskFunc(void *data) ExeInterest(pServ->pExecutor,taskData->name, "finished with problem"); } traceSys("drive","DriveTask %s finished with state %d", taskData->name,status); - if(taskData->pCon->transID > 100000) { - SCPrintf(taskData->pCon,eLog,"TASKEND %d", taskData->pCon->transID); + if(SCGetTransID(taskData->pCon) > 100000) { + SCPrintf(taskData->pCon,eLog,"TASKEND %d", SCGetTransID(taskData->pCon)); } return 0; } @@ -288,8 +288,8 @@ long StartDriveTask(void *obj, SConnection *pCon, char *name, float fTarget) ExeInterest(pServ->pExecutor,name,"started"); DevexecLog("START",name); InvokeNewTarget(pServ->pExecutor,name,fTarget); - if(pCon->transID > 100000) { - SCPrintf(pCon,eLog,"TASKSTART %d", pCon->transID); + if(SCGetTransID(pCon) > 100000) { + SCPrintf(pCon,eLog,"TASKSTART %d", SCGetTransID(pCon)); } taskData->id = DRIVEID; @@ -415,8 +415,8 @@ static int CountTaskFunc(void *data) ExeInterest(pServ->pExecutor,taskData->name, "finished with problem"); } traceSys("count","CountTask %s finished with state %d", taskData->name,status); - if(taskData->pCon->transID > 100000) { - SCPrintf(taskData->pCon,eLog,"TASKEND %d", taskData->pCon->transID); + if(SCGetTransID(taskData->pCon) > 100000) { + SCPrintf(taskData->pCon,eLog,"TASKEND %d", SCGetTransID(taskData->pCon)); } return 0; } @@ -443,8 +443,8 @@ long StartCountTask(void *obj, SConnection *pCon, char *name) } ExeInterest(pServ->pExecutor,name,"started"); DevexecLog("START",name); - if(pCon->transID > 100000) { - SCPrintf(pCon,eLog,"TASKSTART %d", pCon->transID); + if(SCGetTransID(pCon) > 100000) { + SCPrintf(pCon,eLog,"TASKSTART %d", SCGetTransID(pCon)); } taskData->id = COUNTID; diff --git a/macro.c b/macro.c index cf68f1bb..37a72e75 100644 --- a/macro.c +++ b/macro.c @@ -171,7 +171,7 @@ static int SicsUnknownProc(ClientData pData, Tcl_Interp * pInter, } /* invoke */ - pCon->sicsError = 0; + SCSetSicsError(pCon,0); iMacro = SCinMacro(pCon); SCsetMacro(pCon, 1); old = StatisticsBegin(pCommand->stat); @@ -202,7 +202,7 @@ static int SicsUnknownProc(ClientData pData, Tcl_Interp * pInter, return TCL_OK; } else { Tcl_SetVar(pInter, SICSERROR, "yes", TCL_GLOBAL_ONLY); - pCon->sicsError = 1; + SCSetSicsError(pCon,1); return TCL_ERROR; } } diff --git a/motor.c b/motor.c index 76ec8b00..7d884e06 100644 --- a/motor.c +++ b/motor.c @@ -1227,7 +1227,7 @@ static int EndScriptCallback(int iEvent, void *pEvent, void *pUser) pMotInfo pMoti = (pMotInfo) pUserData; SConnection *pCon = (SConnection*) context; if (VerifyConnection(pCon) && VerifyConnection(pMoti->pCon)) { - if (pMoti->pCon->ident == pCon->ident) + if (SCGetIdent(pMoti->pCon) == SCGetIdent(pCon)) return 0; } return 1; diff --git a/motreg.c b/motreg.c index 6408130d..cd4fc8fe 100644 --- a/motreg.c +++ b/motreg.c @@ -118,7 +118,7 @@ int StartRegMot(pMotReg self, SConnection * pCon, float fValue) pDriv->SetValue = self->originalSetValue; ret = StartDevice(pServ->pExecutor, self->motorName, FindDescriptor(self->motorData), - self->motorData, pCon, pCon->runLevel, fValue); + self->motorData, pCon, SCGetRunLevel(pCon), fValue); /* snprintf(pBueffel,sizeof(pBueffel)-1,"anticollision started %s to %f",self->motorName, fValue); diff --git a/nread.c b/nread.c index 90f9c700..a77f8def 100644 --- a/nread.c +++ b/nread.c @@ -194,439 +194,6 @@ int NetReadRemove(pNetRead self, mkChannel * pSock) } return 0; } - -/*-------------------------------------------------------------------------*/ -static int NetReadAccept(pNetRead self, mkChannel * pSock) -{ - mkChannel *pNew = NULL; - char pBuffer[1064]; - int iRet; - SConnection *pRes = NULL; - char *pUser = NULL, *pPasswd = NULL; - time_t target; - - if (!VerifyChannel(pSock)) { - return 0; - } - - /* check for new connection */ - pNew = NETAccept(pSock, 0); - if (pNew) { - /* create connection object */ - /* TODO - pRes = SCreateConnection(self->pMain->pSics,pNew,3); - */ - if (!pRes) { - Log(ERROR,"sys","%s","Failure to allocate new Connection"); - NETClosePort(pNew); - free(pNew); - return 0; - } else { - /* register the connection and create a task for it here */ - NetReadRegister(self, pNew, command, pRes); - TaskRegisterN(self->pMain->pTasker, "NetReadAccept", - SCTaskFunction, - SCSignalFunction, SCDeleteConnection, pRes, TASK_PRIO_LOW); - SCSendOK(pRes); - return 1; - } - } else { - return 0; - } -} - -/*--------------------------------------------------------------------------*/ -#define COLLECT 0 -#define SKIPTERM 1 -/*------------------------------------------------------------------------*/ -static int NetReadRead(pNetRead self, pNetItem pItem) -{ - char *pPtr, *pEnd, *pBufEnd; - char pBuffer[1024], pMuell[20]; - char pBueffel[80]; - int i, iInt, iRet, iStat, state; - - /* read first */ - memset(pBuffer, 0, 1024); - iRet = NETRead(pItem->pCon->pSock, pBuffer, 1022, 0); - if (iRet < 0) { /* EOF */ - pItem->pCon->iEnd = 1; - NetReadRemove(self, pItem->pCon->pSock); - return 0; - } else if (iRet == 0) { /* should not happen */ - return 1; - } - /* iRet is now the number of bytes read. Now we check for command - interrupts */ - pPtr = strstr(pBuffer, "INT1712"); - if (pPtr) { - sscanf(pPtr, "%s %d", pMuell, &iInt); - if (SCMatchRights(pItem->pCon, usUser)) { - TaskSignal(self->pMain->pTasker, SICSINT, &iInt); - Log(ERROR,"com","sock%03.3d:INTERRUPT", pItem->pCon->pSock->sockid); - if (iInt == eEndServer) { - TaskStop(self->pMain->pTasker); - } - } else { - SCWrite(pItem->pCon, - "ERROR: insufficient privilege to invoke Interrupt", eError); - } - return 0; - } - - /* split into command lines - and put into fifo - */ - pBufEnd = pBuffer + iRet; - pPtr = pBuffer; - pEnd = pBuffer; - state = COLLECT; - while (pEnd < pBufEnd) { - switch (state) { - case COLLECT: - if ((*pEnd != '\r') && (*pEnd != '\n')) { - pEnd++; - } else { - /* there is a string between pPtr and pEnd */ - *pEnd = '\0'; - /* do we have something in hold ? */ - if (strlen(pItem->pHold) > 0) { - strlcat(pItem->pHold, pPtr, 511); - /* DFC locking for protocol zero only */ - if (pItem->pCon->iProtocolID == PROTSICS && - CostaLocked(pItem->pCon->pStack)) - iStat = 0; - else - iStat = CostaTop(pItem->pCon->pStack, pItem->pHold); - if (!iStat) { - SCWrite(pItem->pCon, "ERROR: Busy", eError); - } - pItem->pHold[0] = '\0'; - } else { - /* no, normal command */ - /* DFC locking for protocol zero only */ - if (pItem->pCon->iProtocolID == PROTSICS && - CostaLocked(pItem->pCon->pStack)) - iStat = 0; - else - iStat = CostaTop(pItem->pCon->pStack, pPtr); - if (!iStat) { - SCWrite(pItem->pCon, "ERROR: Busy", eError); - } - } - pPtr = pEnd + 1; - pEnd = pPtr; - state = SKIPTERM; - } - break; - case SKIPTERM: - if ((*pEnd != '\r') && (*pEnd != '\n')) { - state = COLLECT; - pPtr = pEnd; - } else { - pEnd++; - pPtr = pEnd; - } - break; - } - } - /* when we are here we may still have an incomplete command pending */ - if (pEnd != pPtr && strlen(pPtr) > 0) { - strlcpy(pItem->pHold, pPtr, 511); - } - return 1; -} - -/*-------------------------- telnet protocoll code -------------------------*/ -static int TelnetAccept(pNetRead self, mkChannel * pSock) -{ - mkChannel *pNew = NULL; - char pBuffer[1064]; - int iRet; - SConnection *pRes = NULL; - pTelTask pTel = NULL; - - if (!VerifyChannel(pSock)) { - return 0; - } - - /* check for new connection */ - pNew = NETAccept(pSock, 0); - if (pNew) { - /* create connection object */ - /* TODO - pRes = SCreateConnection(self->pMain->pSics,pNew,usSpy); - */ - if (!pRes) { - Log(ERROR,"sys","%s","Failure to allocate new Connection"); - NETClosePort(pNew); - free(pNew); - return 0; - } else { - /* Create a task object for the telnet connection */ - pTel = CreateTelnet(pRes); - if (!pTel) { - Log(ERROR,"sys","%s","Failure to allocate new Telnet Task Object"); - SCDeleteConnection(pRes); - return 0; - } - /* register connection and task */ - pRes->iTelnet = 1; - NetReadRegister(self, pNew, tcommand, pRes); - TaskRegisterN(self->pMain->pTasker," TelnetAccept", - TelnetTask, TelnetSignal, DeleteTelnet, pTel, TASK_PRIO_LOW); - return 1; - } - } else { - return 0; - } -} - -/*------------------------------------------------------------------------ - Telnet is fully described in RFC-854. This implementation is very simple. - It just supports the NVT and no options. Implementation is via a state - machine with the state tStatus in the pItem structure. This is necessary - as single characters may be sent by telnet clients. --------------------------------------------------------------------------*/ -/* Telnet codes */ -#define SE 240 -#define NOP 241 -#define DM 242 /* data mark */ -#define BRK 243 -#define IP 244 -#define AO 245 -#define AYT 246 -#define EC 247 -#define EL 248 -#define GA 249 -#define SB 250 -#define WILL 251 -#define WONT 252 -#define DO 253 -#define DONT 254 -#define IAC 255 -#define EOR 239 -/*-----------------------------------------------------------------------*/ -static int TelnetReply(pNetItem pItem, char code, char cChar) -{ - char pReply[3]; - - pReply[0] = IAC; - pReply[1] = code; - pReply[2] = cChar; - - NETWrite(pItem->pCon->pSock, pReply, 3); - return 1; -} - -/*------------------------------------------------------------------------*/ -static int TelnetRead(pNetRead self, pNetItem pItem) -{ - char pBuffer[1024], pMuell[20], pError[256]; - int i, iStat, iInt, iRet; - int cChar; - char *pPtr = NULL; - SConnection *pOwner = NULL; - - /* read first */ - memset(pBuffer, 0, 1023); - iRet = NETRead(pItem->pCon->pSock, pBuffer, 1022, 0); - if (iRet < 0) { /* EOF */ - pItem->pCon->iEnd = 1; - NetReadRemove(self, pItem->pCon->pSock); - return 0; - } else if (iRet == 0) { /* should not happen */ - return 1; - } - - /* iRet is now the number of bytes read. Now we check for command - interrupts */ - pPtr = strstr(pBuffer, "INT1712"); - if (pPtr) { - sscanf(pPtr, "%s %d", pMuell, &iInt); - if (SCMatchRights(pItem->pCon, usUser)) { - /* owners may kill own tasks, otherwise manager - privilege is required. - */ - pOwner = GetExeOwner(pServ->pExecutor); - if (pOwner) { - if (pOwner != pItem->pCon) { - if (!SCMatchRights(pItem->pCon, usMugger)) { - SCWrite(pItem->pCon, - "ERROR: Insufficient privilege to stop other people's experiments", - eError); - return 1; - } - } - } - TaskSignal(self->pMain->pTasker, SICSINT, &iInt); - Log(ERROR,"com","%s:interrupt: %d", ConID(pItem->pCon),iInt); - if (iInt == eEndServer) { - TaskStop(self->pMain->pTasker); - } - } else { - SCWrite(pItem->pCon, - "ERROR: insufficient privilege to invoke Interrupt", eError); - } - return 1; - } - - /* do telnet analysis of the data buffer */ - for (i = 0; i < iRet; i++) { - cChar = (int) pBuffer[i]; -#ifdef TELNETDEBUG - if ((cChar > 48) && (cChar < 128)) { - printf("char: %c\n", cChar); - } else { - printf("Control: %d\n", cChar); - } -#endif - /* Telnet status switching */ - switch (pItem->tStatus) { - case tData: - switch (cChar) { - case IAC: - pItem->tStatus = tIAC; - break; - case '\r': - case '\n': - /* DFC locking for protocol zero only */ - if (pItem->pCon->iProtocolID == PROTSICS && - CostaLocked(pItem->pCon->pStack)) - iStat = 0; - else - iStat = CostaTop(pItem->pCon->pStack, pItem->pHold); - /* printf("%s\n",pItem->pHold); */ - if (!iStat) { - SCWrite(pItem->pCon, "ERROR: Busy", eError); - } - memset(pItem->pHold, 0, 511); - pItem->iEOD = 0; - pItem->tStatus = tCR; - break; - case (char) 8: /* backspace */ - pItem->iEOD--; - if (pItem->iEOD < 0) { - pItem->iEOD = 0; - } - break; - case (char) 0: /* ignore 0 character sent as end of text */ - break; - default: - pItem->pHold[pItem->iEOD] = cChar; - pItem->iEOD++; - break; - - } /* end of tData case */ - break; - case tCR: /* ignore the second character after a newline. - Telnet gives you two characters for newline - */ - pItem->tStatus = tData; - break; - case tIAC: - switch (cChar) { - case IAC: - pItem->tStatus = tData; - break; - case WILL: - pItem->tStatus = tWill; - break; - case WONT: - pItem->tStatus = tWont; - break; - case DONT: - pItem->tStatus = tDont; - break; - case DO: - pItem->tStatus = tDo; - break; - case EOR: - pItem->tStatus = tData; - break; - case SB: - pItem->tStatus = tSB; - break; - case EC: - pItem->iEOD--; - if (pItem->iEOD < 0) { - pItem->iEOD = 0; - } - pItem->tStatus = tData; - break; - case EL: - memset(pItem->pHold, 0, 511); - pItem->iEOD = 0; - pItem->tStatus = tData; - break; - case IP: - SCSetInterrupt(pItem->pCon, eAbortBatch); - pItem->tStatus = tData; - break; - default: - pItem->tStatus = tData; - break; - } /* end of tIAC */ - break; - case tWill: /* we do not do options! */ - TelnetReply(pItem, DONT, cChar); - pItem->tStatus = tData; - break; - case tWont: /* we do not do options! A Wont is sent by the client - if it cannot do a option we requested it to have. As - we do not try to force options, this should not happen - */ - pItem->tStatus = tData; - break; - case tDo: /* we do not do options! */ - TelnetReply(pItem, WONT, cChar); - pItem->tStatus = tData; - break; - case tDont: /* we do not do options! A Dont is sent by the client - if it cannot do a option we requested it to have. As - we do not try to force options, this should not happen - */ - pItem->tStatus = tData; - break; - case tSB: /* as we do not have options, we cannot have suboption - negotaitions. Something is seriously wrong when - we are here. It is a protocoll error. However, we - ignore it silently. tSB marks the start of the - subnegotiation. The current character must be the - option code we are dealing with. - */ - pItem->tStatus = tSE; - break; - case tSE: - /* now we are in the suboption parameter. Normally data - should be copied to a suboption string buffer here - until SE. - */ - switch (cChar) { - case IAC: - break; - case SE: - pItem->tStatus = tData; - /* suboption interpretation would go here */ - break; - default: - /* copy data to suboption buffer */ - break; - } - break; - - default: - /* There is something wrong here! */ - snprintf(pError,sizeof(pError)-1, "ERROR: bad telnet code %d", cChar); - Log(ERROR,"sys","%s",pError); - pItem->tStatus = tData; - break; - - } - } - return 1; -} - /*---------------------------------------------------------------------------*/ static int NetReadUDP(pNetRead self, pNetItem pItem) { @@ -708,13 +275,6 @@ int NetReaderTask(void *pData) GetCharArray(self->conList)); } - /* - * This costs a surprising amount of CPU-time, in a test 10%! - * This is why it has been commented away - snprintf(num, sizeof num, "%d", conCount); - IFSetOption(pSICSOptions, "ConnectionCount", num); - IFSetOption(pSICSOptions, "ConMask", GetCharArray(self->conList)); -*/ /* the select itself */ tmo.tv_usec = self->iReadTimeout; @@ -731,29 +291,6 @@ int NetReaderTask(void *pData) LLDnodeDataTo(self->iList, &NItem); if (FD_ISSET(NItem.pSock->sockid, &lMask)) { /* data */ switch (NItem.eType) { - /* lists have been changed after accept, return */ - case naccept: - NetReadAccept(self, NItem.pSock); - return 1; - break; - case taccept: - TelnetAccept(self, NItem.pSock); - return 1; - break; - case command: - iStatus = NetReadRead(self, &NItem); - if (iStatus == 0) { /* there was an eof */ - /* do not continue, list is messy */ - return 1; - } - break; - case tcommand: - iStatus = TelnetRead(self, &NItem); - if (iStatus == 0) { /* there was an eof */ - /* do not continue, list is messy */ - return 1; - } - break; case udp: NetReadUDP(self, &NItem); break; @@ -1048,6 +585,10 @@ static int testAndInvokeInterrupt(pCommandCBData self, int handle) } return 0; } +/*----------------------------------------------------------------------------------*/ +#define COLLECT 0 +#define SKIPTERM 1 + /*----------------------------------------------------------------------------------*/ static int CommandDataCB(int handle, void *userData) @@ -1067,10 +608,10 @@ static int CommandDataCB(int handle, void *userData) if (pPtr[i] == '\r' || pPtr[i] == '\n') { self->state = SKIPTERM; if (!testAndInvokeInterrupt(self, handle)) { - if (self->pCon->iProtocolID == PROTSICS && CostaLocked(self->pCon->pStack)) + if (SCGetProtocolID(self->pCon) == PROTSICS && SCCostaLocked(self->pCon)) status = 0; else - status = CostaTop(self->pCon->pStack, GetCharArray(self->command)); + status = SCCostaTop(self->pCon, GetCharArray(self->command)); if (!status) { SCWrite(self->pCon, "ERROR: Busy", eError); } @@ -1114,7 +655,7 @@ static int CommandAcceptCB(int handle, void *userData) } usData->pCon = pCon; usData->state = COLLECT; - snprintf(buffer,sizeof(buffer),"con%ld", pCon->ident); + snprintf(buffer,sizeof(buffer),"con%ld", SCGetIdent(pCon)); TaskRegisterN(pServ->pTasker, buffer, SCTaskFunction, @@ -1123,6 +664,30 @@ static int CommandAcceptCB(int handle, void *userData) SCSendOK(pCon); return 1; } +/*------------------------------------------------------------------------ + Telnet is fully described in RFC-854. This implementation is very simple. + It just supports the NVT and no options. Implementation is via a state + machine with the state tStatus in the pItem structure. This is necessary + as single characters may be sent by telnet clients. +-------------------------------------------------------------------------*/ +/* Telnet codes */ +#define SE 240 +#define NOP 241 +#define DM 242 /* data mark */ +#define BRK 243 +#define IP 244 +#define AO 245 +#define AYT 246 +#define EC 247 +#define EL 248 +#define GA 249 +#define SB 250 +#define WILL 251 +#define WONT 252 +#define DO 253 +#define DONT 254 +#define IAC 255 +#define EOR 239 /*-----------------------------------------------------------------------*/ static int ANETTelnetReply(int sockHandle, char code, char cChar) @@ -1171,10 +736,10 @@ static int ANETTelnetProcess(int handle, void *usData) case '\r': case '\n': if (!testAndInvokeInterrupt(self, handle)) { - if (self->pCon->iProtocolID == PROTSICS && CostaLocked(self->pCon->pStack)) + if (SCGetProtocolID(self->pCon) == PROTSICS && SCCostaLocked(self->pCon)) status = 0; else - status = CostaTop(self->pCon->pStack, GetCharArray(self->command)); + status = SCCostaTop(self->pCon, GetCharArray(self->command)); if (!status) { SCWrite(self->pCon, "ERROR: Busy", eError); } @@ -1329,8 +894,8 @@ static int TelnetAcceptCB(int handle, void *userData) return 0; } /* register connection and task */ - pCon->iTelnet = 1; - snprintf(buffer,sizeof(buffer),"con%ld", pCon->ident); + SCSetTelnet(pCon,1); + snprintf(buffer,sizeof(buffer),"con%ld", SCGetIdent(pCon)); TaskRegisterN(pServ->pTasker, buffer, TelnetTask, TelnetSignal, DeleteTelnet, pTel, TASK_PRIO_LOW); diff --git a/nserver.c b/nserver.c index 6d004c29..d40434f9 100644 --- a/nserver.c +++ b/nserver.c @@ -201,17 +201,6 @@ int InitServer(char *file, pServer * pServ) IFDeleteOptions(pSICSOptions); return 0; } - /* - self->pServerPort = NETOpenPort(iPort); - if(!self->pServerPort) - { - printf("Cannot open Server Socket\n"); - DeleteInterp(self->pSics); - IFDeleteOptions(pSICSOptions); - return 0; - } - NetReadRegister(pReader, self->pServerPort, naccept, NULL); - */ NetReadInstallANETPort(pReader, naccept, iPort); /* the device executor */ @@ -361,7 +350,6 @@ void StopServer(pServer self) */ killTclDrivable(); - KillFreeConnections(); killSICSHipadaba(); diff --git a/protocol.c b/protocol.c index cb7e1be5..139110b6 100644 --- a/protocol.c +++ b/protocol.c @@ -306,8 +306,8 @@ static int ProtocolSet(SConnection * pCon, Protocol * pPro, char *pProName) SCSetWriteFunc(pCon, pPro->defaultWriter); break; } - pCon->iProtocolID = proID; - pMaster->iProtocolID = proID; + SCSetProtocolID(pCon,proID); + SCSetProtocolID(pMaster,proID); SCSendOK(pCon); return 1; } @@ -322,7 +322,7 @@ int ProtocolGet(SConnection * pCon, void *pData, char *pProName, int len) } if (pData == NULL) { - pCon->iProtocolID = 0; + SCSetProtocolID(pCon,0); return 1; } @@ -330,12 +330,12 @@ int ProtocolGet(SConnection * pCon, void *pData, char *pProName, int len) if (0 == pPro->isDefaultSet) { pPro->defaultWriter = SCGetWriteFunc(pCon); pPro->isDefaultSet = 1; - pCon->iProtocolID = 0; + SCSetProtocolID(pCon,0); } - strlcpy(pProName, pPro->pProList[pCon->iProtocolID], len); + strlcpy(pProName, pPro->pProList[SCGetProtocolID(pCon)], len); return 1; #if 0 - Index = pCon->iProtocolID; + Index = SCGetProtocolID(pCon); /* check list of protocols for valid name */ switch (Index) { @@ -448,7 +448,7 @@ static int InitDefaultProtocol(SConnection * pCon, Protocol * pPro) if (0 == pPro->isDefaultSet) { pPro->defaultWriter = SCGetWriteFunc(pCon); pPro->isDefaultSet = 1; - pCon->iProtocolID = PROTSICS; + SCSetProtocolID(pCon,PROTSICS); } return pPro->isDefaultSet; } @@ -498,7 +498,7 @@ struct json_object *mkJSON_Object(SConnection * pCon, char *pBuffer, } /* field 1: connID */ json_object_object_add(msg_json, "con", - json_object_new_int(pCon->ident)); + json_object_new_int(SCGetIdent(pCon))); /* field 2: taskID */ json_object_object_add(msg_json, "trans", json_object_new_int(taskID)); /* deviceID */ @@ -565,11 +565,7 @@ int SCWriteJSON_String(SConnection * pCon, char *pBuffer, int iOut) return 1; /* log it for any case */ - if (pCon->pSock) { - iRet = pCon->pSock->sockid; - } else { - iRet = 0; - } + iRet = SCGetSockHandle(pCon); /* write to commandlog if user or manager privilege */ if (SCGetRights(pCon) <= usUser && !SCinMacro(pCon)) { @@ -627,13 +623,13 @@ char *GetProtocolName(SConnection * pCon) InitDefaultProtocol(pCon, pPro); /* check list of protocols for valid name */ - switch (pCon->iProtocolID) { + switch (SCGetProtocolID(pCon)) { case PROTSICS: /* default = psi_sics */ case PROTNORM: /* normal (connection start default) */ case PROTCODE: /* outcodes */ case PROTJSON: /* json */ case PROTACT: /* act */ - return strdup(pPro->pProList[pCon->iProtocolID]); + return strdup(pPro->pProList[SCGetProtocolID(pCon)]); break; default: return strdup("invalid"); @@ -644,17 +640,14 @@ char *GetProtocolName(SConnection * pCon) /*----------------------------------*/ int GetProtocolID(SConnection * pCon) { - if (NULL != pCon) { - return pCon->iProtocolID; - } - return -1; + return SCGetProtocolID(pCon); } /*---------------------------------------------------------------------------*/ writeFunc GetProtocolWriteFunc(SConnection * pCon) { if (pCon != NULL) { - switch (pCon->iProtocolID) { + switch (SCGetProtocolID(pCon)) { case PROTCODE: /* outcodes */ return SCWriteWithOutcode; break; diff --git a/proxy.c b/proxy.c index 7f8c474f..246bc7f8 100644 --- a/proxy.c +++ b/proxy.c @@ -294,7 +294,7 @@ static hdbCallbackReturn ProxyCallback(pHdb node, void *userData, GetHdbProperty(node, "proxy", proxyDev, 80); status = StartDevice(pServ->pExecutor, proxyDev, self->pDes, self, pCon, - pCon->runLevel, (float) set->v->v.doubleValue); + SCGetRunLevel(pCon), (float) set->v->v.doubleValue); if (status == 1) { return hdbContinue; } else { diff --git a/scan.c b/scan.c index 0493471d..ebe8b569 100644 --- a/scan.c +++ b/scan.c @@ -1308,7 +1308,7 @@ static int ScanUUInterest(int iEvent, void *pEventData, void *pUser) assert(self); assert(pCon); /* - printf("ScanUUInterest called for pCon = %p handle %d\n", pCon, pCon->sockHandle); + printf("ScanUUInterest called for pCon = %p handle %d\n", pCon, SCGetSockHandle(pCon)); */ if (!SCisConnected(pCon)) { return -1; @@ -1812,7 +1812,7 @@ int ScanWrapper(SConnection * pCon, SicsInterp * pSics, void *pData, /*-------- interest */ else if (strcmp(argv[1], "interest") == 0) { /* - printf("Registering scan callbacks on handle %d\n", pCon->sockHandle); + printf("Registering scan callbacks on handle %d\n", SCGetSockHandle(pCon)); */ lID = RegisterCallback(self->pCall, SCANSTART, ScanInterest, SCCopyConnection(pCon), SCDeleteConnection); @@ -1826,7 +1826,7 @@ int ScanWrapper(SConnection * pCon, SicsInterp * pSics, void *pData, /*-------- interest */ else if (strcmp(argv[1], "dyninterest") == 0) { /* - printf("Registering scanDyn callbacks on handle %d\n", pCon->sockHandle); + printf("Registering scanDyn callbacks on handle %d\n", SCGetSockHandle(pCon)); */ lID = RegisterCallback(self->pCall, SCANSTART, ScanDynInterest, SCCopyConnection(pCon), SCDeleteConnection); @@ -1841,7 +1841,7 @@ int ScanWrapper(SConnection * pCon, SicsInterp * pSics, void *pData, else if (strcmp(argv[1], "uuinterest") == 0) { /* printf("Registering scanUU callbacks on handle %d, con = %p\n", - conSave->sockHandle, conSave); + SCGetSockHandle(conSave), conSave); */ lID = RegisterCallback(self->pCall, SCANSTART, ScanUUInterest, SCCopyConnection(pCon), SCDeleteConnection); diff --git a/selector.c b/selector.c index ff74ef35..171d0e1b 100644 --- a/selector.c +++ b/selector.c @@ -614,13 +614,13 @@ int MonoRun(pSicsSelector self, SConnection * pCon, float fWaveLength) /* start each motor in turn */ iRet = StartDevice(GetExecutor(), self->pTheta->name, self->pTheta->pDescriptor, - self->pTheta, pCon, pCon->runLevel, sNeu.fTheta); + self->pTheta, pCon, SCGetRunLevel(pCon), sNeu.fTheta); if (!iRet) { return 0; } iRet = StartDevice(GetExecutor(), self->pTwoTheta->name, self->pTwoTheta->pDescriptor, - self->pTwoTheta, pCon, pCon->runLevel, sNeu.fTwoTheta); + self->pTwoTheta, pCon, SCGetRunLevel(pCon), sNeu.fTwoTheta); if (!iRet) { return 0; } @@ -630,7 +630,7 @@ int MonoRun(pSicsSelector self, SConnection * pCon, float fWaveLength) if (self->pBend1) { iRet = StartDevice(GetExecutor(), self->pBend1->name, self->pBend1->pDescriptor, - self->pBend1, pCon, pCon->runLevel, sNeu.fVert); + self->pBend1, pCon, SCGetRunLevel(pCon), sNeu.fVert); if (!iRet) { return 0; } @@ -639,7 +639,7 @@ int MonoRun(pSicsSelector self, SConnection * pCon, float fWaveLength) if (self->pBend2) { iRet = StartDevice(GetExecutor(), self->pBend2->name, self->pBend2->pDescriptor, - self->pBend2, pCon, pCon->runLevel, sNeu.fHor); + self->pBend2, pCon, SCGetRunLevel(pCon), sNeu.fHor); if (!iRet) { return 0; } diff --git a/sicshipadaba.c b/sicshipadaba.c index fa3e1f8c..19809423 100644 --- a/sicshipadaba.c +++ b/sicshipadaba.c @@ -316,7 +316,7 @@ static hdbCallbackReturn SICSDriveCallback(pHdb node, void *userData, userData, pCon, RUNDRIVE, (float) v.v.doubleValue); } else { status = StartDevice(pServ->pExecutor, node->name, dum->pDescriptor, - userData, pCon, pCon->runLevel, (float) v.v.doubleValue); + userData, pCon, SCGetRunLevel(pCon), (float) v.v.doubleValue); } if (status == 1) { return hdbContinue; @@ -617,7 +617,7 @@ static hdbCallbackReturn SICSNotifyCallback(pHdb node, void *userData, } if ((cmm = GetKillPtrMessage(message)) != NULL) { tstCon = cmm->pPtr; - if (tstCon != NULL && tstCon->ident == cbInfo->pCon->ident) { + if (tstCon != NULL && SCGetIdent(tstCon) == SCGetIdent(cbInfo->pCon)) { return hdbKill; } else { return hdbContinue; @@ -769,7 +769,7 @@ static hdbCallbackReturn TreeChangeCallback(pHdb node, void *userData, } if ((cmm = GetKillPtrMessage(message)) != NULL) { tstCon = cmm->pPtr; - if (tstCon != NULL && tstCon->ident == cbInfo->pCon->ident) { + if (tstCon != NULL && SCGetIdent(tstCon) == SCGetIdent(cbInfo->pCon)) { return hdbKill; } else { return hdbContinue; @@ -2769,7 +2769,7 @@ static int BinReadHdbNode(SConnection *pCon, SicsInterp *pSics, return 0; } if(data != NULL){ - ANETwrite(pCon->sockHandle,data,(val.arrayLength+2)*sizeof(int)); + ANETwrite(SCGetSockHandle(pCon),data,(val.arrayLength+2)*sizeof(int)); free(data); } diff --git a/statemon.c b/statemon.c index 06171088..d5b83d44 100644 --- a/statemon.c +++ b/statemon.c @@ -305,7 +305,7 @@ int StateMonFactory(SConnection * pCon, SicsInterp * pSics, void *pData, SConnection *pKon = (SConnection*) pUserData; SConnection *pCon = (SConnection*) context; if (VerifyConnection(pCon) && VerifyConnection(pKon)) { - if (pCon->ident == pKon->ident) + if (SCGetIdent(pCon) == SCGetIdent(pKon)) return 0; } return 1; diff --git a/status.c b/status.c index 6b55e785..a6dd3481 100644 --- a/status.c +++ b/status.c @@ -118,14 +118,6 @@ void KillStatus(void *pData) /*--------------------------------------------------------------------------*/ void SetStatus(Status eNew) { -/* if (!fixed) { - if (eCode == eNew) { - return; - } - eCode = eNew; - InvokeCallBack(pCall, VALUECHANGE, NULL); - } -*/ /* This now only manages the userWait status */ @@ -141,12 +133,7 @@ void SetStatus(Status eNew) /*----------------------------------------------------------------------*/ void SetStatusFixed(Status eNew) { - // if (eCode == eNew) { - // return; - // } - // eCode = eNew; - // InvokeCallBack(pCall, VALUECHANGE, NULL); - // fixed = 1; + /* pass */ } /*----------------------------------------------------------------------*/ @@ -309,57 +296,6 @@ int ResetStatus(SConnection * pCon, SicsInterp * pSics, void *pData, return 1; } -/* ===================== Control Connection Management ====================*/ -static SConnection *pOwner = NULL; - -void SetControl(SConnection * pCon) -{ - pOwner = pCon; -} - -/*--------------------------------------------------------------------------*/ -int IsControl(SConnection * pCon) -{ - if (pCon == pOwner) { - return 1; - } else { - return 0; - } -} - -/*--------------------------------------------------------------------------*/ -SConnection *GetControl(void) -{ - return pOwner; -} - -/*--------------------------------------------------------------------------*/ -int RedirectControl(SConnection * pCon, SicsInterp * pSics, void *pData, - int argc, char *argv[]) -{ - assert(pCon); - assert(pSics); - - /* check user Rights */ - if (!SCMatchRights(pCon, usUser)) { - SCWrite(pCon, - "You have NO, I repeat NO, Privilege to grab a control connection", - eError); - return 0; - } - - /* check if the connection is dead at all */ - if (pCon->sockHandle < 0) { - SCWrite(pCon, - "GOTCHA!!! Control still lives! You CANNOT grab it! FUCK OFF", - eError); - return 0; - } - - /* now the wizardry */ - pOwner->sockHandle = pCon->sockHandle; - return 1; -} /*---------------------------------------------------------------------------- Message pipe based new status calculation code ------------------------------------------------------------------------------*/ diff --git a/statusfile.c b/statusfile.c index d1593d13..03ffd987 100644 --- a/statusfile.c +++ b/statusfile.c @@ -370,13 +370,13 @@ int RestoreStatus(SConnection * pCon, SicsInterp * pSics, void *pData, LLDdeleteBlob(self->errList); self->errList = LLDstringCreate(); iRights = SCGetRights(pCon); - pCon->iUserRights = usInternal; + SCSetRights(pCon,usInternal); oldWrite = SCGetWriteFunc(pCon); SCSetWriteFunc(pCon, SCNotWrite); iRet = exeBufProcessErrList(buffi, pSics, pCon, self->errList); restoreOccurred = 1; SCSetWriteFunc(pCon, oldWrite); - pCon->iUserRights = iRights; + SCSetRights(pCon,iRights); exeBufDelete(buffi); /* if we do not override parameterChange here, the backup file diff --git a/telnet.c b/telnet.c index d3706b28..9deaf9c0 100644 --- a/telnet.c +++ b/telnet.c @@ -13,6 +13,8 @@ #include "passwd.h" #include "telnet.h" #include "fortify.h" +#include "asynnet.h" +#include "conman.h" #define LOGINWAIT 300 /* 300 == 5 minutes to wait for valid username password */ /*-------------------------------------------------------------------------*/ @@ -96,114 +98,7 @@ static void SendGA(SConnection * pCon) pReply[0] = (char) 255; pReply[1] = (char) 249; - NETWrite(pCon->pSock, pReply, 2); -} - -/*--------------------------------------------------------------------------*/ -int TelnetTaskOld(void *pData) -{ - pTelTask self = NULL; - char *pPtr = NULL; - char *pLogin = NULL; - char *pUser = NULL, *pPasswd = NULL; - char pBuffer[512], pHost[131]; - int iRet; - time_t shit; - - self = (pTelTask) pData; - assert(self); - - if (self->pCon->iEnd) { - if (SCActive(self->pCon)) { - return 1; - } else { - return 0; - } - } - - /* pop and execute */ - iRet = CostaPop(self->pCon->pStack, &pPtr); - if (iRet) { - if (pPtr) { - if (self->iLogin) { /* handle normal command */ - /* check for logoff */ - if (strstr(pPtr, "logoff") != NULL) { - NetReadRemove(pServ->pReader, self->pCon->pSock); - free(pPtr); - self->pCon->iEnd = 1; - return 0; - } - /* invoke command */ - CostaLock(self->pCon->pStack); - SCInvoke(self->pCon, pServ->pSics, pPtr); - CostaUnlock(self->pCon->pStack); - SendGA(self->pCon); - free(pPtr); - } else { /* handle login messages */ - - pLogin = strstr(pPtr, self->pLoginWord); - if (!pLogin) { - SCWrite(self->pCon, - "------------------- Get Lost -------------------", - eError); - if (time(&shit) > self->tStart + LOGINWAIT) { - SCWrite(self->pCon, - "I cannot stand your login attempts anymore!", eError); - NetReadRemove(pServ->pReader, self->pCon->pSock); - self->pCon->iEnd = 1; - free(pPtr); - return 0; - } - free(pPtr); - return 1; - } else { /* check username / password */ - - pLogin += strlen(self->pLoginWord); - pUser = strtok(pLogin, " \t"); - pPasswd = strtok(NULL, " \t\r\n"); - iRet = IsValidUser(pUser, pPasswd); - if (iRet < 0) { - snprintf(pBuffer,sizeof(pBuffer)-1, "SYSTEM ATTACK by %s / %s", pUser, pPasswd); - Log(FATAL,"sys","%s",pBuffer); - SCWrite(self->pCon, - "I do not know you, I do not let you in", eError); - SendGA(self->pCon); - free(pPtr); - return 1; - } else { - NETInfo(self->pCon->pSock, pHost, 131); - snprintf(pBuffer,sizeof(pBuffer)-1, "Accepted connection on socket %d from %s", - self->pCon->pSock->sockid, pHost); - Log(INFO,"com",pBuffer); - SendWelcome(self->pCon); - SCSetRights(self->pCon, iRet); - self->iLogin = 1; - SendGA(self->pCon); - free(pPtr); - return 1; - } - } - } - } - } - /* check for no commands but timeout on telnet */ - if (!self->iLogin && (time(&shit) > self->tStart + LOGINWAIT)) { - self->pCon->iEnd = 1; - NetReadRemove(pServ->pReader, self->pCon->pSock); - return 0; - } - - - /* check for end */ - if (self->pCon->iEnd) { - if (SCActive(self->pCon)) { - return 1; - } else { - return 0; - } - } - - return 1; + ANETwrite(SCGetSockHandle(pCon), pReply, 2); } /*--------------------------------------------------------------------------*/ @@ -220,7 +115,7 @@ int TelnetTask(void *pData) self = (pTelTask) pData; assert(self); - if (self->pCon->iEnd) { + if (SCGetEnd(self->pCon)) { if (SCActive(self->pCon)) { return 1; } else { @@ -230,22 +125,20 @@ int TelnetTask(void *pData) } /* pop and execute */ - iRet = CostaPop(self->pCon->pStack, &pPtr); + iRet = SCCostaTop(self->pCon,pPtr); if (iRet) { if (pPtr) { if (self->iLogin) { /* handle normal command */ /* check for logoff */ if (strstr(pPtr, "logoff") != NULL) { - Log(INFO,"sys","Handle %d logging off", self->pCon->sockHandle); - ANETclose(self->pCon->sockHandle); + SCClose(self->pCon); free(pPtr); - self->pCon->iEnd = 1; return 0; } /* invoke command */ - CostaLock(self->pCon->pStack); + SCCostaLock(self->pCon); SCInvoke(self->pCon, pServ->pSics, pPtr); - CostaUnlock(self->pCon->pStack); + SCCostaUnLock(self->pCon); SendGA(self->pCon); free(pPtr); } else { /* handle login messages */ @@ -258,8 +151,7 @@ int TelnetTask(void *pData) if (time(&shit) > self->tStart + LOGINWAIT) { SCWrite(self->pCon, "I cannot stand your login attempts anymore!", eError); - ANETclose(self->pCon->sockHandle); - self->pCon->iEnd = 1; + SCClose(self->pCon); free(pPtr); return 0; } @@ -281,8 +173,13 @@ int TelnetTask(void *pData) return 1; } else { snprintf(pBuffer,sizeof(pBuffer)-1, "Accepted telnet connection on handle %d", +<<<<<<< HEAD self->pCon->sockHandle); Log(INFO,"sys","%s",pBuffer); +======= + SCGetSockHandle(self->pCon)); + Log(INFO,"com","%s",pBuffer); +>>>>>>> conclean SendWelcome(self->pCon); SCSetRights(self->pCon, iRet); self->iLogin = 1; @@ -296,14 +193,14 @@ int TelnetTask(void *pData) } /* check for no commands but timeout on telnet */ if (!self->iLogin && (time(&shit) > self->tStart + LOGINWAIT)) { - self->pCon->iEnd = 1; - ANETclose(self->pCon->sockHandle); + SCSetEnd(self->pCon,1); + SCClose(self->pCon); return 0; } /* check for end */ - if (self->pCon->iEnd) { + if (SCGetEnd(self->pCon)) { if (SCActive(self->pCon)) { return 1; } else { @@ -328,7 +225,7 @@ void TelnetSignal(void *pData, int iSignal, void *pSigData) iInt = (int *) pSigData; SCSetInterrupt(self->pCon, *iInt); if (*iInt == eEndServer) { - self->pCon->iEnd = 1; + SCSetEnd(self->pCon,1); } } else if (iSignal == SICSBROADCAST) { pPtr = (char *) pSigData; @@ -336,9 +233,9 @@ void TelnetSignal(void *pData, int iSignal, void *pSigData) SCWrite(self->pCon, pPtr, eWarning); } } else if (iSignal == TOKENRELEASE) { - self->pCon->iGrab = 0; + SCSetGrab(self->pCon,0); } else if (iSignal == TOKENGRAB) { - self->pCon->iGrab = 1; + SCSetGrab(self->pCon,1); } } @@ -359,18 +256,6 @@ void InstallTelnet(void) return; } i = sscanf(pPtr, "%d", &iPort); - /* - if(i > 0) - { - pTelnet = NETOpenPort(iPort); - } - - if(pTelnet) - { - NetReadRegister(pServ->pReader,pTelnet, taccept, NULL); - } - */ - /* when we have a port have the NetReader listen and handle it */ NetReadInstallANETPort(pServ->pReader, taccept, iPort); } @@ -380,7 +265,6 @@ void KillTelnet(void) { if (pTelnet) { NETClosePort(pTelnet); -/* NetReadRemove(pServ->pReader,pTelnet); */ free(pTelnet); pTelnet = NULL; } diff --git a/token.c b/token.c index 2305efb4..942044d0 100644 --- a/token.c +++ b/token.c @@ -94,11 +94,11 @@ int TokenWrapper(SConnection * pCon, SicsInterp * pSics, void *pData, /* we can do it */ iToken = 1; TaskSignal(pServ->pTasker, TOKENGRAB, NULL); - pCon->iGrab = 0; /* to enable us to do commands */ + SCSetGrab(pCon,0); /* to enable us to do commands */ SCSendOK(pCon); return 1; } else if (strcmp(argv[1], "release") == 0) { - if (pCon->iGrab != 0) { + if (SCGetGrab(pCon) != 0) { SCWrite(pCon, "ERROR: you cannot release somebody elses control token!", eError);