Example #1
0
/*
 *===========================================================================
 *                    ipcom_cmd_sockperf_connect
 *===========================================================================
 * Description: Active open of socket(s)
 * Parameters:
 * Returns:
 */
IP_STATIC void
ipcom_cmd_sockperf_connect(Ipcom_cmd_sockperf_t *cmd)
{
    Ip_fd            *s = cmd->sock_array;
    Ip_u32            created_sockets;
    struct Ip_timeval start;
    struct Ip_timeval stop;

    ipcom_printf("sockperf-c: connecting to %s[%d]"IP_LF,
                 cmd->res->ai_canonname,
                 ip_ntohs(((struct Ip_sockaddr_in *)cmd->res->ai_addr)->sin_port));
    ipcom_microtime(&start);
    for (created_sockets = 0; created_sockets < cmd->num_sock; created_sockets++)
    {
        s[created_sockets] = ipcom_socket(cmd->res->ai_family,
                                          cmd->res->ai_socktype,
                                          cmd->res->ai_protocol);
        if (s[created_sockets] == IP_SOCKERR)
        {
            ipcom_printf("sockperf-c: Failed to create socket: %s"IP_LF,
                         ipcom_strerror(ipcom_errno));
            created_sockets--;
            goto cleanup;
        }

        if (ipcom_setsockopt(s[created_sockets],
                             IP_SOL_SOCKET,
                             IP_SO_LINGER,
                             &cmd->linger,
                             sizeof(cmd->linger)) < 0)
        {
            ipcom_printf("sockperf-c: setsockopt SO_LINGER failed : %s"IP_LF,
                         ipcom_strerror(ipcom_errno));
            goto cleanup;
        }

        if (ipcom_connect(s[created_sockets], cmd->res->ai_addr, cmd->res->ai_addrlen) < 0)
        {
            ipcom_printf("sockperf-c: Failed to connect: %s"IP_LF,
                         ipcom_strerror(ipcom_errno));
            goto cleanup;
        }
    }
    ipcom_microtime(&stop);
    ipcom_printf("sockperf-t: %ld sockets connected in %ld ms"IP_LF,
                 created_sockets,
                 ipcom_cmd_sockperf_tv_to_msec(&start, &stop));

    ipcom_cmd_sockperf_run(cmd);

 cleanup:
    while (created_sockets > 0)
        (void)ipcom_socketclose(s[created_sockets--]);
}
/*
 *===========================================================================
 *                      ipnet_cmd_qc_show_queue
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 *
 */
IP_STATIC int
ipnet_cmd_qc_show_queue(Ipnet_cmd_qc *p)
{
    Ipnet_cmd_qc_type_handler *h;

    if (ipcom_socketioctl(p->fd, IPNET_SIOCGIFQUEUE, &p->p.ifq) < 0)
    {
        ipcom_printf("Failed to get the queue: %s"IP_LF, ipcom_strerror(ipcom_errno));
        return -1;
    }

    h = ipnet_cmd_qc_get_type_handler(p->p.ifq.ifq_type);
    if (h == IP_NULL)
    {
        ipcom_printf("No 'show' handler implemented for queue type '%s'"IP_LF,
                     p->p.ifq.ifq_type);
        return -1;
    }

    ipnet_cmd_qc_output_indent(p);
    ipcom_printf("%s/%d[%d] queue at %s"IP_LF,
                 p->p.ifq.ifq_type,
                 p->p.ifq.ifq_id,
                 p->p.ifq.ifq_parent_id,
                 p->p.ifq.ifq_name);
    ipnet_cmd_qc_output_indent(p);

    return h->show(p);
}
/*
 *===========================================================================
 *                    ipsecctrl_selectorTable_print
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 *
 */
static void
ipsecctrl_selectorTable_print(Argvars *vars)
{
    Ipipsec_ctrl_selector   sel;
#ifdef IPCOM_USE_INET6
    char str[IP_INET6_ADDRSTRLEN];
#else
    char str[16];
#endif

    ipcom_printf(IP_LF"***** selectorTable ::"IP_LF);

    sel.Index = 0;  /* get first selector. */

    /* Get and print the Selectors in MIB format. */
    for (;;)
    {
        /* Get first/next Selector */
        if (ipcom_socketioctl(vars->fd, IP_SIOCXIPSEC_SELECTOR, &sel) < 0)
        {
            ipcom_printf("ipsecctrl: ipcom_socketioctl(SELECTOR) failed (%s)"IP_LF,
                         ipcom_strerror(ipcom_errno));
            return;
        }

        if (sel.Index == 0)
            break;

        /* Print selector index */
        ipcom_printf(IP_LF"Selector Entry #%ld:"IP_LF
                     "selectorIndex = %ld"IP_LF,
                     sel.Index,
                     sel.Index);

        ipcom_printf("selectorLocalId = %s"IP_LF
                     "selectorLocalIdType = %s"IP_LF,
                     ipcom_inet_ntop(sel.domain, &sel.LocalId, str, sizeof(str)),
                     sel.domain == IP_AF_INET ? "idIpv4Addr" : "idIpv6Addr");


        ipcom_printf("selectorRemoteId = %s"IP_LF
                     "selectorRemoteIdType = %s"IP_LF,
                     ipcom_inet_ntop(sel.domain, &sel.RemoteId, str, sizeof(str)),
                     sel.domain == IP_AF_INET ? "idIpv4Addr" : "idIpv6Addr");

        ipcom_printf("selectorProtocol = %s"IP_LF,
                     ipcom_ipproto_name(sel.Protocol));

        ipcom_printf("selectorLocalPort = %d"IP_LF,
                     ip_ntohs(sel.LocalPort));
        ipcom_printf("selectorRemotePort = %d"IP_LF,
                     ip_ntohs(sel.RemotePort));
    }
}
/*
 *===========================================================================
 *                      ipnet_cmd_qc_del_queue
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 *
 */
IP_STATIC int
ipnet_cmd_qc_del_queue(Ipnet_cmd_qc *p)
{
    ipcom_strcpy(p->p.ifq.ifq_type, "none");
    if (ipcom_socketioctl(p->fd, IPNET_SIOCSIFQUEUE, &p->p.ifq) < 0)
    {
        ipcom_printf("Failed to delete the queue: %s"IP_LF, ipcom_strerror(ipcom_errno));
        return -1;
    }
    return 0;
}
/*
 *===========================================================================
 *                      ipnet_cmd_qc_del_filter
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 *
 */
IP_STATIC int
ipnet_cmd_qc_del_filter(Ipnet_cmd_qc *p)
{
    ipnet_cmd_qc_set_filter_id(p);
    if (ipcom_socketioctl(p->fd, IPNET_SIOCXDIFQFILTER, &p->p.filter) < 0)
    {
        ipcom_printf("Failed to delete the filter: %s"IP_LF, ipcom_strerror(ipcom_errno));
        return -1;
    }

    return 0;
}
/*
 *===========================================================================
 *                      ipnet_cmd_qc_add_queue
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 *
 */
IP_STATIC int
ipnet_cmd_qc_add_queue(Ipnet_cmd_qc *p)
{
    if (ipnet_cmd_qc_parse_queue_type(p) < 0)
        return -1;

    if (ipcom_socketioctl(p->fd, IPNET_SIOCSIFQUEUE, &p->p.ifq) < 0)
    {
        ipcom_printf("Failed to add the queue: %s"IP_LF, ipcom_strerror(ipcom_errno));
        return -1;
    }
    return 0;
}
/*
 *===========================================================================
 *                      ipnet_pkt_queue_mbc_configure
 *===========================================================================
 * Description: Configures a queue with new parameters.
 * Parameters:  q - A packet queue.
 *              m - The queue parameters.
 * Returns:     0  = success
 *              <0 = error code
 */
IP_STATIC int
ipnet_pkt_queue_mbc_configure(Ipnet_pkt_queue_mbc *q, struct Ipnet_ifqueue_mbc *m)
{
    Ip_u32 band;

    if (q->number_of_bands)
    {
        IPCOM_LOG0(ERR, "MBC configure: Can only be configured once");
        return -IP_ERRNO_EINVAL;
    }

    if (m->mbc_bands < 2 || m->mbc_bands > IPNET_IFQ_CONTAINER_MAX_COUNT)
    {
        IPCOM_LOG1(ERR,
                   "MBC configure: Number of priority bands must be between 2 and %d",
                   IPNET_IFQ_CONTAINER_MAX_COUNT);
        return -IP_ERRNO_EINVAL;
    }

    if (m->mbc_bands <= m->mbc_default_band)
    {
        IPCOM_LOG1(ERR, "MBC configure: invalid default band, must be [0..%d]",
                   m->mbc_bands - 1);
        return -IP_ERRNO_EINVAL;
    }

    q->bands = ipcom_calloc(m->mbc_bands, sizeof(Ipnet_pkt_queue *));
    if (q->bands == IP_NULL)
    {
        IPCOM_LOG0(CRIT, "MBC configure: out of memory");
        return -IP_ERRNO_ENOMEM;
    }

    q->number_of_bands = m->mbc_bands;
    q->default_band    = m->mbc_default_band;

    for (band = 0; band < q->number_of_bands; band++)
    {
        int                    ret;

        ret = ipnet_pkt_queue_mbc_insert_default_queue(q, band);
        if (ret < 0)
        {
            IPCOM_LOG1(ERR, "MBC configure: Failed to create default queue: %s",
                       ipcom_strerror(-ret));
            return ret;
        }
    }

    return 0;
}
/*
 *===========================================================================
 *                    ipcom_cmd_smptest_server
 *===========================================================================
 * Description: This is the server part of the smptest command
 * Parameters:  The address we should listen to, and info about how many sockets
 *              we should use.
 * Returns:     0 on success. -1 on failure.
 */
