source: trunk/minix/commands/talkd/net.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 4.7 KB
RevLine 
[9]1/* net.c Copyright Michael Temari 07/22/1996 All Rights Reserved */
2
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>
18
19#include "talk.h"
20#include "talkd.h"
21#include "net.h"
22
23static unsigned char buffer[8192];
24
25static int udp_in;
26static int udp_out;
27
28static udpport_t ntalk_port;
29
30int NetInit()
31{
32int s;
33struct servent *servent;
34char *udp_device;
35nwio_udpopt_t udpopt;
36
37 if((udp_device = getenv("UDP_DEVICE")) == (char *)NULL)
38 udp_device = UDP_DEVICE;
39
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);
44 }
45
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);
51 }
52
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);
58 }
59
60 ntalk_port = (udpport_t)servent->s_port;
61
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;
67
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);
74 }
75
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);
82 }
83
84 return(0);
85}
86
87int getrequest(request)
88struct talk_request *request;
89{
90int s;
91udp_io_hdr_t *udp_io_hdr;
92
93 s = read(udp_in, buffer, sizeof(buffer));
94 if(s < 0) {
95 perror("talkd: Read error in getrequest");
96 return(-1);
97 }
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);
101 }
102 udp_io_hdr = (udp_io_hdr_t *)buffer;
103 s = s - sizeof(udp_io_hdr_t);
104
105 /* why is uih_data_len already in host order??? */
106
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);
111 }
112
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);
117 }
118
119 memcpy((char *)request, buffer + sizeof(udp_io_hdr_t), s);
120
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);
133 }
134
135 return(0);
136}
137
138int sendreply(request, reply)
139struct talk_request *request;
140struct talk_reply *reply;
141{
142int s;
143nwio_udpopt_t udpopt;
144udp_io_hdr_t *udp_io_hdr;
145
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;
153
154 s = ioctl(udp_out, NWIOSUDPOPT, &udpopt);
155 if(s < 0) {
156 perror("talkd: ioctl NWIOSUDPOPT");
157 return(-1);
158 }
159
160 s = ioctl(udp_out, NWIOGUDPOPT, &udpopt);
161 if(s < 0) {
162 perror("talkd: ioctl NWIOGUDPOPT");
163 return(-1);
164 }
165
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");
174 }
175
176 s = write(udp_out, reply, sizeof(struct talk_reply));
177 if(s < 0) {
178 perror("talkd: write");
179 return(-1);
180 }
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);
185 }
186
187 return(0);
188}
Note: See TracBrowser for help on using the repository browser.