forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpi_connect.c
87 lines (74 loc) · 2.21 KB
/
tpi_connect.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
#include "tpi_daytime.h"
void
tpi_connect(int fd, const void *addr, size_t addrlen)
{
struct {
struct T_conn_req msg_hdr;
char addr[128];
} conn_req;
struct {
struct T_conn_con msg_hdr;
char addr[128];
} conn_con;
struct strbuf ctlbuf;
union T_primitives rcvbuf;
struct T_error_ack *error_ack;
struct T_discon_ind *discon_ind;
int flags;
conn_req.msg_hdr.PRIM_type = T_CONN_REQ;
conn_req.msg_hdr.DEST_length = addrlen;
conn_req.msg_hdr.DEST_offset = sizeof(struct T_conn_req);
conn_req.msg_hdr.OPT_length = 0;
conn_req.msg_hdr.OPT_offset = 0;
memcpy(conn_req.addr, addr, addrlen); /* sockaddr_in{} */
ctlbuf.len = sizeof(struct T_conn_req) + addrlen;
ctlbuf.buf = (char *) &conn_req;
Putmsg(fd, &ctlbuf, NULL, 0);
ctlbuf.maxlen = sizeof(union T_primitives);
ctlbuf.len = 0;
ctlbuf.buf = (char *) &rcvbuf;
flags = RS_HIPRI;
Getmsg(fd, &ctlbuf, NULL, &flags);
/* *INDENT-OFF* */
if (ctlbuf.len < (int) sizeof(long))
err_quit("tpi_connect: bad length from getmsg");
/* *INDENT-ON* */
switch(rcvbuf.type) {
case T_OK_ACK:
break;
case T_ERROR_ACK:
/* *INDENT-OFF* */
if (ctlbuf.len < (int) sizeof(struct T_error_ack))
err_quit("tpi_connect: bad length for T_ERROR_ACK");
error_ack = (struct T_error_ack *) &rcvbuf;
err_quit("tpi_connect: T_ERROR_ACK from conn (%d, %d)",
error_ack->TLI_error, error_ack->UNIX_error);
/* *INDENT-ON* */
default:
err_quit("tpi_connect: unexpected message type: %d", rcvbuf.type);
}
ctlbuf.maxlen = sizeof(conn_con);
ctlbuf.len = 0;
ctlbuf.buf = (char *) &conn_con;
flags = 0;
Getmsg(fd, &ctlbuf, NULL, &flags);
/* *INDENT-OFF* */
if (ctlbuf.len < (int) sizeof(long))
err_quit("tpi_connect2: bad length from getmsg");
/* *INDENT-ON* */
switch(conn_con.msg_hdr.PRIM_type) {
case T_CONN_CON:
break;
case T_DISCON_IND:
/* *INDENT-OFF* */
if (ctlbuf.len < (int) sizeof(struct T_discon_ind))
err_quit("tpi_connect2: bad length for T_DISCON_IND");
discon_ind = (struct T_discon_ind *) &conn_con.msg_hdr;
err_quit("tpi_connect2: T_DISCON_IND from conn (%d)",
discon_ind->DISCON_reason);
/* *INDENT-ON* */
default:
err_quit("tpi_connect2: unexpected message type: %d",
conn_con.msg_hdr.PRIM_type);
}
}