Annotation of embedaddon/libpdel/net/if_ip.c, revision 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/param.h>
        !            43: #include <sys/socket.h>
        !            44: #include <sys/sysctl.h>
        !            45: #include <sys/sockio.h>
        !            46: #include <sys/ioctl.h>
        !            47: 
        !            48: #include <net/if.h>
        !            49: #include <net/if_dl.h>
        !            50: #include <net/if_types.h>
        !            51: #include <net/route.h>
        !            52: 
        !            53: #include <netinet/in.h>
        !            54: #include <netinet/if_ether.h>
        !            55: #include <arpa/inet.h>
        !            56: 
        !            57: #include <stdlib.h>
        !            58: #include <unistd.h>
        !            59: #include <string.h>
        !            60: #include <assert.h>
        !            61: #include <errno.h>
        !            62: #include <time.h>
        !            63: 
        !            64: #include "structs/structs.h"
        !            65: #include "structs/type/array.h"
        !            66: 
        !            67: #include "net/if_util.h"
        !            68: 
        !            69: /*
        !            70:  * Internal functions
        !            71:  */
        !            72: static int     if_do_ip(const char *iface, int cmd, struct in_addr ip,
        !            73:                        struct in_addr mask, struct in_addr dest);
        !            74: 
        !            75: /*
        !            76:  * Add an IP address assignment to a broadcast or p2p interface.
        !            77:  *
        !            78:  * Returns -1 and sets errno if there was a problem.
        !            79:  */
        !            80: int
        !            81: if_add_ip_addr(const char *iface, struct in_addr ip,
        !            82:        struct in_addr mask, struct in_addr dest)
        !            83: {
        !            84:        return (if_do_ip(iface, SIOCAIFADDR, ip, mask, dest));
        !            85: }
        !            86: 
        !            87: /*
        !            88:  * Remove an IP address assignment from a or p2p broadcast interface.
        !            89:  *
        !            90:  * Returns -1 and sets errno if there was a problem.
        !            91:  */
        !            92: int
        !            93: if_del_ip_addr(const char *iface, struct in_addr ip,
        !            94:        struct in_addr mask, struct in_addr dest)
        !            95: {
        !            96:        return (if_do_ip(iface, SIOCDIFADDR, ip, mask, dest));
        !            97: }
        !            98: 
        !            99: static int
        !           100: if_do_ip(const char *iface, int cmd, struct in_addr ip,
        !           101:        struct in_addr mask, struct in_addr dest)
        !           102: {
        !           103:        struct in_addr bcast;
        !           104:        const struct in_addr *ips[] = {
        !           105:                &ip,
        !           106:                &mask,
        !           107:                &bcast
        !           108:        };
        !           109:        struct ifaliasreq ifa;
        !           110:        struct sockaddr_in *const sas[] = {
        !           111:                (struct sockaddr_in *)(void *)&ifa.ifra_addr,
        !           112:                (struct sockaddr_in *)(void *)&ifa.ifra_mask,
        !           113:                (struct sockaddr_in *)(void *)&ifa.ifra_broadaddr
        !           114:        };
        !           115:        int sock;
        !           116:        int rtn;
        !           117:        int i;
        !           118: 
        !           119:        /* Set up broadcast/point2point destination */
        !           120:        if (dest.s_addr == 0)
        !           121:                bcast.s_addr = ip.s_addr | ~mask.s_addr;
        !           122:        else
        !           123:                bcast = dest;
        !           124: 
        !           125:        /* Set up message */
        !           126:        memset(&ifa, 0, sizeof(ifa));
        !           127:        strncpy(ifa.ifra_name, iface, sizeof(ifa.ifra_name));
        !           128:        for (i = 0; i < 3; i++) {
        !           129:                struct sockaddr_in *const sin = sas[i];
        !           130: 
        !           131:                memset(sin, 0, sizeof(*sin));
        !           132:                sin->sin_family = AF_INET;
        !           133:                sin->sin_len = sizeof(*sin);
        !           134:                sin->sin_addr = *ips[i];
        !           135:        }
        !           136: 
        !           137:        /* Send message */
        !           138:        if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
        !           139:                return (-1);
        !           140:        rtn = ioctl(sock, cmd, &ifa);
        !           141:        (void)close(sock);
        !           142: 
        !           143:        /* Done */
        !           144:        return (rtn);
        !           145: }
        !           146: 

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