1 |
/* |
2 |
* Copyright (C) 2004-2007 Anders Gavare. All rights reserved. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
6 |
* |
7 |
* 1. Redistributions of source code must retain the above copyright |
8 |
* notice, this list of conditions and the following disclaimer. |
9 |
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
* notice, this list of conditions and the following disclaimer in the |
11 |
* documentation and/or other materials provided with the distribution. |
12 |
* 3. The name of the author may not be used to endorse or promote products |
13 |
* derived from this software without specific prior written permission. |
14 |
* |
15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
* SUCH DAMAGE. |
26 |
* |
27 |
* |
28 |
* $Id: net_misc.c,v 1.5 2006/12/30 13:31:02 debug Exp $ |
29 |
* |
30 |
* Misc. helper functions. |
31 |
*/ |
32 |
|
33 |
#include <stdio.h> |
34 |
#include <stdlib.h> |
35 |
#include <string.h> |
36 |
#include <sys/types.h> |
37 |
#include <sys/socket.h> |
38 |
#include <netinet/in.h> |
39 |
#include <arpa/inet.h> |
40 |
#include <unistd.h> |
41 |
|
42 |
#include "machine.h" |
43 |
#include "misc.h" |
44 |
#include "net.h" |
45 |
|
46 |
|
47 |
/* |
48 |
* net_debugaddr(): |
49 |
* |
50 |
* Print an address using debug(). |
51 |
*/ |
52 |
void net_debugaddr(void *addr, int type) |
53 |
{ |
54 |
int i; |
55 |
unsigned char *p = addr; |
56 |
|
57 |
switch (type) { |
58 |
|
59 |
case NET_ADDR_IPV4: |
60 |
for (i=0; i<4; i++) |
61 |
debug("%s%i", i? "." : "", p[i]); |
62 |
break; |
63 |
|
64 |
case NET_ADDR_IPV6: |
65 |
for (i=0; i<16; i+=2) |
66 |
debug("%s%4x", i? ":" : "", p[i] * 256 + p[i+1]); |
67 |
break; |
68 |
|
69 |
case NET_ADDR_ETHERNET: |
70 |
for (i=0; i<6; i++) |
71 |
debug("%s%02x", i? ":" : "", p[i]); |
72 |
break; |
73 |
|
74 |
default: |
75 |
fatal("net_debugaddr(): UNIMPLEMTED type %i\n", type); |
76 |
exit(1); |
77 |
} |
78 |
} |
79 |
|
80 |
|
81 |
/* |
82 |
* net_generate_unique_mac(): |
83 |
* |
84 |
* Generate a "unique" serial number for a machine. The machine's serial |
85 |
* number is combined with the machine's current number of NICs to form a |
86 |
* more-or-less valid MAC address. |
87 |
* |
88 |
* The return value (6 bytes) are written to macbuf. |
89 |
*/ |
90 |
void net_generate_unique_mac(struct machine *machine, unsigned char *macbuf) |
91 |
{ |
92 |
int x, y; |
93 |
|
94 |
if (macbuf == NULL || machine == NULL) { |
95 |
fatal("**\n** net_generate_unique_mac(): NULL ptr\n**\n"); |
96 |
return; |
97 |
} |
98 |
|
99 |
x = machine->serial_nr; |
100 |
y = machine->nr_of_nics; |
101 |
|
102 |
macbuf[0] = 0x10; |
103 |
macbuf[1] = 0x20; |
104 |
macbuf[2] = 0x30; |
105 |
macbuf[3] = 0; |
106 |
macbuf[4] = 0; |
107 |
/* NOTE/TODO: This only allows 8 nics per machine! */ |
108 |
macbuf[5] = (machine->serial_nr << 4) + (machine->nr_of_nics << 1); |
109 |
|
110 |
if (macbuf[0] & 1 || macbuf[5] & 1) { |
111 |
fatal("Internal error in net_generate_unique_mac().\n"); |
112 |
exit(1); |
113 |
} |
114 |
|
115 |
/* TODO: Remember the mac addresses somewhere? */ |
116 |
machine->nr_of_nics ++; |
117 |
} |
118 |
|
119 |
|
120 |
/* |
121 |
* send_udp(): |
122 |
* |
123 |
* Send a simple UDP packet to a real (physical) host. Used for distributed |
124 |
* network simulations. |
125 |
*/ |
126 |
void send_udp(struct in_addr *addrp, int portnr, unsigned char *packet, |
127 |
size_t len) |
128 |
{ |
129 |
int s; |
130 |
struct sockaddr_in si; |
131 |
|
132 |
s = socket(AF_INET, SOCK_DGRAM, 0); |
133 |
if (s < 0) { |
134 |
perror("send_udp(): socket"); |
135 |
return; |
136 |
} |
137 |
|
138 |
/* fatal("send_udp(): sending to port %i\n", portnr); */ |
139 |
|
140 |
si.sin_family = AF_INET; |
141 |
si.sin_addr = *addrp; |
142 |
si.sin_port = htons(portnr); |
143 |
|
144 |
if (sendto(s, packet, len, 0, (struct sockaddr *)&si, |
145 |
sizeof(si)) != (ssize_t)len) { |
146 |
perror("send_udp(): sendto"); |
147 |
} |
148 |
|
149 |
close(s); |
150 |
} |
151 |
|