Blame src/libnet_link_nit.c

Packit 03b34a
/*
Packit 03b34a
 *  $Id: libnet_link_nit.c,v 1.6 2004/01/03 20:31:02 mike Exp $
Packit 03b34a
 *
Packit 03b34a
 *  libnet
Packit 03b34a
 *  libnet_nit.c - network interface tap routines
Packit 03b34a
 *
Packit 03b34a
 *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
Packit 03b34a
 *  All rights reserved.
Packit 03b34a
 *
Packit 03b34a
 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
Packit 03b34a
 *	The Regents of the University of California.  All rights reserved.
Packit 03b34a
 *
Packit 03b34a
 * Redistribution and use in source and binary forms, with or without
Packit 03b34a
 * modification, are permitted provided that: (1) source code distributions
Packit 03b34a
 * retain the above copyright notice and this paragraph in its entirety, (2)
Packit 03b34a
 * distributions including binary code include the above copyright notice and
Packit 03b34a
 * this paragraph in its entirety in the documentation or other materials
Packit 03b34a
 * provided with the distribution, and (3) all advertising materials mentioning
Packit 03b34a
 * features or use of this software display the following acknowledgement:
Packit 03b34a
 * ``This product includes software developed by the University of California,
Packit 03b34a
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
Packit 03b34a
 * the University nor the names of its contributors may be used to endorse
Packit 03b34a
 * or promote products derived from this software without specific prior
Packit 03b34a
 * written permission.
Packit 03b34a
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
Packit 03b34a
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
Packit 03b34a
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Packit 03b34a
 */
Packit 03b34a
Packit 03b34a
#if (HAVE_CONFIG_H)
Packit 03b34a
#include "../include/config.h"
Packit 03b34a
#endif
Packit 03b34a
#if (!(_WIN32) || (__CYGWIN__)) 
Packit 03b34a
#include "../include/libnet.h"
Packit 03b34a
#else
Packit 03b34a
#include "../include/win32/libnet.h"
Packit 03b34a
#endif
Packit 03b34a
Packit 03b34a
#include "../include/gnuc.h"
Packit 03b34a
#ifdef HAVE_OS_PROTO_H
Packit 03b34a
#include "../include/os-proto.h"
Packit 03b34a
#endif
Packit 03b34a
Packit 03b34a
struct libnet_link_int *
Packit 03b34a
libnet_open_link_interface(int8_t *device, int8_t *ebuf)
Packit 03b34a
{
Packit 03b34a
    struct sockaddr_nit snit;
Packit 03b34a
    register struct libnet_link_int *l;
Packit 03b34a
Packit 03b34a
    l = (struct libnet_link_int *)malloc(sizeof(*p));
Packit 03b34a
    if (l == NULL)
Packit 03b34a
    {
Packit 03b34a
        strcpy(ebuf, strerror(errno));
Packit 03b34a
        return (NULL);
Packit 03b34a
    }
Packit 03b34a
Packit 03b34a
    memset(l, 0, sizeof(*l));
Packit 03b34a
Packit 03b34a
    l->fd = socket(AF_NIT, SOCK_RAW, NITPROTO_RAW);
Packit 03b34a
    if (l->fd < 0)
Packit 03b34a
    {
Packit 03b34a
        sprintf(ebuf, "socket: %s", strerror(errno));
Packit 03b34a
        goto bad;
Packit 03b34a
    }
Packit 03b34a
    snit.snit_family = AF_NIT;
Packit 03b34a
	strncpy(snit.snit_ifname, device, NITIFSIZ -1);
Packit 03b34a
    snit.snit_ifname[NITIFSIZ] = '\0';
Packit 03b34a
Packit 03b34a
    if (bind(l->fd, (struct sockaddr *)&snit, sizeof(snit)))
Packit 03b34a
    {
Packit 03b34a
        sprintf(ebuf, "bind: %s: %s", snit.snit_ifname, strerror(errno));
Packit 03b34a
        goto bad;
Packit 03b34a
    }
Packit 03b34a
Packit 03b34a
    /*
Packit 03b34a
     * NIT supports only ethernets.
Packit 03b34a
     */
Packit 03b34a
    l->linktype = DLT_EN10MB;
Packit 03b34a
Packit 03b34a
    return (l);
Packit 03b34a
Packit 03b34a
bad:
Packit 03b34a
    if (l->fd >= 0)
Packit 03b34a
    {
Packit 03b34a
        close(l->fd);
Packit 03b34a
    }
Packit 03b34a
    free(l);
Packit 03b34a
    return (NULL);
Packit 03b34a
}
Packit 03b34a
Packit 03b34a
Packit 03b34a
int
Packit 03b34a
libnet_close_link_interface(struct libnet_link_int *l)
Packit 03b34a
{
Packit 03b34a
    if (close(l->fd) == 0)
Packit 03b34a
    {
Packit 03b34a
        free(l);
Packit 03b34a
        return (1);
Packit 03b34a
    }
Packit 03b34a
    else
Packit 03b34a
    {
Packit 03b34a
        free(l);
Packit 03b34a
        return (-1);
Packit 03b34a
    }
Packit 03b34a
}
Packit 03b34a
Packit 03b34a
Packit 03b34a
int
Packit 03b34a
write_link_layer(struct libnet_link_int *l, const int8_t *device,
Packit 03b34a
            uint8_t *buf, int len)
Packit 03b34a
{
Packit 03b34a
    int c;
Packit 03b34a
    struct sockaddr sa;
Packit 03b34a
Packit 03b34a
    memset(&sa, 0, sizeof(sa));
Packit 03b34a
    strncpy(sa.sa_data, device, sizeof(sa.sa_data));
Packit 03b34a
Packit 03b34a
    c = sendto(l->fd, buf, len, 0, &sa, sizeof(sa));
Packit 03b34a
    if (c != len)
Packit 03b34a
    {
Packit 03b34a
        /* error */
Packit 03b34a
    }
Packit 03b34a
    return (c);
Packit 03b34a
}
Packit 03b34a