int servantCommand(nbCELL context,void *skillHandle,nbServant *servant,nbCELL arglist,char *text){ char msgbuf[NB_BUFSIZE]; int len; if(servant->process==NULL){ nbLogMsg(context,0,'E',"Servant not enabled"); return(1); } if(strlen(text)>=sizeof(msgbuf)){ nbLogMsg(context,0,'E',"Command too long for buffer"); return(1); } sprintf(msgbuf,"%s\n",text); // for now assume the process is running and accepts commands if(nbMedullaProcessStatus(servant->process)&NB_MEDULLA_PROCESS_STATUS_ENDED){ nbLogMsg(context,0,'E',"Servant ended - restart not yet supported"); return(1); } else if(!(nbMedullaProcessStatus(servant->process)&NB_MEDULLA_PROCESS_STATUS_STARTED)){ nbLogMsg(context,0,'E',"Servant not started - auto start not yet supported"); return(1); } else len=nbMedullaProcessPut(servant->process,msgbuf); return(0); }
static void treeStore(nbCELL context,BTreeSkill *skillHandle,BTree *tree,char *text){ char buffer[NB_BUFSIZE],*cursor=text,filename[512]; FILE *file=NULL; int qualifiers=0; nbCELL element[32]; int learning=1; int len; if(text){ learning=0; while(*cursor!=0 && strchr(" ;",*cursor)==NULL) cursor++; len=cursor-text; if(len>sizeof(filename)-1){ nbLogMsg(context,0,'E',"File name too large for buffer."); return; } strncpy(filename,text,len); *(filename+len)=0; } else sprintf(filename,"%s/%8.8d.nb",tree->directory,tree->period*tree->interval); // write to file if(!(tree->options&BTREE_OPTION_STATIC) && (file=fopen(filename,"w"))==NULL){ nbLogMsg(context,0,'E',"Unable to open %s",filename); return; } if(tree->root){ if(learning) strcpy(buffer,".("); else strcpy(buffer,"assert ("); cursor=buffer+strlen(buffer); treeStoreNode(context,skillHandle,tree,learning,tree->root,element,qualifiers,file,buffer,cursor,buffer+sizeof(buffer)); } if(file) fclose(file); }
/* * Create a proxy structure for listening or connecting. * * Buffers are not allocated until a connection is established. */ nbProxy *nbProxyConstruct(nbCELL context,int client,nbCELL tlsContext,void *handle, int (*producer)(nbCELL context,nbProxy *proxy,void *handle), int (*consumer)(nbCELL context,nbProxy *proxy,void *handle), void (*shutdown)(nbCELL context,nbProxy *proxy,void *handle,int code)){ nbProxy *proxy; nbTLSX *tlsx=NULL; char *uri; if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyConstruct: called"); // allocate a proxy structure proxy=(nbProxy *)nbAlloc(sizeof(nbProxy)); memset(proxy,0,sizeof(nbProxy)); proxy->handle=handle; proxy->producer=producer; proxy->consumer=consumer; proxy->shutdown=shutdown; if(!client){ // for a server go ahead and uri=nbTermOptionString(tlsContext,"uri",""); if(!*uri){ nbLogMsg(context,0,'E',"nbProxyConstruct: uri not defined in %s",nbTermGetName(context,tlsContext)); nbFree(proxy,sizeof(nbProxy)); return(NULL); } nbLogMsg(context,0,'T',"nbProxyConstruct: uri=%s",uri); if(strncmp(uri,"tls:",4)==0 || strncmp(uri,"https:",6)==0) tlsx=nbTlsLoadContext(context,tlsContext,proxy,client); proxy->tls=nbTlsCreate(tlsx,uri); if(!proxy->tls){ nbLogMsg(context,0,'E',"nbProxyConstruct: unable to create tls for uri=%s",uri); nbFree(proxy,sizeof(nbProxy)); return(NULL); } } return(proxy); }
/* * Call producer after TLS handshake is complete */ static void nbProxyConnecter(nbCELL context,int sd,void *handle){ nbProxy *proxy=(nbProxy *)handle; if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyConnecter: Proxy %d flags=%x",sd,proxy->flags); if(proxy->other && !proxy->producer) nbProxyProducer(context,proxy,handle,nbProxyForwardProducer); if(proxy->producer){ nbListenerAddWrite(context,sd,proxy,nbProxyWriter); proxy->flags|=NB_PROXY_FLAG_WRITE_WAIT; } else if(proxy->flags&NB_PROXY_FLAG_WRITE_WAIT){ nbLogMsg(context,0,'T',"nbProxyConnecter: Proxy %d flags=%x has no producer",sd,proxy->flags); nbListenerRemoveWrite(context,sd); proxy->flags&=0xff-NB_PROXY_FLAG_WRITE_WAIT; } if(proxy->consumer){ nbListenerAdd(context,sd,proxy,nbProxyReader); proxy->flags|=NB_PROXY_FLAG_READ_WAIT; } else{ nbLogMsg(context,0,'T',"nbProxyConnecter: Proxy %d flags=%x has no consumer",sd,proxy->flags); nbListenerRemove(context,sd); proxy->flags&=0xff-NB_PROXY_FLAG_READ_WAIT; } nbLogMsg(context,0,'T',"nbProxyConnecter: Proxy %d flags=%x",sd,proxy->flags); }
/* * command() method * * <node>[(<args>)][:<text>] */ static int *baselineCommand(nbCELL context,BTreeSkill *skillHandle,BTree *tree,nbCELL arglist,char *text){ char *cursor=text,ident[512]; int len; if(skillHandle->trace || tree->options&BTREE_OPTION_TRACE){ nbLogMsg(context,0,'T',"nb_baseline:baselineCommand() text=[%s]\n",text); } len=treeGetIdent(&cursor,ident,sizeof(ident)); if(len<0){ nbLogMsg(context,0,'E',"Verb not recognized at \"%s\".",cursor); return(0); } while(*cursor==' ') cursor++; if(strcmp(ident,"trace")==0){ len=treeGetIdent(&cursor,ident,sizeof(ident)); if(len==0 || strcmp(ident,"on")) tree->options|=BTREE_OPTION_TRACE; else if(strcmp(ident,"off")) tree->options&=0xffffffff-BTREE_OPTION_TRACE; else nbLogMsg(context,0,'E',"Trace argument \"%s\" not recognized.",ident); } else if(strcmp(ident,"flatten")==0) treeFlatten(context,skillHandle,tree); else if(strcmp(ident,"balance")==0) treeBalance(context,skillHandle,tree); else if(strcmp(ident,"set")==0) treeSet(context,skillHandle,tree,arglist,cursor); else if(strcmp(ident,"store")==0) treeStore(context,skillHandle,tree,cursor); else if(strcmp(ident,"prune")==0) treePrune(context,skillHandle,tree,arglist,cursor); else nbLogMsg(context,0,'E',"Verb \"%s\" not recognized.",ident); return(0); }
/* * Read incoming packets */ static void serverRead(nbCELL context,int serverSocket,void *handle){ NB_MOD_Snmptrap *snmptrap=handle; unsigned char buffer[NB_BUFSIZE]; size_t buflen=NB_BUFSIZE; int len; unsigned short rport; char daddr[40],raddr[40]; char cmd[NB_BUFSIZE]; size_t cmdlen=NB_BUFSIZE; char *msg; char *handlerName=NULL; nbIpGetSocketAddrString(serverSocket,daddr); len=nbIpGetDatagram(context,serverSocket,&snmptrap->sourceAddr,&rport,buffer,buflen); if(snmptrap->trace) nbLogMsg(context,0,'I',"Datagram %s:%5.5u -> %s len=%d\n",nbIpGetAddrString(raddr,snmptrap->sourceAddr),rport,daddr,len); if(snmptrap->dump) nbLogDump(context,buffer,len); msg=translate(snmptrap,buffer,len,cmd,cmdlen,&handlerName); if(msg!=NULL){ nbLogMsg(context,0,'E',msg); return; } if(snmptrap->trace && !snmptrap->echo) nbLogMsg(context,0,'I',cmd); if(handlerName){ *(cmd+5)=':'; // convert to node command, stepping over "alert" verb nbNodeCmd(context,handlerName,cmd+5); } else nbCmd(context,cmd,snmptrap->echo); }
static void treeSet(nbCELL context,BTreeSkill *skillHandle,BTree *tree,nbCELL arglist,char *text){ NB_TreePath path; BTreeNode *node=NULL,**nodeP=&tree->root; nbSET argSet; nbCELL argCell; double average,deviation; average=strtod(text,&text); while(*text==' ') text++; if(*text!=','){ nbLogMsg(context,0,'E',"Expecting ',' at: %s",text); return; } text++; deviation=strtod(text,&text); while(*text==' ') text++; if(*text!=';'){ nbLogMsg(context,0,'E',"Expecting ';' at: %s",text); return; } if(arglist==NULL || (argSet=nbListOpen(context,arglist))==NULL){ nbLogMsg(context,0,'E',"Expecting argument list"); return; } argCell=nbListGetCellValue(context,&argSet); while(argCell!=NULL){ if(tree->options&BTREE_OPTION_ORDER) node=nbTreeLocateValue(&path,argCell,(NB_TreeNode **)nodeP,treeCompare,context); else node=nbTreeLocate(&path,argCell,(NB_TreeNode **)nodeP); if(node==NULL){ node=nbAlloc(sizeof(BTreeNode)); memset(node,0,sizeof(BTreeNode)); nbTreeInsert(&path,(NB_TreeNode *)node); nodeP=&(node->root); while((argCell=nbListGetCellValue(context,&argSet))!=NULL){ node=nbAlloc(sizeof(BTreeNode)); memset(node,0,sizeof(BTreeNode)); node->bnode.key=(void *)argCell; *nodeP=node; nodeP=&(node->root); } node->average=average; node->deviation=deviation; node->threshold=deviation*tree->tolerance; node->level=0; return; } nodeP=&(node->root); nbCellDrop(context,argCell); argCell=nbListGetCellValue(context,&argSet); } if(node){ /* matched - change value */ node->average=average; node->deviation=deviation; node->threshold=(double)((int)1<<node->level)*deviation*tree->tolerance; } return; }
static void nbProxyForwardShutdown(nbCELL context,nbProxy *proxy,void *handle,int code){ nbLogMsg(context,0,'T',"nbProxyForwardShutdown: called"); if(proxy->other){ if(proxy->other->other) proxy->other->other=NULL; nbProxyShutdown(context,proxy->other,code); } nbLogMsg(context,0,'T',"nbProxyForwardShutdown: returning"); }
int nbProxyDestroy(nbCELL context,nbProxy *proxy){ if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyDestroy: called"); if(proxy->tls) nbLogMsg(context,0,'T',"nbProxyDestroy: uri=%s",proxy->tls->uriMap[0].uri); if(proxy->tls) nbTlsFree(proxy->tls); nbProxyBookClose(context,&proxy->ibook); nbProxyBookClose(context,&proxy->obook); nbFree(proxy,sizeof(nbProxy)); return(0); }
int nbProxyBookReadWhere(nbCELL context,nbProxyBook *book,void **data){ nbProxyPage *page=book->readPage; if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyBookReadWhere: called"); if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyBookReadWhere: page=%p",page); if(!page) return(0); if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyBookReadWhere: have page"); *data=((char *)page->data)+book->readOffset; if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyBookReadWhere: dataLen=%d, readOffset=%d",page->dataLen,book->readOffset); return(page->dataLen-book->readOffset); }
int servantEnable(nbCELL context,void *skillHandle,nbServant *servant,nbCELL arglist,nbCELL value){ char msg[NB_MSGSIZE]; nbLogMsg(context,0,'I',"Enabling %s",servant->cmd); servant->process=nbMedullaProcessOpen(NB_CHILD_TERM|NB_CHILD_SESSION,servant->cmd,servant->log,servant,NULL,cmdMsgWriter,cmdMsgReader,logMsgReader,msg,sizeof(msg)); if(servant->process==NULL){ nbLogMsg(context,0,'E',"%s",msg); return(1); } nbLogMsg(context,0,'I',"Enabled [%d] %s",nbMedullaProcessPid(servant->process),servant->cmd); return(0); }
int nbProxyPageProduced(nbCELL context,nbProxyPage *page,int len){ int unwritten=page->size-page->dataLen; if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyPageProduced: called len=%d unwritten=%d",len,unwritten); if(len>unwritten){ nbLogMsg(context,0,'L',"nbProxyPageProduced: claim to have produced more than available space - terminating"); exit(1); } page->dataLen+=len; if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyPageProduced: returning dataLen=%d",page->dataLen); return(0); }
/* * disable method * * disable <node> */ static int auditDisable(nbCELL context,void *skillHandle,nbAudit *audit){ if(audit->trace) nbLogMsg(context,0,'T',"auditDisable() called"); if(audit->synapseCell) audit->synapseCell=nbSynapseClose(context,audit->synapseCell); // release the synapse else return(0); // already disabled if(audit->file){ if(fclose(audit->file)!=0) nbLogMsg(context,0,'L',"File close failed - errno=%d %s",errno,strerror(errno)); audit->file=NULL; } audit->pos=0; nbLogMsg(context,0,'I',"Disabled audit of %s using %s",audit->fileName,audit->translatorName); return(0); }
static int nbProxyForwardConsumer(nbCELL context,nbProxy *proxy,void *handle){ nbProxyPage *page; int rc; nbLogMsg(context,0,'T',"nbProxyForwardConsumer: called"); page=nbProxyGetPage(context,proxy); if(!page) return(0); nbLogMsg(context,0,'T',"nbProxyForwardConsumer: have page"); rc=nbProxyPutPage(context,proxy->other,page); nbLogMsg(context,0,'T',"nbProxyForwardConsumer: returning"); return(0); }
static void treeBalance(nbCELL context,BTreeSkill *skillHandle,BTree *tree){ BTreeNode *node; int n=0; if(tree->options&BTREE_OPTION_TRACE) nbLogMsg(context,0,'T',"treeBalance called"); if(tree->root!=NULL){ treeFlatten(context,skillHandle,tree); // make the tree a list for(node=tree->root;node!=NULL;node=(BTreeNode *)node->bnode.right) n++; // count the nodes if(n>2) tree->root=(BTreeNode *)nbTreeBalance((NB_TreeNode *)tree->root,n,(NB_TreeNode **)&node); // balance the tree } if(tree->options&BTREE_OPTION_TRACE) nbLogMsg(context,0,'T',"treeBalance returning"); }
/* * enable() method * * enable <node> */ static int serverEnable(nbCELL context,void *skillHandle,NB_MOD_Server *server){ int fd; if((fd=nbIpGetUdpServerSocket(context,server->interfaceAddr,server->port))<0){ // 2012-12-27 eat 0.8.13 - CID 761574 nbLogMsg(context,0,'E',"Unable to listen on port %s\n",server->port); return(1); } server->socket=fd; nbListenerAdd(context,server->socket,server,serverRead); if(strncmp(server->uri,"udp://",6)==0) nbLogMsg(context,0,'I',"Listening on %s for syslog",server->uri); else nbLogMsg(context,0,'I',"Listening on UDP port %u for syslog",server->port); //nbLogMsg(context,0,'I',"This is version 0.6.5"); return(0); }
void nbProxyConsumer(nbCELL context,nbProxy *proxy,void *handle, int (*consumer)(nbCELL context,nbProxy *proxy,void *handle)){ nbLogMsg(context,0,'T',"nbProxyConsumer: called"); proxy->handle=handle; proxy->consumer=consumer; proxy->flags&=0xff-NB_PROXY_FLAG_CONSUMER_STOP; if(consumer && !(proxy->flags&NB_PROXY_FLAG_READ_WAIT)){ nbLogMsg(context,0,'T',"nbProxyConsumer: setting read wait"); nbListenerAdd(context,proxy->tls->socket,proxy,nbProxyReader); proxy->flags|=NB_PROXY_FLAG_READ_WAIT; } nbLogMsg(context,0,'T',"nbProxyConsumer: returning"); }
/* * enable() method * * enable <node> */ static int serverEnable(nbCELL context,void *skillHandle,NB_MOD_Snmptrap *snmptrap){ int fd; snmptrap->handlerContext=nbTermLocateHere(context,"handler"); snmptrap->syntaxContext=nbTermLocateHere(context,"syntax"); snmptrap->attributeContext=nbTermLocateHere(context,"attribute"); if((fd=nbIpGetUdpServerSocket(context,snmptrap->interfaceAddr,snmptrap->port))<0){ // 2012-12-27 eat 0.8.13 - CID 751574 nbLogMsg(context,0,'E',"Unable to listen on port %d\n",snmptrap->port); return(1); } snmptrap->socket=fd; nbListenerAdd(context,snmptrap->socket,snmptrap,serverRead); nbLogMsg(context,0,'I',"Listening on port %u for SNMP Trap Datagrams",snmptrap->port); return(0); }
/* * Tell proxy we have written to the output book after calling nbProxyBookWriteWhere * Here we schedule writing to peer. */ int nbProxyProduced(nbCELL context,nbProxy *proxy,int len){ int rc; if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyProduced: called SD=%d len=%d",proxy->tls->socket,len); rc=nbProxyBookProduced(context,&proxy->obook,len); if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyProduced: nbProxyBookProduced rc=%d",rc); if(rc) return(rc); if(!(proxy->flags&NB_PROXY_FLAG_WRITE_WAIT)){ if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyProduced: nbListenerAddWrite SD=%d flags=%x",proxy->tls->socket,proxy->flags); nbListenerAddWrite(context,proxy->tls->socket,proxy,nbProxyWriter); proxy->flags|=NB_PROXY_FLAG_WRITE_WAIT; } return(0); }
/* * enable() method * * enable <node> * */ int baselineEnable(nbCELL context,void *skillHandle,BTree *tree){ int remaining; time_t utime; if(tree->options&BTREE_OPTION_TRACE) nbLogMsg(context,0,'T',"baselineEnable() called for baseline %s",tree->directory); tree->synapse=nbSynapseOpen(context,skillHandle,tree,NULL,treeAlarm); treeLoad(context,skillHandle,tree); // load baseline // set timer time(&utime); remaining=tree->interval-utime%tree->interval; nbSynapseSetTimer(context,tree->synapse,remaining); nbLogMsg(context,0,'I',"Enabled baseline %s",tree->directory); nbLogFlush(context); return(0); }
/* * Establish a connection with a server * * This is a prototype. Not totally keen on the need for this function * to build the nbTLSX and nbTLS structures. Would be better if we * could just clone an nbTLS structure build in advance. However, this * requires some thought to get the TLS/SSL context and handle right. * Will look at this more later. * * Returns: * * NULL on error * Pointer to proxy on success * */ nbProxy *nbProxyConnect(nbCELL context,nbTLSX *tlsx,char *uri,void *handle, int (*producer)(nbCELL context,nbProxy *proxy,void *handle), int (*consumer)(nbCELL context,nbProxy *proxy,void *handle), void (*shutdown)(nbCELL context,nbProxy *proxy,void *handle,int code)){ int rc; nbProxy *proxy; proxy=(nbProxy *)nbAlloc(sizeof(nbProxy)); memset(proxy,0,sizeof(nbProxy)); proxy->tls=nbTlsCreate(tlsx,uri); if(!proxy->tls){ nbLogMsg(context,0,'E',"nbProxyConnect: unable to create tls handle"); //nbTlsFreeContext(tlsx); nbFree(proxy,sizeof(nbProxy)); return(NULL); } nbLogMsg(context,0,'I',"Attempting proxy connection with %s",nbTlsGetUri(proxy->tls)); proxy->handle=handle; proxy->producer=producer; proxy->consumer=consumer; proxy->shutdown=shutdown; proxy->flags|=NB_PROXY_FLAG_CLIENT; // flag as client for failover retries rc=nbTlsConnectNonBlocking(proxy->tls); switch(rc){ case 0: nbLogMsg(context,0,'I',"Proxy connection established with %s",nbTlsGetUri(proxy->tls)); nbListenerAddWrite(context,proxy->tls->socket,proxy,nbProxyConnecter); proxy->flags|=NB_PROXY_FLAG_WRITE_WAIT; break; case 1: nbListenerAddWrite(context,proxy->tls->socket,proxy,nbProxyConnectHandshaker); proxy->flags|=NB_PROXY_FLAG_WRITE_WAIT; // fall thru to also wait for read to get notification on errors case 2: nbListenerAdd(context,proxy->tls->socket,proxy,nbProxyConnectHandshaker); proxy->flags|=NB_PROXY_FLAG_READ_WAIT; if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyConnect: waiting on %s",nbTlsGetUri(proxy->tls)); break; default: nbLogMsg(context,0,'W',"nbProxyConnect: Unable to connect to %s - %s",nbTlsGetUri(proxy->tls),strerror(errno)); while((rc=nbProxyConnectNext(context,proxy))==-1); if(rc==1){ nbProxyShutdown(context,proxy,-1); return(NULL); } } return(proxy); }
static void myAlert( nbCELL context, void *skillHandle, void *nodeHandle, nbCELL cell ) { char name[1024]; char *cursor=name; int size; nbLogMsg( context, 0, 'T', "myAlert was called"); size = nbCellGetName( context, cell, &cursor, sizeof(name) ); if( size > 0 ) nbLogPut( context, " Cell: %s\n ", name ); else nbLogMsg( context, 0, 'E', "myAlert: name too large for buffer" ); cursor=name; size = nbCellGetValueName( context, cell, &cursor, size ); if( size > 0 ) nbLogPut( context, "Value: %s\n", name ); else nbLogMsg( context, 0, 'E', "myAlert: value too large for buffer" ); }
extern void *baselineBind(nbCELL context,void *moduleHandle,nbCELL skill,nbCELL arglist,char *text){ BTreeSkill *skillHandle; char *cursor=text; skillHandle=(BTreeSkill *)nbAlloc(sizeof(BTreeSkill)); skillHandle->trace=0; while(*cursor==' ') cursor++; while(*cursor!=0 && *cursor!=';'){ if(strncmp(cursor,"trace",5)==0){ skillHandle->trace=1; cursor+=5; } else{ nbLogMsg(context,0,'T',"Option not recognized at \"%s\".",cursor); nbFree(skillHandle,sizeof(BTreeSkill)); return(NULL); } while(*cursor==' ' || *cursor==',') cursor++; } /* Still trying to figure out if we want to require method binding */ nbSkillSetMethod(context,skill,NB_NODE_CONSTRUCT,baselineConstruct); nbSkillSetMethod(context,skill,NB_NODE_ENABLE,baselineEnable); nbSkillSetMethod(context,skill,NB_NODE_ASSERT,baselineAssert); nbSkillSetMethod(context,skill,NB_NODE_EVALUATE,baselineEvaluate); nbSkillSetMethod(context,skill,NB_NODE_SHOW,baselineShow); nbSkillSetMethod(context,skill,NB_NODE_COMMAND,baselineCommand); return(skillHandle); }
/* * command() method * * <node>[(<args>)][:<text>] */ static int *serverCommand(nbCELL context,void *skillHandle,NB_MOD_Snmptrap *snmptrap,nbCELL arglist,char *text){ if(snmptrap->trace){ nbLogMsg(context,0,'T',"nb_snmptrap:serverCommand() text=[%s]\n",text); } /* insert command parsing code here */ return(0); }
/* * Get page from proxy input book * * Returns: pointer page or NULL if input book is empty */ nbProxyPage *nbProxyGetPage(nbCELL context,nbProxy *proxy){ nbProxyPage *page; if(proxyTrace){ if(proxy->tls) nbLogMsg(context,0,'I',"nbProxyGetPage: called for %s",nbTlsGetUri(proxy->tls)); } page=nbProxyBookGetPage(context,&proxy->ibook); proxy->flags&=0xff-NB_PROXY_FLAG_CONSUMER_STOP; if(!(proxy->flags&NB_PROXY_FLAG_READ_WAIT) && proxy->tls){ if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyGetPage: nbListenerAdd SD=%d flags=%x",proxy->tls->socket,proxy->flags); nbListenerAdd(context,proxy->tls->socket,proxy,nbProxyReader); proxy->flags|=NB_PROXY_FLAG_READ_WAIT; } // 2012-12-27 eat 0.8.13 - CID 751550 - included proxy->tls condition if(proxyTrace && page && proxy->tls) nbLogMsg(context,0,'T',"nbProxyGetPage: called with proxy=%p SD=%d size=%d flags=%x",proxy,proxy->tls->socket,page->size,proxy->flags); return(page); }
/* * Start listening as a proxy * * The proxy structure is cloned when a connection is accepted * and the producer is invoked. The producer may then call other * API functions to change the handle, producer, or consumer, and * may also call nbProxySend. The proxy structure created by nbProxyListen * does not have allocated buffers. * */ int nbProxyListen(nbCELL context,nbProxy *proxy){ //if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyListen: called uri=%s",proxy->tls->uriMap[0].uri); if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyListen: called uri=%s",nbTlsGetUri(proxy->tls)); if(nbTlsListen(proxy->tls)<0){ nbLogMsg(context,0,'E',"Unable to listen - %s",nbTlsGetUri(proxy->tls)); return(-1); } if(fcntl(proxy->tls->socket,F_SETFL,fcntl(proxy->tls->socket,F_GETFL)|O_NONBLOCK)){ // 2012-12-27 eat 0.8.13 - CID 751524 nbLogMsg(context,0,'E',"Unable to listen - %s - unable to set non-blocking - %s",nbTlsGetUri(proxy->tls),strerror(errno)); return(-1); } nbListenerAdd(context,proxy->tls->socket,proxy,nbProxyAccepter); proxy->flags|=NB_PROXY_FLAG_READ_WAIT; if(proxyTrace) nbLogMsg(context,0,'T',"nbProxyListen: returning - handing off to nbProxyAccepter"); return(0); }
/* * command() method * * <node>[(<args>)][:<text>] */ static int *serverCommand(nbCELL context,void *skillHandle,NB_MOD_Server *server,nbCELL arglist,char *text){ if(server->trace){ nbLogMsg(context,0,'T',"serverCommand: text=[%s]\n",text); } /* insert command parsing code here */ return(0); }
// Prune a tree at the selected node without removing the selected node // static void treePrune(nbCELL context,BTreeSkill *skillHandle,BTree *tree,nbCELL arglist,char *text){ BTreeNode *node=NULL; nbSET argSet; nbCELL argCell; void *ptr; argSet=nbListOpen(context,arglist); if(argSet==NULL){ if(tree->root!=NULL){ removeTree(context,tree,tree->root); tree->root=NULL; } } else{ ptr=&tree->root; if(argSet!=NULL){ while((argCell=nbListGetCellValue(context,&argSet))!=NULL && (node=treeFindArg(context,tree,argCell,&ptr))!=NULL){ nbCellDrop(context,argCell); ptr=&node->root; } if(argCell!=NULL){ nbLogMsg(context,0,'E',"Entry not found."); return; } if(node->root!=NULL){ removeTree(context,tree,node->root); node->root=NULL; } } } }
/* * command() method * * <node>[(<args>)][:<text>] */ static int *loggerCommand(nbCELL context,void *skillHandle,NB_MOD_Logger *logger,nbCELL arglist,char *text){ if(logger->trace){ nbLogMsg(context,0,'T',"loggerCommand: text=[%s]\n",text); } syslog(LOG_INFO,"%s",text); return(0); }
/* * construct() method * * define <term> node <skill>[("<ident>")][:<text>] * * <ident> - name of translator file * <text> - flag keywords * trace - display input packets * dump - display dump of syslog packets * silent - don't echo generated NodeBrain commands * * define logger node syslog.logger("foobar"); */ void *loggerConstruct(nbCELL context,void *skillHandle,nbCELL arglist,char *text){ NB_MOD_Logger *logger; nbCELL cell=NULL; nbSET argSet; char *cursor=text,*delim,saveDelim; int trace=0,dump=0,echo=1; char *ident="nodebrain"; argSet=nbListOpen(context,arglist); cell=nbListGetCellValue(context,&argSet); if(cell!=NULL){ if(nbCellGetType(context,cell)!=NB_TYPE_STRING){ nbLogMsg(context,0,'E',"First argument must be string message identifier"); return(NULL); } ident=strdup(nbCellGetString(context,cell)); if(!ident) nbExit("loggerConstruct: out of memory"); // 2013-01-17 eat - VID 6546 } cell=nbListGetCellValue(context,&argSet); if(cell!=NULL){ nbLogMsg(context,0,'E',"The syslog.logger skill only accepts one argument."); return(NULL); } while(*cursor==' ') cursor++; while(*cursor!=';' && *cursor!=0){ delim=strchr(cursor,' '); if(delim==NULL) delim=strchr(cursor,','); if(delim==NULL) delim=strchr(cursor,';'); if(delim==NULL) delim=cursor+strlen(cursor); saveDelim=*delim; *delim=0; if(strcmp(cursor,"trace")==0){trace=1;} else if(strcmp(cursor,"dump")==0){trace=1;dump=1;} else if(strcmp(cursor,"silent")==0) echo=0; *delim=saveDelim; cursor=delim; if(*cursor==',') cursor++; while(*cursor==' ' || *cursor==',') cursor++; } logger=nbAlloc(sizeof(NB_MOD_Logger)); logger->ident=ident; logger->trace=trace; logger->dump=dump; logger->echo=echo; openlog(logger->ident,LOG_PID,LOG_LOCAL0); return(logger); }