File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / src / libnet_link_snit.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Sep 27 11:11:38 2023 UTC (9 months ago) by misho
Branches: libnet, MAIN
CVS tags: v1_2p1, HEAD
Version 1.2p1

    1: /*
    2:  *  $Id: libnet_link_snit.c,v 1.1.1.3 2023/09/27 11:11:38 misho Exp $
    3:  *
    4:  *  libnet
    5:  *  libnet_snit.c - snit routines
    6:  *
    7:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
    8:  *  All rights reserved.
    9:  *
   10:  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
   11:  *	The Regents of the University of California.  All rights reserved.
   12:  *
   13:  * Redistribution and use in source and binary forms, with or without
   14:  * modification, are permitted provided that: (1) source code distributions
   15:  * retain the above copyright notice and this paragraph in its entirety, (2)
   16:  * distributions including binary code include the above copyright notice and
   17:  * this paragraph in its entirety in the documentation or other materials
   18:  * provided with the distribution, and (3) all advertising materials mentioning
   19:  * features or use of this software display the following acknowledgement:
   20:  * ``This product includes software developed by the University of California,
   21:  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
   22:  * the University nor the names of its contributors may be used to endorse
   23:  * or promote products derived from this software without specific prior
   24:  * written permission.
   25:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
   26:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
   27:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
   28:  *
   29:  * Modifications made to accommodate the new SunOS4.0 NIT facility by
   30:  * Micky Liu, micky@cunixc.cc.columbia.edu, Columbia University in May, 1989.
   31:  * This module now handles the STREAMS based NIT.
   32:  */
   33: 
   34: #include "common.h"
   35: 
   36: #include "../include/gnuc.h"
   37: #ifdef HAVE_OS_PROTO_H
   38: #include "../include/os-proto.h"
   39: #endif
   40: 
   41: struct libnet_link_int *
   42: libnet_open_link_interface(int8_t *device, int8_t *ebuf)
   43: {
   44:     struct strioctl si;	    /* struct for ioctl() */
   45:     struct ifreq ifr;       /* interface request struct */
   46:     static int8_t dev[] = "/dev/nit";
   47:     register struct libnet_link_int *l;
   48: 
   49:     l = (struct libnet_link_int *)malloc(sizeof(*l));
   50:     if (l == NULL)
   51:     {
   52:         strcpy(ebuf, strerror(errno));
   53:         return (NULL);
   54:     }
   55: 
   56:     memset(l, 0, sizeof(*l));
   57: 
   58:     l->fd  = open(dev, O_RDWR);
   59:     if (l->fd < 0)
   60:     {
   61:         snprintf(ebuf, LIBNET_ERRBUF_SIZE,
   62:                  "%s: %s", dev, strerror(errno));
   63:         goto bad;
   64:     }
   65: 
   66:     /*
   67:      *  arrange to get discrete messages from the STREAM and use NIT_BUF
   68:      */
   69:     if (ioctl(l->fd, I_SRDOPT, (int8_t *)RMSGD) < 0)
   70:     {
   71:         snprintf(ebuf, LIBNET_ERRBUF_SIZE,
   72:                  "I_SRDOPT: %s", strerror(errno));
   73:         goto bad;
   74:     }
   75:     if (ioctl(l->fd, I_PUSH, "nbuf") < 0)
   76:     {
   77:         snprintf(ebuf, LIBNET_ERRBUF_SIZE,
   78:                  "push nbuf: %s", strerror(errno));
   79:         goto bad;
   80:     }
   81:     /*
   82:      *  request the interface
   83:      */
   84:     strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name) -1);
   85:     ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
   86:     si.ic_cmd = NIOCBIND;
   87:     si.ic_len = sizeof(ifr);
   88:     si.ic_dp = (int8_t *)&ifr;
   89:     if (ioctl(l->fd, I_STR, (int8_t *)&si) < 0)
   90:     {
   91:         snprintf(ebuf, LIBNET_ERRBUF_SIZE,
   92:                  "NIOCBIND: %s: %s", ifr.ifr_name, strerror(errno));
   93:         goto bad;
   94:     }
   95: 
   96:     ioctl(l->fd, I_FLUSH, (int8_t *)FLUSHR);
   97:     /*
   98:      * NIT supports only ethernets.
   99:      */
  100:     l->linktype = DLT_EN10MB;
  101: 
  102:     return (l);
  103: bad:
  104:     if (l->fd >= 0)
  105:     {
  106:         close(l->fd);
  107:     }
  108:     free(l);
  109:     return (NULL);
  110: }
  111: 
  112: 
  113: int
  114: libnet_close_link_interface(struct libnet_link_int *l)
  115: {
  116:     if (close(l->fd) == 0)
  117:     {
  118:         free(l);
  119:         return (1);
  120:     }
  121:     else
  122:     {
  123:         free(l);
  124:         return (-1);
  125:     }
  126: }
  127: 
  128: 
  129: int
  130: libnet_write_link_layer(struct libnet_link_int *l, const int8_t *device,
  131:             const uint8_t *buf, int len)
  132: {
  133:     int c;
  134:     struct sockaddr sa;
  135: 
  136:     memset(&sa, 0, sizeof(sa));
  137:     strncpy(sa.sa_data, device, sizeof(sa.sa_data));
  138: 
  139:     c = sendto(l->fd, buf, len, 0, &sa, sizeof(sa));
  140:     if (c != len)
  141:     {
  142:         /* err */
  143:         return (-1);
  144:     }
  145:     return (c);
  146: }
  147: 
  148: /**
  149:  * Local Variables:
  150:  *  indent-tabs-mode: nil
  151:  *  c-file-style: "stroustrup"
  152:  * End:
  153:  */

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