static void net_params(mbi_t *mbi) { net_info_t *net = &info->hrd.dev.net; module_t *mod = (module_t*)((offset_t)mbi->mods_addr + sizeof(module_t)); mbi_opt_hdl_t hdl = (mbi_opt_hdl_t)ip_from_str; if(!mbi_get_opt(mbi, mod, "ip", hdl, (void*)&net->ip)) panic("need ip=x.x.x.x on vmm module cmd line\n"); if(!mbi_get_opt(mbi, mod, "netmask", hdl, (void*)&net->mk)) panic("need netmask=x.x.x.x on vmm module cmd line\n"); if(!mbi_get_opt(mbi, mod, "gateway", hdl, (void*)&net->gw)) panic("need gateway=x.x.x.x on vmm module cmd line\n"); net->peer.ip = net->gw; net->port = 1337; #ifdef CONFIG_NET_DBG { char str[IP_STR_SZ]; ip_str(net->ip, str); debug(NET, "ip addr %s\n", str); ip_str(net->mk, str); debug(PMEM, "netmask %s\n", str); ip_str(net->gw, str); debug(PMEM, "gateway %s\n", str); } #endif }
static int clear_affinity(int ifindex, int protocol, unsigned laddr, unsigned lport, unsigned raddr, unsigned rport) { struct sfc_aff_clear sac; int rc, affinity_fd; if( (affinity_fd = get_affinity_fd()) < 0 ) return 0; if( log_level ) printf("> %s %s:%d %s:%d ifindex %d clear\n", proto_to_str(protocol), ip_str(laddr), ntohs(lport), ip_str(raddr), ntohs(rport), ifindex); sac.ifindex = ifindex; sac.protocol = protocol; sac.daddr = laddr; sac.dport = lport; sac.saddr = raddr; sac.sport = rport; sac.err_out = sfc_aff_no_error; rc = ioctl(affinity_fd, SFC_AFF_CLEAR, &sac); if( sac.err_out != sfc_aff_no_error || rc != 0 ) { err("%s: ERROR: Failed to clear affinity\n", me); err("%s: %s %s:%d %s:%d ifindex %d\n", me, proto_to_str(protocol), ip_str(laddr), ntohs(lport), ip_str(raddr), ntohs(rport), ifindex); err("%s: errno=%d %s\n", me, errno, strerror(errno)); if( sac.err_out != sfc_aff_no_error ) err("%s: err=%d %s\n", me, (int) sac.err_out, sfc_aff_err_msg(sac.err_out)); return 0; } return 1; }
/* Get address from other nodes: */ void Membership::getAdress(std::string filename) { logFile<<"getAdress:" << std::endl; std::ifstream addFile(filename); std::string str; int i=0; address.clear(); while(!addFile.eof()) { getline(addFile, str); address.push_back(str); struct hostent *server = gethostbyname(str.c_str()); struct in_addr addr; char ip[20]; memcpy(&addr, server->h_addr_list[0], sizeof(struct in_addr)); strcpy(ip,inet_ntoa(addr)); std::string ip_str (ip); struct Node newnode; //newnode.name = str; newnode.ip_str = ip_str; nodes.push_back(newnode); logFile << "read node " << i++ << " from addr file: " << str << " : " << ip_str << std::endl; } }
size_t _net_gen_ip_pkt(ip_gen_t builder, ip_addr_t src, ip_addr_t dst, loc_t pkt, size_t dlen) { ip_hdr_t *hdr; loc_t ip; mac_addr_t mac; ip.linear = pkt.linear + sizeof(eth_hdr_t); hdr = (ip_hdr_t*)ip.addr; if(!arp_cache_lookup(dst, &mac)) { #ifdef CONFIG_NET_DBG { char ips[IP_STR_SZ]; ip_str(dst, ips); debug(NET, "can't find %s MAC addr\n", ips); } #endif return 0; } return net_gen_eth_ip_pkt(&mac, pkt) + builder(hdr, src, dst, dlen); }
bool ResolveIPV4HostName (const char *hostname, in_addr_t &addr) { if (hostname == NULL || hostname[0] == '\0' || strcmp(hostname, "localhost") == 0 || strcmp(hostname, "127.0.0.1") == 0) { addr = htonl (INADDR_LOOPBACK); return true; } else if (strcmp(hostname, "*") == 0) { addr = htonl (INADDR_ANY); return true; } else { // See if an IP address was specified as numbers int inet_pton_result = ::inet_pton (AF_INET, hostname, &addr); if (inet_pton_result == 1) return true; struct hostent *host_entry = gethostbyname (hostname); if (host_entry) { std::string ip_str (::inet_ntoa (*(struct in_addr *)*host_entry->h_addr_list)); inet_pton_result = ::inet_pton (AF_INET, ip_str.c_str(), &addr); if (inet_pton_result == 1) return true; } } return false; }
static void print_ip(char *buf, ip_addr *i) { ASSERT_NOT_NULL(buf); ASSERT_NOT_NULL(i); l4_uint32_t addr = i->a; snprintf(buf, ipbuf_size, ip_fmt, ip_str(&addr)); }
bool IPAddress::setIncrementValue ( int p_value ) { c_address_binary += p_value; in_addr ip_in_addr; ip_in_addr.s_addr = htonl( c_address_binary ); std::string ip_str( inet_ntoa( ip_in_addr ) ); c_address = ip::address::from_string( ip_str ); calculateNetaddress(); calculateBroadcast(); return true; }
static int set_affinity(int ifindex, int protocol, unsigned laddr, unsigned lport, unsigned raddr, unsigned rport, int rxq, int cpu) { struct sfc_aff_set sas; int rc, affinity_fd; if( (affinity_fd = get_affinity_fd()) < 0 ) return 0; if( log_level ) printf("> %s %s:%d %s:%d ifindex %d rxq %d cpu %d\n", proto_to_str(protocol), ip_str(laddr), ntohs(lport), ip_str(raddr), ntohs(rport), ifindex, rxq, cpu); sas.cpu = cpu; sas.rxq = rxq; sas.ifindex = ifindex; sas.protocol = protocol; sas.daddr = laddr; sas.dport = lport; sas.saddr = raddr; sas.sport = rport; sas.err_out = sfc_aff_no_error; rc = ioctl(affinity_fd, SFC_AFF_SET, &sas); if( sas.err_out != sfc_aff_no_error || rc != 0 ) { err("%s: ERROR: Failed to set affinity\n", me); err("%s: %s %s:%d %s:%d ifindex %d rxq %d cpu %d\n", me, proto_to_str(protocol), ip_str(laddr), ntohs(lport), ip_str(raddr), ntohs(rport), ifindex, rxq, cpu); err("%s: errno=%d %s\n", me, errno, strerror(errno)); if( sas.err_out != sfc_aff_no_error ) err("%s: err=%d %s\n", me, (int) sas.err_out, sfc_aff_err_msg(sas.err_out)); return 0; } return 1; }
LLHost::LLHost(const std::string& ip_and_port) { std::string::size_type colon_index = ip_and_port.find(":"); if (colon_index == std::string::npos) { mIP = ip_string_to_u32(ip_and_port.c_str()); mPort = 0; } else { std::string ip_str(ip_and_port, 0, colon_index); std::string port_str(ip_and_port, colon_index+1); mIP = ip_string_to_u32(ip_str.c_str()); mPort = atol(port_str.c_str()); } }
int icmp_dissect(ip_addr_t ip, loc_t pkt, size_t len) { icmp_hdr_t *hdr = (icmp_hdr_t*)pkt.addr; debug(ICMP, "rcv ICMP %s\n", __icmp_type_str(hdr->type)); if(hdr->type == ICMP_TYPE_ECHO_REQUEST) { net_info_t *net = &info->hrd.dev.net; loc_t rpkt, data; size_t rlen; rpkt.linear = net_tx_get_pktbuf(); if(!rpkt.linear) { debug(ICMP, "faild to echo-reply (no more pkt)\n"); return NET_DISSECT_FAIL; } hdr->ping.id = swap16(hdr->ping.id); hdr->ping.seq = swap16(hdr->ping.seq); data.linear = pkt.linear + sizeof(icmp_hdr_t); rlen = net_gen_pong_pkt(ip, rpkt, hdr->ping.id, hdr->ping.seq, data.addr, len - sizeof(icmp_hdr_t)); if(!rlen) { #ifdef CONFIG_ICMP_DBG char ips[IP_STR_SZ]; ip_str(ip, ips); debug(ICMP, "failed to echo-reply to %s\n", ips); #endif return NET_DISSECT_FAIL; } net_send_pkt(net, rpkt, rlen); return NET_DISSECT_OK; } return NET_DISSECT_IGN; }
static void bsdkame_process_raw_ifaces(struct raw_iface *rifaces) { struct raw_iface *ifp; /* * There are no virtual interfaces, so all interfaces are valid */ for (ifp = rifaces; ifp != NULL; ifp = ifp->next) { bool after = FALSE; /* has vfp passed ifp on the list? */ bool bad = FALSE; struct raw_iface *vfp; for (vfp = rifaces; vfp != NULL; vfp = vfp->next) { if (vfp == ifp) { after = TRUE; } else if (sameaddr(&ifp->addr, &vfp->addr)) { if (after) { loglog(RC_LOG_SERIOUS , "IP interfaces %s and %s share address %s!" , ifp->name, vfp->name, ip_str(&ifp->addr)); } bad = TRUE; } } if (bad) continue; /* We've got all we need; see if this is a new thing: * search old interfaces list. */ { struct iface_port **p = &interfaces; for (;;) { struct iface_port *q = *p; struct iface_dev *id = NULL; /* search is over if at end of list */ if (q == NULL) { /* matches nothing -- create a new entry */ int fd = create_socket(ifp, ifp->name, pluto_port); if (fd < 0) break; #ifdef NAT_TRAVERSAL if (nat_traversal_support_non_ike && addrtypeof(&ifp->addr) == AF_INET) { nat_traversal_espinudp_socket(fd, "IPv4", ESPINUDP_WITH_NON_IKE); } #endif q = alloc_thing(struct iface_port, "struct iface_port"); id = alloc_thing(struct iface_dev, "struct iface_dev"); LIST_INSERT_HEAD(&interface_dev, id, id_entry); q->ip_dev = id; id->id_rname = clone_str(ifp->name, "real device name"); id->id_vname = clone_str(ifp->name, "virtual device name"); id->id_count++; q->ip_addr = ifp->addr; q->fd = fd; q->next = interfaces; q->change = IFN_ADD; q->port = pluto_port; q->ike_float = FALSE; interfaces = q; openswan_log("adding interface %s/%s %s:%d" , q->ip_dev->id_vname , q->ip_dev->id_rname , ip_str(&q->ip_addr) , q->port); #ifdef NAT_TRAVERSAL /* * right now, we do not support NAT-T on IPv6, because * the kernel did not support it, and gave an error * it one tried to turn it on. */ if (nat_traversal_support_port_floating && addrtypeof(&ifp->addr) == AF_INET) { fd = create_socket(ifp, id->id_vname, NAT_T_IKE_FLOAT_PORT); if (fd < 0) break; nat_traversal_espinudp_socket(fd, "IPv4" , ESPINUDP_WITH_NON_ESP); q = alloc_thing(struct iface_port, "struct iface_port"); q->ip_dev = id; id->id_count++; q->ip_addr = ifp->addr; setportof(htons(NAT_T_IKE_FLOAT_PORT), &q->ip_addr); q->port = NAT_T_IKE_FLOAT_PORT; q->fd = fd; q->next = interfaces; q->change = IFN_ADD; q->ike_float = TRUE; interfaces = q; openswan_log("adding interface %s/%s %s:%d" , q->ip_dev->id_vname, q->ip_dev->id_rname , ip_str(&q->ip_addr) , q->port); } #endif break; } /* search over if matching old entry found */ if (streq(q->ip_dev->id_rname, ifp->name) && streq(q->ip_dev->id_vname, ifp->name) && sameaddr(&q->ip_addr, &ifp->addr)) { /* matches -- rejuvinate old entry */ q->change = IFN_KEEP; #ifdef NAT_TRAVERSAL /* look for other interfaces to keep (due to NAT-T) */ for (q = q->next ; q ; q = q->next) { if (streq(q->ip_dev->id_rname, ifp->name) && streq(q->ip_dev->id_vname, ifp->name) && sameaddr(&q->ip_addr, &ifp->addr)) { q->change = IFN_KEEP; } } #endif break; } /* try again */ p = &q->next; } /* for (;;) */ }
struct pending *next; }; /* queue a Quick Mode negotiation pending completion of a suitable Main Mode */ void add_pending(int whack_sock , struct state *isakmp_sa , struct connection *c , lset_t policy , unsigned long try , so_serial_t replacing) { struct pending *p = alloc_thing(struct pending, "struct pending"); DBG(DBG_CONTROL, DBG_log("Queuing pending Quick Mode with %s \"%s\"" , ip_str(&c->spd.that.host_addr) , c->name)); p->whack_sock = whack_sock; p->isakmp_sa = isakmp_sa; p->connection = c; p->policy = policy; p->try = try; p->replacing = replacing; p->pend_time = time(NULL); host_pair_enqueue_pending(c, p, &p->next); } /* Release all the whacks awaiting the completion of this state. * This is accomplished by closing all the whack socket file descriptors. * We go to a lot of trouble to tell each whack, but to not tell it twice.
/** * DPD Out Initiator * * @param p2st A state struct that is already in phase2 * @return void */ static void dpd_outI(struct state *p1st, struct state *st, bool eroute_care ,time_t delay, time_t timeout) { time_t tm; time_t last; u_int32_t seqno; bool eroute_idle; time_t nextdelay; DBG(DBG_DPD, DBG_log("processing dpd for state #%lu (\"%s\")" , st->st_serialno , st->st_connection->name)); /* If no DPD, then get out of here */ if (!st->hidden_variables.st_dpd) return; /* If there is no state, there can be no DPD */ if (!IS_ISAKMP_SA_ESTABLISHED(p1st->st_state)) return; /* find out when now is */ tm = now(); /* * pick least recent activity value, since with multiple phase 2s, * it may well be that one phase 2 is very active, while the other * for some reason, gets stomped upon by some network screw up. * * (this would only happen if the network was sensitive to different * SPI#, since for NAT-T, all traffic should be on the same UDP port. * At worst, this means that we send a bit more traffic then we need * to when there are multiple SAs and one is much less active. * */ last = (p1st->st_last_dpd > st->st_last_dpd ? st->st_last_dpd : p1st->st_last_dpd ); nextdelay = p1st->st_last_dpd + delay - tm; /* has there been enough activity of late? */ if(nextdelay > 0) { /* Yes, just reschedule "phase 2" */ DBG(DBG_DPD, DBG_log("not yet time for dpd event: %lu < %lu" , (unsigned long)tm , (unsigned long)(p1st->st_last_dpd + delay))); event_schedule(EVENT_DPD, nextdelay, st); return; } /* now plan next check time */ if(nextdelay < 1) { nextdelay = delay; } /* * check the phase 2, if we are supposed to, * and return if it is active recently */ if(eroute_care && !st->hidden_variables.st_nat_traversal) { eroute_idle = was_eroute_idle(st, delay); if(!eroute_idle) { DBG(DBG_DPD, DBG_log("dpd out event not sent, phase 2 active")); /* update phase 2 time stamp only */ st->st_last_dpd = tm; event_schedule(EVENT_DPD, nextdelay, st); return; } } if(st != p1st) { /* * reschedule next event, since we can not do it from the activity * routine. */ event_schedule(EVENT_DPD, nextdelay, st); } if (!p1st->st_dpd_seqno) { /* Get a non-zero random value that has room to grow */ get_rnd_bytes((u_char *)&p1st->st_dpd_seqno , sizeof(p1st->st_dpd_seqno)); p1st->st_dpd_seqno &= 0x7fff; p1st->st_dpd_seqno++; } seqno = htonl(p1st->st_dpd_seqno); /* make sure that the timeout occurs. We do this before the send, * because the send may fail due to network issues, etc, and * the timeout has to occur anyway */ dpd_sched_timeout(p1st, tm, timeout); DBG(DBG_DPD, DBG_log("sending R_U_THERE %u to %s:%d (state #%lu)" , seqno , ip_str(&p1st->st_remoteaddr) , p1st->st_remoteport , p1st->st_serialno)); if (send_isakmp_notification(p1st, R_U_THERE , &seqno, sizeof(seqno)) != STF_IGNORE) { loglog(RC_LOG_SERIOUS, "DPD Error: could not send R_U_THERE"); return; } st->st_last_dpd = tm; p1st->st_last_dpd = tm; p1st->st_dpd_expectseqno = p1st->st_dpd_seqno++; }
static void klips_process_raw_ifaces(struct raw_iface *rifaces) { struct raw_iface *ifp; /* Find all virtual/real interface pairs. * For each real interface... */ for (ifp = rifaces; ifp != NULL; ifp = ifp->next) { struct raw_iface *v = NULL; /* matching ipsecX interface */ struct raw_iface fake_v; bool after = FALSE; /* has vfp passed ifp on the list? */ bool bad = FALSE; struct raw_iface *vfp; ip_address lip; if (pluto_listen) { err_t e; e = ttoaddr(pluto_listen, 0, 0, &lip); if (e) { DBG_log("invalid listen= option ignored: %s\n", e); pluto_listen = NULL; } } /* ignore if virtual (ipsec*) interface */ if (strncmp(ifp->name, IPSECDEVPREFIX, sizeof(IPSECDEVPREFIX) - 1) == 0) continue; /* ignore if virtual (mast*) interface */ if (strncmp(ifp->name, MASTDEVPREFIX, sizeof(MASTDEVPREFIX) - 1) == 0) continue; for (vfp = rifaces; vfp != NULL; vfp = vfp->next) { if (vfp == ifp) { after = TRUE; } else if (sameaddr(&ifp->addr, &vfp->addr)) { /* Different entries with matching IP addresses. * Many interesting cases. */ if (strncmp(vfp->name, IPSECDEVPREFIX, sizeof(IPSECDEVPREFIX) - 1) == 0) { if (v != NULL) { loglog(RC_LOG_SERIOUS, "ipsec interfaces %s and %s share same address %s", v->name, vfp->name, ip_str(&ifp->addr)); bad = TRUE; } else { v = vfp; /* current winner */ } } else { /* ugh: a second real interface with the same IP address * "after" allows us to avoid double reporting. */ #if defined(linux) && defined(NETKEY_SUPPORT) if (kern_interface == USE_NETKEY) { if (after) { bad = TRUE; break; } continue; } #endif if (after) { loglog(RC_LOG_SERIOUS, "IP interfaces %s and %s share address %s!", ifp->name, vfp->name, ip_str(&ifp->addr)); } bad = TRUE; } } } if (bad) continue; #if defined(linux) && defined(NETKEY_SUPPORT) if (kern_interface == USE_NETKEY) { v = ifp; goto add_entry; } #endif /* what if we didn't find a virtual interface? */ if (v == NULL) { if (kern_interface == NO_KERNEL) { /* kludge for testing: invent a virtual device */ static const char fvp[] = "virtual"; fake_v = *ifp; passert(sizeof(fake_v.name) > sizeof(fvp)); strcpy(fake_v.name, fvp); addrtot(&ifp->addr, 0, fake_v.name + sizeof(fvp) - 1, sizeof(fake_v.name) - (sizeof(fvp) - 1)); v = &fake_v; } else { DBG(DBG_CONTROL, DBG_log( "IP interface %s %s has no matching ipsec* interface -- ignored", ifp->name, ip_str(&ifp->addr))); continue; } } /* ignore if --listen is specified and we do not match */ if (pluto_listen != NULL) { if (!sameaddr(&lip, &ifp->addr)) { libreswan_log("skipping interface %s with %s", ifp->name, ip_str(&ifp->addr)); continue; } } /* We've got all we need; see if this is a new thing: * search old interfaces list. */ #if defined(linux) && defined(NETKEY_SUPPORT) add_entry: #endif { struct iface_port **p = &interfaces; for (;; ) { struct iface_port *q = *p; struct iface_dev *id = NULL; /* search is over if at end of list */ if (q == NULL) { /* matches nothing -- create a new entry */ int fd = create_socket(ifp, v->name, pluto_port); if (fd < 0) break; DBG(DBG_NATT, DBG_log( "NAT-T KLIPS: checking for nat_traversal_support_non_ike for IPv4")); if (nat_traversal_support_non_ike && addrtypeof(&ifp->addr) == AF_INET) { DBG(DBG_NATT, DBG_log( "NAT-T KLIPS: found, calling nat_traversal_espinudp_socket")); nat_traversal_espinudp_socket( fd, "IPv4", ESPINUDP_WITH_NON_IKE); } else { DBG(DBG_NATT, DBG_log( "NAT-T KLIPS: support not found, nat_traversal_support_non_ike = %s", nat_traversal_support_non_ike ? "TRUE" : "FALSE")); } q = alloc_thing(struct iface_port, "struct iface_port"); id = alloc_thing(struct iface_dev, "struct iface_dev"); LIST_INSERT_HEAD(&interface_dev, id, id_entry); q->ip_dev = id; id->id_rname = clone_str(ifp->name, "real device name"); id->id_vname = clone_str(v->name, "virtual device name klips"); id->id_count++; q->ip_addr = ifp->addr; q->fd = fd; q->next = interfaces; q->change = IFN_ADD; q->port = pluto_port; q->ike_float = FALSE; interfaces = q; libreswan_log( "adding interface %s/%s %s:%d", q->ip_dev->id_vname, q->ip_dev->id_rname, ip_str(&q->ip_addr), q->port); /* * right now, we do not support NAT-T on IPv6, because * the kernel did not support it, and gave an error * it one tried to turn it on. */ if (nat_traversal_support_port_floating && addrtypeof(&ifp->addr) == AF_INET) { DBG(DBG_NATT, DBG_log( "NAT-T KLIPS: found floating port, calling nat_traversal_espinudp_socket")); fd = create_socket(ifp, v->name, pluto_natt_float_port); if (fd < 0) break; nat_traversal_espinudp_socket( fd, "IPv4", ESPINUDP_WITH_NON_ESP); q = alloc_thing( struct iface_port, "struct iface_port"); q->ip_dev = id; id->id_count++; q->ip_addr = ifp->addr; setportof(htons( pluto_natt_float_port), &q->ip_addr); q->port = pluto_natt_float_port; q->fd = fd; q->next = interfaces; q->change = IFN_ADD; q->ike_float = TRUE; interfaces = q; libreswan_log( "adding interface %s/%s %s:%d", q->ip_dev->id_vname, q->ip_dev->id_rname, ip_str(&q-> ip_addr), q->port); } break; } /* search over if matching old entry found */ if (streq(q->ip_dev->id_rname, ifp->name) && streq(q->ip_dev->id_vname, v->name) && sameaddr(&q->ip_addr, &ifp->addr)) { /* matches -- rejuvinate old entry */ q->change = IFN_KEEP; /* look for other interfaces to keep (due to NAT-T) */ for (q = q->next; q; q = q->next) { if (streq(q->ip_dev->id_rname, ifp->name) && streq(q->ip_dev->id_vname, v->name) && sameaddr(&q->ip_addr, &ifp->addr)) q->change = IFN_KEEP; } break; } /* try again */ p = &q->next; } /* for (;;) */ }
void recv_pcap_packet_gen(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes) { struct msg_digest *md; u_int32_t *dlt; struct iphdr *ip; struct udphdr *udp; u_char *ike; const struct iface_port *ifp = &if1; int packet_len; err_t from_ugh; union { struct sockaddr sa; struct sockaddr_in sa_in4; struct sockaddr_in6 sa_in6; } from; md = alloc_md(); dlt = (u_int32_t *)bytes; if (*dlt != PF_INET) return; ip = (struct iphdr *)(dlt + 1); udp = (struct udphdr *)(dlt + ip->ihl + 1); ike = (u_char *)(udp + 1); from.sa_in4.sin_addr.s_addr = ip->saddr; from.sa_in4.sin_port = udp->source; md->iface = ifp; packet_len = h->len - (ike - bytes); happy(anyaddr(addrtypeof(&ifp->ip_addr), &md->sender)); from_ugh = initaddr((void *) &from.sa_in4.sin_addr, sizeof(from.sa_in4.sin_addr), AF_INET, &md->sender); setportof(from.sa_in4.sin_port, &md->sender); md->sender_port = ntohs(from.sa_in4.sin_port); cur_from = &md->sender; cur_from_port = md->sender_port; /* Clone actual message contents * and set up md->packet_pbs to describe it. */ init_pbs(&md->packet_pbs, clone_bytes(ike, packet_len, "message buffer in comm_handle()"), packet_len, "packet"); DBG_log("*received %d bytes from %s:%u on %s (port=%d)", (int) pbs_room(&md->packet_pbs), ip_str(&md->sender), (unsigned) md->sender_port, ifp->ip_dev->id_rname, ifp->port); DBG_dump("", md->packet_pbs.start, pbs_room(&md->packet_pbs)); process_packet(&md); if (md != NULL) release_md(md); cur_state = NULL; reset_cur_connection(); cur_from = NULL; }
struct raw_iface * find_raw_ifaces4(void) { static const int on = TRUE; /* by-reference parameter; constant, we hope */ int j; /* index into buf */ static int num=64; /* number of interfaces */ struct ifconf ifconf; struct ifreq *buf; /* for list of interfaces -- arbitrary limit */ struct raw_iface *rifaces = NULL; int master_sock = safe_socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); /* Get a UDP socket */ /* get list of interfaces with assigned IPv4 addresses from system */ if (master_sock == -1) exit_log_errno((e, "socket() failed in find_raw_ifaces4()")); if (setsockopt(master_sock, SOL_SOCKET, SO_REUSEADDR , (const void *)&on, sizeof(on)) < 0) exit_log_errno((e, "setsockopt() in find_raw_ifaces4()")); /* bind the socket */ { ip_address any; happy(anyaddr(AF_INET, &any)); setportof(htons(pluto_port), &any); if (bind(master_sock, sockaddrof(&any), sockaddrlenof(&any)) < 0) exit_log_errno((e, "bind() failed in find_raw_ifaces4()")); } buf = NULL; /* a million interfaces is probably the maximum, ever... */ while(num < (1024*1024)) { /* Get local interfaces. See netdevice(7). */ ifconf.ifc_len = num * sizeof(struct ifreq); buf = (void *) realloc(buf, ifconf.ifc_len); if (!buf) exit_log_errno((e, "realloc of %d in find_raw_ifaces4()", ifconf.ifc_len)); memset(buf, 0, num*sizeof(struct ifreq)); ifconf.ifc_buf = (void *) buf; if (ioctl(master_sock, SIOCGIFCONF, &ifconf) == -1) exit_log_errno((e, "ioctl(SIOCGIFCONF) in find_raw_ifaces4()")); /* if we got back less than we asked for, we have them all */ if (ifconf.ifc_len < (int)(sizeof(struct ifreq) * num)) break; /* try again and ask for more this time */ num *= 2; } /* Add an entry to rifaces for each interesting interface. */ for (j = 0; (j+1) * sizeof(struct ifreq) <= (size_t)ifconf.ifc_len; j++) { struct raw_iface ri; const struct sockaddr_in *rs = (struct sockaddr_in *) &buf[j].ifr_addr; struct ifreq auxinfo; /* ignore all but AF_INET interfaces */ if (rs->sin_family != AF_INET) continue; /* not interesting */ /* build a NUL-terminated copy of the rname field */ memcpy(ri.name, buf[j].ifr_name, IFNAMSIZ); ri.name[IFNAMSIZ] = '\0'; /* ignore if our interface names were specified, and this isn't one */ if (pluto_ifn_roof != 0) { int i; for (i = 0; i != pluto_ifn_roof; i++) if (streq(ri.name, pluto_ifn[i])) break; if (i == pluto_ifn_roof) continue; /* not found -- skip */ } /* Find out stuff about this interface. See netdevice(7). */ zero(&auxinfo); /* paranoia */ memcpy(auxinfo.ifr_name, buf[j].ifr_name, IFNAMSIZ); if (ioctl(master_sock, SIOCGIFFLAGS, &auxinfo) == -1) exit_log_errno((e , "ioctl(SIOCGIFFLAGS) for %s in find_raw_ifaces4()" , ri.name)); if (!(auxinfo.ifr_flags & IFF_UP)) { DBG(DBG_CONTROL, DBG_log("Ignored interface %s - it is not up" , ri.name)); continue; /* ignore an interface that isn't UP */ } if (auxinfo.ifr_flags & IFF_SLAVE) { DBG(DBG_CONTROL, DBG_log("Ignored interface %s - it is a slave interface" , ri.name)); continue; /* ignore slave interfaces; they share IPs with their master */ } /* ignore unconfigured interfaces */ if (rs->sin_addr.s_addr == 0) { DBG(DBG_CONTROL, DBG_log("Ignored interface %s - it is unconfigured" , ri.name)); continue; } happy(initaddr((const void *)&rs->sin_addr, sizeof(struct in_addr) , AF_INET, &ri.addr)); DBG(DBG_CONTROL, DBG_log("found %s with address %s" , ri.name, ip_str(&ri.addr))); ri.next = rifaces; rifaces = clone_thing(ri, "struct raw_iface"); } close(master_sock); return rifaces; }
/***************************************************************************************** 函数名称: dpd_outI 功能描述: dpd 处理事件,当dpd第一次触发时添加一个超时事件, 如果是重传的dpd处理,不添加超时事件。 输入参数: p1st 输出参数: 无 返 回 值: 无 ------------------------------------------------------------------------------------------- 最近一次修改记录 : 修改作者: 王之云 修改目的: dpd_outI处理 修改日期: 2012年3月20日 ********************************************************************************************/ static void dpd_outI(struct state *p1st) { time_t tm; u_int32_t seqno; time_t timeout = p1st->st_connection->dpd_timeout; EV_ADD ev_arg; DBG(DBG_DPD, DBG_log("processing dpd for state #%lu (\"%s\")" , p1st->st_serialno , p1st->st_connection->name)); /* If no DPD, then get out of here */ if (!p1st->hidden_variables.st_dpd) return; /* If there is no state, there can be no DPD */ if (!IS_ISAKMP_SA_ESTABLISHED(p1st->st_state)) return; /* find out when now is */ tm = now(); if (!p1st->st_dpd_seqno) { p1st->st_dpd_seqno = dpd_seqno++; } seqno = htonl(p1st->st_dpd_seqno); /* 如果dpd事件为空,添加dpd事件 */ if(p1st->st_dpd_event == NULL) { ev_arg.u.st = p1st; event_schedule(EVENT_DPD, p1st->st_connection->dpd_delay, &ev_arg); } /* 如果dpd超时事件为空,添加dpd超时事件 */ if(p1st->st_dpd_timeout_event == NULL) { passert(timeout > 0); ev_arg.u.st = p1st; event_schedule(EVENT_DPD_TIMEOUT, timeout, &ev_arg); } DBG(DBG_DPD, DBG_log("sending R_U_THERE %u to %s:%d (state #%lu)" , seqno , ip_str(&p1st->st_remoteaddr) , p1st->st_remoteport , p1st->st_serialno)); if(p1st->hidden_variables.st_is_dp_dev && p1st->st_connection->modecfg_quick_dpd ) { if (send_quick_isakmp_notification(p1st, R_U_THERE, &seqno, sizeof(seqno)) != STF_IGNORE) { IPSEC_log(IPSEC_LOGLEVEL_PRIVATE, "QUICK DPD Error: connection(%s) send R_U_THERE error\n", p1st->st_connection->name); return; } } else { if (send_isakmp_notification(p1st, R_U_THERE, &seqno, sizeof(seqno)) != STF_IGNORE) { IPSEC_log(IPSEC_LOGLEVEL_PRIVATE, "DPD Error: connection(%s) send R_U_THERE error\n", p1st->st_connection->name); return; } } p1st->st_dpd_expectseqno = p1st->st_dpd_seqno++; }
void send_v2_notification(struct state *p1st, u_int16_t type , struct state *encst , u_char *icookie , u_char *rcookie , chunk_t *n_data) { u_char buffer[1024]; pb_stream reply; pb_stream rbody; /* this function is not generic enough yet just enough for 6msg * TBD accept HDR FLAGS as arg. default ISAKMP_FLAGS_R * TBD when there is a child SA use that SPI in the notify paylod. * TBD support encrypted notifications payloads. * TBD accept Critical bit as an argument. default is set. * TBD accept exchange type as an arg, default is ISAKMP_v2_SA_INIT * do we need to send a notify with empty data? * do we need to support more Protocol ID? more than PROTO_ISAKMP */ IPSEC_dbg("sending %snotification %s to %s:%u" , encst ? "encrypted " : "" , enum_name(&ikev2_notify_names, type) , ip_str(&p1st->st_remoteaddr) , p1st->st_remoteport); if(n_data == NULL) { DBG(DBG_CONTROLMORE, DBG_log("don't send packet when notification data empty")); return; } memset(buffer, 0, sizeof(buffer)); init_pbs(&reply, buffer, sizeof(buffer), "notification msg"); /* HDR out */ { struct isakmp_hdr n_hdr ; zero(&n_hdr); /* default to 0 */ /* AAA should we copy from MD? */ n_hdr.isa_version = IKEv2_MAJOR_VERSION << ISA_MAJ_SHIFT | IKEv2_MINOR_VERSION; memcpy(n_hdr.isa_rcookie, rcookie, COOKIE_SIZE); memcpy(n_hdr.isa_icookie, icookie, COOKIE_SIZE); n_hdr.isa_xchg = ISAKMP_v2_SA_INIT; n_hdr.isa_np = ISAKMP_NEXT_v2N; n_hdr.isa_flags &= ~ISAKMP_FLAGS_I; n_hdr.isa_flags |= ISAKMP_FLAGS_R; if (!out_struct(&n_hdr, &isakmp_hdr_desc, &reply, &rbody)) { IPSEC_dbg("error initializing hdr for notify message"); return; } } chunk_t child_spi; child_spi.ptr = NULL; child_spi.len = 0; /* build and add v2N payload to the packet */ ship_v2N (ISAKMP_NEXT_NONE, ISAKMP_PAYLOAD_CRITICAL, PROTO_ISAKMP, &child_spi,type, n_data, &rbody); close_message(&rbody); close_output_pbs(&reply); clonetochunk(p1st->st_tpacket, reply.start, pbs_offset(&reply) , "notification packet"); ipsec_child_send_packet(p1st, "notification", TRUE); }
int main(int argc, char**argv) { kprintf("Started Client"); //Iterator int i; //host file string hostfile; //Server Port number int server_port; //string server port string server_port_str; //Command file string commandfile; //client id int client_id; //Map to store Hostname to IP address map<string,string> host_to_id; //Map to store Hostname to id map<string,int> hostname_to_id; //current update int current_update; if(argc < 9) { kprintf("Usage: "); kprintf("client -s server_port -f command_file -i client_id -h host_file"); exit(EXIT_FAILURE); } for(i=0; i<argc; i++) { string arg = argv[i]; if(arg == "-s") { if(argv[i+1]) { string temp = argv[i+1]; server_port_str = temp; server_port = atoi(temp.c_str()); if(server_port == 0 || (server_port <=1024 || server_port >= 65535)) { kprintf("Invalid port. Please specify a server port greater than 1024"); exit(EXIT_FAILURE); } i = i + 1; } else { kprintf("Please specify server port number"); exit(EXIT_FAILURE); } } if(arg == "-f") { if(argv[i+1]) { commandfile = argv[i+1]; i = i + 1; } else { kprintf("Please specify command file"); exit(EXIT_FAILURE); } } if(arg == "-i") { if(argv[i+1]) { string temp = argv[i+1]; client_id = atoi(temp.c_str()); i = i + 1; } else { kprintf("Please specify client id"); exit(EXIT_FAILURE); } } if(arg == "-h") { if(argv[i+1]) { hostfile = argv[i+1]; i = i + 1; } else { kprintf("Please specify host file"); exit(EXIT_FAILURE); } } } kprintf("Client ID: ",client_id); kprintf("Command file: ",commandfile); kprintf("Server Port: ",server_port); ifstream hostfile_stream(hostfile.c_str()); string hostname,hostname_split; int host_id = 0; if(hostfile_stream.is_open()) { while(hostfile_stream.good()) { getline(hostfile_stream,hostname); if(!hostname.empty()) { hostname_to_id[hostname] = host_id; kprintf(hostname.c_str(),hostname_to_id[hostname]); host_id = host_id + 1; } } } //Socket variables for client int client_sockfd,n; struct sockaddr_in servaddr; socklen_t lensock; client_sockfd = socket(AF_INET,SOCK_STREAM,0); if(client_sockfd == -1) { kprintf("Could not create socket"); exit(EXIT_FAILURE); } ifstream commandfile_stream(commandfile.c_str()); if(commandfile_stream.is_open()) { while(commandfile_stream.good()) { getline(commandfile_stream,hostname); unsigned pos = hostname.find(" "); if(pos != -1) { string update = hostname.substr(pos+1); int update_length = update.length(); int hostname_length = hostname.length(); hostname_split = hostname.substr(0,hostname_length-update_length-1); kprintf("hostname",hostname_split.length()); kprintf("update",update); if(!hostname_split.empty()) { //Get IP address struct hostent *hp; hp = gethostbyname(hostname_split.c_str()); if(!hp) { kprintf(" not found ",hostname_split); exit(EXIT_FAILURE); } if((inet_ntoa(*(struct in_addr *)hp->h_addr_list[0]))) { string s_local(inet_ntoa(*(struct in_addr *)hp->h_addr_list[0])); kprintf(s_local.c_str()); if(s_local.find("127") != 0) { host_to_id[hostname_split] = s_local; } else { host_to_id[hostname_split] = string(inet_ntoa(*(struct in_addr *)hp->h_addr_list[1])); } } else { host_to_id[hostname_split] = string(inet_ntoa(*(struct in_addr *)hp->h_addr_list[1])); } kprintf(hostname_split.c_str()); } kprintf("Sending update to: ",host_to_id[hostname_split]); //send update struct addrinfo hints,*res,*p; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; getaddrinfo(hostname_split.c_str(),server_port_str.c_str(),&hints,&res); char ipstr[INET6_ADDRSTRLEN]; for(p=res; p!=NULL; p= p->ai_next) { void *addr; char *ipver; if(p->ai_family == AF_INET) { struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; addr = &(ipv4->sin_addr); } inet_ntop(p->ai_family,addr,ipstr,sizeof(ipstr)); string ip_str(ipstr); if(ip_str.find("127") == string::npos) { res->ai_addr = p->ai_addr; res->ai_addrlen = p->ai_addrlen; } printf(" %s:\n",ipstr); } int rval = connect(client_sockfd,res->ai_addr,res->ai_addrlen); if(rval==-1) { kprintf("Connect failed skipping update"); } fprintf(stderr,"%d: Sending update %s to server %s\n",client_id,update.c_str(),hostname_split.c_str()); Client_Update* cli_update = (Client_Update*)malloc(sizeof(Client_Update)); cli_update->type = TYPE_CLI_UPDATE; cli_update->client_id = client_id; cli_update->server_id = hostname_to_id[hostname_split]; kprintf("Server id: ",cli_update->server_id); cli_update->timestamp = host_id; cli_update->update = atoi(update.c_str()); host_network(cli_update); rval = send(client_sockfd,cli_update,sizeof(Client_Update),0); if(rval == -1) { kprintf("Send failed"); fprintf(stderr,"%d: Unable to send update %s to server %s\n",client_id,update.c_str(),hostname_split.c_str()); } network_host(cli_update); char recv_buff[CLIENT_UPDATE_LEN]; rval = recv(client_sockfd,recv_buff,CLIENT_UPDATE_LEN,0); kprintf("Recieved",rval); if(rval == 0) fprintf(stderr,"%d: Connection for update %s is closed by server %s\n",client_id,update.c_str(),hostname_split.c_str()); else if(rval == -1) { kprintf("Recv failed"); } else { fprintf(stderr,"%d: Update %s sent to server %d is executed\n",client_id,update.c_str(),cli_update->server_id); } } close(client_sockfd); } commandfile_stream.close(); } else { kprintf("Unable to read host file"); exit(EXIT_FAILURE); } }
void gdginp ( GDDIAG_input *ui, int *iret ) /************************************************************************ * gdginp * * * * This subroutine gets the input parameters for GDDIAG. * * * * gdginp ( ui, iret ) * ** * * Log: * * M. Goodman/RDS 10/85 * * M. desJardins/GSFC 8/88 * * M. Li/SAIC 04/04 Added grdhdr * * R. Tian/SAIC 1/05 Added parameters for creating file * * R. Tian/SAIC 9/06 Recoded from Fortran * ************************************************************************/ { int ier1, ier2, ier3, ier4, ier5, ier6, ier7, ier8, ier9, ier10, ier11, ier12, ier13, ier14, ier15, ier16, len; /*----------------------------------------------------------------------*/ *iret = 0; /* * Get user input. */ ip_str ( "GDFILE", ui->gdfile, &ier1, strlen("GDFILE"), sizeof(ui->gdfile) ); ip_str ( "GDOUTF", ui->gdoutf, &ier2, strlen("GDOUTF"), sizeof(ui->gdoutf) ); ip_str ( "GDATTIM", ui->gdatim, &ier3, strlen("GDATTIM"), sizeof(ui->gdatim) ); ip_str ( "GLEVEL", ui->glevel, &ier4, strlen("GLEVEL"), sizeof(ui->glevel) ); ip_str ( "GVCORD", ui->gvcord, &ier5, strlen("GVCORD"), sizeof(ui->gvcord) ); ip_str ( "GFUNC", ui->gfunc, &ier6, strlen("GFUNC"), sizeof(ui->gfunc) ); ip_str ( "GRDNAM", ui->grdnam, &ier7, strlen("GRDNAM"), sizeof(ui->grdnam) ); ip_str ( "GRDTYP", ui->grdtyp, &ier8, strlen("GRDTYP"), sizeof(ui->grdtyp) ); ip_str ( "GPACK", ui->gpack, &ier9, strlen("GPACK"), sizeof(ui->gpack) ); ip_str ( "GRDHDR", ui->grdhdr, &ier10, strlen("GRDHDR"), sizeof(ui->grdhdr) ); ip_str ( "PROJ", ui->proj, &ier11, strlen("PROJ"), sizeof(ui->proj) ); ip_str ( "GRDAREA", ui->gdarea, &ier12, strlen("GRDAREA"), sizeof(ui->gdarea) ); ip_str ( "KXKY", ui->kxky, &ier13, strlen("KXKY"), sizeof(ui->kxky) ); ip_str ( "MAXGRD", ui->maxgrd, &ier14, strlen("MAXGRD"), sizeof(ui->maxgrd) ); ip_str ( "CPYFIL", ui->cpyfil, &ier15, strlen("CPYFIL"), sizeof(ui->cpyfil) ); ip_str ( "ANLYSS", ui->anlyss, &ier16, strlen("ANLYSS"), sizeof(ui->anlyss) ); /* * Fortran string to C string. */ st_null ( ui->gdfile, ui->gdfile, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->gdoutf, ui->gdoutf, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->gdatim, ui->gdatim, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->glevel, ui->glevel, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->gvcord, ui->gvcord, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->gfunc, ui->gfunc, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->grdnam, ui->grdnam, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->grdtyp, ui->grdtyp, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->gpack, ui->gpack, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->grdhdr, ui->grdhdr, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->proj, ui->proj, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->gdarea, ui->gdarea, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->kxky, ui->kxky, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->maxgrd, ui->maxgrd, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->cpyfil, ui->cpyfil, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->anlyss, ui->anlyss, &len, iret, LLMXLN, LLMXLN ); /* * Check for return code. */ *iret = ier1 + ier2 + ier3 + ier4 + ier5 + ier6 + ier7 + ier8 + ier9 + ier10 + ier11 + ier12 + ier13 + ier14 + ier15 + ier16; if ( *iret != 0 ) *iret = -2; return; }
void gdlinp ( GDLIST_input *ui, int *iret ) /************************************************************************ * gdlinp * * * * This subroutine gets the input parameters for GDLIST from the TAE. * * * * gdlinp ( UI, IRET ) * * * * Output parameters: * * *ui GDLIST_input User input struct * * *iret int Return code * ** * * Log: * * M. desJardins/GSFC 2/85 * * M. Goodman/RDS 11/85 Replaced GPARM with GFUNC * * M. Goodman/RDS 11/85 Added SCALE * * M. desJardins/GSFC 6/88 Replaced area with garea & proj * * R. Tian/SAIC 9/06 Recoded from Fortran * ************************************************************************/ { int ier1, ier2, ier3, ier4, ier5, ier6, ier7, ier8, ier9, len; /*----------------------------------------------------------------------*/ *iret = 0; /* * Get user input. */ ip_str ( "GDFILE", ui->gdfile, &ier1, strlen("GDFILE"), sizeof(ui->gdfile) ); ip_str ( "GDATTIM", ui->gdatim, &ier2, strlen("GDATTIM"), sizeof(ui->gdatim) ); ip_str ( "GLEVEL", ui->glevel, &ier3, strlen("GLEVEL"), sizeof(ui->glevel) ); ip_str ( "GVCORD", ui->gvcord, &ier4, strlen("GVCORD"), sizeof(ui->gvcord) ); ip_str ( "GFUNC", ui->gfunc, &ier5, strlen("GFUNC"), sizeof(ui->gfunc) ); ip_str ( "GAREA", ui->garea, &ier6, strlen("GAREA"), sizeof(ui->garea) ); ip_str ( "PROJ", ui->proj, &ier7, strlen("PROJ"), sizeof(ui->proj) ); ip_str ( "SCALE", ui->scale, &ier8, strlen("SCALE"), sizeof(ui->scale) ); ip_str ( "OUTPUT", ui->output, &ier9, strlen("OUTPUT"), sizeof(ui->output) ); /* * Fortran string to C string. */ st_null ( ui->gdfile, ui->gdfile, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->gdatim, ui->gdatim, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->glevel, ui->glevel, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->gvcord, ui->gvcord, &len, iret, LLMXLN, LLMXLN ); st_null ( ui->gfunc , ui->gfunc , &len, iret, LLMXLN, LLMXLN ); st_null ( ui->garea , ui->garea , &len, iret, LLMXLN, LLMXLN ); st_null ( ui->proj , ui->proj , &len, iret, LLMXLN, LLMXLN ); st_null ( ui->scale , ui->scale , &len, iret, LLMXLN, LLMXLN ); st_null ( ui->output, ui->output, &len, iret, LLMXLN, LLMXLN ); /* * Check for return code. */ *iret = ier1 + ier2 + ier3 + ier4 + ier5 + ier6 + ier7 + ier8 + ier9; if ( *iret != 0 ) *iret = -2; return; }