static bool imap_remote_ip_is_usable(const struct ip_addr *ip) { unsigned int addr; if (ip->family == 0) return FALSE; if (ip->family == AF_INET) { #define IP4(a,b,c,d) ((unsigned)(a)<<24|(unsigned)(b)<<16|(unsigned)(c)<<8|(unsigned)(d)) addr = ip->u.ip4.s_addr; if (addr >= IP4(10,0,0,0) && addr <= IP4(10,255,255,255)) return FALSE; /* 10/8 */ if (addr >= IP4(192,168,0,0) && addr <= IP4(192,168,255,255)) return FALSE; /* 192.168/16 */ if (addr >= IP4(172,16,0,0) && addr <= IP4(172,31,255,255)) return FALSE; /* 172.16/12 */ if (addr >= IP4(127,0,0,0) && addr <= IP4(127,255,255,255)) return FALSE; /* 127/8 */ #undef IP4 } #ifdef HAVE_IPV6 else if (ip->family == AF_INET6) { addr = ip->u.ip6.s6_addr[0]; if (addr == 0xfc || addr == 0xfd) return FALSE; /* fc00::/7 */ } #endif return TRUE; }
int send_ip4_udp_local_ip(int fd) { struct ofp_sockaddr_in addr = {0}; const char *buf = "socket_test"; addr.sin_len = sizeof(struct ofp_sockaddr_in); addr.sin_family = OFP_AF_INET; addr.sin_port = odp_cpu_to_be_16(TEST_PORT + 1); addr.sin_addr.s_addr = IP4(192, 168, 100, 1); if (ofp_connect(fd, (const struct ofp_sockaddr *)&addr, sizeof(struct ofp_sockaddr_in)) == -1) { OFP_ERR("Faild to connect socket (errno = %d)\n", ofp_errno); return -1; } if (ofp_send(fd, buf, strlen(buf), 0) == -1) { OFP_ERR("Faild to send data(errno = %d)\n", ofp_errno); return -1; } OFP_INFO("Data (%s) sent successfully.\n", buf); OFP_INFO("SUCCESS.\n"); return 0; }
int init_udp_bind_local_ip(int *pfd_thread1, int *pfd_thread2) { struct ofp_sockaddr_in addr = {0}; *pfd_thread1 = ofp_socket(OFP_AF_INET, OFP_SOCK_DGRAM, OFP_IPPROTO_UDP); if (*pfd_thread1 == -1) { OFP_ERR("Faild to create SEND socket (errno = %d)\n", ofp_errno); return -1; } addr.sin_len = sizeof(struct ofp_sockaddr_in); addr.sin_family = OFP_AF_INET; addr.sin_port = odp_cpu_to_be_16(TEST_PORT); addr.sin_addr.s_addr = IP4(192, 168, 100, 1); if (ofp_bind(*pfd_thread1, (const struct ofp_sockaddr *)&addr, sizeof(struct ofp_sockaddr_in)) == -1) { OFP_ERR("Faild to bind socket (errno = %d)\n", ofp_errno); return -1; } *pfd_thread2 = ofp_socket(OFP_AF_INET, OFP_SOCK_DGRAM, OFP_IPPROTO_UDP); if (*pfd_thread2 == -1) { OFP_ERR("Faild to create RCV socket (errno = %d)\n", ofp_errno); return -1; } addr.sin_len = sizeof(struct ofp_sockaddr_in); addr.sin_family = OFP_AF_INET; addr.sin_port = odp_cpu_to_be_16(TEST_PORT + 1); addr.sin_addr.s_addr = IP4(192, 168, 100, 1); if (ofp_bind(*pfd_thread2, (const struct ofp_sockaddr *)&addr, sizeof(struct ofp_sockaddr_in)) == -1) { OFP_ERR("Faild to bind socket (errno = %d)\n", ofp_errno); return -1; } return 0; }
static void app_processing(void) { int fd_rcv = -1; char buf[1500]; int len = sizeof(buf); struct ofp_sockaddr_in addr = {0}; fd_rcv = ofp_socket(OFP_AF_INET, OFP_SOCK_DGRAM, OFP_IPPROTO_UDP); if (fd_rcv == -1) { OFP_ERR("Faild to create RCV socket (errno = %d)\n", ofp_errno); return; } addr.sin_len = sizeof(struct ofp_sockaddr_in); addr.sin_family = OFP_AF_INET; addr.sin_port = odp_cpu_to_be_16(TEST_PORT); addr.sin_addr.s_addr = IP4(192, 168, 100, 1); if (ofp_bind(fd_rcv, (const struct ofp_sockaddr *)&addr, sizeof(struct ofp_sockaddr_in)) == -1) { OFP_ERR("Faild to bind socket (errno = %d)\n", ofp_errno); return; } do { len = ofp_recv(fd_rcv, buf, len, 0); if (len == -1) { OFP_ERR("Faild to receive data (errno = %d)\n", ofp_errno); break; } OFP_INFO("Data received: length = %d.\n", len); } while (1); if (fd_rcv != -1) { ofp_close(fd_rcv); fd_rcv = -1; } OFP_INFO("Test ended.\n"); }
static int nfq_daq_set_filter (void* handle, const char* filter) { NfqImpl* impl = (NfqImpl*)handle; struct sfbpf_program fcode; int dlt = IP4(impl) ? DLT_IPV4 : DLT_IPV6; if (sfbpf_compile(impl->snaplen, dlt, &fcode, filter, 1, 0) < 0) { DPE(impl->error, "%s: failed to compile bpf '%s'", __FUNCTION__, filter); return DAQ_ERROR; } if ( impl->filter ) free((void *)impl->filter); if ( impl->fcode.bf_insns ) free(impl->fcode.bf_insns); impl->filter = strdup(filter); impl->fcode = fcode; return DAQ_SUCCESS; }
static int nfq_daq_initialize ( const DAQ_Config_t* cfg, void** handle, char* errBuf, size_t errMax) { if(cfg->name && *(cfg->name)) { snprintf(errBuf, errMax, "The nfq DAQ module does not support interface or readback mode!"); return DAQ_ERROR_INVAL; } // setup internal stuff NfqImpl *impl = calloc(1, sizeof(*impl)); if ( !impl ) { snprintf(errBuf, errMax, "%s: failed to allocate nfq context\n", __FUNCTION__); return DAQ_ERROR_NOMEM; } if ( nfq_daq_get_setup(impl, cfg, errBuf, errMax) != DAQ_SUCCESS ) { nfq_daq_shutdown(impl); return DAQ_ERROR; } if ( (impl->buf = malloc(MSG_BUF_SIZE)) == NULL ) { snprintf(errBuf, errMax, "%s: failed to allocate nfq buffer\n", __FUNCTION__); nfq_daq_shutdown(impl); return DAQ_ERROR_NOMEM; } // setup input stuff // 1. get a new q handle if ( !(impl->nf_handle = nfq_open()) ) { snprintf(errBuf, errMax, "%s: failed to get handle for nfq\n", __FUNCTION__); nfq_daq_shutdown(impl); return DAQ_ERROR; } // 2. now use the new q handle to rip the rug out from other // nfq users / handles? actually that doesn't seem to // happen which is good, but then why is this *supposed* // to be necessary? especially since we haven't bound to // a qid yet, and that is exclusive anyway. if ( (IP4(impl) && nfq_unbind_pf(impl->nf_handle, PF_INET) < 0) || (IP6(impl) && nfq_unbind_pf(impl->nf_handle, PF_INET6) < 0) ) { snprintf(errBuf, errMax, "%s: failed to unbind protocols for nfq\n", __FUNCTION__); //nfq_daq_shutdown(impl); //return DAQ_ERROR; } // 3. select protocols for the q handle // this is necessary but insufficient because we still // must configure iptables externally, eg: // // iptables -A OUTPUT -p icmp -j NFQUEUE [--queue-num <#>] // (# defaults to 0). // // :( iptables rules should be managed automatically to avoid // queueing packets to nowhere or waiting for packets that // will never come. (ie this bind should take the -p, -s, // etc args you can pass to iptables and create the dang // rule!) if ( (IP4(impl) && nfq_bind_pf(impl->nf_handle, PF_INET) < 0) || (IP6(impl) && nfq_bind_pf(impl->nf_handle, PF_INET6) < 0) ) { snprintf(errBuf, errMax, "%s: failed to bind protocols for nfq\n", __FUNCTION__); nfq_daq_shutdown(impl); return DAQ_ERROR; } // 4. bind to/allocate the specified nfqueue instance // (this is the puppy specified via iptables as in // above example.) // // ** there can be at most 1 nf_queue per qid if ( !(impl->nf_queue = nfq_create_queue( impl->nf_handle, impl->qid, daq_nfq_callback, impl)) ) { snprintf(errBuf, errMax, "%s: nf queue creation failed\n", __FUNCTION__); nfq_daq_shutdown(impl); return DAQ_ERROR; } // 5. configure copying for maximum overhead if ( nfq_set_mode(impl->nf_queue, NFQNL_COPY_PACKET, IP_MAXPACKET) < 0 ) { snprintf(errBuf, errMax, "%s: unable to set packet copy mode\n", __FUNCTION__); nfq_daq_shutdown(impl); return DAQ_ERROR; } // 6. set queue length (optional) if ( impl->qlen > 0 && nfq_set_queue_maxlen(impl->nf_queue, impl->qlen)) { snprintf(errBuf, errMax, "%s: unable to set queue length\n", __FUNCTION__); nfq_daq_shutdown(impl); return DAQ_ERROR; } // 7. get the q socket descriptor // (after getting not 1 but 2 handles!) impl->sock = nfq_fd(impl->nf_handle); // setup output stuff // we've got 2 handles and a socket descriptor but, incredibly, // no way to inject? if ( impl->device && strcasecmp(impl->device, "ip") ) { impl->link = eth_open(impl->device); if ( !impl->link ) { snprintf(errBuf, errMax, "%s: can't open %s!\n", __FUNCTION__, impl->device); nfq_daq_shutdown(impl); return DAQ_ERROR; } } else { impl->net = ip_open(); if ( !impl->net ) { snprintf(errBuf, errMax, "%s: can't open ip!\n", __FUNCTION__); nfq_daq_shutdown(impl); return DAQ_ERROR; } } impl->state = DAQ_STATE_INITIALIZED; *handle = impl; return DAQ_SUCCESS; }
(unsigned)((addr).s6_addr[13]), \ (unsigned)((addr).s6_addr[14]), \ (unsigned)((addr).s6_addr[15]) #endif struct ipaddr_str { const char * str; cmdline_ipaddr_t addr; unsigned flags; }; const struct ipaddr_str ipaddr_valid_strs[] = { {"0.0.0.0", {AF_INET, {IP4(0,0,0,0)}, 0}, CMDLINE_IPADDR_V4}, {"0.0.0.0/0", {AF_INET, {IP4(0,0,0,0)}, 0}, CMDLINE_IPADDR_V4 | CMDLINE_IPADDR_NETWORK}, {"0.0.0.0/24", {AF_INET, {IP4(0,0,0,0)}, 24}, CMDLINE_IPADDR_V4 | CMDLINE_IPADDR_NETWORK}, {"192.168.1.0/24", {AF_INET, {IP4(192,168,1,0)}, 24}, CMDLINE_IPADDR_V4 | CMDLINE_IPADDR_NETWORK}, {"012.34.56.78/24", {AF_INET, {IP4(12,34,56,78)}, 24}, CMDLINE_IPADDR_V4 | CMDLINE_IPADDR_NETWORK}, {"34.56.78.90/1", {AF_INET, {IP4(34,56,78,90)}, 1}, CMDLINE_IPADDR_V4 | CMDLINE_IPADDR_NETWORK}, {"::", {AF_INET6, {IP6(0,0,0,0,0,0,0,0)}, 0}, CMDLINE_IPADDR_V6}, {"::1", {AF_INET6, {IP6(0,0,0,0,0,0,0,1)}, 0}, CMDLINE_IPADDR_V6},
/* main(): * The equivalent of the MONCMD server built into uMon... * Blocks on port 777 waiting for an incoming message, then * responds with "thanks". */ int main(int argc, char *argv[]) { int rc = 0; unetStart(); cmdInit(); mon_printf("%s: MONCMD server using uMon's ethernet API...\n", argv[0]); /* This loop processes incoming UDP and incoming serial port * (console) activity... Terminate on reception of ctrl-c * (0x03) from console. */ while(1) { if (mon_gotachar()) { int c = mon_getchar(); if (c == 0x03) { mon_printf("\n<ctrl-c>\n"); break; } else processChar(c); } /* Set up the server to receive packets on any port... * Then after udpRecvFrom returns, use the value of u.dport * to determine which port the packet came in on. */ udpInfo.dport = ANY_UDP_PORT; udpInfo.packet = (char *)unetPacket; udpInfo.plen = sizeof(unetPacket); /* Check for an incoming packet. This does not block. It will * return negative if there is an error, zero of there is no packet * pending; else some positive number representing the incoming * packet size. */ rc = udpRecvFrom(&udpInfo); if (rc > 0) { if (udpInfo.dport == 777) { udp_umoncmd((char *)udpInfo.udata); } else { mon_printf("\nUDP rcv'd on port %d, from %d.%d.%d.%d:%d ...\n", udpInfo.dport, IP1(udpInfo.sip.s_addr), IP2(udpInfo.sip.s_addr), IP3(udpInfo.sip.s_addr), IP4(udpInfo.sip.s_addr), udpInfo.sport); mon_printmem((char *)udpInfo.udata,rc,1); } } else if (rc < 0) { unetError("recv",rc,UNETACT_ALL); } } unetStop(); return(0); }
static void *mcasttest(void *arg) { int fd; struct ofp_sockaddr_in my_addr; struct ofp_ip_mreq mreq; (void)arg; logprint("Multicast thread started\n"); if (odp_init_local(ODP_THREAD_CONTROL)) { OFP_ERR("Error: ODP local init failed.\n"); return NULL; } if (ofp_init_local()) { OFP_ERR("Error: OFP local init failed.\n"); return NULL; } sleep(1); while (myaddr == 0) { myaddr = ofp_port_get_ipv4_addr(0, 0, OFP_PORTCONF_IP_TYPE_IP_ADDR); sleep(1); } if ((fd = ofp_socket(OFP_AF_INET, OFP_SOCK_DGRAM, OFP_IPPROTO_UDP)) < 0) { perror("socket"); logprint("Cannot open socket!\n"); return NULL; } memset(&my_addr, 0, sizeof(my_addr)); my_addr.sin_family = OFP_AF_INET; my_addr.sin_port = odp_cpu_to_be_16(2048); my_addr.sin_addr.s_addr = 0; my_addr.sin_len = sizeof(my_addr); if (ofp_bind(fd, (struct ofp_sockaddr *)&my_addr, sizeof(struct ofp_sockaddr)) < 0) { logprint("Cannot bind socket (%s)!\n", ofp_strerror(ofp_errno)); return NULL; } memset(&mreq, 0, sizeof(mreq)); mreq.imr_multiaddr.s_addr = IP4(234,5,5,5); mreq.imr_interface.s_addr = myaddr; if (ofp_setsockopt(fd, OFP_IPPROTO_IP, OFP_IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) { perror("setsockopt"); } memset(&mreq, 0, sizeof(mreq)); mreq.imr_multiaddr.s_addr = IP4(234,7,7,7); mreq.imr_interface.s_addr = myaddr; if (ofp_setsockopt(fd, OFP_IPPROTO_IP, OFP_IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) { perror("setsockopt"); } for (;;) { char buf[100]; int len = sizeof(buf); struct ofp_sockaddr_in addr = {0}; ofp_socklen_t addr_len = 0; len = ofp_recvfrom(fd, buf, len, 0, (struct ofp_sockaddr *)&addr, &addr_len); if (len == -1) { OFP_ERR("Faild to rcv data(errno = %d)\n", ofp_errno); continue; } buf[len] = 0; OFP_INFO("Data (%s, len = %d) was received.\n", buf, len); if (addr_len != sizeof(addr)) { OFP_ERR("Faild to rcv source address: %d (errno = %d)\n", addr_len, ofp_errno); continue; } if (strstr(buf, "add")) { OFP_INFO("Add membership to 234.7.7.7\n"); memset(&mreq, 0, sizeof(mreq)); mreq.imr_multiaddr.s_addr = IP4(234,7,7,7); mreq.imr_interface.s_addr = myaddr; if (ofp_setsockopt(fd, OFP_IPPROTO_IP, OFP_IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) { perror("setsockopt"); } } else if (strstr(buf, "drop")) { OFP_INFO("Drop membership from 234.7.7.7\n"); memset(&mreq, 0, sizeof(mreq)); mreq.imr_multiaddr.s_addr = IP4(234,7,7,7); mreq.imr_interface.s_addr = myaddr; if (ofp_setsockopt(fd, OFP_IPPROTO_IP, OFP_IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) { perror("setsockopt"); } } else if (strstr(buf, "quit")) { exit(0); } OFP_INFO("Data was received from address 0x%x, port = %d.\n", odp_be_to_cpu_32(addr.sin_addr.s_addr), odp_be_to_cpu_16(addr.sin_port)); sprintf(buf, "%d bytes\n", len); if (ofp_sendto(fd, buf, strlen(buf), 0, (struct ofp_sockaddr *)&addr, sizeof(addr)) == -1) { OFP_ERR("Faild to send data (errno = %d)\n", ofp_errno); } } logprint("mcast exit\n"); return NULL; }
static odp_cos_t build_cos_w_queue(const char *name) { odp_cos_t cos; odp_queue_t queue_cos; odp_queue_param_t qparam; cos = odp_cos_create(name); if (cos == ODP_COS_INVALID) { OFP_ERR("Failed to create COS"); return ODP_COS_INVALID; } memset(&qparam, 0, sizeof(odp_queue_param_t)); qparam.sched.prio = ODP_SCHED_PRIO_DEFAULT; qparam.sched.sync = ODP_SCHED_SYNC_ATOMIC; qparam.sched.group = ODP_SCHED_GROUP_ALL; queue_cos = odp_queue_create(name, ODP_QUEUE_TYPE_SCHED, &qparam); if (queue_cos == ODP_QUEUE_INVALID) { OFP_ERR("Failed to create queue\n"); odp_cos_destroy(cos); return ODP_COS_INVALID; } #if ODP_VERSION < 104 if (odp_cos_set_queue(cos, queue_cos) < 0) { #else if (odp_cos_queue_set(cos, queue_cos) < 0) { #endif OFP_ERR("Failed to set queue on COS"); odp_cos_destroy(cos); odp_queue_destroy(queue_cos); return ODP_COS_INVALID; } return cos; } static odp_cos_t build_cos_set_queue(const char *name, odp_queue_t queue_cos) { odp_cos_t cos; cos = odp_cos_create(name); if (cos == ODP_COS_INVALID) { OFP_ERR("Failed to create COS"); return ODP_COS_INVALID; } #if ODP_VERSION < 104 if (odp_cos_set_queue(cos, queue_cos) < 0) { #else if (odp_cos_queue_set(cos, queue_cos) < 0) { #endif OFP_ERR("Failed to set queue on COS"); odp_cos_destroy(cos); return ODP_COS_INVALID; } return cos; } static odp_pmr_t build_udp_prm(void) { uint32_t pmr_udp_val = TEST_PORT; uint32_t pmr_udp_mask = 0xffffffff; #if ODP_VERSION < 104 return odp_pmr_create(ODP_PMR_UDP_DPORT, &pmr_udp_val, &pmr_udp_mask, 1); #else const odp_pmr_match_t match = { .term = ODP_PMR_UDP_DPORT, .val = &pmr_udp_val, .mask = &pmr_udp_mask, .val_sz = 1 }; return odp_pmr_create(&match); #endif } static void app_processing(void) { int fd_rcv = -1; char buf[1500]; int len = sizeof(buf); do { struct ofp_sockaddr_in addr = {0}; fd_rcv = ofp_socket(OFP_AF_INET, OFP_SOCK_DGRAM, OFP_IPPROTO_UDP); if (fd_rcv == -1) { OFP_ERR("Faild to create RCV socket (errno = %d)\n", ofp_errno); break; } addr.sin_len = sizeof(struct ofp_sockaddr_in); addr.sin_family = OFP_AF_INET; addr.sin_port = odp_cpu_to_be_16(TEST_PORT); addr.sin_addr.s_addr = IP4(192, 168, 100, 1); if (ofp_bind(fd_rcv, (const struct ofp_sockaddr *)&addr, sizeof(struct ofp_sockaddr_in)) == -1) { OFP_ERR("Faild to bind socket (errno = %d)\n", ofp_errno); break; } len = ofp_recv(fd_rcv, buf, len, 0); if (len == -1) OFP_ERR("Faild to receive data (errno = %d)\n", ofp_errno); else OFP_INFO("Data received: length = %d.\n", len); } while (0); if (fd_rcv != -1) { ofp_close(fd_rcv); fd_rcv = -1; } OFP_INFO("Test ended.\n"); }
static int nfq_daq_get_datalink_type(void *handle) { NfqImpl* impl = (NfqImpl*)handle; int dlt = IP4(impl) ? DLT_IPV4 : DLT_IPV6; return dlt; }
void FC_handle_message( sys_scatter *scat ) { int16 *cur_fc_buf; packet_header *pack_ptr; proc dummy_proc; int my_index; int16 temp_window,temp_personal_window; configuration *Cn; Cn = Conf_ref(); pack_ptr = (packet_header *)scat->elements[0].buf; if ( ! Conf_get_dangerous_monitor_state() ) { Alarm( FLOW_CONTROL, "FC_handle_message: Request to change flow control from (%d.%d.%d.%d) denied. Monitor in safe mode\n", IP1(pack_ptr->proc_id), IP2(pack_ptr->proc_id), IP3(pack_ptr->proc_id), IP4(pack_ptr->proc_id) ); return; } if( ! ( pack_ptr->memb_id.proc_id == 15051963 && Conf_id_in_conf( Cn, pack_ptr->proc_id ) != -1 ) ) { Alarm( FLOW_CONTROL, "FC_handle_message: Illegal monitor request\n"); return; } cur_fc_buf = (int16 *)scat->elements[1].buf; my_index = Conf_proc_by_id( Conf_my().id, &dummy_proc ); if( Same_endian( pack_ptr->type ) ) { temp_window = cur_fc_buf[Conf_num_procs( Cn )]; temp_personal_window = cur_fc_buf[my_index]; }else{ temp_window = Flip_int16( cur_fc_buf[Conf_num_procs( Cn )] ); temp_personal_window = Flip_int16( cur_fc_buf[my_index] ); } if( temp_window != -1 ) Window = temp_window; if( temp_personal_window != -1 ) Personal_window = temp_personal_window; GlobalStatus.window = Window; GlobalStatus.personal_window = Personal_window; Alarm( FLOW_CONTROL, "FC_handle_message: Got monitor mess, Window %d Personal %d\n", Window, Personal_window ); }