231 |
char **ap; |
char **ap; |
232 |
struct nbd_startup ns; |
struct nbd_startup ns; |
233 |
ssize_t cc; |
ssize_t cc; |
234 |
|
size_t ofs; |
235 |
|
|
236 |
/* First we have to parse the store name to get the host name and TCP |
/* First we have to parse the store name to get the host name and TCP |
237 |
port number to connect to and the block size to use. */ |
port number to connect to and the block size to use. */ |
277 |
for (ap = he->h_addr_list; *ap != 0; ++ap) |
for (ap = he->h_addr_list; *ap != 0; ++ap) |
278 |
{ |
{ |
279 |
sin.sin_addr = *(const struct in_addr *) *ap; |
sin.sin_addr = *(const struct in_addr *) *ap; |
280 |
|
errno = 0; |
281 |
if (connect (sock, &sin, sizeof sin) == 0 || errno == ECONNREFUSED) |
if (connect (sock, &sin, sizeof sin) == 0 || errno == ECONNREFUSED) |
282 |
break; |
break; |
283 |
} |
} |
284 |
if (*ap != 0) /* last connect failed */ |
if (errno != 0) /* last connect failed */ |
285 |
{ |
{ |
286 |
error_t err = errno; |
error_t err = errno; |
287 |
close (sock); |
close (sock); |
289 |
} |
} |
290 |
|
|
291 |
/* Read the startup packet, which tells us the size of the store. */ |
/* Read the startup packet, which tells us the size of the store. */ |
292 |
|
ofs = 0; |
293 |
|
do { |
294 |
|
cc = read (sock, (char *) &ns + ofs, sizeof ns - ofs); |
295 |
|
if (cc < 0) |
296 |
|
{ |
297 |
|
error_t err = errno; |
298 |
|
close (sock); |
299 |
|
return err; |
300 |
|
} |
301 |
|
ofs += cc; |
302 |
|
} while (cc > 0 && ofs < sizeof ns); |
303 |
|
|
304 |
cc = read (sock, &ns, sizeof ns); |
if (cc < sizeof ns |
|
if (cc < 0) |
|
|
{ |
|
|
error_t err = errno; |
|
|
close (sock); |
|
|
return err; |
|
|
} |
|
|
if (cc != sizeof ns |
|
305 |
|| memcmp (ns.magic, NBD_INIT_MAGIC, sizeof ns.magic) != 0) |
|| memcmp (ns.magic, NBD_INIT_MAGIC, sizeof ns.magic) != 0) |
306 |
{ |
{ |
307 |
close (sock); |
close (sock); |