net_misc.cc Source File

Back to the index.

net_misc.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2018 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  * Misc. helper functions.
29  */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <unistd.h>
39 
40 #include "machine.h"
41 #include "misc.h"
42 #include "net.h"
43 
44 
45 /*
46  * net_debugaddr():
47  *
48  * Print an address using debug().
49  */
50 void net_debugaddr(void *addr, int type)
51 {
52  int i;
53  unsigned char *p = (unsigned char *) addr;
54 
55  switch (type) {
56 
57  case NET_ADDR_IPV4:
58  for (i=0; i<4; i++)
59  debug("%s%i", i? "." : "", p[i]);
60  break;
61 
62  case NET_ADDR_IPV6:
63  for (i=0; i<16; i+=2)
64  debug("%s%4x", i? ":" : "", p[i] * 256 + p[i+1]);
65  break;
66 
67  case NET_ADDR_ETHERNET:
68  for (i=0; i<6; i++)
69  debug("%s%02x", i? ":" : "", p[i]);
70  break;
71 
72  default:
73  fatal("net_debugaddr(): UNIMPLEMTED type %i\n", type);
74  exit(1);
75  }
76 }
77 
78 
79 /*
80  * net_generate_unique_mac():
81  *
82  * Generate a "unique" serial number for a machine. The machine's serial
83  * number is combined with the machine's current number of NICs to form a
84  * more-or-less valid MAC address.
85  *
86  * The return value (6 bytes) are written to macbuf.
87  */
88 void net_generate_unique_mac(struct machine *machine, unsigned char *macbuf)
89 {
90  int x, y;
91 
92  if (macbuf == NULL || machine == NULL) {
93  fatal("**\n** net_generate_unique_mac(): NULL ptr\n**\n");
94  return;
95  }
96 
97  x = machine->serial_nr;
98  y = machine->nr_of_nics;
99 
100  // Special case: SGI machines have 0x08 as their first byte?
101  macbuf[0] = machine->machine_type == MACHINE_SGI ? 0x08 : 0x10;
102  macbuf[1] = 0x20;
103  macbuf[2] = 0x30;
104  macbuf[3] = 0;
105  macbuf[4] = 0;
106  /* NOTE/TODO: This only allows 8 nics per machine! */
107  macbuf[5] = (machine->serial_nr << 4) + (machine->nr_of_nics << 1);
108 
109  if (macbuf[0] & 1 || macbuf[5] & 1) {
110  fatal("Internal error in net_generate_unique_mac().\n");
111  exit(1);
112  }
113 
114  /* TODO: Remember the mac addresses somewhere? */
115  machine->nr_of_nics ++;
116 }
117 
118 
119 /*
120  * send_udp():
121  *
122  * Send a simple UDP packet to a real (physical) host. Used for distributed
123  * network simulations.
124  */
125 void send_udp(struct in_addr *addrp, int portnr, unsigned char *packet,
126  size_t len)
127 {
128  int s;
129  struct sockaddr_in si;
130 
131  s = socket(AF_INET, SOCK_DGRAM, 0);
132  if (s < 0) {
133  perror("send_udp(): socket");
134  return;
135  }
136 
137  /* fatal("send_udp(): sending to port %i\n", portnr); */
138 
139  si.sin_family = AF_INET;
140  si.sin_addr = *addrp;
141  si.sin_port = htons(portnr);
142 
143  if (sendto(s, packet, len, 0, (struct sockaddr *)&si,
144  sizeof(si)) != (ssize_t)len) {
145  perror("send_udp(): sendto");
146  }
147 
148  close(s);
149 }
150 
debug
#define debug
Definition: dev_adb.cc:57
NET_ADDR_IPV4
#define NET_ADDR_IPV4
Definition: net.h:218
net_generate_unique_mac
void net_generate_unique_mac(struct machine *machine, unsigned char *macbuf)
Definition: net_misc.cc:88
addr
uint32_t addr
Definition: tmp_arm_multi.cc:52
net_debugaddr
void net_debugaddr(void *addr, int type)
Definition: net_misc.cc:50
fatal
void fatal(const char *fmt,...)
Definition: main.cc:152
machine::serial_nr
int serial_nr
Definition: machine.h:120
misc.h
machine.h
machine
Definition: machine.h:97
NET_ADDR_ETHERNET
#define NET_ADDR_ETHERNET
Definition: net.h:220
MACHINE_SGI
#define MACHINE_SGI
Definition: machine.h:217
send_udp
void send_udp(struct in_addr *addrp, int portnr, unsigned char *packet, size_t len)
Definition: net_misc.cc:125
machine::machine_type
int machine_type
Definition: machine.h:111
net.h
machine::nr_of_nics
int nr_of_nics
Definition: machine.h:121
NET_ADDR_IPV6
#define NET_ADDR_IPV6
Definition: net.h:219

Generated on Tue Aug 25 2020 19:25:06 for GXemul by doxygen 1.8.18