IP_STATIC int
ipcom_cmd_smptest_server()
{
    Ipcom_proc_attr    attr;
    unsigned int i;
    Ipcom_proc *proc;
    int retval;

    spawn_number_server     = 0;

    if ( 0 != ipcom_sem_create( &sem_wait_server, 0 ) )
    {
        ipcom_printf("ipcom_sem_create failed: %s"IP_LF, ipcom_strerror(ipcom_errno));
        return -1;
    }
    ipcom_atomic_set( &num_wait_server, smp_opt_server.num_sock );
    ipcom_atomic_set( &listen_wait, smp_opt_server.num_sock );

    ipcom_proc_attr_init(&attr);
    attr.priority   = IPCOM_PRIORITY_DEFAULT;


    /* Hacky hacky way to write from a task.. copied from ipcom_mshell.c */
    proc = ipcom_proc_self();
    ip_assert( proc != 0 );
    server_out = proc->shell_fd;

    for ( i = 0;i < smp_opt_server.num_sock;++i )
    {
        retval = ipcom_proc_acreate("ipcom_cmd_smptest_server_spawn",
                                        (Ipcom_proc_func)ipcom_cmd_smptest_server_spawn, &attr, 0 );
        if (retval != 0)
        {
            ipcom_printf( "Error spawning smptest server"IP_LF );
            return retval;
        }
    }

    ipcom_sem_wait( sem_wait_server );

    ipcom_sem_delete( &sem_wait_server );

    return 0;
}
Example #9
0
IP_PUBLIC void
ipcom_perror(const char *s)
{
#if IPCOM_USE_SHELL == IPCOM_SHELL_IPCOM || IPCOM_USE_SHELL == IPCOM_SHELL_MINI
    if(s != IP_NULL)
        ipcom_fprintf(ip_stderr, "%s: ", s);

#if IPCOM_USE_SHELL == IPCOM_SHELL_MINI
    ipcom_fprintf(ip_stderr, "%d"IP_LF, ipcom_errno);
#else
    ipcom_fprintf(ip_stderr, "%s"IP_LF, ipcom_strerror(ipcom_errno));
#endif

#elif IPCOM_USE_SHELL == IPCOM_SHELL_NATIVE
    perror(s);
#else
    (void)s;
#endif
}
/*
 *===========================================================================
 *                  ipnet_pkt_queue_mbc_remove
 *===========================================================================
 * Description: Removes an queue from a container queue and put pack a
 *              default queue type.
 * Parameters:  c - The MBC parent queue for 'q'.
 *              q - The queue to remove.
 * Returns:     0 = success, <0 = error code (-IP_ERRNO_xxx)
 *
 */
IP_STATIC int
ipnet_pkt_queue_mbc_remove(Ipnet_pkt_queue_mbc *c, Ipnet_pkt_queue *q)
{
    Ip_u32           band;

    ip_assert(c->hdr.id == q->parent_id);
    for (band = 0; band < c->number_of_bands; band++)
    {
        if (q == c->bands[band])
        {
            int ret = ipnet_pkt_queue_mbc_insert_default_queue(c, band);
            if (ret < 0)
                IPCOM_LOG3(WARNING, "MBC: Failed to replace %s:%d with a fifo in remove(): %s",
                           q->type, q->id, ipcom_strerror(-ret));
            return ret;
        }
    }

    return -IP_ERRNO_ESRCH;
}
/*
 *===========================================================================
 *                    ipipsec_cmd_ipsecctrl
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 *
 */
IP_PUBLIC int
ipipsec_cmd_ipsecctrl(int argc, char **argv)
{
    Argvars   vars;
    int       ret;

    if (argc < 2)
        goto usage;

    vars.fd = ipcom_socket(IP_PF_KEY, IP_SOCK_RAW, PF_KEY_V2);
    if (vars.fd < 0)
    {
        ipcom_printf("ipsecctrl: ipcom_socket() failed (%s)"IP_LF,
                     ipcom_strerror(ipcom_errno));
        return -1;
    }

    vars.argc = argc;
    vars.argv = argv;

    ret = ipsecctrl_run_command(argv[1], &vars);
    ipcom_socketclose(vars.fd);

    if (ret == 0)
        return 0;

 usage:
    ipcom_printf(
                 "IPsec control command, version 2.0"IP_LF
                 "Copyright (c) 2000-2009, Interpeak AB <www.interpeak.com>"IP_LF
                 "usage: ipsecctrl <command>"IP_LF
                 "   Commands:"IP_LF
                 "\tflows\t\t  list all IPsec output flows"IP_LF
                 "\tsa\t\t  list all kernel Security Associations"IP_LF
                 "\tmib\t\t  print IPSec monitor MIB"IP_LF
                 "\tget\t\t  get IPSec configuration values"IP_LF
                 "\tset <name> <val>  set IPSec configuration values"IP_LF
                 );

    return 0;
}
/*
 *===========================================================================
 *                    ipcom_cmd_smptest_client
 *===========================================================================
 * Description: This is the client part of the smptest command
 * Parameters:  The address we should connect to, and info about how many sockets
 *              we should use.
 * Returns:     0 on success. -1 on failure.
 */
IP_STATIC int
ipcom_cmd_smptest_client()
{
    Ipcom_proc_attr    attr;
    unsigned int i;
    int retval;

    spawn_number_client     = 0;

    if ( 0 != ipcom_sem_create( &sem_wait_client, 0 ) )
    {
        ipcom_printf("ipcom_sem_create failed: %s"IP_LF, ipcom_strerror(ipcom_errno));
        return -1;
    }
    ipcom_atomic_set( &num_wait_client, smp_opt_client.num_sock );

    ipcom_proc_attr_init(&attr);
    attr.priority   = IPCOM_PRIORITY_DEFAULT;

    for ( i = 0;i < smp_opt_client.num_sock;++i )
    {
        retval = ipcom_proc_acreate("ipcom_cmd_smptest_client_spawn",
                                        (Ipcom_proc_func)ipcom_cmd_smptest_client_spawn, &attr, 0 );
        if (retval != 0)
        {
            ipcom_printf( "Error spawning smptest client"IP_LF );
            return retval;
        }
    }

    ipcom_sem_wait( sem_wait_client ); /* wait for tasks to complete */

    ipcom_sem_delete( &sem_wait_client );

    return 0;
}
/*
 *===========================================================================
 *                    ipl2tps
 *===========================================================================
 * Description: 
 * Parameters:  
 * Returns:     
 *
 */
