File: bootsockio.c

package info (click to toggle)
lam 7.1.2-1.4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 54,608 kB
  • ctags: 17,034
  • sloc: ansic: 156,264; sh: 9,976; cpp: 7,699; makefile: 5,589; perl: 476; fortran: 260; asm: 83
file content (315 lines) | stat: -rw-r--r-- 6,922 bytes parent folder | download | duplicates (10)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright (c) 2001-2003 The Trustees of Indiana University.  
 *                         All rights reserved.
 * Copyright (c) 1998-2001 University of Notre Dame. 
 *                         All rights reserved.
 * Copyright (c) 1994-1998 The Ohio State University.  
 *                         All rights reserved.
 * 
 * This file is part of the LAM/MPI software package.  For license
 * information, see the LICENSE file in the top level directory of the
 * LAM/MPI source distribution.
 * 
 * $HEADER$
 *
 * $Id: bootsockio.c,v 6.12 2003/07/05 21:52:08 jsquyres Exp $
 *
 *	Function:	- OTB booting socket I/O functions
 *				- readsockint4(): read an int4 from a socket
 *				- writesockint4(): read an int4 to a socket
 *				- readcltcoord(): read client nodeid/port #
 *				- readcltnbr(): read client neighbour info
 *				- writecltcoord(): write client nodeid/port #
 *				- writecltnbr(): write client neighbour info
 */

#include <errno.h>
#include <portable.h>
#include <t_types.h>
#include <typical.h>
#include <sys/param.h>
#include <netdb.h>
#include <string.h>

#include <laminternal.h>
#include <net.h>
#include <etc_misc.h>


/*
 *	readsockint4
 *
 *	Function:	- read a 32-bit integer from a socket
 *			- handles byte ordering issue
 *	Accepts:	- socket
 *			- ptr to integer
 *	Returns:	- 0 or ERROR
 */
int
readsockint4(int sock, int4 *pdata)
{
	int4		data;
	int		ret;

	ret = mread(sock, (char *) &data, sizeof(int4));
	if (ret < (int)sizeof(int4)) {
		errno = (ret < 0) ? errno : EIO;
		return (ERROR);
	}

	*pdata = ttol(data);
	return(0);
}


/*
 *	writesockint4
 *
 *	Function:	- write a 32-bit integer to a socket
 *			- handles byte ordering issue
 *	Accepts:	- socket
 *			- integer
 *	Returns:	- 0 or ERROR
 */
int
writesockint4(int sock, int4 data)
{
	int		ret;

	data = ltot(data);
	ret = mwrite(sock, (char *) &data, sizeof(int4));
	if (ret < (int)sizeof(int4)) {
		errno = (ret < 0) ? errno : EIO;
		return (ERROR);
	}

	return(0);
}


/*
 *	readcltcoord
 *
 *	Function:	- read client STREAM and UDP port numbers
 *	Accepts:	- socket
 *			- ptr to node ID #
 *			- ptr to STREAM port #
 *			- ptr to UDP port #
 *	Returns:	- 0 or ERROR
 */
int
readcltcoord(int sock, int4 *pid, int4 *pstrport, int4 *pudpport)
{
	int4		status;

	if (readsockint4(sock, &status)) {
		return (ERROR);
	}

	if (status) {
		errno = status;
		return (ERROR);
	}

	if (readsockint4(sock, pid)) {
		return (ERROR);
	}

	if (readsockint4(sock, pstrport)) {
		return (ERROR);
	}

	if (readsockint4(sock, pudpport)) {
		return (ERROR);
	}

	return(0);
}


/*
 *	writecltcoord
 *
 *	Function:	- write client STREAM and UDP port numbers
 *			- called by the booted client
 *	Accepts:	- socket
 *			- node ID #
 *			- STREAM port #
 *			- UDP port #
 *	Returns:	- 0 or ERROR
 */
int
writecltcoord(int sock, int4 id, int4 strport, int4 udpport)
{
	if (writesockint4(sock, INT4_NIL)) {
		return (ERROR);
	}

	if (writesockint4(sock, id)) {
		return (ERROR);
	}

	if (writesockint4(sock, strport)) {
		return (ERROR);
	}

	if (writesockint4(sock, udpport)) {
		return (ERROR);
	}

	return(0);
}


