int CmdWorker(q_msg *m) { char *c = &(m->cmd[0]); char s[MAX_MSGLEN]; int rc; NETDEBUG(MEXECD, NETLOG_DEBUG2, ("To: %x From: %x Flag: %x Cmd: %s\n", m->self_id, m->peer_id, m->flag, c)); if (ISSET_BIT(m, OUT_BIT)) { rc = System(c, s, MAX_CMDLEN); } else { rc = System(c, NULL, 0); s[0]='\0'; } NETDEBUG(MEXECD, NETLOG_DEBUG2, ("Cmd returned %d\n", rc)); if (ISSET_BIT(m, REQ_BIT)) { strncpy(c, s, MAX_CMDLEN); SendResp(m, rc); } free(m); return(rc); }
static void HelloProtMainLoop() { int numus = 0; int rval = 0; exec_hsm_event(HSM_EVENT_START); for(;;) { numus = NetFdsSetupPoll(&hcp->nfs, MRSD, NETLOG_DEBUG4); NETDEBUG(MRSD, NETLOG_DEBUG4, ("%d ready for Hello Protocol\n", numus)); rval = poll(hcp->nfs.pollA, numus, -1); switch (rval) { case -1: NETDEBUG(MRSD, NETLOG_DEBUG4, ("poll failure - %d", errno)); break; case 0: NETDEBUG(MRSD, NETLOG_DEBUG4, ("poll timeout")); break; default: NETDEBUG(MRSD, NETLOG_DEBUG4, ("poll process")); rval = NetFdsProcessPoll(&hcp->nfs, MRSD, NETLOG_DEBUG4); if (rval < 0) { NETERROR(MRSD, ("found a bad fd %d, deactivating it!\n", -rval)); NetFdsDeactivate(&hcp->nfs, -rval, FD_RW); } break; } } }
static void err_doit(int errnoflag, int level, const char *fmt, va_list ap) { int errno_save, n; char buf[MAXLINE]; errno_save = errno; /* value caller might want printed */ #ifdef HAVE_VSNPRINTF vsnprintf(buf, sizeof(buf), fmt, ap); /* this is safe */ #else vsprintf(buf, fmt, ap); /* this is not safe */ #endif n = strlen(buf); if (errnoflag) snprintf(buf+n, sizeof(buf)-n, ": %s", strerror(errno_save)); strcat(buf, "\n"); if (daemon_proc) { // syslog(level, buf); if (level == LOG_ERR) { NETERROR(MRSD, ("%s", buf)); } if (level == LOG_INFO) { NETDEBUG(MRSD, NETLOG_DEBUG2, ("%s", buf)); } if (level == LOG_WARNING) { NETDEBUG(MRSD, NETLOG_DEBUG3, ("%s", buf)); } } else { fflush(stdout); /* in case stdout and stderr are the same */ fputs(buf, stderr); fflush(stderr); } return; }
/** * returns the next available port in the range, returns -1 if no ports are available * if reqport is not 0 then assigns the requested port */ int allocPort (PortAlloc *ptr, int reqport, dirxn d) { int port = -1; int count, index = 0; NETDEBUG(MFCE, NETLOG_DEBUG4, ("allocPort: request to allocate port %d for %s\n", reqport, dirxn2str(d))); if (!ptr->inited) { NETERROR(MFCE, ("allocPort: trying to allocate port before initialization\n")); return -1; } if (reqport) { index = (reqport - ptr->low)/2; } else { if ((index = nextIndex(ptr)) == -1) { NETERROR(MFCE, ("allocPort: cannot allocate port, no more ports available in the range %d - %d\n", ptr->low, ptr->high)); return -1; // no ports available in the port range } incrementIndex(ptr); /* get ready for the next request */ reqport = (index * 2) + ptr->low; } /* set the current port as used */ if (d == rx) { if (GET_RX_CNT(ptr->bArray[index])) { /* port is already allocated */ NETERROR(MFCE, ("allocPort: can not allocate rx port on %d.\n", reqport)); return port; } SET_RX_CNT(ptr->bArray[index], 1); } else if (d == tx) { count = GET_TX_CNT(ptr->bArray[index]); count++; if (count <= MAX_TX_CNT) { SET_TX_CNT(ptr->bArray[index], count); } else { NETERROR(MFCE, ("allocPort: can not allocate %d tx ports on %d.\n", count, reqport)); return port; // no ports available in the port range } } else { NETERROR(MFCE, ("allocPort: can not allocate port for %s(%d).\n", dirxn2str(d), d)); return port; } port = reqport; NETDEBUG(MFCE, NETLOG_DEBUG4, ("allocPort: allocating port %d for %s\n", port, dirxn2str(d))); return port; }
void * HelloHandleTimers(void *arg) { int rval = 0; static struct timeval tout; int msec = -1; int numus = 0; timerThread = pthread_self(); #ifndef NETOID_LINUX ThreadSetRT(); #endif // NETOID_LINUX // ThreadSetPriority(timerThread, SCHED_RR, 50); for (;;) { rval = HelloSetupTimeout(&tout); if (rval < 0) { NETDEBUG(MRSD, NETLOG_DEBUG4, ("Timer already timed out\n")); HelloProcessTimeout(); continue; } msec = INFTIM; if (rval) { /* Find the timeout */ HelloAdjustTimeout(&tout, &msec); } numus = NetFdsSetupPoll(&Hellotimerfds, MRSD, NETLOG_DEBUG4); NETDEBUG(MRSD, NETLOG_DEBUG4, ("%d ready for Hello Protocol\n", numus)); rval = poll(Hellotimerfds.pollA, numus, msec); switch (rval) { case -1: NETDEBUG(MSEL, NETLOG_DEBUG4, ("poll failure %d", errno)); break; case 0: NETDEBUG(MSEL, NETLOG_DEBUG4, ("poll timeout")); break; default: //fprintf(stdout, "Timer activated: %d\n", rval); NETDEBUG(MSEL, NETLOG_DEBUG4, ("poll process")); rval = NetFdsProcessPoll(&Hellotimerfds, MSEL, NETLOG_DEBUG4); if (rval < 0) { NETERROR(MSEL, ("Found a bad fd %d, deactivating it!\n", -rval)); NetFdsDeactivate(&Hellotimerfds, -rval, FD_RW); } break; } } return 0; }
int System(const char *cmdstr, char *buf, int buflen) { int status; int rval = -128; #ifdef USE_SYS_POPEN int cmdfd; int nbytes; if ( (cmdfd = sys_popen(cmdstr, 1)) >= 0) { if (buf) { if ((nbytes = read(cmdfd, buf, buflen-1)) >=0) { *(buf+nbytes) = '\0'; // terminate the string NETDEBUG(MEXECD, NETLOG_DEBUG4, ("Cmd Output: %s", buf)); } else { NETERROR(MEXECD, ("read: %s, for output of - %s\n", strerror(errno), cmdstr)); status = 1; } } status = sys_pclose(cmdfd); } else { NETERROR(MEXECD, ("sys_popen: failed for cmd %s, error: %s\n", cmdstr, strerror(errno))); status = 1; } #else status = system(cmdstr); #endif if (WIFEXITED(status)) { NETDEBUG(MEXECD, NETLOG_DEBUG3, ("normal termination, exit status = %d\n", WEXITSTATUS(status))); rval = (WEXITSTATUS(status)) - ( ((WEXITSTATUS(status)) & 0x80) ? 0x100 : 0 ); } else if (WIFSIGNALED(status)) { NETDEBUG(MEXECD, NETLOG_DEBUG3, ("abnormal termination, signal number = %d%s\n", WTERMSIG(status), WCOREDUMP(status) ? " (core file generated)" : "")); rval = -128; } else if (WIFSTOPPED(status)) { NETDEBUG(MEXECD, NETLOG_DEBUG3, ("child stopped. signal number = %d\n", WSTOPSIG(status))); rval = -128; } return(rval); }
/* UA Event processor code */ int SipRegProcessEventWorker(SipEventHandle *evHandle) { char fn[] = "SipRegProcessEventWorker():"; SipRegSMEntry *smEntry = NULL; CallHandle *callHandle = NULL; SipAppMsgHandle *appMsgHandle = NULL; int state = SipReg_sIdle; appMsgHandle = SipEventAppHandle(evHandle); /* change state */ SipRegChangeState(evHandle, appMsgHandle, &state); smEntry = &SipRegSM[state][evHandle->event]; if (smEntry == NULL) { NETERROR(MSIP, ("%s No State Machine Entry found\n", fn)); SipFreeEventHandle(evHandle); goto _release_locks; } /* Call the action routine */ if (smEntry->action) { if ((smEntry->action)(evHandle) < 0) { /* Action routine failed */ NETDEBUG(MSIP, NETLOG_DEBUG1, ("%s Action routine failed\n", fn)); goto _error; } } else { // No action routine ?? Event must be freed SipFreeEventHandle(evHandle); } return 0; _release_locks: _error: NETDEBUG(MSIP, NETLOG_DEBUG1, ("%s Returning Error\n", fn)); return -1; }
static struct ipq *ip_create(struct sk_buff *skb, struct iphdr *iph, struct device *dev) { struct ipq *qp; int ihlen; qp = (struct ipq *) frag_kmalloc(sizeof(struct ipq), GFP_ATOMIC); if (qp == NULL) { NETDEBUG(printk("IP: create: no memory left !\n")); return(NULL); } memset(qp, 0, sizeof(struct ipq)); /* * Allocate memory for the IP header (plus 8 octets for ICMP). */ ihlen = iph->ihl * 4; qp->iph = (struct iphdr *) frag_kmalloc(64 + 8, GFP_ATOMIC); if (qp->iph == NULL) { NETDEBUG(printk("IP: create: no memory left !\n")); frag_kfree_s(qp, sizeof(struct ipq)); return(NULL); } memcpy(qp->iph, iph, ihlen + 8); qp->len = 0; qp->ihlen = ihlen; qp->fragments = NULL; qp->dev = dev; /* Start a timer for this entry. */ qp->timer.expires = jiffies + IP_FRAG_TIME; /* about 30 seconds */ qp->timer.data = (unsigned long) qp; /* pointer to queue */ qp->timer.function = ip_expire; /* expire function */ add_timer(&qp->timer); /* Add this entry to the queue. */ qp->prev = NULL; cli(); qp->next = ipqueue; if (qp->next != NULL) qp->next->prev = qp; ipqueue = qp; sti(); return(qp); }
int RunHelloProtocol(int argc, char **argv) { // ThreadSetUniprocessorMode(); NETDEBUG(MRSD, NETLOG_DEBUG1, ("starting hello protocol ... \n")); libavl_init(1); timerLibInit(); // hcp_lock = LockAlloc(); // LockInit(hcp_lock, 0); /* Not sharing with other processes */ hcpInit(); timerInit(&hcp->Mtimer, 128, 0); NetFdsInit(&Hellotimerfds); IpcInit(); ThreadLaunch(HelloHandleTimers, NULL, 1); pthread_mutex_lock(&(hellocv.mutex)); hellocv.value = 1; pthread_cond_signal(&(hellocv.cond)); pthread_mutex_unlock(&(hellocv.mutex)); /* Main Loop */ HelloProtMainLoop(); return(0); }
int LeaderChanged(void) { host_rec *hitem; IPADDRESS prev_ldr, cur_ldr, ldr; struct in_addr lead; char buf[INET_ADDRSTRLEN]; hitem = listGetFirstItem(hcp->hrecs); if (hitem == NULL) { /* We should never come here */ NETERROR(MRSD, ("Fatal: Can not find self host record. Exiting... \n")); kill(getpid(), SIGTERM); } prev_ldr = hitem->primary; cur_ldr = UpdateSelfRec(); ldr = cur_ldr; memcpy(&lead, &ldr, sizeof(ldr)); if ( prev_ldr != cur_ldr ) { NETDEBUG(MRSD, NETLOG_DEBUG1, ("Leader changed to %s\n", inet_ntop( AF_INET, &lead, buf, INET_ADDRSTRLEN))); NETINFOMSG(MRSD, ("Leader changed to %s based on priority calculations\n", inet_ntop( AF_INET, &lead, buf, INET_ADDRSTRLEN))); hcp->printflag = 1; publish_host_table(); return(1); } else return(0); }
int hsm_init(void) { struct itimerval hellotmr, selftmr; char fn[] = "hsm_init"; NETDEBUG(MRSD, NETLOG_DEBUG2, ("%s: called \n", fn)); /* DOWN State */ hcp->nsm_list = listInit(); /* Add an interval timer which wakes up and sends a hello message */ memset(&hellotmr, 0, sizeof(hellotmr)); hellotmr.it_interval.tv_sec = HELLO_INTERVAL; hcp->hellotid = timerAddToList(&hcp->Mtimer, &hellotmr, 0, \ PSOS_TIMER_REL, "HelloTimer", SendHello, NULL); /* Add a timer which makes the host master if there is no response seen */ memset(&selftmr, 0, sizeof(selftmr)); selftmr.it_value.tv_sec = HELLO_INTERVAL*DEAD_FACTOR; hcp->selftid = timerAddToList(&hcp->Mtimer, &selftmr, 0, \ PSOS_TIMER_REL, "SelfTimer", (TimerFn)HostIsLeader, NULL); return(1); }
Uint32 get_msg(t_client *client) { int result; static char *data = 0; if (!data) // can't be dealloced, so using malloc system if (!(data = (char*)malloc(sizeof(*data) * NET_MSS))) { fprintf(stderr, "Not enough memory\n"); exit(43); } result = my_recv(client->sock, data, NET_MSS); if (result <= 0) { if (SDLNet_GetError() && strlen(SDLNet_GetError())) { NETDEBUG(SDLNet_GetError()); } client->state = (unsigned long)STATE_FAIL_RECV; fprintf(stderr, "loss state :%s, %d, %d\n", strerror(errno), result, (int)data); // met dans list deadclient, avec un etat 'fail_recv' // (essaie donc d'ecrire qd meme sur la socket) // ou la liste de commandes est trop importante.. return (0); } if (cut_trame_in_msg(client, data, result)) return (1); return (0); }
/* * If a sender wishes the packet to remain unfreed * we add it to his send queue. This arguably belongs * in the TCP level since nobody else uses it. BUT * remember IPng might change all the rules. */ static inline void add_to_send_queue(struct sock * sk, struct sk_buff * skb) { unsigned long flags; /* The socket now has more outstanding blocks */ sk->packets_out++; /* Protect the list for a moment */ save_flags(flags); cli(); if (skb->link3 != NULL) { NETDEBUG(printk("ip.c: link3 != NULL\n")); skb->link3 = NULL; } if (sk->send_head == NULL) { sk->send_tail = skb; sk->send_head = skb; sk->send_next = skb; } else { sk->send_tail->link3 = skb; sk->send_tail = skb; } restore_flags(flags); }
static struct ipfrag *ip_frag_create(int offset, int end, struct sk_buff *skb, unsigned char *ptr) { struct ipfrag *fp; unsigned long flags; fp = (struct ipfrag *) frag_kmalloc(sizeof(struct ipfrag), GFP_ATOMIC); if (fp == NULL) { NETDEBUG(printk("IP: frag_create: no memory left !\n")); return(NULL); } memset(fp, 0, sizeof(struct ipfrag)); /* Fill in the structure. */ fp->offset = offset; fp->end = end; fp->len = end - offset; fp->skb = skb; fp->ptr = ptr; /* * Charge for the SKB as well. */ save_flags(flags); cli(); ip_frag_mem+=skb->truesize; restore_flags(flags); return(fp); }
int NotifyHelloProt(void *data) { char fn[] = "NotifyHelloProt():"; TimerNotifyCBData *tcbdata = (TimerNotifyCBData *)data; if (tcbdata->wnotify != tcbdata->rnotify) { // already notification in queue NETDEBUG(MRSD, NETLOG_DEBUG4, ("%s Already Notified r=%x w=%x\n", fn, tcbdata->rnotify, tcbdata->wnotify)); return 0; } if (write(tcbdata->notifyPipe[NOTIFY_WRITE]," ",1) < 0) { // Check the error if (errno == EAGAIN) { NETERROR(MRSD, ("Blocking error in notification\n")); } } else { tcbdata->wnotify ++; } return(0); }
/* Create a new fragment entry. */ static struct ipfrag *ip_frag_create(int offset, int end, struct sk_buff *skb, unsigned char *ptr) { struct ipfrag *fp; fp = (struct ipfrag *) frag_kmalloc(sizeof(struct ipfrag), GFP_ATOMIC); if (fp == NULL) goto out_nomem; /* Fill in the structure. */ fp->offset = offset; fp->end = end; fp->len = end - offset; fp->skb = skb; fp->ptr = ptr; fp->next = fp->prev = NULL; /* Charge for the SKB as well. */ atomic_add(skb->truesize, &ip_frag_mem); return(fp); out_nomem: NETDEBUG(printk(KERN_ERR "IP: frag_create: no memory left !\n")); return(NULL); }
int nsm_kill(nbr_info *nbrp) { host_rec *nbr_rec; List tmp; char fn[] = "nsm_kill"; struct in_addr nbr_ip_addr; char buf[INET_ADDRSTRLEN]; if (nbrp == NULL) { return(1); } NETDEBUG(MRSD, NETLOG_DEBUG2, ("%s: called \n", fn)); nbr_rec = (host_rec *)listGetFirstItem(nbrp->records); if ((tmp = SearchListforMatch(hcp->hrecs, (void *)nbr_rec, \ match_rec_nbr)) == NULL) { NETDEBUG(MRSD, NETLOG_DEBUG1, ("host does not contain record for neigbour which died\n")); } else { nbr_ip_addr.s_addr = nbr_rec->host; NETINFOMSG(MRSD, ("Neighbour %s died \n", inet_ntop( AF_INET, &nbr_ip_addr, buf, INET_ADDRSTRLEN) )); listDeleteItem(hcp->hrecs, tmp->item); } hcp->printflag = 1; /* Print list is true */ publish_host_table(); Free(nbrp->sa); Free(nbrp->curpkt); listDestroy(nbrp->records); listDeleteItem(hcp->nsm_list, nbrp); Free(nbrp); if (LeaderChanged()) exec_hsm_event(HSM_EVENT_NBRCHNG); if (CheckNSMStates() && LeaderAgreed()) { /* A kludgy way to give system some time before we call init */ msleep(500); exec_hsm_event(HSM_EVENT_FULLCONN); } return(1); }
int hsm_ignore(void) { char fn[] = "hsm_ignore"; NETDEBUG(MRSD, NETLOG_DEBUG3, ("%s: called \n", fn)); return(1); }
int hsm_negt(void) { char fn[] = "hsm_negt"; NETDEBUG(MRSD, NETLOG_DEBUG2, ("%s: called \n", fn)); hcp->reg->cb_kill(hcp->reg->kill_arg, (void *)hcp->role); return(1); }
int HelloRegCb(AppCB *appcb) { memcpy(hcp->reg, appcb, sizeof(AppCB)); NETDEBUG(MRSD, NETLOG_DEBUG3, ("Application in thread %d registered callback with hello protocol\n", pthread_self())); return(0); }
int hsm_kill(void) { char fn[] = "hsm_kill"; NETDEBUG(MRSD, NETLOG_DEBUG2, ("%s: called \n", fn)); HelloDeleteLocks(); return(1); }
int publish_host_table(void) { List list = hcp->hrecs; host_rec *item, *hitem; char cmd[RS_LINELEN]; int len, flag; int rval; NETDEBUG(MRSD, NETLOG_DEBUG2, ("publish_host_table called - printflag = %d\n", hcp->printflag)); if (!(hcp->printflag)) return(0); snprintf(cmd, RS_LINELEN, "%s rsd clear", RS_CLI_CMD_STR); // rval = ExecuteCliCommand(cmd); // hcp->printflag = rval; rval = System(cmd); hcp->printflag = rval; NETDEBUG(MRSD, NETLOG_DEBUG2, ("executed - %s, rval =%d\n", cmd, rval)); for (item = listGetFirstItem(list), hitem = item; item; \ item = listGetNextItem(list, item)) { flag = (memcmp(&(hitem->primary), &(item->host), sizeof(IPADDRESS))==0); len = 0; //len += snprintf(cmd, RS_LINELEN, "%s rsd add ", RS_CLI_CMD_STR); //len += snprintf(cmd, RS_LINELEN, "%d", ntohs(item->port)); len += snprintf(cmd+len, RS_LINELEN-len, "%s rsd add ", RS_CLI_CMD_STR); len += snprintf(cmd+len, RS_LINELEN-len, "%d", (atoi(rs_port))); len += snprintf(cmd+len, RS_LINELEN-len, " "); FormatIpAddress(ntohl(item->host), cmd+len); strcat(cmd+len, flag?" master":" slave"); // rval = ExecuteCliCommand(cmd); // hcp->printflag = rval; rval = System(cmd); hcp->printflag = rval; NETDEBUG(MRSD, NETLOG_DEBUG2, ("executed - %s, rval = %d\n", cmd, rval)); } /* We should check the status of all the system() and return an appropriate status */ return(0); }
static void HandleTERM(int sig) { char fn[] = "HandleTERM"; NETDEBUG(MEXECD, NETLOG_DEBUG2, ("%s: Signal SIGTERM caught - stopping process\n", fn)); NETINFOMSG(MEXECD, ("*** NexTone Cmd Execution Server shutdown: started ***\n")); q_vdelete(msgqid); exit(0); }
static void HandleHUP(int sig) { char fn[] = "HandleHUP"; NETDEBUG(MEXECD, NETLOG_DEBUG2, ("%s: Signal SIGHUP caught - Reconfiguring\n", fn)); NetLogClose(); DoConfig(ConfigureExecD); // exit(0); return; }
int nsm_ignore(nbr_info *nbrp) { char fn[] = "nsm_ignore"; if (nbrp == NULL) { return(1); } NETDEBUG(MRSD, NETLOG_DEBUG3, ("%s: called \n", fn)); return(1); }
int GisPostCliFormatGkRegCmd(PostCmd *cmd, char *tags, char *cmdstr, int cmdlen, int len) { char fn[] = "GisPostCliFormatGkRegCmd():"; CacheGkInfo *cacheGkInfo = NULL, cacheGkInfoEntry; // Look up the entry in the cache if (CacheFind(gkCache, &cmd->key, &cacheGkInfoEntry, sizeof(cacheGkInfoEntry)) < 0) { NETDEBUG(MDB, NETLOG_DEBUG1, ("%s Could not find gk %s/%d\n", fn, cmd->key.regid, cmd->key.uport)); goto _return; } cacheGkInfo = &cacheGkInfoEntry; len += snprintf(CMDSTR, CMDMAX, "gk reg \"%s\" %d ", cmd->key.regid, cmd->key.uport); if (BITA_TEST(tags, TAG_EPID)) { len += snprintf(CMDSTR, CMDMAX, "epid \""); len += hex2chr(CMDSTR, CMDMAX, cacheGkInfo->endpointIDString, cacheGkInfo->endpointIDLen); len += snprintf(CMDSTR, CMDMAX, "\" "); } if (BITA_TEST(tags, TAG_GKREGTTL)) { len += snprintf(CMDSTR, CMDMAX, "ttl %d ", cacheGkInfo->regttl); } if (BITA_TEST(tags, TAG_GKFLAGS)) { len += snprintf(CMDSTR, CMDMAX, "flags %d ", cacheGkInfo->flags); } if (BITA_TEST(tags, TAG_REGSTATUS)) { len += snprintf(CMDSTR, CMDMAX, "reg %s ", (cacheGkInfo->regState == GKREG_REGISTERED)?"active":"inactive"); } _return: return len; }
void sip_indicateRefer ( SipMessage *s, SipEventContext *context ) { char fn[] = "sip_indicateRefer():"; NETDEBUG(MSIP, NETLOG_DEBUG4, ("%s Request Received\n", fn)); return SipIncomingRequest(s, "REFER", context); }
static void HandleCHLD(int sig) { char fn[] = "HandleCHLD"; pid_t pid; int stat; NETDEBUG(MEXECD, NETLOG_DEBUG2, ("%s: Signal SIGCHLD caught - waiting for child\n", fn)); while ((pid = waitpid((pid_t)-1, &stat, WNOHANG)) > 0) fprintf(stderr, "Child terminated\n"); return; }
int hsm_up(void) { char fn[] = "hsm_up"; NETDEBUG(MRSD, NETLOG_DEBUG2, ("%s: called \n", fn)); hcp->reg->cb_init(hcp->reg->init_arg, (void *)hcp); hcp->printflag = 1; /* Print list is true */ publish_host_table(); return(1); }
int nsm_oneway(nbr_info *nbrp) { char fn[] = "nsm_oneway"; if (nbrp == NULL) { return(1); } NETDEBUG(MRSD, NETLOG_DEBUG2, ("%s: called \n", fn)); nbrp->state = NSM_STATE_UNIDIR; return(1); }