|
|
1.1 root 1: /* %W%
2: */
3: #include "uucp.h"
4: VERSION(%W%);
5:
6: #include <dk.h>
7:
8: #define XBUFSIZ 4096
9: time_t time();
10: static jmp_buf Dfailbuf;
11:
12: /*
13: * Datakit protocol
14: */
15: static dalarm() {longjmp(Dfailbuf);}
16: static int (*dsig)();
17: #define EOFCTL 106
18:
19: #ifndef V8
20: static short dkrmode[3] = { DKR_BLOCK, 0, 0 };
21: static short dkeof[3] = { EOFCTL, 0, 0 }; /* End of File signal */
22: #define MSGread read
23: #define MSGwrite write
24: #else
25: #include <sys/stream.h>
26: #include <sys/filio.h>
27: #include <sys/dkio.h>
28: static struct mesg rhdr;
29: static struct mesg whdr = { M_DATA, MSGMAGIC, 0, 0 };
30: static struct mesg delim = { M_DELIM, MSGMAGIC, 0, 0 };
31: /*struct mesg eofmsg = { M_CTL, MSGMAGIC, 1, 0 };*/
32: static u_char eofmsg[] = { M_CTL, MSGMAGIC, 1, 0, EOFCTL };
33: extern int mesg_ld;
34: static rcount = 0;
35: static int usemesg;
36: static char ctlmsg[12]; /* sizeof struct sgttyb -- hack */
37: #endif
38:
39: /*
40: * turn on protocol
41: */
42: dturnon()
43: {
44: int ret; extern int errno;
45:
46: dsig=signal(SIGALRM, dalarm);
47: #ifndef V8
48: if(ioctl(Ofn, DIOCRMODE, dkrmode) < 0) {
49: ret=ioctl(Ofn, DIOCRMODE, dkrmode);
50: DEBUG(4, "dturnon: ret=%d, ", ret);
51: DEBUG(4, "Ofn=%d, ", Ofn);
52: DEBUG(4, "errno=%d\n", errno);
53: return(-1);
54: }
55: #else
56: ctlmsg[0] = 0;
57: if (ioctl(Ofn, DIOCSCTL, ctlmsg) >= 0) {
58: DEBUG(4, "can send ctls\n", 0);
59: usemesg = 0;
60: } else {
61: DEBUG(4, "must use mesgld\n", 0);
62: usemesg = 1;
63: if ((ret=ioctl(Ofn, FIOPUSHLD, &mesg_ld)) < 0) {
64: DEBUG(4, "pushld: ret=%d, ", ret);
65: DEBUG(4, "Ofn=%d, ", Ofn);
66: DEBUG(4, "errno=%d\n", errno);
67: return(-1);
68: }
69: }
70: rcount = 0;
71: #endif
72: return(0);
73: }
74: dturnoff()
75: {
76: (void) signal(SIGALRM, dsig);
77: #ifdef V8
78: if (usemesg)
79: ioctl(Ofn, FIOPOPLD, 0);
80: #endif
81: return(0);
82: }
83:
84: /*
85: * write message across Datakit link
86: * type -> message type
87: * str -> message body (ascii string)
88: * fn -> Datakit file descriptor
89: * return
90: * SUCCESS -> message sent
91: * FAIL -> write failed
92: */
93: dwrmsg(type, str, fn)
94: register char *str;
95: int fn;
96: char type;
97: {
98: register char *s;
99: char bufr[XBUFSIZ];
100:
101: bufr[0] = type;
102: s = &bufr[1];
103: while (*str)
104: *s++ = *str++;
105: *s = '\0';
106: if (*(--s) == '\n')
107: *s = '\0';
108: return(MSGwrite(fn, bufr, (unsigned) strlen(bufr) + 1) < 0 ? FAIL : SUCCESS);
109: }
110:
111: /*
112: * read message from Datakit link
113: * str -> message buffer
114: * fn -> Datakit file descriptor
115: * return
116: * FAIL -> send timed out
117: * SUCCESS -> ok message in str
118: */
119: drdmsg(str, fn)
120: register char *str;
121: {
122:
123: register int len;
124:
125: if(setjmp(Dfailbuf))
126: return(FAIL);
127:
128: (void) alarm(300);
129: for (;;) {
130: if( (len = MSGread(fn, str, XBUFSIZ)) <= 0) {
131: (void) alarm(0);
132: return(FAIL);
133: }
134: str += len;
135: if (*(str - 1) == '\0')
136: break;
137: }
138: (void) alarm(0);
139: return(SUCCESS);
140: }
141:
142: /*
143: * read data from file fp1 and write
144: * on Datakit link
145: * fp1 -> file descriptor
146: * fn -> Datakit descriptor
147: * returns:
148: * FAIL ->failure in Datakit link
149: * SUCCESS -> ok
150: */
151: dwrdata(fp1, fn)
152: register FILE *fp1;
153: {
154: register int len, ret;
155: long bytes;
156: char bufr[XBUFSIZ];
157: char text[128];
158: time_t ticks;
159: int fd;
160:
161: bytes = 0L;
162: fd = fileno(fp1);
163: (void) millitick(); /* set msec timer */
164: while ((len = read(fd, bufr, XBUFSIZ)) > 0) {
165: bytes += len;
166: ret = MSGwrite(fn, bufr, (unsigned) len);
167: if (ret != len) {
168: return(FAIL);
169: }
170: if (len != XBUFSIZ)
171: break;
172: }
173: #ifndef V8
174: ioctl(fn, DIOCXCTL, dkeof);
175: #else
176: MSGeof(fn);
177: #endif
178: ticks = millitick();
179: (void) sprintf(text, "-> %ld / %ld.%.3d secs",
180: bytes, ticks / 1000, ticks % 1000);
181: DEBUG(4, "%s\n", text);
182: syslog(text);
183: return(SUCCESS);
184: }
185:
186:
187: /*
188: * read data from Datakit link and
189: * write into file
190: * fp2 -> file descriptor
191: * fn -> Datakit descriptor
192: * returns:
193: * SUCCESS -> ok
194: * FAIL -> failure on Datakit link
195: */
196: drddata(fn, fp2)
197: register FILE *fp2;
198: {
199: register int len;
200: long bytes;
201: char text[128];
202: char bufr[XBUFSIZ];
203: time_t ticks;
204: int fd;
205:
206: bytes = 0L;
207: fd = fileno(fp2);
208: (void) millitick(); /* set msec timer */
209: for (;;) {
210: len = drdblk(bufr, XBUFSIZ, fn);
211: if (len < 0)
212: return(FAIL);
213: bytes += len;
214: if (write(fd, bufr, len) != len)
215: return (FAIL);
216: if (len < XBUFSIZ)
217: break;
218: }
219: /* should check for control character here */
220: ticks = millitick();
221: (void) sprintf(text, "<- %ld / %ld.%.3d secs",
222: bytes, ticks / 1000, ticks % 1000);
223: DEBUG(4, "%s\n", text);
224: syslog(text);
225: return(SUCCESS);
226: }
227:
228: /*
229: * read block from Datakit link
230: * reads are timed
231: * blk -> address of buffer
232: * len -> size to read
233: * fn -> Datakit descriptor
234: * returns:
235: * FAIL -> link error timeout on link
236: * i -> # of bytes read
237: */
238: drdblk(blk, len, fn)
239: register char *blk;
240: {
241: register int i, ret;
242:
243: if(setjmp(Dfailbuf))
244: return(FAIL);
245:
246: for (i = 0; i < len; i += ret) {
247: (void) alarm(300);
248: if ((ret = MSGread(fn, blk, (unsigned) len - i)) < 0) {
249: (void) alarm(0);
250: return(FAIL);
251: }
252: blk += ret;
253: if (ret == 0) /* zero length block contains only EOF signal */
254: break;
255: }
256: (void) alarm(0);
257: return(i);
258: }
259:
260: #ifdef V8
261: MSGread(fd, buf, count)
262: char *buf;
263: {
264: int mycount = 0, i;
265: char junk[256];
266:
267: if (usemesg == 0)
268: return (read(fd, buf, count));
269: more:
270: if (rcount==0) {
271: if ((i = read(fd, (char *)&rhdr, sizeof(rhdr))) != sizeof(rhdr)) {
272: DEBUG(4, "DK msg bad read: %d\n", i);
273: return(-1);
274: }
275: if (rhdr.magic != MSGMAGIC) {
276: DEBUG(1, "Bad magic %o on DK message read\n", rhdr.magic);
277: return(-1);
278: }
279: rcount = rhdr.losize + (rhdr.hisize<<8);
280: }
281: switch (rhdr.type) {
282:
283: case M_DELIM:
284: case M_CTL:
285: default:
286: DEBUG(5, "Got ctl %o\n", rhdr.type + (rcount<<6));
287: while (rcount>0) {
288: i = read(fd, junk, min(rcount, sizeof(junk)));
289: if (i<0)
290: break;
291: rcount -= i;
292: }
293: rcount = 0;
294: if (rhdr.type != M_DELIM)
295: goto more;
296: return (mycount);
297:
298: case M_DATA:
299: DEBUG(5, "Got data rcount %d\n", rcount);
300: while (count > 0 && rcount > 0) {
301: i = read(fd, buf, min(count, rcount));
302: if (i <= 0)
303: break;
304: buf += i;
305: mycount += i;
306: count -= i;
307: rcount -= i;
308: }
309: if (rcount == 0)
310: goto more;
311: return (mycount);
312: }
313: }
314:
315: MSGwrite(fd, buf, count)
316: char *buf;
317: {
318: if (usemesg == 0)
319: return (write(fd, buf, count));
320: whdr.hisize = count>>8;
321: whdr.losize = count;
322: if (write(fd, (char *)&whdr, sizeof(whdr)) != sizeof(whdr))
323: return(-1);
324: if (write(fd, buf, count) != count)
325: return(-1);
326: if (write(fd, (char *)&delim, sizeof(delim)) != sizeof(delim))
327: return(-1);
328: return(count);
329: }
330:
331: MSGeof(fn)
332: int fn;
333: {
334: if (usemesg)
335: write(fn, eofmsg, sizeof(eofmsg));
336: else {
337: ctlmsg[0] = EOFCTL;
338: ioctl(fn, DIOCSCTL, ctlmsg);
339: }
340: }
341: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.