forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpi_bind.c
55 lines (47 loc) · 1.35 KB
/
tpi_bind.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
#include "tpi_daytime.h"
void
tpi_bind(int fd, const void *addr, size_t addrlen)
{
struct {
struct T_bind_req msg_hdr;
char addr[128];
} bind_req;
struct {
struct T_bind_ack msg_hdr;
char addr[128];
} bind_ack;
struct strbuf ctlbuf;
struct T_error_ack *error_ack;
int flags;
bind_req.msg_hdr.PRIM_type = T_BIND_REQ;
bind_req.msg_hdr.ADDR_length = addrlen;
bind_req.msg_hdr.ADDR_offset = sizeof(struct T_bind_req);
bind_req.msg_hdr.CONIND_number = 0;
memcpy(bind_req.addr, addr, addrlen); /* sockaddr_in{} */
ctlbuf.len = sizeof(struct T_bind_req) + addrlen;
ctlbuf.buf = (char *) &bind_req;
Putmsg(fd, &ctlbuf, NULL, 0);
ctlbuf.maxlen = sizeof(bind_ack);
ctlbuf.len = 0;
ctlbuf.buf = (char *) &bind_ack;
flags = RS_HIPRI;
Getmsg(fd, &ctlbuf, NULL, &flags);
/* *INDENT-OFF* */
if (ctlbuf.len < (int) sizeof(long))
err_quit("bad length from getmsg");
/* *INDENT-ON* */
switch(bind_ack.msg_hdr.PRIM_type) {
case T_BIND_ACK:
return;
case T_ERROR_ACK:
/* *INDENT-OFF* */
if (ctlbuf.len < (int) sizeof(struct T_error_ack))
err_quit("bad length for T_ERROR_ACK");
error_ack = (struct T_error_ack *) &bind_ack.msg_hdr;
err_quit("T_ERROR_ACK from bind (%d, %d)",
error_ack->TLI_error, error_ack->UNIX_error);
/* *INDENT-ON* */
default:
err_quit("unexpected message type: %d", bind_ack.msg_hdr.PRIM_type);
}
}