-
Notifications
You must be signed in to change notification settings - Fork 0
/
n2h.c
executable file
·183 lines (145 loc) · 3.38 KB
/
n2h.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* $Id: n2h.h,v 1.1 2000/02/23 01:00:30 ktg Exp $
* Node 2 Hostname
*/
#ifndef _N2H_C_
#define _N2H_C_
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include "common.h"
#include "n2h.h"
#include "queue.h"
#include "rt.h"
#define logf (stdout)
static struct n2h *g_n2h;
static int my_id;
int create_n2h()
{
InitDQ(g_n2h, struct n2h);
assert (g_n2h);
g_n2h->nid = -1;
g_n2h->hostname = NULL;
return (g_n2h != 0x0);
}
/*
* returns a socket that is bound on the port
*/
int bind_port(int port) {
struct sockaddr_in ServAddr; /* Server address */
unsigned short ServPort; /* Server port */
int sock;
/* Socket */
ServPort = port; /* First arg: local port */
/* Create socket for sending/receiving datagrams */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
DieWithSystemMessage("socket() failed");
/* Set up the server address structure */
memset(&ServAddr, 0, sizeof(ServAddr)); /* Zero out structure */
ServAddr.sin_family = AF_INET; /* Internet family */
ServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
ServAddr.sin_port = htons(ServPort); /* Port */
/* Bind to the local address */
if (bind(sock, (struct sockaddr *) &ServAddr, sizeof(ServAddr)) < 0)
DieWithSystemMessage("bind() failed");
return sock;
}
/*
* Check if <hostname> is a valid host
* Add into node->hostname mapping
*/
int add_n2h(node nid, char *hostname)
{
struct n2h* nl = (struct n2h *) getmem (sizeof (struct n2h));
nl->nid = nid;
assert(gethostbyname(hostname));
nl->hostname = (char *) malloc (strlen(hostname)+1);
strcpy (nl->hostname, hostname);
InsertDQ(g_n2h, nl);
return (nl != 0x0);
}
/*
* do "node_id->hostname mapping"
*/
char *gethostbynode(node nid)
{
struct n2h *i;
for (i = g_n2h->next; i != g_n2h; i= i->next) {
assert(i);
if ( i->nid == nid )
return i->hostname;
}
return 0x0;
}
/*
* Using node->hostname list to initiailize the routing table
*/
int init_rt_from_n2h()
{
struct n2h *i;
for (i = g_n2h->next; i != g_n2h; i= i->next) {
assert(i);
if ( i->nid != get_myid() ) {
// dest, cost, next-hop
assert(add_rte(i->nid, -1, i->nid));
}
}
return true;
}
/*
* Visit the while node->hostname list
*/
void print_n2h()
{
struct n2h *i;
fprintf (logf, "\n[n2h] ***** dumping node-to-hostname list *****\n");
for (i=g_n2h->next; i != g_n2h; i=i->next){
assert(i);
fprintf (logf, "[n2h]\tnode(%d) <--> hostname(%s)\n",
i->nid, i->hostname);
}
}
/*
* store my node id given from command line
*/
void set_myid (node myid)
{
my_id = myid;
}
/*
* return my node id given from command line
*/
node get_myid()
{
return my_id;
}
/*
* Is <nid> on my machine ?
*/
bool is_me(node nid)
{
int ret;
int my_net_num;
int node_net_num;
char myhostname[256];
struct hostent *hp;
// get my net number
ret = gethostname(myhostname, 256);
assert (ret >= 0);
assert ((hp = gethostbyname(myhostname)));
memcpy (&my_net_num, hp->h_addr, 4);
// get nid's net number
assert ((hp = gethostbyname(gethostbynode(nid))));
memcpy(&node_net_num, hp->h_addr, 4);
// it's me?
if (my_net_num == node_net_num)
return true;
else
return false;
}
#endif