-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmconnect.c
146 lines (110 loc) · 2.96 KB
/
mconnect.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
#include "marignotti.h"
#include <gno/kerntool.h>
#include <errno.h>
#include <sys/socket.h>
#include <misctool.h>
#include "s16debug.h"
#pragma noroot
#pragma optimize 79
int mconnect(Entry *e, void *p1, void *p2, void *p3, void *p4, void *p5)
{
Word port;
Word t;
Word terr;
int xerrno;
LongWord timeout;
// todo -- if non-blocking,
// return EINPROGRESS
//
xsockaddr_in *addr = (xsockaddr_in *)p3;
int addrlen = p4 ? *(int *)p4 : 0;
port = addr->sin_port;
asm {
lda <port
xba
sta <port
}
if (Debug > 0)
{
union xsplit s;
s.i32 = addr->sin_addr;
s16_debug_printf("connect family = %d address = %d.%d.%d.%d port = %d",
addr->sin_family,
s.i8[0], s.i8[1], s.i8[2], s.i8[3],
port);
}
if (addrlen < 8 || !addr) return EINVAL;
// AF_UNSPEC (aka 0) is commonly used.
// TODO -- AF_LOCAL could use pipes
if (addr->sin_family != AF_UNSPEC && addr->sin_family != AF_INET)
return EINVAL;
if (e->_TYPE == SOCK_DGRAM)
{
IncBusy();
TCPIPSetNewDestination(e->ipid, addr->sin_addr, port);
t = _toolErr;
DecBusy();
return 0;
}
// check if already connected.
IncBusy();
terr = TCPIPStatusTCP(e->ipid, &e->sr);
t = _toolErr;
if (t) terr = t;
DecBusy();
// todo -- if non-blocking,
// return EINPROGRESS first time,
// return EALREADY on subsequent calls.
if (e->sr.srState != TCPSCLOSED)
return EISCONN;
IncBusy();
TCPIPSetNewDestination(e->ipid, addr->sin_addr, port);
t = _toolErr;
DecBusy();
if (t)
{
return ENETDOWN;
}
IncBusy();
terr = TCPIPOpenTCP(e->ipid);
t = _toolErr;
if (t) terr = t;
DecBusy();
// todo -- better errors.
if (terr)
{
return ENETDOWN;
}
timeout = GetTick() + 60 * 30;
for (;;)
{
int xerrno;
int state;
xerrno = queue_command(e, kCommandConnect, 0, timeout);
// hmmm .. should these abort?
if (xerrno == EINTR)
{
IncBusy();
e->command = kCommandNone;
TCPIPAbortTCP(e->ipid);
DecBusy();
return EINTR; // ?
}
if (xerrno) return EIO; // semaphore destroyed?
if (e->command) continue; // reset to 0 if processed.
state = e->sr.srState;
if (state == TCPSESTABLISHED)
return 0;
if (state == TCPSCLOSED)
// todo -- differentiate ECONNREFUSED vs EHOSTUNREACH
return ECONNREFUSED;
if (timeout && timeout <= GetTick())
{
IncBusy();
TCPIPAbortTCP(e->ipid);
DecBusy();
return ETIMEDOUT;
}
}
return 0; // should never hit.
}