|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* $Id: libnet_link_snit.c,v 1.6 2004/01/03 20:31:02 mike Exp $
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* libnet
|
|
Packit |
03b34a |
* libnet_snit.c - snit 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 |
* Modifications made to accommodate the new SunOS4.0 NIT facility by
|
|
Packit |
03b34a |
* Micky Liu, micky@cunixc.cc.columbia.edu, Columbia University in May, 1989.
|
|
Packit |
03b34a |
* This module now handles the STREAMS based NIT.
|
|
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 |
#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 |
|
|
Packit |
03b34a |
struct libnet_link_int *
|
|
Packit |
03b34a |
libnet_open_link_interface(int8_t *device, int8_t *ebuf)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
struct strioctl si; /* struct for ioctl() */
|
|
Packit |
03b34a |
struct ifreq ifr; /* interface request struct */
|
|
Packit |
03b34a |
static int8_t dev[] = "/dev/nit";
|
|
Packit |
03b34a |
register struct libnet_link_int *l;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
l = (struct libnet_link_int *)malloc(sizeof(*l));
|
|
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 = open(dev, O_RDWR);
|
|
Packit |
03b34a |
if (l->fd < 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
sprintf(ebuf, "%s: %s", dev, strerror(errno));
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* arrange to get discrete messages from the STREAM and use NIT_BUF
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
if (ioctl(l->fd, I_SRDOPT, (int8_t *)RMSGD) < 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
sprintf(ebuf, "I_SRDOPT: %s", strerror(errno));
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
if (ioctl(l->fd, I_PUSH, "nbuf") < 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
sprintf(ebuf, "push nbuf: %s", strerror(errno));
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* request the interface
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name) -1);
|
|
Packit |
03b34a |
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
|
|
Packit |
03b34a |
si.ic_cmd = NIOCBIND;
|
|
Packit |
03b34a |
si.ic_len = sizeof(ifr);
|
|
Packit |
03b34a |
si.ic_dp = (int8_t *)𝔦
|
|
Packit |
03b34a |
if (ioctl(l->fd, I_STR, (int8_t *)&si) < 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
sprintf(ebuf, "NIOCBIND: %s: %s", ifr.ifr_name, strerror(errno));
|
|
Packit |
03b34a |
goto bad;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
ioctl(l->fd, I_FLUSH, (int8_t *)FLUSHR);
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* NIT supports only ethernets.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
l->linktype = DLT_EN10MB;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
return (l);
|
|
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 |
libnet_write_link_layer(struct libnet_link_int *l, const int8_t *device,
|
|
Packit |
03b34a |
const 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 |
/* err */
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
return (c);
|
|
Packit |
03b34a |
}
|