IP_STATIC
IPCOM_PROCESS(ipl2tps)
{
    Ip_fd                   ipd_fd = IP_INVALID_SOCKET;
    Ip_bool                 ipd_init = 0;
#ifdef IPPPP_USE_PPPL2TP
    Ip_fd                   ipl2tp_ppp_ipc_recv_fd = IP_INVALID_SOCKET;
#endif

    ipcom_proc_init();

#ifdef IPPPP_USE_PPPL2TP
    if ((ipl2tp_ppp_ipc_recv_fd = ipppp_pppl2tp_pkt_que_open()) == IP_INVALID_SOCKET)
    {
        ipcom_printf("Cannot allocate data packet output socket, errno: %d"IP_LF, ipcom_errno);
        goto leave;
    }
#endif

    /* Get socket */
    if ((udp_fd = ipcom_socket(IP_AF_INET, IP_SOCK_DGRAM, IP_IPPROTO_UDP)) < 0)
    {
        ipcom_printf("Cannot allocate socket, errno: %d"IP_LF,
                     ipcom_errno);
        goto leave;
    }
    else
    {
        union Ip_sockaddr_union uaddr;

        /* Bind to L2TP port */
        ipcom_memset(&uaddr, 0, sizeof(uaddr));

        uaddr.sin.sin_family        = IP_AF_INET;
        uaddr.sin.sin_addr.s_addr   = IP_INADDR_ANY;
        uaddr.sin.sin_port          = ip_htons(IPL2TP_PORT_NUMBER);
        uaddr.sin.sin_len           = sizeof(struct Ip_sockaddr_in);
        if (ipcom_bind(udp_fd, &uaddr.sa, sizeof(struct Ip_sockaddr_in)) < 0)
        {
            ipcom_printf("Cannot bind socket, errno: %d"IP_LF,
                         ipcom_errno);
            ipcom_socketclose(udp_fd);
            goto leave;
        }
    }

    /* Get socket */
    if ((ip_fd = ipcom_socket(IP_AF_INET, IP_SOCK_RAW, IP_IPPROTO_L2TP)) < 0)
    {
        ipcom_printf("Cannot allocate socket, errno: %d"IP_LF,
                     ipcom_errno);
        ipcom_socketclose(udp_fd);
        goto leave;
    }


    /* Init IPD */
    if (ipd_init == 0 &&
        ipcom_ipd_init("ipl2tp", IPCOM_SUCCESS, &ipd_fd) != IPCOM_SUCCESS)
    {
        ipcom_printf("Cannot initialize ipd: %s"IP_LF,
                     ipcom_strerror(ipcom_errno));
        goto leave;
    }

    ipd_init = 1;

    /* Enter read loop (never return) */
    for (;;)
    {
        Ip_fd_set                   fds;            
        Ipl2tp_example_fd_entry_t   *fd;
        int                         fd_max;
        struct Ip_timeval           tv;
        int                         ret;

        IP_FD_ZERO(&fds);

        IP_FD_SET(udp_fd, &fds);
        IP_FD_SET(ip_fd, &fds);
        IP_FD_SET(ipd_fd, &fds);
        fd_max = IP_MAX(udp_fd, ip_fd);
        fd_max = IP_MAX(fd_max, ipd_fd);

#ifdef IPPPP_USE_PPPL2TP
        IP_FD_SET(ipl2tp_ppp_ipc_recv_fd, &fds); 
        fd_max = IP_MAX(fd_max, ipl2tp_ppp_ipc_recv_fd);
#endif

        fd = IPCOM_LIST_FIRST(&ipl2tp_example_fds);
        while (fd != IP_NULL)
        {
            Ipl2tp_example_fd_entry_t   *nfd = IPCOM_LIST_NEXT(&fd->next);
            if (fd->data != IP_NULL)
            {
                IP_FD_SET(fd->fd, &fds);
                fd_max = IP_MAX(fd_max, fd->fd);
            }
            else
            {
                ipcom_list_remove(&fd->next);
                ipcom_socketclose(fd->fd);
                ipcom_free(fd);
            }

            fd = nfd;
        }

        tv.tv_sec   = 0;
        tv.tv_usec  = 10000;
        if ((ret = ipcom_socketselect(fd_max + 1, &fds, IP_NULL, IP_NULL, &tv)) > 0)
        {
            union Ip_sockaddr_union uaddr;
            int                     length;
            Ip_u8                   *buffer;

            if (IP_FD_ISSET(ipd_fd, &fds))
            {
                int event;

                event = ipcom_ipd_input(ipd_fd);
                if (event == IPCOM_IPD_EVENT_RECONFIGURE)
                    goto leave;
            }


#ifdef IPPPP_USE_PPPL2TP
            if (IP_FD_ISSET(ipl2tp_ppp_ipc_recv_fd, &fds))
            {
                Ipppp_pppl2tp_pkt_que_entry que_entry;
                Ipcom_netif *netif;
                Ipcom_pkt *pkt;
                char addrstr[IP_INET_ADDRSTRLEN];

                /* Read message and feed it to L2TP main input */
                if (ipppp_pppl2tp_pkt_que_recv(&que_entry, &uaddr) == IPCOM_SUCCESS)
                {
                    if ((que_entry.netif == IP_NULL) ||
                        (que_entry.pkt == IP_NULL))
                    {
                        ipcom_printf("ipl2tps: L2TPv2 pkt from %s:%d contains null ptr(s)."IP_LF,
                            ipcom_inet_ntop(IP_AF_INET, &uaddr.sin.sin_addr, addrstr, sizeof(addrstr)),
                            ipcom_ntohs(uaddr.sin.sin_port));
                    }
                    else
                    {
                        pkt = que_entry.pkt;
                        netif = que_entry.netif;
                        ipppp_pppl2tp_output(netif, pkt);
                    }
                }
                else
                    ipcom_printf("ipl2tps: PPP pkt recv failed."IP_LF);
            }
#endif /* IPPPP_USE_PPPL2TP */

            if (IP_FD_ISSET(udp_fd, &fds))
            {
                /* Read message and feed it to L2TP main input */
                if ((length = ipl2tp_l2tp_read(udp_fd, &buffer, &uaddr)) > 0)
                {
                    ipl2tp_api_l2tp_input(buffer, 
                                          buffer + HEADER_SPACE,
                                          (Ip_u16)length, 
                                          &uaddr, 
                                          IPL2TP_ATTR_TRANSPORT_UDP);

                    /* Free buffer if not in PPP output queue */
                    if (ipl2tp_buffer_free)
                    {
                        ipl2tp_cache_free(buffer);
                    }
                }
            }

            if (IP_FD_ISSET(ip_fd, &fds))
            {
                /* Read message and feed it to L2TP main input */
                if ((length = ipl2tp_l2tp_read(ip_fd, &buffer, &uaddr)) > 0)
                {
                    /* Remove the IP header before invoking */
                    ipl2tp_api_l2tp_input(buffer, 
                                          buffer + HEADER_SPACE + 20,
                                          (Ip_u16)(length - 20), 
                                          &uaddr, 
                                          IPL2TP_ATTR_TRANSPORT_IP);

                    /* Free buffer if not in PPP output queue */
                    if (ipl2tp_buffer_free)
                    {
                        ipl2tp_cache_free(buffer);
                    }
                }
            }

            fd = IPCOM_LIST_FIRST(&ipl2tp_example_fds);
            while (fd != IP_NULL)
            {
                Ipl2tp_example_fd_entry_t   *nfd = IPCOM_LIST_NEXT(&fd->next);
                if (fd->data!= IP_NULL && IP_FD_ISSET(fd->fd, &fds))
                {
                    /* Read message and feed it to L2TP main input */
                    if ((length = ipl2tp_l2tp_read(fd->fd, &buffer, &uaddr)) > 0)
                    {
                        if (fd->data != IP_NULL)
                        {
                            (*fd->data) (fd,
                                         buffer, 
                                         buffer + HEADER_SPACE, 
                                         length);
                        }

                        /* Free buffer if not in PPP output queue */
                        if (ipl2tp_buffer_free)
                        {
                            ipl2tp_cache_free(buffer);
                        }
                    }
                }

                fd = nfd;
            }
        }
        else if (ret < 0)
        {
            ipcom_printf("select error"IP_LF);
        }        
    }

leave:
    /* Have to call ipd init if not done yet */
    if (ipd_init == 0 &&
        ipcom_ipd_init("ipl2tp", IPCOM_ERR_FAILED, &ipd_fd) == IPCOM_SUCCESS)
    {
        ipd_init = 1;
    }
    if (ipd_init != 0)
    {
        (void)ipcom_ipd_exit("ipl2tp", ipd_fd);
    }

    ipcom_proc_exit();
}
Example #14
0
void test_if(char *parent)
{
	
	int                     name[6];
	Ip_u8                  *if_data;
	Ip_size_t               if_data_len;
	Ip_size_t               if_data_offset;
	struct Ipnet_if_msghdr *ifm;
	struct Ip_sockaddr     *hwaddr;
	char                    ifname[IP_IFNAMSIZ];
	char                    ifname2[IP_IFNAMSIZ];
	char                    str[80];

    
	name[0] = IP_CTL_NET;
	name[1] = IP_AF_ROUTE;
	name[2] = 0;    /* Always 0 */
	name[3] = 0;
	name[4] = IP_NET_RT_IFLIST;
	name[5] = 0;
	
	 if (ipcom_sysctl(name, 6, IP_NULL, &if_data_len, IP_NULL, 0) < 0)
	 {
		 printf("ifconfig: ipnet_sysctl() failed: %s"IP_LF, ipcom_strerror(ipcom_errno));
		 return;
	 }

	 if_data = ipcom_malloc(if_data_len);
	 if (if_data == IP_NULL)
	 {
		 printf("ifconfig: out of memory"IP_LF);
		 return;
	 }

	 if (ipcom_sysctl(name, 6, if_data, &if_data_len, IP_NULL, 0) < 0)
	 {
		 if (ipcom_errno == IP_ERRNO_ENOMEM)
		 {
			 ipcom_free(if_data);
		 }
		 printf("ifconfig: ipnet_sysctl() failed: %s"IP_LF, ipcom_strerror(ipcom_errno));
	 }

	 if_data_offset = 0;
	 while (if_data_len > if_data_offset + sizeof(struct Ipnet_if_msghdr))
	 {
		 ifm = (struct Ipnet_if_msghdr *) &if_data[if_data_offset];
		 if_data_offset += ifm->ifm_msglen;

		 /* Resolve the interface name */
		 		 if (ipcom_if_indextoname(ifm->ifm_index, ifname) == IP_NULL)
		 			 continue;	

		 		if (ifm->ifm_data.ifi_type == IP_IFT_L2VLAN)
		 		{
		 			
		 			if (( vlan_check(ifm->ifm_index, parent) == 0) || (vlan_check(ifm->ifm_index, parent) == -1))
		 				continue;
		 			else
		 			{
		 				printf("Interface=%s\n", ifname);
		 				ipcom_if_indextoname(ifm->ifm_index, ifname2);
		 				return get_ip(ifname2);
		 				
		 			}
		 		}	
		 		 		
   }		
}							
/*
 *===========================================================================
 *                         ipsecctrl_flows
 *===========================================================================
 */
static int
ipsecctrl_flows(Argvars *vars)
{
    Ipipsec_ctrl_selector   sel;
    char   srcport[32], dstport[32];
    int    num = 0;
    char   str[IP_INET6_ADDRSTRLEN];
    char   str2[IP_INET6_ADDRSTRLEN+1];

    ipcom_printf("Flows:"IP_LF);

    sel.Index = 0;  /* get first selector. */

    /* Get and print the Selectors in MIB format. */
    for (;;)
    {
        /* Get first/next Selector */
        if (ipcom_socketioctl(vars->fd, IP_SIOCXIPSEC_SELECTOR, &sel) < 0)
        {
            ipcom_printf("ipsecctrl: ipcom_socketioctl(SELECTOR) failed (%s)"IP_LF,
                         ipcom_strerror(ipcom_errno));
            return -1;
        }

        if (sel.Index == 0)
            break;

        /* Print selector aka flow: */
        num++;
        ipcom_printf("[%ld] :: %s ", sel.Index, FLOW_TYPE(sel.flowtype));

        if (sel.flowpri != 0)
            ipcom_printf("pri=%d ", (signed char)sel.flowpri);

        /* source */
        if (sel.LocalPort != sel.LocalMaxPort)
            ipcom_sprintf(srcport, "%d-%d",
                          (int)ip_ntohs(sel.LocalPort), (int)ip_ntohs(sel.LocalMaxPort));
        else if (sel.LocalPort == 0)
            ipcom_strcpy(srcport, "any");
        else
            ipcom_sprintf(srcport, "%d", (int)ip_ntohs(sel.LocalPort));
        if (ipcom_memcmp(&sel.LocalId, &sel.LocalMaxId, sizeof(union Ip_in_addr_union)))
            ipcom_sprintf(str2, "-%s",
                          ipcom_inet_ntop(sel.domain, &sel.LocalMaxId, str, sizeof(str)));
        else
            *str2 = '\0';
        ipcom_printf("src=%s%s/%d:%s ",
                     ipcom_inet_ntop(sel.domain, &sel.LocalId, str, sizeof(str)),
                     str2,
                     ipipsec_addrmasklen(sel.domain, (Ip_u8 *)&sel.local_mask),
                     srcport);

        /* destination */
        if (sel.RemotePort != sel.RemoteMaxPort)
            ipcom_sprintf(dstport, "%d-%d",
                          (int)ip_ntohs(sel.RemotePort), (int)ip_ntohs(sel.RemoteMaxPort));
        else if (sel.RemotePort == 0)
            ipcom_strcpy(dstport, "any");
        else
            ipcom_sprintf(dstport, "%d", (int)ip_ntohs(sel.RemotePort));
        if (ipcom_memcmp(&sel.RemoteId, &sel.RemoteMaxId, sizeof(union Ip_in_addr_union)))
            ipcom_sprintf(str2, "-%s",
                          ipcom_inet_ntop(sel.domain, &sel.RemoteMaxId, str, sizeof(str)));
        else
            *str2 = '\0';
        ipcom_printf("dst=%s%s/%d:%s ",
                     ipcom_inet_ntop(sel.domain, &sel.RemoteId, str, sizeof(str)),
                     str2,
                     ipipsec_addrmasklen(sel.domain, (Ip_u8 *)&sel.remote_mask),
                     dstport);

        ipcom_printf("%s", ipcom_ipproto_name(sel.Protocol));
        switch (sel.Protocol)
        {
        case IP_IPPROTO_ICMP:
        case IP_IPPROTO_ICMPV6:
            if (sel.ports[0] || sel.ports[1])
                ipcom_printf(" type=%d code=%d", sel.ports[0], sel.ports[1]);
            break;
        case IP_IPPROTO_MH:
            if (sel.ports[0])
                ipcom_printf(" type=%d", sel.ports[0]);
            break;
        default:
            break;
        }

        /* Print SA */
        if (sel.sa.domain)
        {
            if (sel.sa.domain == IPIPSEC_AF_BYPASS)
                ipcom_printf(" %s SA: %s",
                             sel.direction == IPIPSEC_SADIR_INPUT ? "<-" :
                             sel.direction == IPIPSEC_SADIR_OUTPUT ? "->" : "--",
                             IPSECSATYPENAME(sel.sa.satype));
            else
                ipcom_printf(" %s SA: %s spi=0x%lx src=%s dst=%s",
                             sel.direction == IPIPSEC_SADIR_INPUT ? "<-" :
                             sel.direction == IPIPSEC_SADIR_OUTPUT ? "->" : "--",
                             IPSECSATYPENAME(sel.sa.satype),
                             ip_ntohl(sel.sa.spi_n),
                             ipcom_inet_ntop(sel.sa.domain, &sel.sa.src, str, sizeof(str)),
                             ipcom_inet_ntop(sel.sa.domain, &sel.sa.dst, str2, sizeof(str2)));
        }
        else
            ipcom_printf(" %s SA: none",
                         sel.direction == IPIPSEC_SADIR_INPUT ? "<-" :
                         sel.direction == IPIPSEC_SADIR_OUTPUT ? "->" : "--");
        ipcom_printf(IP_LF);
    }
    ipcom_printf("Total of %d flows."IP_LF, num);

    return 0;
}
/*
 *===========================================================================
 *                         ipsecctrl_sa
 *===========================================================================
 */
