File: eth-ndd.c

package info (click to toggle)
ganglia-monitor-core 2.5.7-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,300 kB
  • ctags: 3,900
  • sloc: ansic: 27,889; sh: 8,492; makefile: 148
file content (117 lines) | stat: -rw-r--r-- 2,142 bytes parent folder | download | duplicates (4)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * eth-ndd.c
 *
 * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
 *
 * $Id: eth-ndd.c,v 1.1 2002/08/20 18:52:04 massie Exp $
 */

#include "config.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ndd_var.h>
#include <sys/kinfo.h>

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "dnet.h"

struct eth_handle {
	char	device[16];
	int	fd;
};

eth_t *
eth_open(const char *device)
{
	struct sockaddr_ndd_8022 sa;
	eth_t *e;
	
	if ((e = calloc(1, sizeof(*e))) == NULL)
		return (NULL);

	if ((e->fd = socket(AF_NDD, SOCK_DGRAM, NDD_PROT_ETHER)) < 0)
		return (eth_close(e));
	
	sa.sndd_8022_family = AF_NDD;
        sa.sndd_8022_len = sizeof(sa);
	sa.sndd_8022_filtertype = NS_ETHERTYPE;
	sa.sndd_8022_ethertype = ETH_TYPE_IP;
	sa.sndd_8022_filterlen = sizeof(struct ns_8022);
	strlcpy(sa.sndd_8022_nddname, device, sizeof(sa.sndd_8022_nddname));
	
	if (bind(e->fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
		return (eth_close(e));
	
	if (connect(e->fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
		return (eth_close(e));
	
	/* XXX - SO_BROADCAST needed? */
	
	return (e);
}

size_t
eth_send(eth_t *e, const void *buf, size_t len)
{
	return ((ssize_t)write(e->fd, buf, len));
}

eth_t *
eth_close(eth_t *e)
{
	assert(e != NULL);

	if (e->fd > 0)
		close(e->fd);
	free(e);
	return (NULL);
}

int
eth_get(eth_t *e, eth_addr_t *ea)
{
	struct kinfo_ndd *nddp;
	int size;
	void *end;
	
	if ((size = getkerninfo(KINFO_NDD, 0, 0, 0)) == 0) {
		errno = ENOENT;
		return (-1);
	} else if (size < 0)
		return (-1);
	
	if ((nddp = malloc(size)) == NULL)
		return (-1);
                     
	if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) {
		free(nddp);
		return (-1);
	}
	for (end = (void *)nddp + size; (void *)nddp < end; nddp++) {
		if (strcmp(nddp->ndd_alias, e->device) == 0 ||
		    strcmp(nddp->ndd_name, e->device) == 0) {
			memcpy(ea, nddp->ndd_addr, sizeof(*ea));
		}
	}
	free(nddp);
	
	if ((void *)nddp >= end) {
		errno = ESRCH;
		return (-1);
	}
	return (0);
}

int
eth_set(eth_t *e, const eth_addr_t *ea)
{
	errno = ENOSYS;
	return (-1);
}