1 /* net.c Copyright Michael Temari 08/01/1996 All Rights Reserved */
12 #include <net/netlib.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>
26 _PROTOTYPE(void TimeOut
, (int sig
));
28 static unsigned char buffer
[8192];
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];
40 ipaddr_t laddr
, raddr
;
45 struct servent
*servent
;
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
));
60 if((servent
= getservbyname("ntalk", "udp")) == (struct servent
*)NULL
) {
61 fprintf(stderr
, "talk: Could not find ntalk udp service\n");
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
);
77 perror("talk: ioctl NWIOSUDPOPT");
82 s
= ioctl(udp_ctl
, NWIOGUDPOPT
, &udpopt
);
84 perror("talk: ioctl NWIOGUDPOPT");
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
));
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
);
107 perror("talk: ioctl NWIOSTCPCONF");
113 s
= ioctl(tcp_fd
, NWIOGTCPCONF
, &tcpconf
);
115 perror("talk: ioctl NWIOGTCPCONF");
121 dataport
= tcpconf
.nwtc_locport
;
126 int getreply(reply
, timeout
)
127 struct talk_reply
*reply
;
132 udp_io_hdr_t
*udp_io_hdr
;
134 signal(SIGALRM
, TimeOut
);
136 s
= read(udp_ctl
, buffer
, sizeof(buffer
));
140 if(s
< 0 && errno
== EINTR
)
143 perror("talk: Read error in getreply");
147 if(s
== sizeof(struct talk_reply
))
148 memcpy((char *)reply
, buffer
, s
);
153 int sendrequest(request
, here
)
154 struct talk_request
*request
;
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
;
167 udpopt
.nwuo_remaddr
= laddr
;
169 udpopt
.nwuo_remaddr
= raddr
;
170 udpopt
.nwuo_remport
= ntalk_port
;
172 s
= ioctl(udp_ctl
, NWIOSUDPOPT
, &udpopt
);
174 perror("talk: ioctl NWIOSUDPOPT");
178 s
= ioctl(udp_ctl
, NWIOGUDPOPT
, &udpopt
);
180 perror("talk: ioctl NWIOGUDPOPT");
184 s
= write(udp_ctl
, request
, sizeof(struct talk_request
));
186 perror("talk: write error in sendrequest");
190 if(s
!= sizeof(struct talk_request
)) {
191 fprintf(stderr
, "talk: sendrequest size mismatch %d %d\n", s
, sizeof(struct talk_request
));
198 void TimeOut(int sig
)
202 int NetConnect(u16_t port
)
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
);
216 perror("talk: ioctl NWIOSTCPCONF");
220 s
= ioctl(tcp_fd
, NWIOGTCPCONF
, &tcpconf
);
222 perror("talk: ioctl NWIOGTCPCONF");
226 tcpcopt
.nwtcl_flags
= 0;
228 s
= ioctl(tcp_fd
, NWIOTCPCONN
, &tcpcopt
);
229 if(s
< 0 && errno
== ECONNREFUSED
)
232 perror("talk: ioctl NWIOTCPCONN");
239 int NetListen(timeout
)
243 nwio_tcpcl_t tcplopt
;
246 tcplopt
.nwtcl_flags
= 0;
248 signal(SIGALRM
, TimeOut
);
250 s
= ioctl(tcp_fd
, NWIOTCPLISTEN
, &tcplopt
);
255 if(s
< 0 && errno
== EINTR
)
259 perror("talk: ioctl NWIOTCPLISTEN");