/*
 *	readcltnbr
 *
 *	Function:	- read client neighbour <link index, hostaddr, port,
 *                                               ncpus>
 *	Accepts:	- socket
 *			- ptr link index
 *			- ptr host address (4 bytes, in network byte order)
 *			- ptr port number
 *			- ptr num cpus
 *	Returns:	- 0 or ERROR
 */
int
readcltnbr(int sock, int4 *plink, unsigned char *phostaddr, int4 *pport,
	   int4 *pnode_type, int4 *pnum_cpus)
{
  int ret;
  int name_len;

  if (readsockint4(sock, plink))
    return (ERROR);

  /* If the link info is for an invalid node, nothing else is sent */

  if (*plink == NOTNODEID) {
    *pport = -1;
    *pnode_type = -1;
    *pnum_cpus = -1;
    return 0;
  }

  if (readsockint4(sock, pport))
    return (ERROR);

  if (readsockint4(sock, pnode_type))
    return (ERROR);

  if (readsockint4(sock, pnum_cpus))
    return (ERROR);

  /* Suggestion/modified patch from Tim Mattox (KLAT group) to pass
     around host *names* instead of IP addresses.  Necessary for
     non-uniform networks (e.g., KLAT flat networks) where IP addr
     lookup for name "foo" may result in different answers on
     different nodes.  If name_len > 0, then read that many chars from
     the remote side and then do a lookup on that name to get the
     address.  Otherwise, just read the raw address from the other
     side. */

  if (readsockint4(sock, &name_len))
    return (ERROR);
  if (name_len < 0) {
    errno = EINVAL;
    return (ERROR);
  } else if (name_len > MAXHOSTNAMELEN) {
    errno = ENAMETOOLONG;
    return (ERROR);
  }
  if (name_len > 0) {
    char buf[MAXHOSTNAMELEN + 1];
    ret = mread(sock, buf, name_len);
    if (ret < name_len) {
      errno = (ret < 0) ? errno : EIO;
      return (ERROR);
    }
    buf[name_len] = '\0';
    if (getinetaddr(buf, phostaddr)) 
      return (ERROR);
  } else {
    ret = mread(sock, (char *) phostaddr, 4);
    if (ret < 4) {
      errno = (ret < 0) ? errno : EIO;
      return (ERROR);
    }
  }

  return 0;
}

/*
 *	writecltnbr
 *
 *	Function:	- write client neighbour <link index, hostaddr, port, 
 *                                                ncpus>
 *			- called by the booting server
 *	Accepts:	- socket
 *			- link index
 *			- host address (4 bytes, in network byte order)
 *			- port number
 *			- num cpus
 *	Returns:	- 0 or ERROR
 */
int
writecltnbr(int sock, int4 link, int is_hostname, 
	    unsigned char *hostaddr_or_name, int4 port, 
	    int4 node_type, int4 num_cpus)
{
  int ret;
  int4 name_len;

  if (writesockint4(sock, link))
    return (ERROR);

  /* If the link is an invalid node, nothing else is sent */

  if (link == NOTNODEID)
    return 0;

  if (writesockint4(sock, port))
    return (ERROR);
  
  if (writesockint4(sock, node_type))
    return (ERROR);

  if (writesockint4(sock, num_cpus))
    return (ERROR);

  /* Suggestion/modified patch from Tim Mattox (KLAT group) to pass
     around host *names* instead of IP addresses.  See comment above
     for explanation. */
  /* Note that it is acceptable to have NULL for the hostaddr_or_name
     entry; this may happen if we are passing information about
     invalid nodes (e.g., nodes that have died). */

  name_len = is_hostname ? strlen((char*) hostaddr_or_name) : 0;

  /* Send the name_len, even if it's too long, so that both sides will
     abort cleanly. */

  if (writesockint4(sock, name_len)) 
    return(ERROR);
  
  if (name_len > MAXHOSTNAMELEN) {
    errno = ENAMETOOLONG;
    return (ERROR);
  } else if (name_len < 0) {
    errno = EINVAL;
    return (ERROR);
  }
  
  /* If it's an address, hardwire the length to send to 4 */

  if (!is_hostname)
    name_len = 4;
  ret = mwrite(sock, (char *) hostaddr_or_name, name_len);
  if (ret < name_len) {
    errno = (ret < 0) ? errno : EIO;
    return LAMERROR; 
  }

  return 0;
}