addr_storage parser::get_addr(group_mem_protocol gmp) { HC_LOG_TRACE(""); std::ostringstream s; while (true) { if (m_current_token.get_type() == TT_STRING) { s << m_current_token.get_string(); } else if (m_current_token.get_type() == TT_DOT) { s << "."; } else if (m_current_token.get_type() == TT_DOUBLE_DOT) { s << ":"; } else { break; } get_next_token(); } addr_storage result(s.str()); if (result.is_valid()) { if (result.get_addr_family() == get_addr_family(gmp)) { return result; } else { HC_LOG_ERROR("failed to parse line " << m_current_line << " ip address: " << s.str() << " has a wrong IP version"); throw "failed to parse config file"; } } else { HC_LOG_ERROR("failed to parse line " << m_current_line << " ip address: " << s.str() << " is invalid"); throw "failed to parse config file"; } }
struct addrinfo * cmd_proxy_getinfo(char *address, int type, short port) { struct addrinfo ai, *aitop; char strport[NI_MAXSERV]; memset(&ai, 0, sizeof(ai)); ai.ai_family = get_addr_family(address); ai.ai_socktype = type; ai.ai_flags = 0; snprintf(strport, sizeof(strport), "%d", port); if (getaddrinfo(address, strport, &ai, &aitop) != 0) { warn("getaddrinfo: %s:%d", address, port); return (NULL ); } return (aitop); }
/* * esi_scn_thr() is the thread creates an end point to receive and process * ESI & SCN messages. This thread is created when isns_access is enabled * and for the duration of the iscsi daemon */ static void * esi_scn_thr(void *arg) { struct sockaddr sa, *ai; struct sockaddr_in sin; struct sockaddr_in6 sin6; int so, fd, pf; socklen_t len; char strport[NI_MAXSERV]; isns_pdu_t *scn = NULL; struct timeval timeout; fd_set fdset; int socket_ready = 0; pf = get_addr_family(isns_args.entity); if (pf == PF_INET) { bzero(&sin, sizeof (sin)); sin.sin_family = PF_INET; sin.sin_port = htons(0); sin.sin_addr.s_addr = INADDR_ANY; ai = (struct sockaddr *)&sin; len = sizeof (sin); } else if (pf == PF_INET6) { bzero(&sin6, sizeof (sin6)); sin6.sin6_family = PF_INET6; sin6.sin6_port = htons(0); sin6.sin6_addr = in6addr_any; ai = (struct sockaddr *)&sin6; len = sizeof (sin6); } else { syslog(LOG_ERR, "Bad address family. Exit esi_scn_thr"); return (NULL); } /* * create and bind SCN socket * save the scn port info */ if ((so = socket(pf, SOCK_STREAM, 0)) == -1) { syslog(LOG_ALERT, "create isns socket failed"); return (NULL); } (void) setsockopt(so, SOL_SOCKET, SO_REUSEADDR, 0, 0); if (bind(so, ai, len) < 0) { syslog(LOG_ALERT, "esi_scn_thr: bind failed"); (void) close(so); return (NULL); } /* get scn port info */ len = sizeof (sa); if (getsockname(so, &sa, &len) < 0) { syslog(LOG_ALERT, "isns getsockname failed"); (void) close(so); return (NULL); } if (getnameinfo(&sa, len, NULL, 0, strport, NI_MAXSERV, NI_NUMERICSERV) != 0) { syslog(LOG_ALERT, "isns getnameinfo failed"); (void) close(so); return (NULL); } scn_port = atoi(strport); if (listen(so, 5) < 0) { syslog(LOG_ALERT, "esi_scn_thr: failed listen"); (void) close(so); return (NULL); } /* listen for esi or scn messages */ while (isns_shutdown == False) { /* ISNS_ESI_INTERVAL_ATTR_ID is set to 10s */ timeout.tv_sec = 10; timeout.tv_usec = 0; FD_ZERO(&fdset); FD_SET(so, &fdset); socket_ready = select(so + 1, &fdset, NULL, NULL, &timeout); /* If disabled bail out, dont care about packets */ if (isns_enabled() == False) { syslog(LOG_INFO, "isns server is disabled, dergister target"); isns_fini(); (void) close(so); return (NULL); } if (socket_ready < 0) { syslog(LOG_ERR, "esi_scn_thr: select failed, retrying."); continue; } else if (socket_ready == 0) { /* timeout */ continue; } else { /* Socket is ready */ if ((fd = accept(so, &sa, &len)) < 0) { syslog(LOG_ALERT, "esi_scn_thr: failed accept"); continue; } } if (isns_recv(fd, (isns_rsp_t **)&scn) == 0) { /* Just return success for ESI */ switch (scn->func_id) { case ISNS_ESI: process_esi(fd, scn); break; case ISNS_SCN: /* call the SCN process function */ process_scn(fd, scn); break; default: syslog(LOG_ERR, "esi_scn_thr: Invalid funcid %d\n", scn->func_id); break; } /* free response resource */ isns_free_pdu(scn); } else { syslog(LOG_ALERT, "esi_scn_thr fails isns_recv "); } (void) close(fd); } (void) close(so); return (NULL); }
std::unique_ptr<addr_match> parser::parse_rule_part(group_mem_protocol gmp) { HC_LOG_TRACE(""); //TT_STAR //TT_STRING //single_addr //addr_range addr_storage addr_from(get_addr_family(gmp)); addr_storage addr_to(get_addr_family(gmp)); if (m_current_token.get_type() == TT_STRING || m_current_token.get_type() == TT_STAR) { if (m_current_token.get_type() == TT_STRING) { addr_from = get_addr(gmp); } else { //TT_STAR get_next_token(); } if (m_current_token.get_type() == TT_SLASH) { get_next_token(); if (m_current_token.get_type() == TT_STRING) { try { unsigned int prefix = std::stoi(m_current_token.get_string()); if (prefix > 128) { throw; } addr_to = addr_from; addr_from.mask(prefix); addr_to.broadcast_addr(prefix); get_next_token(); if (m_current_token.get_type() == TT_RIGHT_BRACKET || m_current_token.get_type() == TT_PIPE) { std::unique_ptr<addr_match> result(new addr_range(addr_from, addr_to)); return result; } } catch (...) { HC_LOG_ERROR("failed to parse line " << m_current_line << " token " << get_token_type_name(m_current_token.get_type()) << " with value " << m_current_token.get_string() << " cant be converted to a prefix or subnet mask"); throw "failed to parse config file"; } } } else if (m_current_token.get_type() == TT_RIGHT_BRACKET || m_current_token.get_type() == TT_PIPE) { std::unique_ptr<addr_match> result(new single_addr(addr_from)); return result; } else if (m_current_token.get_type() == TT_RANGE) { get_next_token(); if (m_current_token.get_type() == TT_STRING || m_current_token.get_type() == TT_STAR) { if (m_current_token.get_type() == TT_STRING) { addr_to = get_addr(gmp); } else { get_next_token(); } if (m_current_token.get_type() == TT_RIGHT_BRACKET || m_current_token.get_type() == TT_PIPE) { std::unique_ptr<addr_match> result(new addr_range(addr_from, addr_to)); return result; } } } } HC_LOG_ERROR("failed to parse line " << m_current_line << " unknown token " << get_token_type_name(m_current_token.get_type()) << " with value " << m_current_token.get_string() << " in this context"); throw "failed to parse config file"; }