pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / talk / net.c
blobeee4acfc5dda5c702b384eaeba7bb8562957a42a
1 /* net.c Copyright Michael Temari 08/01/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 <signal.h>
12 #include <net/netlib.h>
13 #include <net/hton.h>
14 #include <net/gen/netdb.h>
15 #include <net/gen/in.h>
16 #include <net/gen/inet.h>
17 #include <net/gen/tcp.h>
18 #include <net/gen/tcp_io.h>
19 #include <net/gen/udp.h>
20 #include <net/gen/udp_io.h>
21 #include <net/gen/udp_hdr.h>
23 #include "talk.h"
24 #include "net.h"
26 _PROTOTYPE(void TimeOut, (int sig));
28 static unsigned char buffer[8192];
30 static int udp_ctl;
31 int tcp_fd;
33 static udpport_t ntalk_port;
35 char luser[USER_SIZE+1], ruser[USER_SIZE+1];
36 char lhost[HOST_SIZE+1], rhost[HOST_SIZE+1];
37 char ltty[TTY_SIZE+1], rtty[TTY_SIZE+1];
38 udpport_t ctlport;
39 tcpport_t dataport;
40 ipaddr_t laddr, raddr;
42 int NetInit()
44 int s;
45 struct servent *servent;
46 char *udp_device;
47 char *tcp_device;
48 nwio_udpopt_t udpopt;
49 nwio_tcpconf_t tcpconf;
51 if((udp_device = getenv("UDP_DEVICE")) == (char *)NULL)
52 udp_device = UDP_DEVICE;
54 if((udp_ctl = open(udp_device, O_RDWR)) < 0) {
55 fprintf(stderr, "talk: Could not open %s: %s\n",
56 udp_device, strerror(errno));
57 return(-1);
60 if((servent = getservbyname("ntalk", "udp")) == (struct servent *)NULL) {
61 fprintf(stderr, "talk: Could not find ntalk udp service\n");
62 close(udp_ctl);
63 return(-1);
66 ntalk_port = (udpport_t)servent->s_port;
68 udpopt.nwuo_flags = NWUO_NOFLAGS;
69 udpopt.nwuo_flags |= NWUO_COPY | NWUO_LP_SEL | NWUO_EN_LOC;
70 udpopt.nwuo_flags |= NWUO_DI_BROAD | NWUO_RP_SET | NWUO_RA_SET;
71 udpopt.nwuo_flags |= NWUO_RWDATONLY | NWUO_DI_IPOPT;
72 udpopt.nwuo_remaddr = raddr;
73 udpopt.nwuo_remport = ntalk_port;
75 s = ioctl(udp_ctl, NWIOSUDPOPT, &udpopt);
76 if(s < 0) {
77 perror("talk: ioctl NWIOSUDPOPT");
78 close(udp_ctl);
79 return(-1);
82 s = ioctl(udp_ctl, NWIOGUDPOPT, &udpopt);
83 if(s < 0) {
84 perror("talk: ioctl NWIOGUDPOPT");
85 close(udp_ctl);
86 return(-1);
88 laddr = udpopt.nwuo_locaddr;
89 ctlport = udpopt.nwuo_locport;
91 if((tcp_device = getenv("TCP_DEVICE")) == (char *)NULL)
92 tcp_device = TCP_DEVICE;
94 if((tcp_fd = open(tcp_device, O_RDWR)) < 0) {
95 fprintf(stderr, "talk: Could not open %s: %s\n",
96 tcp_device, strerror(errno));
97 close(udp_ctl);
98 return(-1);
101 tcpconf.nwtc_flags = NWTC_NOFLAGS;
102 tcpconf.nwtc_flags |= NWTC_LP_SEL | NWTC_SET_RA | NWTC_UNSET_RP;
103 tcpconf.nwtc_remaddr = raddr;
105 s = ioctl(tcp_fd, NWIOSTCPCONF, &tcpconf);
106 if(s < 0) {
107 perror("talk: ioctl NWIOSTCPCONF");
108 close(udp_ctl);
109 close(tcp_fd);
110 return(-1);
113 s = ioctl(tcp_fd, NWIOGTCPCONF, &tcpconf);
114 if(s < 0) {
115 perror("talk: ioctl NWIOGTCPCONF");
116 close(udp_ctl);
117 close(tcp_fd);
118 return(-1);
121 dataport = tcpconf.nwtc_locport;
123 return(0);
126 int getreply(reply, timeout)
127 struct talk_reply *reply;
128 int timeout;
130 int s;
131 int terrno;
132 udp_io_hdr_t *udp_io_hdr;
134 signal(SIGALRM, TimeOut);
135 alarm(timeout);
136 s = read(udp_ctl, buffer, sizeof(buffer));
137 terrno = errno;
138 alarm(0);
139 errno = terrno;
140 if(s < 0 && errno == EINTR)
141 return(1);
142 if(s < 0) {
143 perror("talk: Read error in getreply");
144 return(-1);
147 if(s == sizeof(struct talk_reply))
148 memcpy((char *)reply, buffer, s);
150 return(0);
153 int sendrequest(request, here)
154 struct talk_request *request;
155 int here;
157 int s;
158 nwio_udpopt_t udpopt;
159 udp_io_hdr_t *udp_io_hdr;
161 udpopt.nwuo_flags = NWUO_NOFLAGS;
162 udpopt.nwuo_flags |= NWUO_COPY | NWUO_LP_SET | NWUO_EN_LOC;
163 udpopt.nwuo_flags |= NWUO_DI_BROAD | NWUO_RP_SET | NWUO_RA_SET;
164 udpopt.nwuo_flags |= NWUO_RWDATONLY | NWUO_DI_IPOPT;
165 udpopt.nwuo_locport = ctlport;
166 if(here)
167 udpopt.nwuo_remaddr = laddr;
168 else
169 udpopt.nwuo_remaddr = raddr;
170 udpopt.nwuo_remport = ntalk_port;
172 s = ioctl(udp_ctl, NWIOSUDPOPT, &udpopt);
173 if(s < 0) {
174 perror("talk: ioctl NWIOSUDPOPT");
175 return(-1);
178 s = ioctl(udp_ctl, NWIOGUDPOPT, &udpopt);
179 if(s < 0) {
180 perror("talk: ioctl NWIOGUDPOPT");
181 return(-1);
184 s = write(udp_ctl, request, sizeof(struct talk_request));
185 if(s < 0) {
186 perror("talk: write error in sendrequest");
187 return(-1);
190 if(s != sizeof(struct talk_request)) {
191 fprintf(stderr, "talk: sendrequest size mismatch %d %d\n", s, sizeof(struct talk_request));
192 return(-1);
195 return(0);
198 void TimeOut(int sig)
202 int NetConnect(u16_t port)
204 int s;
205 nwio_tcpconf_t tcpconf;
206 nwio_tcpcl_t tcpcopt;
208 tcpconf.nwtc_flags = NWTC_NOFLAGS;
209 tcpconf.nwtc_flags |= NWTC_LP_SET | NWTC_SET_RA | NWTC_SET_RP;
210 tcpconf.nwtc_locport = dataport;
211 tcpconf.nwtc_remaddr = raddr;
212 tcpconf.nwtc_remport = port;
214 s = ioctl(tcp_fd, NWIOSTCPCONF, &tcpconf);
215 if(s < 0) {
216 perror("talk: ioctl NWIOSTCPCONF");
217 return(-1);
220 s = ioctl(tcp_fd, NWIOGTCPCONF, &tcpconf);
221 if(s < 0) {
222 perror("talk: ioctl NWIOGTCPCONF");
223 return(-1);
226 tcpcopt.nwtcl_flags = 0;
228 s = ioctl(tcp_fd, NWIOTCPCONN, &tcpcopt);
229 if(s < 0 && errno == ECONNREFUSED)
230 return(1);
231 if(s < 0) {
232 perror("talk: ioctl NWIOTCPCONN");
233 return(-1);
236 return(0);
239 int NetListen(timeout)
240 int timeout;
242 int s;
243 nwio_tcpcl_t tcplopt;
244 int terrno;
246 tcplopt.nwtcl_flags = 0;
248 signal(SIGALRM, TimeOut);
249 alarm(timeout);
250 s = ioctl(tcp_fd, NWIOTCPLISTEN, &tcplopt);
251 terrno = errno;
252 alarm(0);
253 errno = terrno;
255 if(s < 0 && errno == EINTR)
256 return(1);
258 if(s < 0) {
259 perror("talk: ioctl NWIOTCPLISTEN");
260 return(-1);
263 return(0);