Annotation of embedaddon/libnet/src/libnet_link_nit.c, revision 1.1.1.3
1.1 misho 1: /*
2: * $Id: libnet_link_nit.c,v 1.6 2004/01/03 20:31:02 mike Exp $
3: *
4: * libnet
5: * libnet_nit.c - network interface tap 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:
1.1.1.3 ! misho 30: #include "common.h"
1.1 misho 31:
32: #include "../include/gnuc.h"
33: #ifdef HAVE_OS_PROTO_H
34: #include "../include/os-proto.h"
35: #endif
36:
37: struct libnet_link_int *
38: libnet_open_link_interface(int8_t *device, int8_t *ebuf)
39: {
40: struct sockaddr_nit snit;
41: register struct libnet_link_int *l;
42:
43: l = (struct libnet_link_int *)malloc(sizeof(*p));
44: if (l == NULL)
45: {
46: strcpy(ebuf, strerror(errno));
47: return (NULL);
48: }
49:
50: memset(l, 0, sizeof(*l));
51:
52: l->fd = socket(AF_NIT, SOCK_RAW, NITPROTO_RAW);
53: if (l->fd < 0)
54: {
1.1.1.3 ! misho 55: snprintf(ebuf, LIBNET_ERRBUF_SIZE,
! 56: "socket: %s", strerror(errno));
1.1 misho 57: goto bad;
58: }
59: snit.snit_family = AF_NIT;
60: strncpy(snit.snit_ifname, device, NITIFSIZ -1);
61: snit.snit_ifname[NITIFSIZ] = '\0';
62:
63: if (bind(l->fd, (struct sockaddr *)&snit, sizeof(snit)))
64: {
1.1.1.3 ! misho 65: snprintf(ebuf, LIBNET_ERRBUF_SIZE,
! 66: "bind: %s: %s", snit.snit_ifname, strerror(errno));
1.1 misho 67: goto bad;
68: }
69:
70: /*
71: * NIT supports only ethernets.
72: */
73: l->linktype = DLT_EN10MB;
74:
75: return (l);
76:
77: bad:
78: if (l->fd >= 0)
79: {
80: close(l->fd);
81: }
82: free(l);
83: return (NULL);
84: }
85:
86:
87: int
88: libnet_close_link_interface(struct libnet_link_int *l)
89: {
90: if (close(l->fd) == 0)
91: {
92: free(l);
93: return (1);
94: }
95: else
96: {
97: free(l);
98: return (-1);
99: }
100: }
101:
102:
103: int
104: write_link_layer(struct libnet_link_int *l, const int8_t *device,
1.1.1.2 misho 105: uint8_t *buf, int len)
1.1 misho 106: {
107: int c;
108: struct sockaddr sa;
109:
110: memset(&sa, 0, sizeof(sa));
111: strncpy(sa.sa_data, device, sizeof(sa.sa_data));
112:
113: c = sendto(l->fd, buf, len, 0, &sa, sizeof(sa));
114: if (c != len)
115: {
116: /* error */
117: }
118: return (c);
119: }
120:
1.1.1.3 ! misho 121: /**
! 122: * Local Variables:
! 123: * indent-tabs-mode: nil
! 124: * c-file-style: "stroustrup"
! 125: * End:
! 126: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>