void init_signals(void) { size_t sh; /* The mask of all our handlers will block all our other handlers. */ (void)sigemptyset(&mask); for (sh = 0; sh < sizeof sighandlers / sizeof sighandlers[0]; sh++) sigaddset(&mask, sighandlers[sh].sig); /* Install our signal handlers with that shared mask. */ for (sh = 0; sh < sizeof sighandlers / sizeof sighandlers[0]; sh++) { struct sigaction sa; memset(&sa, 0, sizeof sa); sa.sa_mask = mask; sa.sa_handler = sighandlers[sh].hand; if (sigaction(sighandlers[sh].sig, &sa, NULL) < 0) ns_error(ns_log_os, "sigaction failed in set_signal_handler(%d): %s", sighandlers[sh].sig, strerror(errno)); } /* Unblock all signals that we expect to handle. */ if (sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) ns_panic(ns_log_os, 1, "sigblock failed: %s", strerror(errno)); }
static SIG_FN discard_pipe(int sig) { #ifdef SIGPIPE_ONE_SHOT int saved_errno = errno; struct sigaction sa; UNUSED(sig); memset(&sa, 0, sizeof sa); sa.sa_mask = mask; sa.sa_handler = discard_pipe; if (sigaction(SIGPIPE, &sa, NULL) < 0) ns_error(ns_log_os, "sigaction failed in discard_pipe: %s", strerror(errno)); errno = saved_errno; #else UNUSED(sig); #endif }
nsintr() { register struct idp *idp; register struct mbuf *m; register struct nspcb *nsp; register int i; int len, s, error; char oddpacketp; next: /* * Get next datagram off input queue and get IDP header * in first mbuf. */ s = splimp(); IF_DEQUEUE(&nsintrq, m); splx(s); nsintr_getpck++; if (m == 0) return; if ((m->m_flags & M_EXT || m->m_len < sizeof (struct idp)) && (m = m_pullup(m, sizeof (struct idp))) == 0) { idpstat.idps_toosmall++; goto next; } /* * Give any raw listeners a crack at the packet */ for (nsp = nsrawpcb.nsp_next; nsp != &nsrawpcb; nsp = nsp->nsp_next) { struct mbuf *m1 = m_copy(m, 0, (int)M_COPYALL); if (m1) idp_input(m1, nsp); } idp = mtod(m, struct idp *); len = ntohs(idp->idp_len); if (oddpacketp = len & 1) { len++; /* If this packet is of odd length, preserve garbage byte for checksum */ } /* * Check that the amount of data in the buffers * is as at least much as the IDP header would have us expect. * Trim mbufs if longer than we expect. * Drop packet if shorter than we expect. */ if (m->m_pkthdr.len < len) { idpstat.idps_tooshort++; goto bad; } if (m->m_pkthdr.len > len) { if (m->m_len == m->m_pkthdr.len) { m->m_len = len; m->m_pkthdr.len = len; } else m_adj(m, len - m->m_pkthdr.len); } if (idpcksum && ((i = idp->idp_sum)!=0xffff)) { idp->idp_sum = 0; if (i != (idp->idp_sum = ns_cksum(m, len))) { idpstat.idps_badsum++; idp->idp_sum = i; if (ns_hosteqnh(ns_thishost, idp->idp_dna.x_host)) error = NS_ERR_BADSUM; else error = NS_ERR_BADSUM_T; ns_error(m, error, 0); goto next; } } /* * Is this a directed broadcast? */ if (ns_hosteqnh(ns_broadhost,idp->idp_dna.x_host)) { if ((!ns_neteq(idp->idp_dna, idp->idp_sna)) && (!ns_neteqnn(idp->idp_dna.x_net, ns_broadnet)) && (!ns_neteqnn(idp->idp_sna.x_net, ns_zeronet)) && (!ns_neteqnn(idp->idp_dna.x_net, ns_zeronet)) ) { /* * Look to see if I need to eat this packet. * Algorithm is to forward all young packets * and prematurely age any packets which will * by physically broadcasted. * Any very old packets eaten without forwarding * would die anyway. * * Suggestion of Bill Nesheim, Cornell U. */ if (idp->idp_tc < NS_MAXHOPS) { idp_forward(m); goto next; } } /* * Is this our packet? If not, forward. */ } else if (!ns_hosteqnh(ns_thishost,idp->idp_dna.x_host)) { idp_forward(m); goto next; } /* * Locate pcb for datagram. */ nsp = ns_pcblookup(&idp->idp_sna, idp->idp_dna.x_port, NS_WILDCARD); /* * Switch out to protocol's input routine. */ nsintr_swtch++; if (nsp) { if (oddpacketp) { m_adj(m, -1); } if ((nsp->nsp_flags & NSP_ALL_PACKETS)==0) switch (idp->idp_pt) { case NSPROTO_SPP: spp_input(m, nsp); goto next; case NSPROTO_ERROR: ns_err_input(m); goto next; } idp_input(m, nsp); } else { ns_error(m, NS_ERR_NOSOCK, 0); } goto next; bad: m_freem(m); goto next; }
void StompInterpreter::receive(unsigned int _id, const char* _data, int _size) { Stomp::Frame received(_data, _size); switch(received.getCommand()) { case Stomp::CONNECT: { std::string session; std::string username = received.getHeaderValueFor("login"); std::string password = received.getHeaderValueFor("passcode"); if(m_login.login(username, password, session)) { StompSubscriberIterator it = ns_subscribers->find(_id); if(it != ns_subscribers->end()) { // from http://stomp.github.com//stomp-specification-1.1.html#Connecting it->second.session = session; it->second.username = username; std::stringstream ssid; ssid << _id; Stomp::Frame send; send.setCommand(Stomp::CONNECTED); send.addOrReplaceHeader(Stomp::Header("session", ssid.str())); m_websocket.send(_id, send.getFrameData()); } else { ns_error(_id, "CONNECT", "unknown internal user id"); } } else { ns_error(_id, "CONNECT", "LoginHandler error"); } break; } case Stomp::DISCONNECT: { StompSubscriberIterator it = ns_subscribers->find(_id); disconnect(_id); m_websocket.disconnect(_id); break; } case Stomp::SUBSCRIBE: { // from http://stomp.github.com//stomp-specification-1.1.html#SUBSCRIBE std::string subscribe_id = received.getHeaderValueFor("id"); std::string destination = received.getHeaderValueFor("destination"); StompSubscriberIterator it = ns_subscribers->find(_id); if(it != ns_subscribers->end()) { it->second.subscription = subscribe_id; it->second.destination = destination; if(m_chat.join(_id, it->second.username, destination)) { ns_receipt(_id, subscribe_id); } else { ns_error(_id, "SUBSCRIBE", "ChatHall error"); } } else { ns_error(_id, "SUBSCRIBE", "unknown internal user id"); disconnect(_id); m_websocket.disconnect(_id); } break; } case Stomp::SEND: { //from http://stomp.github.com//stomp-specification-1.1.html#SEND std::string destination = received.getHeaderValueFor("destination"); std::string message = received.getBody().str(); StompSubscriberIterator it = ns_subscribers->find(_id); if(it != ns_subscribers->end()) { if(m_chat.message(_id, destination, message)) { ns_receipt(_id, destination); } else { ns_error(_id, "SEND", "ChatHall error"); disconnect(_id); m_websocket.disconnect(_id); } } else { ns_error(_id, "SEND", "unknown internal user id"); disconnect(_id); m_websocket.disconnect(_id); } break; } default: { break; } } }