File: send_packet_bsd.c

package info (click to toggle)
libnet-arp-perl 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 180 kB
  • ctags: 181
  • sloc: ansic: 297; perl: 41; makefile: 26
file content (68 lines) | stat: -rwxr-xr-x 1,369 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Perl ARP Extension
Send the packet
BSD code

Programmed by Bastian Ballmann
Last update: 01.12.2004

This program is free software; you can redistribute 
it and/or modify it under the terms of the 
GNU General Public License version 2 as published 
by the Free Software Foundation.

This program is distributed in the hope that it will 
be useful, but WITHOUT ANY WARRANTY; without even 
the implied warranty of MERCHANTABILITY or FITNESS 
FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details. 
*/

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <net/bpf.h>
#include <net/if.h>
#include "arp.h"

int send_packet_bsd(u_char *dev, u_char *packet, u_int packetsize)
{
  int bpffd, i;
  char bpfdev[12];

  if( (strlen(dev) == 0) ||
      (packetsize == 0) )
    return 0;

  // Open a bpf device
  for(i = 0; i < 512; i++)
    {
      sprintf(bpfdev,"/dev/bpf%d",i);
      if((bpffd = open(bpfdev,O_WRONLY)) > 0)
	{
	  break;	     
	}
    }
  
  if(bpffd < 0)
    {
      perror("open bpf");
      return 0;
    }
  else
    {
      // Lock it
      flock(bpffd,LOCK_EX);
      
      // Bind it to a device
      ioctl(bpffd,BIOCSETIF,dev);
      
      // Send the packet and unlock
      write(bpffd,packet,packetsize);
      flock(bpffd,LOCK_UN);
      close(bpffd);
    }

  return 1;
}