mtree: no more /lib and /lib/i386.
[minix.git] / commands / talkd / net.c
blob4eb136cce7ed54407dba04f863cee48a0fb50156
1 /* net.c Copyright Michael Temari 07/22/1996 All Rights Reserved */
3 #include <sys/types.h>
4 #include <sys/ioctl.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <fcntl.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <net/netlib.h>
12 #include <net/hton.h>
13 #include <net/gen/netdb.h>
14 #include <net/gen/in.h>
15 #include <net/gen/udp.h>
16 #include <net/gen/udp_io.h>
17 #include <net/gen/udp_hdr.h>
19 #include "talk.h"
20 #include "talkd.h"
21 #include "net.h"
23 static unsigned char buffer[8192];
25 static int udp_in;
26 static int udp_out;
28 static udpport_t ntalk_port;
30 int NetInit()
32 int s;
33 struct servent *servent;
34 char *udp_device;
35 nwio_udpopt_t udpopt;
37 if((udp_device = getenv("UDP_DEVICE")) == (char *)NULL)
38 udp_device = UDP_DEVICE;
40 if((udp_in = open(udp_device, O_RDWR)) < 0) {
41 fprintf(stderr, "talkd: Could not open %s: %s\n",
42 udp_device, strerror(errno));
43 return(-1);
46 if((udp_out = open(udp_device, O_RDWR)) < 0) {
47 fprintf(stderr, "talkd: Could not open %s: %s\n",
48 udp_device, strerror(errno));
49 close(udp_in);
50 return(-1);
53 if((servent = getservbyname("ntalk", "udp")) == (struct servent *)NULL) {
54 fprintf(stderr, "talkd: Could not find ntalk udp service\n");
55 close(udp_in);
56 close(udp_out);
57 return(-1);
60 ntalk_port = (udpport_t)servent->s_port;
62 udpopt.nwuo_flags = NWUO_NOFLAGS;
63 udpopt.nwuo_flags |= NWUO_COPY | NWUO_LP_SET | NWUO_EN_LOC;
64 udpopt.nwuo_flags |= NWUO_DI_BROAD | NWUO_RP_ANY | NWUO_RA_ANY;
65 udpopt.nwuo_flags |= NWUO_RWDATALL | NWUO_DI_IPOPT;
66 udpopt.nwuo_locport = ntalk_port;
68 s = ioctl(udp_in, NWIOSUDPOPT, &udpopt);
69 if(s < 0) {
70 perror("talkd: ioctl NWIOSUDPOPT");
71 close(udp_in);
72 close(udp_out);
73 return(-1);
76 s = ioctl(udp_in, NWIOGUDPOPT, &udpopt);
77 if(s < 0) {
78 perror("talkd: ioctl NWIOGUDPOPT");
79 close(udp_in);
80 close(udp_out);
81 return(-1);
84 return(0);
87 int getrequest(request)
88 struct talk_request *request;
90 int s;
91 udp_io_hdr_t *udp_io_hdr;
93 s = read(udp_in, buffer, sizeof(buffer));
94 if(s < 0) {
95 perror("talkd: Read error in getrequest");
96 return(-1);
98 if(s < sizeof(udp_io_hdr_t)) {
99 fprintf(stderr, "talkd: Packet size read %d is smaller the udp_io_hdr\n", s);
100 return(-1);
102 udp_io_hdr = (udp_io_hdr_t *)buffer;
103 s = s - sizeof(udp_io_hdr_t);
105 /* why is uih_data_len already in host order??? */
107 if(udp_io_hdr->uih_data_len != s) {
108 fprintf(stderr, "talkd: Size mismatch Packet %d Udp Data %d\n",
109 s, udp_io_hdr->uih_data_len);
110 return(-1);
113 if(s != sizeof(struct talk_request)) {
114 fprintf(stderr, "talkd: Size mismatch in request %d %d\n",
115 s, sizeof(struct talk_request));
116 return(-1);
119 memcpy((char *)request, buffer + sizeof(udp_io_hdr_t), s);
121 if(opt_d) {
122 fprintf(stderr, "Request: ");
123 fprintf(stderr, "%02x %02x %02x %02x ",
124 request->version, request->type, request->answer, request->junk);
125 fprintf(stderr, "%08lx ", request->id);
126 fprintf(stderr, "%04x %08lx:%04x\n",
127 request->addr.sa_family, request->addr.sin_addr, request->addr.sin_port);
128 fprintf(stderr, " %08lx ", request->pid);
129 fprintf(stderr, "%04x %08lx:%04x\n",
130 request->ctl_addr.sa_family, request->ctl_addr.sin_addr, request->ctl_addr.sin_port);
131 fprintf(stderr, " %-12.12s %-12.12s %-16.16s\n",
132 request->luser, request->ruser, request->rtty);
135 return(0);
138 int sendreply(request, reply)
139 struct talk_request *request;
140 struct talk_reply *reply;
142 int s;
143 nwio_udpopt_t udpopt;
144 udp_io_hdr_t *udp_io_hdr;
146 udpopt.nwuo_flags = NWUO_NOFLAGS;
147 udpopt.nwuo_flags |= NWUO_COPY | NWUO_LP_SET | NWUO_EN_LOC;
148 udpopt.nwuo_flags |= NWUO_DI_BROAD | NWUO_RP_SET | NWUO_RA_SET;
149 udpopt.nwuo_flags |= NWUO_RWDATONLY | NWUO_DI_IPOPT;
150 udpopt.nwuo_locport = ntalk_port;
151 udpopt.nwuo_remaddr = request->ctl_addr.sin_addr;
152 udpopt.nwuo_remport = request->ctl_addr.sin_port;
154 s = ioctl(udp_out, NWIOSUDPOPT, &udpopt);
155 if(s < 0) {
156 perror("talkd: ioctl NWIOSUDPOPT");
157 return(-1);
160 s = ioctl(udp_out, NWIOGUDPOPT, &udpopt);
161 if(s < 0) {
162 perror("talkd: ioctl NWIOGUDPOPT");
163 return(-1);
166 if(opt_d) {
167 fprintf(stderr, "Reply: ");
168 fprintf(stderr, "%02x %02x %02x %02x ",
169 reply->version, reply->type, reply->answer, reply->junk);
170 fprintf(stderr, "%08lx ", reply->id);
171 fprintf(stderr, "%04x %08lx:%04x",
172 reply->addr.sa_family, reply->addr.sin_addr, reply->addr.sin_port);
173 fprintf(stderr, "\n");
176 s = write(udp_out, reply, sizeof(struct talk_reply));
177 if(s < 0) {
178 perror("talkd: write");
179 return(-1);
181 if(s != sizeof(struct talk_reply)) {
182 fprintf(stderr, "talkd: write size mismatch %d %d\n",
183 s, sizeof(struct talk_reply));
184 return(-1);
187 return(0);