static int
ipsecctrl_sa(Argvars *vars)
{
    Ipipsec_ioctl_sactrl   sa_next;
    Ipipsec_ioctl_sactrl   sa_group;
    int       num;
    Ip_err    retval;
    Ip_tag    tags[7];
#ifdef IPCOM_USE_INET6
    char str[IP_INET6_ADDRSTRLEN];
    char str2[IP_INET6_ADDRSTRLEN];
#else
    char str[16], str2[16];
#endif

    /* Print all SAs. */
    ipcom_printf("SA - Security Associations:"IP_LF);

    sa_next.arg.next.spi_n = 0;  /* get first SA. */

    for (num = 0;;)
    {
        ipcom_memcpy(&sa_next.sa, &sa_next.arg.next, sizeof(Ipipsec_sa_handle));
        sa_next.arg.next.direction = IPIPSEC_SADIR_ANY;
        sa_next.arg.next.protocol  = 0;
        sa_next.arg.next.domain    = 0;

        /* Get first/next Selector */
        if (ipcom_socketioctl(vars->fd, IP_SIOCXIPSEC_NEXT_SA, &sa_next) < 0)
        {
            ipcom_printf("ipsecctrl: ipcom_socketioctl(NEXT_SA) failed (%s)"IP_LF,
                         ipcom_strerror(ipcom_errno));
            return -1;
        }

        if (sa_next.arg.next.spi_n == 0)
            break;

        /* Get additional information. */
        tags[0] = IPIPSEC_SACTRLT_GET_AuthAlg;
        tags[2] = IPIPSEC_SACTRLT_GET_EncAlg;
        tags[4] = IP_TAG_END;
        retval = ipipsec_sactrl(&sa_next.arg.next, tags);
        if (retval != IPCOM_SUCCESS)
        {
            ipcom_printf("ipsecctrl: ipipsec_sactrl(info) failed, error = %d"IP_LF, retval);
            return -1;
        }
        ipcom_printf("[%2d] ::  %-6s %s spi=0x%-5lx src=%-17s dst=%-17s auth=%s",
                     ++num,
                     sa_next.arg.next.direction == IPIPSEC_SADIR_INPUT ? "input" :
                     sa_next.arg.next.direction == IPIPSEC_SADIR_OUTPUT ? "output" : "any",
                     ipcom_ipproto_name(sa_next.arg.next.protocol),
                     ip_ntohl(sa_next.arg.next.spi_n),
                     ipcom_inet_ntop(sa_next.arg.next.domain, &sa_next.arg.next.src, str, sizeof(str)),
                     ipcom_inet_ntop(sa_next.arg.next.domain, &sa_next.arg.next.dst, str2, sizeof(str2)),
                     mib_AuthAlg[tags[1]]);


        if (sa_next.arg.next.protocol == IP_IPPROTO_ESP)
            ipcom_printf(" enc=%s", mib_EncAlg[tags[3]]);
        if (sa_next.arg.next.priority != 0)
            ipcom_printf(" pri=%d", (int)sa_next.arg.next.priority);
        if (sa_next.arg.next.dscp != 0)
            ipcom_printf(" DSCP=%d", (int)sa_next.arg.next.dscp);
        ipcom_printf(IP_LF);

        /* Get grouped info */
        ipcom_memcpy(&sa_group.sa, &sa_next.arg.next, sizeof(Ipipsec_sa_handle));
        if (ipcom_socketioctl(vars->fd, IP_SIOCXIPSEC_Grouped, &sa_group) < 0)
        {
            ipcom_printf("ipsecctrl: ipcom_socketioctl(Grouped) failed (%s)"IP_LF,
                         ipcom_strerror(ipcom_errno));
            return -1;
        }
        if (sa_group.arg.group.spi_n) /*lint !e530 */
        {
            ipcom_printf("           ->   %s spi=0x%-5lx src=%-17s dst=%-17s",
                         ipcom_ipproto_name(sa_group.arg.group.protocol),
                         ip_ntohl(sa_group.arg.group.spi_n),
                         ipcom_inet_ntop(sa_next.arg.next.domain, &sa_next.arg.next.src, str, sizeof(str)),
                         ipcom_inet_ntop(sa_group.arg.group.domain, &sa_next.arg.next.dst, str2, sizeof(str2)));
            if (sa_group.arg.group.priority != 0)
                ipcom_printf(" pri=%d", (int)sa_group.arg.group.priority);
            if (sa_group.arg.group.dscp != 0)
                ipcom_printf(" DSCP=%d", (int)sa_group.arg.group.dscp);
            ipcom_printf(IP_LF);
        }
    }

    ipcom_printf("Total of %d SAs."IP_LF, num);
    return 0;
}
Example #17
0
/*
 *===========================================================================
 *                      ipnet_if_mib_handler_ifTable
 *===========================================================================
 * Description: MIB handler for variables in ifTable
 * Parameters: See file 'ipsnmp.h'
 * Returns: IPSNMP_ERROR_XXX
 *
 */
IP_STATIC Ip_s32
ipnet_if_mib_handler_ifTable(Ip_s32 cmd,
                             char *id,
                             Ipsnmp_varbind *vb,
                             Ip_s32 magic,
                             struct Ipsnmp_node_object *nodeobj)
{
    Ip_s32          lid, ret = -1;
    Ipnet_netif    *best_netif;
    Ip_s32          ifAdminStatus = 0;
    char           *iid;
    char           *buf = ipcom_malloc(IPSNMP_CONFIG_MAX_OBJECT_ID);
    char           *best = ipcom_malloc(IPSNMP_CONFIG_MAX_OBJECT_ID);
    struct Ip_ifreq ifreq;
    Ip_fd           fd;

    if (buf == IP_NULL || best == IP_NULL)
    {
        ret = IPSNMP_ERROR_GENERROR;
        goto exit;
    }

    lid = ipsnmp_util_last_subid(nodeobj->id);
    ip_assert(lid >= 1 && lid <= 20);
    ip_assert(lid != 12 && lid != 18);
    best_netif = ipnet_if_mib_table_search_ifTable(id, buf, best, cmd, &ret);
    if (best_netif == IP_NULL)
        goto exit;

