Annotation of embedaddon/libpdel/net/test/iftest.c, revision 1.1.1.1

1.1       misho       1: 
                      2: /*
                      3:  * Copyright (c) 2001-2002 Packet Design, LLC.
                      4:  * All rights reserved.
                      5:  * 
                      6:  * Subject to the following obligations and disclaimer of warranty,
                      7:  * use and redistribution of this software, in source or object code
                      8:  * forms, with or without modifications are expressly permitted by
                      9:  * Packet Design; provided, however, that:
                     10:  * 
                     11:  *    (i)  Any and all reproductions of the source or object code
                     12:  *         must include the copyright notice above and the following
                     13:  *         disclaimer of warranties; and
                     14:  *    (ii) No rights are granted, in any manner or form, to use
                     15:  *         Packet Design trademarks, including the mark "PACKET DESIGN"
                     16:  *         on advertising, endorsements, or otherwise except as such
                     17:  *         appears in the above copyright notice or in the software.
                     18:  * 
                     19:  * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
                     20:  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
                     21:  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
                     22:  * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
                     23:  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
                     24:  * OR NON-INFRINGEMENT.  PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
                     25:  * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
                     26:  * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
                     27:  * RELIABILITY OR OTHERWISE.  IN NO EVENT SHALL PACKET DESIGN BE
                     28:  * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
                     29:  * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
                     30:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
                     31:  * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
                     32:  * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
                     33:  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
                     35:  * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
                     36:  * THE POSSIBILITY OF SUCH DAMAGE.
                     37:  *
                     38:  * Author: Archie Cobbs <archie@freebsd.org>
                     39:  */
                     40: 
                     41: #include <sys/types.h>
                     42: #include <sys/socket.h>
                     43: #include <netinet/in.h>
                     44: #include <arpa/inet.h>
                     45: #include <net/if.h>
                     46: 
                     47: #include <stdio.h>
                     48: #include <stdarg.h>
                     49: #include <string.h>
                     50: #include <unistd.h>
                     51: #include <errno.h>
                     52: #include <err.h>
                     53: 
                     54: #include <pdel/structs/structs.h>
                     55: #include <pdel/structs/type/array.h>
                     56: #include <pdel/util/typed_mem.h>
                     57: #include <pdel/net/if_util.h>
                     58: 
                     59: int
                     60: main(int ac, char **av)
                     61: {
                     62:        struct in_addr *iplist;
                     63:        struct in_addr *nmlist;
                     64:        char **ifnames;
                     65:        int nif;
                     66:        int val;
                     67:        int ch;
                     68:        int i;
                     69: 
                     70:        while ((ch = getopt(ac, av, "")) != -1) {
                     71:                switch (ch) {
                     72:                default:
                     73:                        goto usage;
                     74:                }
                     75:        }
                     76:        ac -= optind;
                     77:        av += optind;
                     78: 
                     79:        if ((nif = if_get_list(&ifnames, TYPED_MEM_TEMP)) == NULL)
                     80:                err(1, "if_list");
                     81:        for (i = 0; i < nif; i++) {
                     82:                printf("interface \"%s\":\n", ifnames[i]);
                     83:                printf("     type=");
                     84:                if ((val = if_get_type(ifnames[i])) != -1)
                     85:                        printf("%d\n", val);
                     86:                else
                     87:                        printf("%s\n", strerror(errno));
                     88:                printf("    flags=");
                     89:                if ((val = if_get_flags(ifnames[i])) != -1)
                     90:                        printf("0x%x\n", val);
                     91:                else
                     92:                        printf("%s\n", strerror(errno));
                     93:                printf("      mtu=");
                     94:                if ((val = if_get_mtu(ifnames[i])) != -1)
                     95:                        printf("%d\n", val);
                     96:                else
                     97:                        printf("%s\n", strerror(errno));
                     98:                if ((val = if_get_ip_addrs(ifnames[i],
                     99:                    &iplist, &nmlist, TYPED_MEM_TEMP)) == -1)
                    100:                        printf("  ipaddrs=%s\n", strerror(errno));
                    101:                else {
                    102:                        int j;
                    103: 
                    104:                        printf("  ipaddrs=[%d]:", val);
                    105:                        for (j = 0; j < val; j++) {
                    106:                                printf(" %s/0x%08x", inet_ntoa(iplist[j]),
                    107:                                    (u_int32_t)ntohl(nmlist[j].s_addr));
                    108:                        }
                    109:                        printf("\n");
                    110:                        FREE(TYPED_MEM_TEMP, iplist);
                    111:                        FREE(TYPED_MEM_TEMP, nmlist);
                    112:                }
                    113:        }
                    114:        if (ac > 0) {
                    115:                int flags;
                    116: 
                    117:                if ((flags = if_get_flags(av[0])) == -1)
                    118:                        err(1, "if_get_flags(%s)", av[0]);
                    119:                if (if_set_flags(av[0], flags | IFF_UP) == -1)
                    120:                        err(1, "if_set_flags(%s)", av[0]);
                    121:        }
                    122:        while (nif > 0)
                    123:                FREE(TYPED_MEM_TEMP, ifnames[--nif]);
                    124:        FREE(TYPED_MEM_TEMP, ifnames);
                    125:        return (0);
                    126: 
                    127: usage:
                    128:        fprintf(stderr, "usage: iftest [interface-to-mark-up]\n");
                    129:        return (1);
                    130: }
                    131: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>