void free_handler(struct handler *hp) { struct rline *li, *pi = NULL; for(li=rlines;li;) { if(li->handler == hp) { if(li->socket) { esocket_write_line(li->socket, "%d,OE%d,%s", li->id, BF_UNLOADED, "Service was unloaded."); } else if(li->callback) { nterfacer_sendcallback(li, BF_UNLOADED, "Service was unloaded."); } if(pi) { pi->next = li->next; ntfree(li); li = pi->next; } else { rlines = li->next; ntfree(li); li = rlines; } } else { pi=li,li=li->next; } } freesstring(hp->command); ntfree(hp); }
void _fini(void) { struct service_node *tp, *lp; int i; if(ping) deregister_service(ping); for(tp=tree;tp;) { lp = tp; tp = tp->next; free_handlers(lp); ntfree(lp); } tree = NULL; if((accept_fd != -1) && nterfacer_sock) { esocket_clean_by_token(nterfacer_token); nterfacer_sock = NULL; accept_fd = -1; } if(permits && permit_count) { for(i=0;i<permit_count;i++) { freesstring(permits[i].hostname); freesstring(permits[i].password); } ntfree(permits); permit_count = 0; permits = NULL; } nrl = nterface_close_log(nrl); nscheckfreeall(POOL_NTERFACER); }
int delEventLoop(EventLoop * eventLoop_p){ apiFree(eventLoop_p); ntfree(eventLoop_p->events); ntfree(eventLoop_p->waiting_events); ntfree(eventLoop_p); return EVENT_OK; }
EventLoop * ntCreateEventLoop(int volumn_size){ EventLoop * eventLoop_p; if (volumn_size < 1) { ntLogging(LOG_FATAL,"create EventLoop volumn is invalid"); return NULL; } if ((eventLoop_p = (EventLoop *)ntmalloc(sizeof(EventLoop))) == NULL) { ntLogging(LOG_FATAL,"malloc EventLoop failed" ); goto error; } if ((eventLoop_p->events = (IoEvent *)ntmalloc(sizeof(IoEvent) * volumn_size)) == NULL) { ntLogging(LOG_FATAL,"malloc EventLoop->events failed" ); goto error; } if ((eventLoop_p->waiting_events = (WaitingEvent *)ntmalloc(sizeof(WaitingEvent ) * volumn_size)) == NULL) { ntLogging(LOG_FATAL,"malloc EventLoop->waiting_events failed" ); goto error; } eventLoop_p->maxfd = -1; eventLoop_p->volumn_size = volumn_size; eventLoop_p->timeEventNextId = 0; eventLoop_p->lastTime = time(NULL); eventLoop_p->timeEventHead = NULL; eventLoop_p->is_stop = 0; eventLoop_p->beforeLoop = NULL; if (apiCreate(eventLoop_p) == -1){ ntLogging(LOG_FATAL,"canot create api loop " ); goto error; } int i; for(i=0; i < volumn_size; i++) eventLoop_p->events[i].mask = IO_NONE; return eventLoop_p; error: if (eventLoop_p){ ntfree(eventLoop_p->events); ntfree(eventLoop_p->waiting_events); ntfree(eventLoop_p); } return NULL; }
int ri_final(struct rline *li) { struct rline *pp, *lp = NULL; int retval = RE_OK; if(li->socket) { if(esocket_write_line(li->socket, "%d,OO%s", li->id, li->buf)) retval = RE_SOCKET_ERROR; } else if(li->callback) { nterfacer_sendcallback(li, 0, li->buf); } for(pp=rlines;pp;lp=pp,pp=pp->next) { if(pp == li) { if(lp) { lp->next = li->next; } else { rlines = li->next; } ntfree(li); break; } } return retval; }
void nterfacer_accept_event(struct esocket *socket) { struct sockaddr_in sin; unsigned int addrsize = sizeof(sin); int newfd = accept(socket->fd, (struct sockaddr *)&sin, &addrsize), i; struct sconnect *temp; struct permitted *item = NULL; struct esocket *newsocket; unsigned int opt = 1; if(newfd == -1) { nterface_log(nrl, NL_WARNING, "Unable to accept nterfacer fd!"); return; } if(ioctl(newfd, FIONBIO, &opt)) { nterface_log(nrl, NL_ERROR, "Unable to set accepted socket non-blocking."); close(newfd); return; } for(i=0;i<permit_count;i++) { if(permits[i].ihost == sin.sin_addr.s_addr) { item = &permits[i]; break; } } if(!item) { nterface_log(nrl, NL_INFO, "Unauthorised connection from %s closed", inet_ntoa(sin.sin_addr)); close(newfd); return; } temp = (struct sconnect *)ntmalloc(sizeof(struct sconnect)); if(!temp) { MemError(); close(newfd); return; } /* do checks on hostname first */ newsocket = esocket_add(newfd, ESOCKET_UNIX_DOMAIN_CONNECTED, &nterfacer_events, nterfacer_token); if(!newsocket) { ntfree(temp); close(newfd); return; } newsocket->tag = temp; nterface_log(nrl, NL_INFO, "New connection from %s.", item->hostname->content); temp->status = SS_IDLE; temp->permit = item; esocket_write_line(newsocket, "nterfacer " PROTOCOL_VERSION); }
void delClient(Client * client_p){ if (client_p == NULL){ ntLogging(LOG_WARNING,"client is empty" ); return ; } if (client_p->fd > 0){ close(client_p->fd); } if (client_p->recv_msg != NULL){ ntfree(client_p->recv_msg); client_p->recv_msg = NULL; } if (client_p->send_msg != NULL){ ntfree(client_p->send_msg); client_p->send_msg = NULL; } ntfree(client_p); client_p = NULL; }
void nterfacer_disconnect_event(struct esocket *sock) { struct sconnect *socket = sock->tag; struct rline *li; /* not tested */ nterface_log(nrl, NL_INFO, "Disconnected from %s.", socket->permit->hostname->content); /* not tested */ for(li=rlines;li;li=li->next) if(li->socket && (li->socket->tag == socket)) li->socket = NULL; ntfree(socket); }
struct service_node *register_service(char *name) { struct service_node *np = ntmalloc(sizeof(service_node)); MemCheckR(np, NULL); np->name = getsstring(name, strlen(name)); if(!np->name) { MemError(); ntfree(np); return NULL; } np->handlers = NULL; np->next = tree; tree = np; return np; }
struct handler *register_handler(struct service_node *service, char *command, int args, handler_function fp) { struct handler *hp = ntmalloc(sizeof(handler)); MemCheckR(hp, NULL); hp->command = getsstring(command, strlen(command)); if(!hp->command) { MemError(); ntfree(hp); return NULL; } hp->function = fp; hp->args = args; hp->next = service->handlers; hp->service = service; service->handlers = hp; return hp; }
int ri_error(struct rline *li, int error_code, char *format, ...) { char buf[MAX_BUFSIZE], escapedbuf[MAX_BUFSIZE * 2 + 1], *p, *tp; struct rline *pp, *lp = NULL; va_list ap; int retval = RE_OK; if(li->socket || li->callback) { va_start(ap, format); vsnprintf(buf, sizeof(buf), format, ap); va_end(ap); for(tp=escapedbuf,p=buf;*p||(*tp='\0');*tp++=*p++) if((*p == ',') || (*p == '\\')) *tp++ = '\\'; if(li->socket) { if(esocket_write_line(li->socket, "%d,OE%d,%s", li->id, error_code, escapedbuf)) retval = RE_SOCKET_ERROR; } else { if(error_code == 0) /* :P */ error_code = -10000; nterfacer_sendcallback(li, error_code, escapedbuf); } } for(pp=rlines;pp;lp=pp,pp=pp->next) { if(pp == li) { if(lp) { lp->next = li->next; } else { rlines = li->next; } ntfree(li); break; } } return retval; }
//TOCSY :o(n) int delTimeEvent(EventLoop * eventLoop_p, long long id){ TimeEvent * te, *prev; prev = NULL; te = eventLoop_p->timeEventHead; while(te){ if (te->id == id){ if (prev == NULL){ eventLoop_p->timeEventHead = te->next; }else{ prev->next = te->next; } if (te->timeFinalCallback) te->timeFinalCallback(eventLoop_p, te->clientData); ntfree(te); return EVENT_OK; }else{ prev = te; te = te->next; } } return EVENT_ERR; }
void deregister_service(struct service_node *service) { struct service_node *sp, *lp = NULL; for(sp=tree;sp;lp=sp,sp=sp->next) { if(sp == service) { if(lp) { lp->next = sp->next; } else { tree = sp->next; } break; } } if(!sp) /* already freed */ return; free_handlers(service); freesstring(service->name); ntfree(service); }
int nterfacer_new_rline(char *line, struct esocket *socket, int *number) { char *sp, *p, *parsebuf = NULL, *pp, commandbuf[MAX_BUFSIZE], *args[MAX_ARGS], *newp; int argcount; struct service_node *service; struct rline *prequest; struct handler *hl; int re; if(!line || !line[0] || (line[0] == ',')) return 0; for(sp=line;*sp;sp++) if(*sp == ',') break; if(!*sp || !*(sp + 1)) return RE_BAD_LINE; *sp = '\0'; for(service=tree;service;service=service->next) if(!strcmp(service->name->content, line)) break; for(p=sp+1;*p;p++) if(*p == ',') break; if(!*p || !*(p + 1)) return RE_BAD_LINE; *p = '\0'; *number = positive_atoi(sp + 1); if((*number < 1) || (*number > 0xffff)) return RE_BAD_LINE; if (!service) { nterface_log(nrl, NL_DEBUG, "Unable to find service: %s", line); return RE_SERVICER_NOT_FOUND; } newp = commandbuf; for(pp=p+1;*pp;pp++) { if((*pp == '\\') && *(pp + 1)) { if(*(pp + 1) == ',') { *newp++ = ','; } else if(*(pp + 1) == '\\') { *newp++ = '\\'; } pp++; } else if(*pp == ',') { break; } else { *newp++ = *pp; } } if(*pp == '\0') { /* if we're at the end already, we have no arguments */ argcount = 0; } else { argcount = 1; /* we have a comma, so we have one already */ } *newp = '\0'; for(hl=service->handlers;hl;hl=hl->next) if(!strncmp(hl->command->content, commandbuf, sizeof(commandbuf))) break; if(!hl) return RE_COMMAND_NOT_FOUND; if(argcount) { parsebuf = (char *)ntmalloc(strlen(pp) + 1); MemCheckR(parsebuf, RE_MEM_ERROR); for(newp=args[0]=parsebuf,pp++;*pp;pp++) { if((*pp == '\\') && *(pp + 1)) { if(*(pp + 1) == ',') { *newp++ = ','; } else if(*(pp + 1) == '\\') { *newp++ = '\\'; } pp++; } else if(*pp == ',') { *newp++ = '\0'; args[argcount++] = newp; if(argcount > MAX_ARGS) { ntfree(parsebuf); return RE_TOO_MANY_ARGS; } } else { *newp++ = *pp; } } *newp = '\0'; } if(argcount < hl->args) { if(argcount && parsebuf) ntfree(parsebuf); return RE_WRONG_ARG_COUNT; } prequest = (struct rline *)ntmalloc(sizeof(struct rline)); if(!prequest) { MemError(); if(argcount && parsebuf) ntfree(parsebuf); return RE_MEM_ERROR; } prequest->service = service; prequest->handler = hl; prequest->buf[0] = '\0'; prequest->curpos = prequest->buf; prequest->tag = NULL; prequest->id = *number; prequest->next = rlines; prequest->socket = socket; prequest->callback = NULL; rlines = prequest; re = (hl->function)(prequest, argcount, args); if(argcount && parsebuf) ntfree(parsebuf); return re; }
int load_permits(void) { int loaded_lines = 0, i, j; struct permitted *new_permits, *resized, *item; struct hostent *host; array *hostnamesa, *passwordsa; sstring **hostnames, **passwords; hostnamesa = getconfigitems("nterfacer", "hostname"); passwordsa = getconfigitems("nterfacer", "password"); if(!hostnamesa || !passwordsa) return 0; if(hostnamesa->cursi != passwordsa->cursi) { nterface_log(nrl, NL_ERROR, "Different number of hostnames/passwords in config file."); return 0; } hostnames = (sstring **)hostnamesa->content; passwords = (sstring **)passwordsa->content; new_permits = ntmalloc(hostnamesa->cursi * sizeof(struct permitted)); memset(new_permits, 0, hostnamesa->cursi * sizeof(struct permitted)); item = new_permits; for(i=0;i<hostnamesa->cursi;i++) { item->hostname = getsstring(hostnames[i]->content, hostnames[i]->length); host = gethostbyname(item->hostname->content); if (!host) { nterface_log(nrl, NL_WARNING, "Couldn't resolve hostname: %s (item %d).", item->hostname->content, i + 1); freesstring(item->hostname); continue; } item->ihost = (*(struct in_addr *)host->h_addr_list[0]).s_addr; for(j=0;j<loaded_lines;j++) { if(new_permits[j].ihost == item->ihost) { nterface_log(nrl, NL_WARNING, "Host with items %d and %d is identical, dropping item %d.", j + 1, i + 1, i + 1); host = NULL; } } if(!host) { freesstring(item->hostname); continue; } item->password = getsstring(passwords[i]->content, passwords[i]->length); nterface_log(nrl, NL_DEBUG, "Loaded permit, hostname: %s.", item->hostname->content); item++; loaded_lines++; } if(!loaded_lines) { ntfree(new_permits); return 0; } resized = ntrealloc(new_permits, sizeof(struct permitted) * loaded_lines); if(!resized) { MemError(); ntfree(new_permits); return 0; } permits = resized; permit_count = loaded_lines; return permit_count; }