    if (cmd == IPSNMP_MIB_COMMAND_GET || cmd == IPSNMP_MIB_COMMAND_NEXT)
    {
        iid = ipsnmp_create_iid_direct(nodeobj->id, best);
        if (iid == IP_NULL)
        {
            ret = IPSNMP_ERROR_GENERROR;
            goto exit;
        }

        switch(lid)
        {
            case 1: /* ifIndex */
                ret = ipsnmp_util_put_integer(magic, iid, best_netif->ipcom.ifindex);
                break;
            case 2: /* ifDescr */
                ret = ipsnmp_util_put_octetstring(magic, iid, (Ip_u8 *)best_netif->ipcom.name, ipcom_strlen(best_netif->ipcom.name));
                break;
            case 3: /* ifType */
                ret = ipsnmp_util_put_integer(magic, iid, best_netif->ipcom.type);
                break;
            case 4: /* ifMtu */
                ret = ipsnmp_util_put_integer(magic, iid, best_netif->ipcom.mtu);
                break;
            case 5: /* ifSpeed */
                if (best_netif->ipcom.type == IP_IFT_PPP)
                    ret = ipsnmp_util_put_gauge32(magic, iid, IPCOM_DRV_PPP_BAUDRATE);
                else
                    ret = ipsnmp_util_put_gauge32(magic, iid, 100000000);
                break;
            case 6: /* ifPhysAddr */
                ret = ipsnmp_util_put_octetstring(magic, iid, best_netif->ipcom.link_addr, best_netif->ipcom.link_addr_size);
                break;
            case 7: /* ifAdminStatus */
                if (IP_BIT_ISSET(best_netif->ipcom.flags, IP_IFF_UP))
                    ret = ipsnmp_util_put_integer(magic, iid, 1);
                else
                    ret = ipsnmp_util_put_integer(magic, iid, 2);
                break;
            case 8: /* ifOperStatus */
                if (IP_BIT_ISSET(best_netif->ipcom.flags, IP_IFF_UP))
                    ret = ipsnmp_util_put_integer(magic, iid, 1);
                else
                    ret = ipsnmp_util_put_integer(magic, iid, 2);
                break;
            case 9: /* ifLastChange */
                ret = ipsnmp_util_put_timeticks(magic, iid, best_netif->ipcom.mib2.ifLastChange);
                break;
            case 10: /* ifInOctets */
                ret = ipsnmp_util_put_counter32(magic, iid, best_netif->ipcom.mib2.ifInOctets);
                break;
            case 11: /* ifInUcastPkts */
                ret = ipsnmp_util_put_counter32(magic, iid, best_netif->ipcom.mib2.ifInUcastPkts);
                break;
            case 13: /* ifInDiscards */
                ret = ipsnmp_util_put_counter32(magic, iid, best_netif->ipcom.mib2.ifInDiscards);
                break;
            case 14: /* ifInErrors */
                ret = ipsnmp_util_put_counter32(magic, iid, best_netif->ipcom.mib2.ifInErrors);
                break;
            case 15: /* ifInUnknownProtos */
                ret = ipsnmp_util_put_counter32(magic, iid, best_netif->ipcom.mib2.ifInUnknownProtos);
                break;
            case 16: /* ifOutOctets */
                ret = ipsnmp_util_put_counter32(magic, iid, best_netif->ipcom.mib2.ifOutOctets);
                break;
            case 17: /* ifOutUcastPkts */
                ret = ipsnmp_util_put_counter32(magic, iid, best_netif->ipcom.mib2.ifOutUcastPkts);
                break;
            case 19: /* ifOutDiscards */
                ret = ipsnmp_util_put_counter32(magic, iid, best_netif->ipcom.mib2.ifOutDiscards);
                break;
            case 20: /* ifOutErrors */
                ret = ipsnmp_util_put_counter32(magic, iid, best_netif->ipcom.mib2.ifOutErrors);
                break;
            default:
                IP_PANIC();
                ret = IPSNMP_ERROR_GENERROR;
                break;
        }

        ipcom_free(iid);
    }

    if (cmd == IPSNMP_MIB_COMMAND_TEST || cmd == IPSNMP_MIB_COMMAND_SET)
    {
        switch(lid)
        {
            case 7: /* ifAdminStatus */
                ret = ipsnmp_util_get_integer(vb, &ifAdminStatus);
                if (ret == IPSNMP_ERROR_NOERROR)
                {
                    if (ifAdminStatus != 1 && ifAdminStatus != 2)
                    {
                        ret = IPSNMP_ERROR_WRONGVALUE;
                    }
                }

                if (ret == IPSNMP_ERROR_NOERROR && cmd == IPSNMP_MIB_COMMAND_SET)
                {
                    fd = ipnet_do_socket(IP_AF_INET, IP_SOCK_DGRAM, IP_IPPROTO_UDP, IP_FALSE);
                    if(fd != IP_INVALID_SOCKET)
                    {
                        /* Change interface status */
                        ipcom_memset(&ifreq, 0, sizeof(struct Ip_ifreq));
                        ipcom_strcpy(ifreq.ifr_name, best_netif->ipcom.name);
                        if (ipnet_sys_socketioctl(fd, IP_SIOCGIFFLAGS, &ifreq) < 0)
                        {
                            IPCOM_LOG1(ERR, "Failed to get interface flags: %s", ipcom_strerror(ipcom_errno));
                            ret = IPSNMP_ERROR_GENERROR;
                        }
                        if (ifAdminStatus == 1)
                            IP_BIT_SET(ifreq.ip_ifr_flags, IP_IFF_UP);
                        else
                            IP_BIT_CLR(ifreq.ip_ifr_flags, IP_IFF_UP);
                        if (ipnet_sys_socketioctl(fd, IP_SIOCSIFFLAGS, &ifreq) < 0)
                        {
                            IPCOM_LOG1(ERR, "Failed to set interface flags: %s", ipcom_strerror(ipcom_errno));
                            ret = IPSNMP_ERROR_GENERROR;
                        }
                        ipnet_sys_socketclose(fd);
                    }
                    else
                    {
                        IPCOM_LOG0(ERR, "Failed to create socket for ioctl call");
                        ret = IPSNMP_ERROR_GENERROR;
                    }
                }
                break;
            default:
                IP_PANIC();
                ret = IPSNMP_ERROR_GENERROR;
                break;
        }
    }

exit:
    if (buf != IP_NULL)
        ipcom_free(buf);
    if (best != IP_NULL)
        ipcom_free(best);
    return ret;
}
Example #18
0
/*
 *===========================================================================
 *                    ipcom_cmd_sockperf_accept
 *===========================================================================
 * Description: Passive open of socket(s).
 * Parameters:
 * Returns:
 */
