1 /* net.c Copyright Michael Temari 07/22/1996 All Rights Reserved */
11 #include <net/netlib.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>
23 static unsigned char buffer
[8192];
28 static udpport_t ntalk_port
;
33 struct servent
*servent
;
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
));
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
));
53 if((servent
= getservbyname("ntalk", "udp")) == (struct servent
*)NULL
) {
54 fprintf(stderr
, "talkd: Could not find ntalk udp service\n");
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
);
70 perror("talkd: ioctl NWIOSUDPOPT");
76 s
= ioctl(udp_in
, NWIOGUDPOPT
, &udpopt
);
78 perror("talkd: ioctl NWIOGUDPOPT");
87 int getrequest(request
)
88 struct talk_request
*request
;
91 udp_io_hdr_t
*udp_io_hdr
;
93 s
= read(udp_in
, buffer
, sizeof(buffer
));
95 perror("talkd: Read error in getrequest");
98 if(s
< sizeof(udp_io_hdr_t
)) {
99 fprintf(stderr
, "talkd: Packet size read %d is smaller the udp_io_hdr\n", s
);
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
);
113 if(s
!= sizeof(struct talk_request
)) {
114 fprintf(stderr
, "talkd: Size mismatch in request %d %d\n",
115 s
, sizeof(struct talk_request
));
119 memcpy((char *)request
, buffer
+ sizeof(udp_io_hdr_t
), s
);
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
);
138 int sendreply(request
, reply
)
139 struct talk_request
*request
;
140 struct talk_reply
*reply
;
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
);
156 perror("talkd: ioctl NWIOSUDPOPT");
160 s
= ioctl(udp_out
, NWIOGUDPOPT
, &udpopt
);
162 perror("talkd: ioctl NWIOGUDPOPT");
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
));
178 perror("talkd: write");
181 if(s
!= sizeof(struct talk_reply
)) {
182 fprintf(stderr
, "talkd: write size mismatch %d %d\n",
183 s
, sizeof(struct talk_reply
));