net_misc.cc Source File

Back to the index.

net_misc.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004-2009 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  macbuf[0] = 0x10;
101  macbuf[1] = 0x20;
102  macbuf[2] = 0x30;
103  macbuf[3] = 0;
104  macbuf[4] = 0;
105  /* NOTE/TODO: This only allows 8 nics per machine! */
106  macbuf[5] = (machine->serial_nr << 4) + (machine->nr_of_nics << 1);
107 
108  if (macbuf[0] & 1 || macbuf[5] & 1) {
109  fatal("Internal error in net_generate_unique_mac().\n");
110  exit(1);
111  }
112 
113  /* TODO: Remember the mac addresses somewhere? */
114  machine->nr_of_nics ++;
115 }
116 
117 
118 /*
119  * send_udp():
120  *
121  * Send a simple UDP packet to a real (physical) host. Used for distributed
122  * network simulations.
123  */
124 void send_udp(struct in_addr *addrp, int portnr, unsigned char *packet,
125  size_t len)
126 {
127  int s;
128  struct sockaddr_in si;
129 
130  s = socket(AF_INET, SOCK_DGRAM, 0);
131  if (s < 0) {
132  perror("send_udp(): socket");
133  return;
134  }
135 
136  /* fatal("send_udp(): sending to port %i\n", portnr); */
137 
138  si.sin_family = AF_INET;
139  si.sin_addr = *addrp;
140  si.sin_port = htons(portnr);
141 
142  if (sendto(s, packet, len, 0, (struct sockaddr *)&si,
143  sizeof(si)) != (ssize_t)len) {
144  perror("send_udp(): sendto");
145  }
146 
147  close(s);
148 }
149 
void net_generate_unique_mac(struct machine *machine, unsigned char *macbuf)
Definition: net_misc.cc:88
void fatal(const char *fmt,...)
Definition: main.cc:152
int serial_nr
Definition: machine.h:120
#define NET_ADDR_IPV6
Definition: net.h:219
#define NET_ADDR_ETHERNET
Definition: net.h:220
void send_udp(struct in_addr *addrp, int portnr, unsigned char *packet, size_t len)
Definition: net_misc.cc:124
#define NET_ADDR_IPV4
Definition: net.h:218
uint32_t addr
#define debug
Definition: dev_adb.cc:57
void net_debugaddr(void *addr, int type)
Definition: net_misc.cc:50
int nr_of_nics
Definition: machine.h:121

Generated on Sun Sep 30 2018 16:05:18 for GXemul by doxygen 1.8.13