[9] | 1 | /* net.c Copyright Michael Temari 08/01/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 <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>
|
---|
| 22 |
|
---|
| 23 | #include "talk.h"
|
---|
| 24 | #include "net.h"
|
---|
| 25 |
|
---|
| 26 | _PROTOTYPE(void TimeOut, (int sig));
|
---|
| 27 |
|
---|
| 28 | static unsigned char buffer[8192];
|
---|
| 29 |
|
---|
| 30 | static int udp_ctl;
|
---|
| 31 | int tcp_fd;
|
---|
| 32 |
|
---|
| 33 | static udpport_t ntalk_port;
|
---|
| 34 |
|
---|
| 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;
|
---|
| 41 |
|
---|
| 42 | int NetInit()
|
---|
| 43 | {
|
---|
| 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;
|
---|
| 50 |
|
---|
| 51 | if((udp_device = getenv("UDP_DEVICE")) == (char *)NULL)
|
---|
| 52 | udp_device = UDP_DEVICE;
|
---|
| 53 |
|
---|
| 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);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 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);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | ntalk_port = (udpport_t)servent->s_port;
|
---|
| 67 |
|
---|
| 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;
|
---|
| 74 |
|
---|
| 75 | s = ioctl(udp_ctl, NWIOSUDPOPT, &udpopt);
|
---|
| 76 | if(s < 0) {
|
---|
| 77 | perror("talk: ioctl NWIOSUDPOPT");
|
---|
| 78 | close(udp_ctl);
|
---|
| 79 | return(-1);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | s = ioctl(udp_ctl, NWIOGUDPOPT, &udpopt);
|
---|
| 83 | if(s < 0) {
|
---|
| 84 | perror("talk: ioctl NWIOGUDPOPT");
|
---|
| 85 | close(udp_ctl);
|
---|
| 86 | return(-1);
|
---|
| 87 | }
|
---|
| 88 | laddr = udpopt.nwuo_locaddr;
|
---|
| 89 | ctlport = udpopt.nwuo_locport;
|
---|
| 90 |
|
---|
| 91 | if((tcp_device = getenv("TCP_DEVICE")) == (char *)NULL)
|
---|
| 92 | tcp_device = TCP_DEVICE;
|
---|
| 93 |
|
---|
| 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);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | tcpconf.nwtc_flags = NWTC_NOFLAGS;
|
---|
| 102 | tcpconf.nwtc_flags |= NWTC_LP_SEL | NWTC_SET_RA | NWTC_UNSET_RP;
|
---|
| 103 | tcpconf.nwtc_remaddr = raddr;
|
---|
| 104 |
|
---|
| 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);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 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);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | dataport = tcpconf.nwtc_locport;
|
---|
| 122 |
|
---|
| 123 | return(0);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | int getreply(reply, timeout)
|
---|
| 127 | struct talk_reply *reply;
|
---|
| 128 | int timeout;
|
---|
| 129 | {
|
---|
| 130 | int s;
|
---|
| 131 | int terrno;
|
---|
| 132 | udp_io_hdr_t *udp_io_hdr;
|
---|
| 133 |
|
---|
| 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);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | if(s == sizeof(struct talk_reply))
|
---|
| 148 | memcpy((char *)reply, buffer, s);
|
---|
| 149 |
|
---|
| 150 | return(0);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | int sendrequest(request, here)
|
---|
| 154 | struct talk_request *request;
|
---|
| 155 | int here;
|
---|
| 156 | {
|
---|
| 157 | int s;
|
---|
| 158 | nwio_udpopt_t udpopt;
|
---|
| 159 | udp_io_hdr_t *udp_io_hdr;
|
---|
| 160 |
|
---|
| 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;
|
---|
| 171 |
|
---|
| 172 | s = ioctl(udp_ctl, NWIOSUDPOPT, &udpopt);
|
---|
| 173 | if(s < 0) {
|
---|
| 174 | perror("talk: ioctl NWIOSUDPOPT");
|
---|
| 175 | return(-1);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | s = ioctl(udp_ctl, NWIOGUDPOPT, &udpopt);
|
---|
| 179 | if(s < 0) {
|
---|
| 180 | perror("talk: ioctl NWIOGUDPOPT");
|
---|
| 181 | return(-1);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | s = write(udp_ctl, request, sizeof(struct talk_request));
|
---|
| 185 | if(s < 0) {
|
---|
| 186 | perror("talk: write error in sendrequest");
|
---|
| 187 | return(-1);
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 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);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | return(0);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | void TimeOut(sig)
|
---|
| 199 | int sig;
|
---|
| 200 | {
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | int NetConnect(port)
|
---|
| 204 | u16_t port;
|
---|
| 205 | {
|
---|
| 206 | int s;
|
---|
| 207 | nwio_tcpconf_t tcpconf;
|
---|
| 208 | nwio_tcpcl_t tcpcopt;
|
---|
| 209 |
|
---|
| 210 | tcpconf.nwtc_flags = NWTC_NOFLAGS;
|
---|
| 211 | tcpconf.nwtc_flags |= NWTC_LP_SET | NWTC_SET_RA | NWTC_SET_RP;
|
---|
| 212 | tcpconf.nwtc_locport = dataport;
|
---|
| 213 | tcpconf.nwtc_remaddr = raddr;
|
---|
| 214 | tcpconf.nwtc_remport = port;
|
---|
| 215 |
|
---|
| 216 | s = ioctl(tcp_fd, NWIOSTCPCONF, &tcpconf);
|
---|
| 217 | if(s < 0) {
|
---|
| 218 | perror("talk: ioctl NWIOSTCPCONF");
|
---|
| 219 | return(-1);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | s = ioctl(tcp_fd, NWIOGTCPCONF, &tcpconf);
|
---|
| 223 | if(s < 0) {
|
---|
| 224 | perror("talk: ioctl NWIOGTCPCONF");
|
---|
| 225 | return(-1);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | tcpcopt.nwtcl_flags = 0;
|
---|
| 229 |
|
---|
| 230 | s = ioctl(tcp_fd, NWIOTCPCONN, &tcpcopt);
|
---|
| 231 | if(s < 0 && errno == ECONNREFUSED)
|
---|
| 232 | return(1);
|
---|
| 233 | if(s < 0) {
|
---|
| 234 | perror("talk: ioctl NWIOTCPCONN");
|
---|
| 235 | return(-1);
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | return(0);
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | int NetListen(timeout)
|
---|
| 242 | int timeout;
|
---|
| 243 | {
|
---|
| 244 | int s;
|
---|
| 245 | nwio_tcpcl_t tcplopt;
|
---|
| 246 | int terrno;
|
---|
| 247 |
|
---|
| 248 | tcplopt.nwtcl_flags = 0;
|
---|
| 249 |
|
---|
| 250 | signal(SIGALRM, TimeOut);
|
---|
| 251 | alarm(timeout);
|
---|
| 252 | s = ioctl(tcp_fd, NWIOTCPLISTEN, &tcplopt);
|
---|
| 253 | terrno = errno;
|
---|
| 254 | alarm(0);
|
---|
| 255 | errno = terrno;
|
---|
| 256 |
|
---|
| 257 | if(s < 0 && errno == EINTR)
|
---|
| 258 | return(1);
|
---|
| 259 |
|
---|
| 260 | if(s < 0) {
|
---|
| 261 | perror("talk: ioctl NWIOTCPLISTEN");
|
---|
| 262 | return(-1);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | return(0);
|
---|
| 266 | }
|
---|