Repository navigation
Expand file tree
/
Copy pathnet.c
More file actions
executable file
·266 lines (219 loc) · 5.52 KB
/
Copy pathnet.c
File metadata and controls
executable file
·266 lines (219 loc) · 5.52 KB
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
/* net.c Copyright Michael Temari 08/01/1996 All Rights Reserved */
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <net/netlib.h>
#include <net/hton.h>
#include <net/gen/netdb.h>
#include <net/gen/in.h>
#include <net/gen/inet.h>
#include <net/gen/tcp.h>
#include <net/gen/tcp_io.h>
#include <net/gen/udp.h>
#include <net/gen/udp_io.h>
#include <net/gen/udp_hdr.h>
#include "talk.h"
#include "net.h"
_PROTOTYPE(void TimeOut, (int sig));
static unsigned char buffer[8192];
static int udp_ctl;
int tcp_fd;
static udpport_t ntalk_port;
char luser[USER_SIZE+1], ruser[USER_SIZE+1];
char lhost[HOST_SIZE+1], rhost[HOST_SIZE+1];
char ltty[TTY_SIZE+1], rtty[TTY_SIZE+1];
udpport_t ctlport;
tcpport_t dataport;
ipaddr_t laddr, raddr;
int NetInit()
{
int s;
struct servent *servent;
char *udp_device;
char *tcp_device;
nwio_udpopt_t udpopt;
nwio_tcpconf_t tcpconf;
if((udp_device = getenv("UDP_DEVICE")) == (char *)NULL)
udp_device = UDP_DEVICE;
if((udp_ctl = open(udp_device, O_RDWR)) < 0) {
fprintf(stderr, "talk: Could not open %s: %s\n",
udp_device, strerror(errno));
return(-1);
}
if((servent = getservbyname("ntalk", "udp")) == (struct servent *)NULL) {
fprintf(stderr, "talk: Could not find ntalk udp service\n");
close(udp_ctl);
return(-1);
}
ntalk_port = (udpport_t)servent->s_port;
udpopt.nwuo_flags = NWUO_NOFLAGS;
udpopt.nwuo_flags |= NWUO_COPY | NWUO_LP_SEL | NWUO_EN_LOC;
udpopt.nwuo_flags |= NWUO_DI_BROAD | NWUO_RP_SET | NWUO_RA_SET;
udpopt.nwuo_flags |= NWUO_RWDATONLY | NWUO_DI_IPOPT;
udpopt.nwuo_remaddr = raddr;
udpopt.nwuo_remport = ntalk_port;
s = ioctl(udp_ctl, NWIOSUDPOPT, &udpopt);
if(s < 0) {
perror("talk: ioctl NWIOSUDPOPT");
close(udp_ctl);
return(-1);
}
s = ioctl(udp_ctl, NWIOGUDPOPT, &udpopt);
if(s < 0) {
perror("talk: ioctl NWIOGUDPOPT");
close(udp_ctl);
return(-1);
}
laddr = udpopt.nwuo_locaddr;
ctlport = udpopt.nwuo_locport;
if((tcp_device = getenv("TCP_DEVICE")) == (char *)NULL)
tcp_device = TCP_DEVICE;
if((tcp_fd = open(tcp_device, O_RDWR)) < 0) {
fprintf(stderr, "talk: Could not open %s: %s\n",
tcp_device, strerror(errno));
close(udp_ctl);
return(-1);
}
tcpconf.nwtc_flags = NWTC_NOFLAGS;
tcpconf.nwtc_flags |= NWTC_LP_SEL | NWTC_SET_RA | NWTC_UNSET_RP;
tcpconf.nwtc_remaddr = raddr;
s = ioctl(tcp_fd, NWIOSTCPCONF, &tcpconf);
if(s < 0) {
perror("talk: ioctl NWIOSTCPCONF");
close(udp_ctl);
close(tcp_fd);
return(-1);
}
s = ioctl(tcp_fd, NWIOGTCPCONF, &tcpconf);
if(s < 0) {
perror("talk: ioctl NWIOGTCPCONF");
close(udp_ctl);
close(tcp_fd);
return(-1);
}
dataport = tcpconf.nwtc_locport;
return(0);
}
int getreply(reply, timeout)
struct talk_reply *reply;
int timeout;
{
int s;
int terrno;
udp_io_hdr_t *udp_io_hdr;
signal(SIGALRM, TimeOut);
alarm(timeout);
s = read(udp_ctl, buffer, sizeof(buffer));
terrno = errno;
alarm(0);
errno = terrno;
if(s < 0 && errno == EINTR)
return(1);
if(s < 0) {
perror("talk: Read error in getreply");
return(-1);
}
if(s == sizeof(struct talk_reply))
memcpy((char *)reply, buffer, s);
return(0);
}
int sendrequest(request, here)
struct talk_request *request;
int here;
{
int s;
nwio_udpopt_t udpopt;
udp_io_hdr_t *udp_io_hdr;
udpopt.nwuo_flags = NWUO_NOFLAGS;
udpopt.nwuo_flags |= NWUO_COPY | NWUO_LP_SET | NWUO_EN_LOC;
udpopt.nwuo_flags |= NWUO_DI_BROAD | NWUO_RP_SET | NWUO_RA_SET;
udpopt.nwuo_flags |= NWUO_RWDATONLY | NWUO_DI_IPOPT;
udpopt.nwuo_locport = ctlport;
if(here)
udpopt.nwuo_remaddr = laddr;
else
udpopt.nwuo_remaddr = raddr;
udpopt.nwuo_remport = ntalk_port;
s = ioctl(udp_ctl, NWIOSUDPOPT, &udpopt);
if(s < 0) {
perror("talk: ioctl NWIOSUDPOPT");
return(-1);
}
s = ioctl(udp_ctl, NWIOGUDPOPT, &udpopt);
if(s < 0) {
perror("talk: ioctl NWIOGUDPOPT");
return(-1);
}
s = write(udp_ctl, request, sizeof(struct talk_request));
if(s < 0) {
perror("talk: write error in sendrequest");
return(-1);
}
if(s != sizeof(struct talk_request)) {
fprintf(stderr, "talk: sendrequest size mismatch %d %d\n", s, sizeof(struct talk_request));
return(-1);
}
return(0);
}
void TimeOut(sig)
int sig;
{
}
int NetConnect(port)
u16_t port;
{
int s;
nwio_tcpconf_t tcpconf;
nwio_tcpcl_t tcpcopt;
tcpconf.nwtc_flags = NWTC_NOFLAGS;
tcpconf.nwtc_flags |= NWTC_LP_SET | NWTC_SET_RA | NWTC_SET_RP;
tcpconf.nwtc_locport = dataport;
tcpconf.nwtc_remaddr = raddr;
tcpconf.nwtc_remport = port;
s = ioctl(tcp_fd, NWIOSTCPCONF, &tcpconf);
if(s < 0) {
perror("talk: ioctl NWIOSTCPCONF");
return(-1);
}
s = ioctl(tcp_fd, NWIOGTCPCONF, &tcpconf);
if(s < 0) {
perror("talk: ioctl NWIOGTCPCONF");
return(-1);
}
tcpcopt.nwtcl_flags = 0;
s = ioctl(tcp_fd, NWIOTCPCONN, &tcpcopt);
if(s < 0 && errno == ECONNREFUSED)
return(1);
if(s < 0) {
perror("talk: ioctl NWIOTCPCONN");
return(-1);
}
return(0);
}
int NetListen(timeout)
int timeout;
{
int s;
nwio_tcpcl_t tcplopt;
int terrno;
tcplopt.nwtcl_flags = 0;
signal(SIGALRM, TimeOut);
alarm(timeout);
s = ioctl(tcp_fd, NWIOTCPLISTEN, &tcplopt);
terrno = errno;
alarm(0);
errno = terrno;
if(s < 0 && errno == EINTR)
return(1);
if(s < 0) {
perror("talk: ioctl NWIOTCPLISTEN");
return(-1);
}
return(0);
}