home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / src / nit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  3.0 KB  |  115 lines

  1. /*
  2.  *  $Id: nit.c,v 1.1.1.1 1999/05/18 15:33:42 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  nit.c - network interface tap routines
  6.  *
  7.  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
  8.  *                           route|daemon9 <route@infonexus.com>
  9.  *  All rights reserved.
  10.  *
  11.  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
  12.  *    The Regents of the University of California.  All rights reserved.
  13.  *
  14.  * Redistribution and use in source and binary forms, with or without
  15.  * modification, are permitted provided that: (1) source code distributions
  16.  * retain the above copyright notice and this paragraph in its entirety, (2)
  17.  * distributions including binary code include the above copyright notice and
  18.  * this paragraph in its entirety in the documentation or other materials
  19.  * provided with the distribution, and (3) all advertising materials mentioning
  20.  * features or use of this software display the following acknowledgement:
  21.  * ``This product includes software developed by the University of California,
  22.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  23.  * the University nor the names of its contributors may be used to endorse
  24.  * or promote products derived from this software without specific prior
  25.  * written permission.
  26.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  27.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  28.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  29.  */
  30.  
  31. #if (HAVE_CONFIG_H)
  32. #include "../include/config.h"
  33. #endif
  34. #include "../include/libnet.h"
  35.  
  36. #include "../include/gnuc.h"
  37. #ifdef HAVE_OS_PROTO_H
  38. #include "../include/os-proto.h"
  39. #endif
  40.  
  41. struct link_int *
  42. open_link_interface(char *device, char *ebuf)
  43. {
  44.     struct sockaddr_nit snit;
  45.     register struct link_int *l;
  46.  
  47.     l = (struct link_int *)malloc(sizeof(*p));
  48.     if (l == NULL)
  49.     {
  50.         strcpy(ebuf, ll_strerror(errno));
  51.         return (NULL);
  52.     }
  53.  
  54.     memset(l, 0, sizeof(*l));
  55.  
  56.     l->fd = socket(AF_NIT, SOCK_RAW, NITPROTO_RAW);
  57.     if (l->fd < 0)
  58.     {
  59.         sprintf(ebuf, "socket: %s", ll_strerror(errno));
  60.         goto bad;
  61.     }
  62.     snit.snit_family = AF_NIT;
  63.     strncpy(snit.snit_ifname, device, NITIFSIZ);
  64.  
  65.     if (bind(l->fd, (struct sockaddr *)&snit, sizeof(snit)))
  66.     {
  67.         sprintf(ebuf, "bind: %s: %s", snit.snit_ifname, ll_strerror(errno));
  68.         goto bad;
  69.     }
  70.  
  71.     /*
  72.      * NIT supports only ethernets.
  73.      */
  74.     l->linktype = DLT_EN10MB;
  75.  
  76.     return (l);
  77.  
  78. bad:
  79.     if (l->fd >= 0)
  80.     {
  81.         close(l->fd);
  82.     }
  83.     free(l);
  84.     return (NULL);
  85. }
  86.  
  87.  
  88. int
  89. close_link_interface(struct link_int *l)
  90. {
  91.     return (close(l->fd));
  92. }
  93.  
  94.  
  95. int
  96. write_link_layer(struct link_int *l, const u_char *device, u_char *buf, int len)
  97. {
  98.     int c;
  99.     struct sockaddr sa;
  100.  
  101.     memset(&sa, 0, sizeof(sa));
  102.     strncpy(sa.sa_data, device, sizeof(sa.sa_data));
  103.  
  104.     c = sendto(l->fd, buf, len, 0, &sa, sizeof(sa));
  105.     if (c != len)
  106.     {
  107. #if (__DEBUG)
  108.         fprintf(stderr, "write_link_layer: %d bytes written (%s)\n", c,
  109.             strerror(errno));
  110. #endif
  111.     }
  112.     return (c);
  113. }
  114.  
  115.