Repository navigation
Expand file tree
/
Copy pathnet.c
More file actions
executable file
·188 lines (158 loc) · 4.68 KB
/
Copy pathnet.c
File metadata and controls
executable file
·188 lines (158 loc) · 4.68 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
/* net.c Copyright Michael Temari 07/22/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 <net/netlib.h>
#include <net/hton.h>
#include <net/gen/netdb.h>
#include <net/gen/in.h>
#include <net/gen/udp.h>
#include <net/gen/udp_io.h>
#include <net/gen/udp_hdr.h>
#include "talk.h"
#include "talkd.h"
#include "net.h"
static unsigned char buffer[8192];
static int udp_in;
static int udp_out;
static udpport_t ntalk_port;
int NetInit()
{
int s;
struct servent *servent;
char *udp_device;
nwio_udpopt_t udpopt;
if((udp_device = getenv("UDP_DEVICE")) == (char *)NULL)
udp_device = UDP_DEVICE;
if((udp_in = open(udp_device, O_RDWR)) < 0) {
fprintf(stderr, "talkd: Could not open %s: %s\n",
udp_device, strerror(errno));
return(-1);
}
if((udp_out = open(udp_device, O_RDWR)) < 0) {
fprintf(stderr, "talkd: Could not open %s: %s\n",
udp_device, strerror(errno));
close(udp_in);
return(-1);
}
if((servent = getservbyname("ntalk", "udp")) == (struct servent *)NULL) {
fprintf(stderr, "talkd: Could not find ntalk udp service\n");
close(udp_in);
close(udp_out);
return(-1);
}
ntalk_port = (udpport_t)servent->s_port;
udpopt.nwuo_flags = NWUO_NOFLAGS;
udpopt.nwuo_flags |= NWUO_COPY | NWUO_LP_SET | NWUO_EN_LOC;
udpopt.nwuo_flags |= NWUO_DI_BROAD | NWUO_RP_ANY | NWUO_RA_ANY;
udpopt.nwuo_flags |= NWUO_RWDATALL | NWUO_DI_IPOPT;
udpopt.nwuo_locport = ntalk_port;
s = ioctl(udp_in, NWIOSUDPOPT, &udpopt);
if(s < 0) {
perror("talkd: ioctl NWIOSUDPOPT");
close(udp_in);
close(udp_out);
return(-1);
}
s = ioctl(udp_in, NWIOGUDPOPT, &udpopt);
if(s < 0) {
perror("talkd: ioctl NWIOGUDPOPT");
close(udp_in);
close(udp_out);
return(-1);
}
return(0);
}
int getrequest(request)
struct talk_request *request;
{
int s;
udp_io_hdr_t *udp_io_hdr;
s = read(udp_in, buffer, sizeof(buffer));
if(s < 0) {
perror("talkd: Read error in getrequest");
return(-1);
}
if(s < sizeof(udp_io_hdr_t)) {
fprintf(stderr, "talkd: Packet size read %d is smaller the udp_io_hdr\n", s);
return(-1);
}
udp_io_hdr = (udp_io_hdr_t *)buffer;
s = s - sizeof(udp_io_hdr_t);
/* why is uih_data_len already in host order??? */
if(udp_io_hdr->uih_data_len != s) {
fprintf(stderr, "talkd: Size mismatch Packet %d Udp Data %d\n",
s, udp_io_hdr->uih_data_len);
return(-1);
}
if(s != sizeof(struct talk_request)) {
fprintf(stderr, "talkd: Size mismatch in request %d %d\n",
s, sizeof(struct talk_request));
return(-1);
}
memcpy((char *)request, buffer + sizeof(udp_io_hdr_t), s);
if(opt_d) {
fprintf(stderr, "Request: ");
fprintf(stderr, "%02x %02x %02x %02x ",
request->version, request->type, request->answer, request->junk);
fprintf(stderr, "%08lx ", request->id);
fprintf(stderr, "%04x %08lx:%04x\n",
request->addr.sa_family, request->addr.sin_addr, request->addr.sin_port);
fprintf(stderr, " %08lx ", request->pid);
fprintf(stderr, "%04x %08lx:%04x\n",
request->ctl_addr.sa_family, request->ctl_addr.sin_addr, request->ctl_addr.sin_port);
fprintf(stderr, " %-12.12s %-12.12s %-16.16s\n",
request->luser, request->ruser, request->rtty);
}
return(0);
}
int sendreply(request, reply)
struct talk_request *request;
struct talk_reply *reply;
{
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 = ntalk_port;
udpopt.nwuo_remaddr = request->ctl_addr.sin_addr;
udpopt.nwuo_remport = request->ctl_addr.sin_port;
s = ioctl(udp_out, NWIOSUDPOPT, &udpopt);
if(s < 0) {
perror("talkd: ioctl NWIOSUDPOPT");
return(-1);
}
s = ioctl(udp_out, NWIOGUDPOPT, &udpopt);
if(s < 0) {
perror("talkd: ioctl NWIOGUDPOPT");
return(-1);
}
if(opt_d) {
fprintf(stderr, "Reply: ");
fprintf(stderr, "%02x %02x %02x %02x ",
reply->version, reply->type, reply->answer, reply->junk);
fprintf(stderr, "%08lx ", reply->id);
fprintf(stderr, "%04x %08lx:%04x",
reply->addr.sa_family, reply->addr.sin_addr, reply->addr.sin_port);
fprintf(stderr, "\n");
}
s = write(udp_out, reply, sizeof(struct talk_reply));
if(s < 0) {
perror("talkd: write");
return(-1);
}
if(s != sizeof(struct talk_reply)) {
fprintf(stderr, "talkd: write size mismatch %d %d\n",
s, sizeof(struct talk_reply));
return(-1);
}
return(0);
}