IP_STATIC void
ipcom_cmd_sockperf_accept(Ipcom_cmd_sockperf_t *cmd)
{
    Ip_fd            *s = cmd->sock_array;
    int               listen_sock;
    Ip_u32            accepted_sockets = 0;
    struct Ip_timeval start;
    struct Ip_timeval stop;
    int               on = 1;

    listen_sock = ipcom_socket(cmd->res->ai_family,
                               cmd->res->ai_socktype,
                               cmd->res->ai_protocol);

    if (listen_sock == IP_SOCKERR)
    {
        ipcom_printf("sockperf-a: Failed to create socket: %s"IP_LF,
                     ipcom_strerror(ipcom_errno));
        goto cleanup;
    }


    if (ipcom_setsockopt(listen_sock,
                         IP_SOL_SOCKET,
                         IP_SO_REUSEADDR,
                         &on,
                         sizeof(on)) < 0)
    {
        ipcom_printf("sockperf-a: setsockopt IP_SO_REUSEADDR failed : %s"IP_LF,
                     ipcom_strerror(ipcom_errno));
        return;
    }

    if (ipcom_bind(listen_sock, cmd->res->ai_addr, cmd->res->ai_addrlen) < 0)
    {
        ipcom_printf("sockperf-a: Failed to bind: %s"IP_LF,
                     ipcom_strerror(ipcom_errno));
        goto cleanup;
    }

    if (ipcom_listen(listen_sock, cmd->num_sock) < 0)
    {
        ipcom_printf("sockperf-a: Failed to listen: %s"IP_LF,
                     ipcom_strerror(ipcom_errno));
        goto cleanup;
    }

    ipcom_printf("sockperf-a: listen on %s[%d]"IP_LF,
                 cmd->res->ai_canonname,
                 ip_ntohs(((struct Ip_sockaddr_in *)cmd->res->ai_addr)->sin_port));

    IP_FD_ZERO(&cmd->read_set);
    IP_FD_SET(listen_sock, &cmd->read_set);
    if (ipcom_socketselect(listen_sock + 1, &cmd->read_set, IP_NULL, IP_NULL, IP_NULL) != 1)
    {
        ipcom_printf("sockperf-a: Select failed for listen socket: %s"IP_LF,
                     ipcom_strerror(ipcom_errno));
        goto cleanup;
    }

    ipcom_microtime(&start);
    for (;accepted_sockets < cmd->num_sock; accepted_sockets++)
    {
        s[accepted_sockets] = ipcom_accept(listen_sock, IP_NULL, IP_NULL);
        if (s[accepted_sockets] == IP_SOCKERR)
        {
            ipcom_printf("sockperf-a: Failed to create socket: %s"IP_LF,
                     ipcom_strerror(ipcom_errno));
            goto cleanup;
        }
        if (ipcom_setsockopt(s[accepted_sockets],
                             IP_SOL_SOCKET,
                             IP_SO_LINGER,
                             &cmd->linger,
                             sizeof(cmd->linger)) < 0)
        {
            ipcom_printf("sockperf-a: setsockopt SO_LINGER failed : %s"IP_LF,
                         ipcom_strerror(ipcom_errno));
            goto cleanup;
        }
    }
    ipcom_microtime(&stop);
    ipcom_printf("sockperf-a: %ld sockets accepted in %ld ms"IP_LF,
                 accepted_sockets,
                 ipcom_cmd_sockperf_tv_to_msec(&start, &stop));

    ipcom_cmd_sockperf_run(cmd);

 cleanup:
    if (listen_sock >= 0)
        (void)ipcom_socketclose(listen_sock);
    while (accepted_sockets-- > 0)
        (void)ipcom_socketclose(s[accepted_sockets]);
}
static void
ipsecctrl_saTables_print(Argvars *vars, Ip_u8 direction, Ip_u8 protocol, int domain)
{
    Ipipsec_ioctl_sactrl   sa_next;
    Ipipsec_ioctl_sactrl   sa_u64;
    Ip_err   retval;
    int  i, num;
    Ip_tag tags[41];
    char table[32];
#ifdef IPCOM_USE_INET6
    char str[IP_INET6_ADDRSTRLEN];
#else
    char str[16];
#endif

    /* pre-init print stuff. */
    ipcom_sprintf(table, "ipsecSa%s%s",
                  protocol == IP_IPPROTO_AH ? "Ah" : "Esp",
                  direction == IPIPSEC_SADIR_INPUT ? "In" :
                  direction == IPIPSEC_SADIR_OUTPUT ? "Out" :
                  "Any");

    /* Fill in the info tags. */
    tags[0] = IPIPSEC_SACTRLT_GET_SaSelector;
    tags[2] = IPIPSEC_SACTRLT_GET_SaCreator;
    tags[4] = IPIPSEC_SACTRLT_GET_Encapsulation;
    tags[6] = IPIPSEC_SACTRLT_GET_EncAlg;
    tags[8] = IPIPSEC_SACTRLT_GET_EncKeyLength;
    tags[10] = IPIPSEC_SACTRLT_GET_AuthAlg;
    tags[12] = IPIPSEC_SACTRLT_GET_AuthKeyLength;
    tags[14] = IPIPSEC_SACTRLT_GET_RepWinSize;
    tags[16] = IPIPSEC_SACTRLT_GET_LimitSeconds;
    tags[18] = IPIPSEC_SACTRLT_GET_LimitKbytes;
    tags[20] = IPIPSEC_SACTRLT_GET_AccSeconds;
    tags[22] = IPIPSEC_SACTRLT_GET_InAccKbytes;
    tags[24] = IPIPSEC_SACTRLT_GET_DecryptErrors;
    tags[26] = IPIPSEC_SACTRLT_GET_AuthErrors;
    tags[28] = IPIPSEC_SACTRLT_GET_ReplayErrors;
    tags[30] = IPIPSEC_SACTRLT_GET_PolicyErrors;
    tags[32] = IPIPSEC_SACTRLT_GET_PadErrors;
    tags[34] = IPIPSEC_SACTRLT_GET_OtherReceiveErrors;
    tags[36] = IPIPSEC_SACTRLT_GET_OutAccKbytes;
    tags[38] = IPIPSEC_SACTRLT_GET_OutSendErrors;
    tags[40] = IP_TAG_END;

    ipcom_printf(IP_LF"***** %stable ::"IP_LF, table);

    /* Get and print the SAs in MIB format. */
    sa_next.arg.next.spi_n = 0;

    for (num = 0;;)
    {
        ipcom_memcpy(&sa_next.sa, &sa_next.arg.next, sizeof(Ipipsec_sa_handle));
        sa_next.arg.next.direction = direction;
        sa_next.arg.next.protocol  = protocol;
        sa_next.arg.next.domain    = (Ip_u8)domain;
        if (ipcom_socketioctl(vars->fd, IP_SIOCXIPSEC_NEXT_SA, &sa_next) < 0)
        {
            ipcom_printf("ipsecctrl: ipcom_socketioctl(NEXT_SA) failed (%s)"IP_LF,
                         ipcom_strerror(ipcom_errno));
            return;
        }

        if (sa_next.arg.next.spi_n == 0)
            break;

        retval = ipipsec_sactrl(&sa_next.arg.next, tags);
        if (retval != IPCOM_SUCCESS)
        {
            ipcom_printf("ipsecctrl: ipipsec_sactrl(info) failed, error = %d"IP_LF, retval);
            return;
        }

        /* SA id */
        ipcom_printf(IP_LF"%sEntry #%d:"IP_LF
                     "%sAddresstype = %d"IP_LF
                     "%sAddress = %s"IP_LF
                     "%sSpi = 0x%lx"IP_LF
                     , table, num
                     , table, domain == IP_AF_INET ? 1 : domain == IP_AF_INET6 ? 2 : 3
                     , table, ipcom_inet_ntop(sa_next.arg.next.domain, &sa_next.arg.next.dst, str, sizeof(str))
                     , table, ip_ntohl(sa_next.arg.next.spi_n));

        /* SA MIB entries */
        for (i = 0; tags[i] != IP_TAG_END; i += 2)
        {
            switch(tags[i])
            {
            case IPIPSEC_SACTRLT_GET_SaSelector :
                ipcom_printf("%sSelector = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_SaCreator :
                ipcom_printf("%sCreator = %s"IP_LF, table, mib_Creator[tags[i+1]]);
                break;

            case IPIPSEC_SACTRLT_GET_Encapsulation :
                ipcom_printf("%sEncapsulation = %s"IP_LF, table, mib_Encapsulation[tags[i+1]]);
                break;

            case IPIPSEC_SACTRLT_GET_EncAlg :
                ipcom_printf("%sEncAlg = %s"IP_LF, table, mib_EncAlg[tags[i+1]]);
                break;

            case IPIPSEC_SACTRLT_GET_EncKeyLength :
                ipcom_printf("%sEncKeyLength = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_AuthAlg :
                ipcom_printf("%sAuthAlg = %s"IP_LF, table, mib_AuthAlg[tags[i+1]]);
                break;

            case IPIPSEC_SACTRLT_GET_AuthKeyLength :
                ipcom_printf("%sAuthKeyLength = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_RepWinSize :
                ipcom_printf("%sRepWinSize = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_LimitSeconds :
                ipcom_printf("%sLimitSeconds = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_LimitKbytes :
                ipcom_printf("%sLimitKbytes = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_AccSeconds :
                ipcom_printf("%sAccSeconds = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_InAccKbytes :
                ipcom_printf("%sInAccKbytes = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_OutAccKbytes :
                ipcom_printf("%sOutAccKbytes = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_DecryptErrors :
                ipcom_printf("%sDecryptErrors = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_AuthErrors :
                ipcom_printf("%sAuthErrors = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_ReplayErrors :
                ipcom_printf("%sReplayErrors = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_PolicyErrors :
                ipcom_printf("%sPolicyErrors = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_PadErrors :
                ipcom_printf("%sPadErrors = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_OtherReceiveErrors :
                ipcom_printf("%sOtherReceiveErrors = %ld"IP_LF, table, tags[i+1]);
                break;

            case IPIPSEC_SACTRLT_GET_OutSendErrors :
                ipcom_printf("%sOutSendErrors = %ld"IP_LF, table, tags[i+1]);
                break;

            default:
                ipcom_printf("error: unknown tag type '%ld'"IP_LF, tags[i]);
                break;
            }
        } /* for */

        ipcom_memcpy(&sa_u64.sa, &sa_next.arg.next, sizeof(Ipipsec_sa_handle));
        if (ipcom_socketioctl(vars->fd, IP_SIOCXIPSEC_InUserOctets, &sa_u64) == 0)
            ipcom_printf("%sInUserOctets = %ld"IP_LF, table, IP_U64_GETLO(sa_u64.arg.u64)); /*lint !e530 */
        if (ipcom_socketioctl(vars->fd, IP_SIOCXIPSEC_InUserPackets, &sa_u64) == 0)
            ipcom_printf("%sInUserPackets = %ld"IP_LF, table, IP_U64_GETLO(sa_u64.arg.u64)); /*lint !e530 */
        if (ipcom_socketioctl(vars->fd, IP_SIOCXIPSEC_OutUserOctets, &sa_u64) == 0)
            ipcom_printf("%sOutUserOctets = %ld"IP_LF, table, IP_U64_GETLO(sa_u64.arg.u64)); /*lint !e530 */
        if (ipcom_socketioctl(vars->fd, IP_SIOCXIPSEC_OutUserPackets, &sa_u64) == 0)
            ipcom_printf("%sOutUserPackets = %ld"IP_LF, table, IP_U64_GETLO(sa_u64.arg.u64)); /*lint !e530 */
    }
}
IP_STATIC
IPCOM_PROCESS( ipcom_cmd_smptest_server_spawn )
{
    union
    {
        struct Ip_sockaddr          sa;
#ifdef IPCOM_USE_INET6
        struct Ip_sockaddr_in6      sin6;
#endif
        struct Ip_sockaddr_storage  ss;
        struct Ip_sockaddr_in       sin;
    } addr;

    Ip_fd  listen_socket;
    Ip_fd  connect_sock = 0;
    int    opt_val = 1;
    int    portadd = spawn_number_server++;
    int    bytes = 0;
    int    sec = SECONDS_CLIENT + SECONDS_SERVER;
    unsigned char *buf = 0;
    int    num_recives = 0;
    unsigned long num_bytes = 0;
    struct Ip_sockaddr from;
    struct Ip_linger   linger;
    Ip_socklen_t       from_length = 0;
    int     retry_count = 0;

    ipcom_proc_init();

    ipcom_memset( &addr.ss, 0, sizeof( addr.ss ) );
    ipcom_memcpy( &addr.sa, smp_opt_server.res->ai_addr, smp_opt_server.res->ai_addrlen );

    linger.l_onoff  = 1;
    linger.l_linger = 2;

    listen_socket = ipcom_socket( smp_opt_server.res->ai_family, smp_opt_server.res->ai_socktype, smp_opt_server.res->ai_protocol );
    if (listen_socket == IP_SOCKERR)
    {
        ipcom_printf("Failed to create socket: %s"IP_LF, ipcom_strerror(ipcom_errno));
        return_server = 1;
        ipcom_sem_post( sem_wait_server );
        return;
    }

    if ( 0 != ipcom_setsockopt(listen_socket, IP_SOL_SOCKET, IP_SO_REUSEPORT, &opt_val, sizeof (opt_val)) )
    {
        ipcom_printf("ipcom_setsockopt failed: %s"IP_LF, ipcom_strerror(ipcom_errno));
        return_server = 1;
        ipcom_sem_post( sem_wait_server );
        return;
    }

    addr.sin.sin_port = ip_htons( smp_opt_server.port + portadd );   /* port is in the same place for IPv4 and IPv6 */
    if ( 0 != ipcom_bind( listen_socket, &addr.sa, sizeof( addr.ss ) ) )
    {
        ipcom_printf("Failed to bind: %s"IP_LF, ipcom_strerror(ipcom_errno));
        return_server = 1;
        ipcom_sem_post( sem_wait_server );
        return;
    }

    buf = ipcom_malloc( smp_opt_server.num_bytes );

    if ( smp_opt_server.tcp )
    {
        if ( -1 == ipcom_listen( listen_socket, 0 ) )
        {
            ipcom_printf("Listen failed: %s"IP_LF, ipcom_strerror(ipcom_errno));
            return_server = 1;
            ipcom_sem_post( sem_wait_server );
            return;
        }
        if ( verbose )
            ipcom_printf("Thread %d listens to %s:%d"IP_LF, portadd, smp_opt_server.res->ai_canonname, ip_ntohs( addr.sin.sin_port ) );
    }

    if ( 0 == ipcom_atomic_sub_and_return( &listen_wait, 1 ) )
    {
        /* Send to the test-server or write to stdout? */
        if ( server_out >= 0 )
        {
            char ready[] = "Ready.";
            ipcom_socketwrite(server_out, ready, 8 );
        }
        else
            ipcom_printf("Ready."IP_LF );
    }

    if ( 0 != ipcom_socketioctl( listen_socket, IP_X_SIOCSINTR, &sec ) )
    {
        ipcom_printf("ipcom_socketioctl failed: %s"IP_LF, ipcom_strerror(ipcom_errno));
        return_server = 1;
        ipcom_sem_post( sem_wait_server );
        return;
    }

    if ( smp_opt_server.tcp )
    {
retry:
        connect_sock = ipcom_accept( listen_socket, IP_NULL, 0 );
        if ( -1 == connect_sock )
        {
            if ( ipcom_errno == IP_ERRNO_EINTR )
            {
                if ( verbose )
                {
                    if ( ++retry_count < 5 )
                    {
                        ipcom_printf("Accept failed for thread %d: %s.. Retrying."IP_LF, portadd, ipcom_strerror(ipcom_errno));
                        goto retry;
                    }
                }
            }
            ipcom_printf("Accept failed for thread %d: %s"IP_LF, portadd, ipcom_strerror(ipcom_errno));
            return_server = 1;
            ipcom_socketclose( listen_socket );
            ipcom_sem_post( sem_wait_server );
            return;
        }

        if ( 0 != ipcom_socketioctl( connect_sock, IP_X_SIOCSINTR, &sec ) )
        {
            ipcom_printf("ipcom_socketioctl failed: %s"IP_LF, ipcom_strerror(ipcom_errno));
            return_server = 1;
            ipcom_sem_post( sem_wait_server );
            return;
        }

        if ( 0 != ipcom_setsockopt( connect_sock, IP_SOL_SOCKET, IP_SO_LINGER, &linger, sizeof (linger)) )
        {
            ipcom_printf("ipcom_setsockopt failed: %s"IP_LF, ipcom_strerror(ipcom_errno));
            return_server = 1;
            ipcom_sem_post( sem_wait_server );
            return;
        }
    }

    while ( 1 )
    {
        num_recives++;
        if ( smp_opt_server.tcp )
            bytes = ipcom_recv( connect_sock, buf, smp_opt_server.num_bytes, 0);
        else
        {
            bytes = ipcom_recvfrom( listen_socket, buf, smp_opt_server.num_bytes, 0, &from, &from_length );
            if ( num_recives == 1 && 0 == smp_opt_server.tcp )
            {
                sec = SECONDS_CLIENT+2+portadd;
                if ( 0 != ipcom_socketioctl( listen_socket, IP_X_SIOCSINTR, &sec ) )
                {
                    ipcom_printf("ipcom_socketioctl failed: %s"IP_LF, ipcom_strerror(ipcom_errno));
                    return_server = 1;
                    ipcom_sem_post( sem_wait_server );
                    return;
                }
            }
        }

        if ( bytes > 0 )
            num_bytes += bytes;
        if (bytes == 0)
        {
            if ( verbose )
            {
                ipcom_printf("Thread %d done."IP_LF, portadd );
                ipcom_printf("  Recives: %d"IP_LF, num_recives );
                ipcom_printf("  MB/s:    %f"IP_LF, ((float)(num_bytes)/(1024.0f*1024.0f) )/5.0f );
            }
            break;
        }
        else if (bytes < 0)
        {
            if ( ipcom_errno == IP_ERRNO_EINTR )
            {
                if ( smp_opt_server.tcp )
                {
                    return_server = 1;
                    ipcom_printf("Error! Out of time!"IP_LF );
                    break;
                }
                else
                {
                    if ( verbose )
                    {
                        ipcom_printf("Thread %d done."IP_LF, portadd );
                        ipcom_printf("  Recives: %d"IP_LF, num_recives );
                        ipcom_printf("  MB/s:    %f"IP_LF, ((float)(num_bytes)/(1024.0f*1024.0f) )/5.0f );
                    }
                    break;
                }
            }
            else if ( bytes == IP_SOCKERR )    /* Connection reset by peer */
            {
            }

            return_server = 1;
            ipcom_printf("recv failed: %s"IP_LF, ipcom_strerror(ipcom_errno));
            return;
        }
    }

    if ( verbose )
    {
        if ( spawn_number_server != smp_opt_server.num_sock )
        {
            ipcom_printf("Error. Only %d server-sockets seemed to work."IP_LF, spawn_number_server );
        }
    }

    if ( smp_opt_server.tcp )
        ipcom_shutdown( connect_sock, IP_SHUT_RDWR );

    ipcom_free( buf );
    ipcom_socketclose( listen_socket );

    if ( 0 == ipcom_atomic_sub_and_return( &num_wait_server, 1 ) )
        ipcom_sem_post( sem_wait_server );

    if ( smp_opt_server.tcp )
        ipcom_socketclose( connect_sock );

    ipcom_proc_exit();
}
/*
 *===========================================================================
 *                    ipnet_config_interface
 *===========================================================================
 * Description: Configures one interface if it exists and is UP.
 * Parameters:  fd - A socket descriptor
 *              ifname - The name of the interface to configure
 *              conf - The configuration to apply to the interface
 *              conf_len - Number of valid entries in the "conf" array
 *              pass - First pass (1) will add all addresses
 *                     second pass (2) will add other things like gateways
 * Returns:     0 = success
 *             <0 = error code
 *
 */
IP_STATIC int
ipnet_config_interface(Ip_fd fd, char *ifname, char *conf[], int conf_len, int pass)
{
    char *option;
    char *option_org;
    char *token;
    int   i;
    int   err;

    ip_assert(pass == 1 || pass == 2);

    if (ipcom_if_nametoindex(ifname) == 0)
    {
        /* Interface does not exist */
        IPCOM_LOG1(INFO, "%s not attached - skipping", ifname);
        return 0;
    }

    if (0 > (err = ipnet_config_bring_up_if(ifname)))
    {
        IPCOM_LOG1(INFO, "Failed to bring UP %s", ifname);
        return 0;
    }

    for (i = 0; i < conf_len; i++)
    {
        option_org = option = ipcom_strdup(conf[i]);
        if (option == IP_NULL)
            return -IP_ERRNO_ENOMEM;

        token = ipcom_strtok_r(option, " \t/", &option);

        if (token == IP_NULL || ipcom_strcmp(token, "ifname") == 0)
        {
            /* Ignore */
            err = 0;
        }
#ifdef IPCOM_USE_INET
        else if (ipcom_strcmp(token, "inet") == 0)
        {
            if (pass == 1)
                err = ipnet_config_add_inet_addr(fd, ifname, option);
        }
        else if (ipcom_strcmp(token, "gateway") == 0)
        {
            if (pass == 2)
                err = ipnet_config_add_gateway(fd, ifname, option);
        }
#else
        else if (ipcom_strcmp(token, "inet") == 0
                 || ipcom_strcmp(token, "gateway") == 0)
            IP_NOOP;
#endif /* IPCOM_USE_INET */
#ifdef IPCOM_USE_INET6
        else if (ipcom_strcmp(token, "inet6") == 0)
        {
            if (pass == 1)
                err = ipnet_config_add_inet6_addr(fd, ifname, option);
        }
        else if (ipcom_strcmp(token, "gateway6") == 0)
        {
            if (pass == 2)
                err = ipnet_config_add_gateway6(ifname, option);
        }
#else
        else if (ipcom_strcmp(token, "inet6") == 0
                 || ipcom_strcmp(token, "gateway6") == 0)
            IP_NOOP;
#endif /* IPCOM_USE_INET6 */
        else if (ipcom_strcmp(token, "devname") != 0)
            /* Invalid configuration */
            err = -IP_ERRNO_EINVAL;

        ipcom_free(option_org);
        if (err != 0)
        {
            if (err < 0)
                err = -err;
            IPCOM_LOG4(ERR, "failed on '%s' when configuring %s: %s (%d)",
                conf[i], ifname, ipcom_strerror(err), err);
            IP_PANIC();
            break;
        }
    }

    return err;
}
IP_STATIC
IPCOM_PROCESS( ipcom_cmd_smptest_client_spawn )
{
    union
    {
        struct Ip_sockaddr          sa;
#ifdef IPCOM_USE_INET6
        struct Ip_sockaddr_in6      sin6;
#endif
        struct Ip_sockaddr_storage  ss;
        struct Ip_sockaddr_in       sin;
    } addr;
    Ip_fd socket;
    unsigned char *buf = 0;
    int portadd = spawn_number_client++;
    int opt_val = 1;
    int sec = SECONDS_CLIENT;
    int send = 0;
    int num_sends = 0;
    unsigned long num_bytes = 0;

    ipcom_proc_init();

    ipcom_memset( &addr.ss, 0, sizeof( addr.ss ) );
    ipcom_memcpy( &addr.sa, smp_opt_client.res->ai_addr, smp_opt_client.res->ai_addrlen );

    socket = ipcom_socket( smp_opt_client.res->ai_family, smp_opt_client.res->ai_socktype, smp_opt_client.res->ai_protocol );
    if (socket == IP_SOCKERR)
    {
        ipcom_printf("Failed to create socket for thread %d: %s"IP_LF, portadd, ipcom_strerror(ipcom_errno));
        return_client = 1;
        ipcom_sem_post( sem_wait_client );
        return;
    }

    if ( 0 != ipcom_setsockopt(socket, IP_SOL_SOCKET, IP_SO_REUSEPORT, &opt_val, sizeof (opt_val)) )
    {
        ipcom_printf("ipcom_setsockopt failed: %s"IP_LF, ipcom_strerror(ipcom_errno));
        return_client = 1;
        ipcom_sem_post( sem_wait_client );
        return;
    }

    if ( 0 != ipcom_socketioctl( socket, IP_X_SIOCSINTR, &sec ) )
    {
        ipcom_printf("ipcom_socketioctl failed: %s"IP_LF, ipcom_strerror(ipcom_errno));
        return_client = 1;
        ipcom_sem_post( sem_wait_client );
        return;
    }

    addr.sin.sin_port = ip_htons( smp_opt_client.port + portadd );   /* port is in the same place for IPv4 and IPv6 */
    if ( smp_opt_client.tcp )
    {
        if ( 0 != ipcom_connect( socket, &addr.sa, smp_opt_client.res->ai_addrlen ) )
        {
            ipcom_printf("Thread %d failed to connect: %s"IP_LF, portadd, ipcom_strerror(ipcom_errno));
            return_client = 1;
            ipcom_sem_post( sem_wait_client );
            return;
        }
        if ( verbose )
            ipcom_printf("Thread %d connected to port %d"IP_LF, portadd, smp_opt_client.port + portadd );
    }

    buf = ipcom_malloc( smp_opt_client.num_bytes );
    while ( 1 )
    {
        if ( smp_opt_client.tcp )
            send = ipcom_send( socket, buf, smp_opt_client.num_bytes, 0);
        else
            send = ipcom_sendto( socket, buf, smp_opt_client.num_bytes, 0, &addr.sa, smp_opt_client.res->ai_addrlen );

        if ( send > 0 )
           num_bytes += send;
        num_sends++;
        if ( send == 0 )
        {
           ipcom_printf( "Error: Disconnected"IP_LF );
           return_client = 1;
           break;
        }
        else if ( send < 0 )
        {
            if ( ipcom_errno == IP_ERRNO_EINTR )
            {
                if ( verbose )
                {
                    ipcom_printf("Thread %d done."IP_LF, portadd );
                    ipcom_printf("  Sends: %d"IP_LF, num_sends );
                    ipcom_printf("  MB/s:  %f"IP_LF, ((float)(num_bytes)/(1024.0f*1024.0f) )/5.0f );
                }
                break;
            }
            return_client = 1;
            ipcom_printf("Error on thread %d: %s"IP_LF, portadd, ipcom_strerror(ipcom_errno));
            break;
        }
    }

    if ( verbose )
    {
        if ( spawn_number_client != smp_opt_client.num_sock )
        {
            ipcom_printf("Error. Only %d client-sockets seemed to work."IP_LF, spawn_number_client );
        }
    }

    ipcom_socketclose( socket );

    ipcom_free( buf );
    if ( 0 == ipcom_atomic_sub_and_return( &num_wait_client, 1 ) )
        ipcom_sem_post( sem_wait_client );

    ipcom_proc_exit();
}
Example #23
0
/*
 *===========================================================================
 *                    ipcom_cmd_sockperf_run
 *===========================================================================
 * Description: Sends/receives data.
 * Parameters:
 * Returns:
 */
IP_STATIC void
ipcom_cmd_sockperf_run(Ipcom_cmd_sockperf_t *cmd)
{
    struct Ip_timeval  tmo = { 0, 0 };
    struct Ip_timeval *ptmo;
    struct Ip_timeval  start;
    struct Ip_timeval  stop;
    Ip_u32             i;
    Ip_u32             c;
    int                num_ready;
    Ip_fd             *s = cmd->sock_array;
    Ip_u32             total_bytes_to_send = cmd->num_buf * cmd->buf_len;
    Ip_u32             finished_sockets = 0;
    Ip_u32             total_bytes_read = 0;
    Ip_ssize_t         bytes;
    Ip_fd_set          read_set;
    int                send_flags = (cmd->transmit && cmd->receive) ? IP_MSG_DONTWAIT : 0;

    if (cmd->transmit)
        ipcom_printf("sockperf-t: send buffer is %u"IP_LF, cmd->sendbuf_size);
    if (cmd->receive)
        ipcom_printf("sockperf-r: receive buffer is %u"IP_LF, cmd->sendbuf_size);
    for (i = 0; i < cmd->num_sock; i++)
    {
        int on = 1;

        if (ipcom_setsockopt(s[i],
                             IP_SOL_SOCKET,
                             IP_SO_REUSEADDR,
                             &on,
                             sizeof(on)) < 0)
        {
            ipcom_printf("sockperf-c: setsockopt IP_SO_REUSEADDR failed : %s"IP_LF,
                         ipcom_strerror(ipcom_errno));
            return;
        }

        if (ipcom_setsockopt(s[i],
                             IP_SOL_SOCKET,
                             IP_SO_SNDBUF,
                             &cmd->sendbuf_size,
                             sizeof(cmd->sendbuf_size)) < 0)
        {
            ipcom_printf("sockperf-c: setsockopt IP_SO_SNDBUF failed : %s"IP_LF,
                         ipcom_strerror(ipcom_errno));
            return;
        }

        if (ipcom_setsockopt(s[i],
                             IP_SOL_SOCKET,
                             IP_SO_RCVBUF,
                             &cmd->recvbuf_size,
                             sizeof(cmd->recvbuf_size)) < 0)
        {
            ipcom_printf("sockperf-c: setsockopt IP_SO_SNDBUF failed : %s"IP_LF,
                         ipcom_strerror(ipcom_errno));
            return;
        }
    }


    if (cmd->receive)
    {
        IP_FD_ZERO(&cmd->read_set);
        cmd->width = s[0];
        IP_FD_SET(s[0], &cmd->read_set);
        for (i = 1; i < cmd->num_sock; i++)
            if (s[i] != IP_INVALID_SOCKET)
            {
                cmd->width = IP_MAX(cmd->width, s[i]);
                IP_FD_SET(s[i], &cmd->read_set);
            }
    }

    ipcom_microtime(&start);
    while ((cmd->transmit && total_bytes_to_send)
           || (cmd->receive && finished_sockets < cmd->num_sock))
    {
        if (cmd->transmit && total_bytes_to_send)
        {
            for (i = 0; i < cmd->num_sock; i++)
            {
                if (cmd->testpattern)
                {
                    /* Test patter is "[B|E|D]xxxxxx ", B = first 8 byte in buffer, E = last 8 bytes, D = all other */
                    for (c = 0; c < cmd->buf_len; c += 8)
                        ipcom_sprintf(cmd->buf + c, "%c%06ld ",
                                      c == 0 ? '>' : (c >= cmd->buf_len - 8 ? '<' : '#'),
                                      cmd->send_pattern[i]++ % 1000000);
                }

                bytes = ipcom_send(s[i],
                                   cmd->buf,
                                   IP_MIN(cmd->buf_len, total_bytes_to_send),
                                   send_flags);
                if (bytes < 0 && ipcom_errno == IP_ERRNO_EWOULDBLOCK)
                    (void)ipcom_sleep(0);
                else
                {
                    if (bytes < 0)
                    {
                        ipcom_printf("sockperf-t: send failed : %s"IP_LF,
                                     ipcom_strerror(ipcom_errno));
                        return;
                    }
                    total_bytes_to_send -= bytes;
                }
            }

            if (cmd->receive && total_bytes_to_send == 0)
                for (i = 0; i < cmd->num_sock; i++)
                    if (ipcom_shutdown(s[i], IP_SHUT_WR) < 0)
                    {
                        ipcom_printf("sockperf-t: shutdown failed: %s"IP_LF,
                                     ipcom_strerror(ipcom_errno));
                        return;
                    }
        }

        if (cmd->receive)
        {
            ptmo = IP_NULL;
            while (finished_sockets < cmd->num_sock)
            {
                read_set = cmd->read_set;
                num_ready = ipcom_socketselect(cmd->width + 1, &read_set, IP_NULL, IP_NULL, ptmo);

                if (num_ready == 0)
                    break;

                if (num_ready < 0)
                {
                    ipcom_printf("sockperf-r: select failed: %s"IP_LF,
                                 ipcom_strerror(ipcom_errno));
                    return;
                }

                for (i = 0; i < cmd->num_sock; i++)
                {
                    if (IP_FD_ISSET(s[i], &read_set))
                    {
                        bytes = ipcom_recv(s[i], cmd->buf, cmd->buf_len, 0);
                        if (bytes < 0)
                        {
                            ipcom_printf("sockperf-r: recv failed: %s"IP_LF,
                                         ipcom_strerror(ipcom_errno));
                            return;
                        }

                        if (cmd->testpattern)
                        {
                            if (cmd->echo)
                                ipcom_cmd_sockperf_echo_buf(cmd->buf, bytes, cmd->recv_pattern[i]);
                            for (c = 0; c < (Ip_u32)bytes; c += 8)
                            {
                                if (cmd->buf[c] != '#' && cmd->buf[c] != '>' && cmd->buf[c] != '<')
                                {
                                    ipcom_printf("\nsockperf-r: test pattern error, expected B, D or E found %c(%d) offset %ld"IP_LF,
                                                 cmd->buf[c], (int)cmd->buf[c], c);
                                    ipcom_cmd_sockperf_echo_buf(cmd->buf, bytes, 0);
                                    return;
                                }

                                if (ipcom_atoi(cmd->buf + c + 1) != (int)(cmd->recv_pattern[i]++ % 1000000))
                                {
                                    ipcom_printf("\nsockperf-r: test pattern error, was %d should be %ld offset %ld"IP_LF,
                                                 ipcom_atoi(cmd->buf + c + 1), (cmd->recv_pattern[i] - 1) % 1000000, c);
                                    ipcom_cmd_sockperf_echo_buf(cmd->buf, bytes, 0);
                                    return;
                                }
                            }
                        }

                        if (bytes > 0)
                            total_bytes_read += bytes;
                        else
                        {
                            finished_sockets++;
                            IP_FD_CLR(s[i], &cmd->read_set);
                        }
                    }
                }

                if (cmd->transmit && total_bytes_to_send)
                    ptmo = &tmo;
            }
        }
    }
    ipcom_microtime(&stop);

    if (cmd->transmit)
        ipcom_printf("sockperf-t: %lu bytes sent in %ld ms (%lu kbyte/s)"IP_LF,
                     cmd->num_buf * cmd->buf_len,
                     ipcom_cmd_sockperf_tv_to_msec(&start, &stop),
                     cmd->num_buf * cmd->buf_len / (Ip_u32)ipcom_cmd_sockperf_tv_to_msec(&start, &stop) * 1000 / 1024);
    if (cmd->receive)
        ipcom_printf("sockperf-r: %lu bytes read in %ld ms (%lu kbyte/s)"IP_LF,
                     total_bytes_read,
                     ipcom_cmd_sockperf_tv_to_msec(&start, &stop),
                     total_bytes_read / (Ip_u32)ipcom_cmd_sockperf_tv_to_msec(&start, &stop) * 1000 / 1024);

}