|
|
1.1 root 1: /* /sccs/src/cmd/uucp/s.xio.c
2: xio.c 1.2 8/30/84 17:38:20
3: */
4: #include "uucp.h"
5: VERSION(@(#)xio.c 1.2);
6:
7: #ifdef X25
8:
9: #define XBUFSIZ 512
10: static jmp_buf Xfailbuf;
11:
12: /*
13: * x.25 protocol
14: */
15: static xalarm()
16: {
17: longjmp(Xfailbuf, 1);
18: }
19:
20: static int (*xsig)();
21:
22: /*
23: * turn on protocol timer
24: */
25: xturnon()
26: {
27: xsig=signal(SIGALRM, xalarm);
28: return(0);
29: }
30: xturnoff()
31: {
32: (void) signal(SIGALRM, xsig);
33: return(0);
34: }
35:
36: /*
37: * write message across x.25 link
38: * type -> message type
39: * str -> message body (ascii string)
40: * fn -> x.25 file descriptor
41: * return
42: * 0
43: */
44: xwrmsg(type, str, fn)
45: register char *str;
46: int fn;
47: char type;
48: {
49: register char *s;
50: char bufr[XBUFSIZ];
51:
52: bufr[0] = type;
53: s = &bufr[1];
54: while (*str)
55: *s++ = *str++;
56: *s = '\0';
57: if (*(--s) == '\n')
58: *s = '\0';
59: (void) write(fn, bufr, strlen(bufr) + 1);
60: return(0);
61: }
62:
63: /*
64: * read message from x.25 link
65: * str -> message buffer
66: * fn -> x.25 file descriptor
67: * return
68: * FAIL -> send timed out
69: * 0 -> ok message in str
70: */
71: xrdmsg(str, fn)
72: register char *str;
73: {
74: register unsigned len;
75:
76: if(setjmp(Xfailbuf))
77: return(FAIL);
78:
79: for (;;) {
80: (void) alarm(60);
81: if( (len = read(fn, str, XBUFSIZ)) == 0)
82: continue;
83: str += len;
84: if (*(str - 1) == '\0')
85: break;
86: }
87: (void) alarm(0);
88: return(0);
89: }
90:
91: /*
92: * read data from file fp1 and write
93: * on x.25 link
94: * fp1 -> file descriptor
95: * fn -> x.25 descriptor
96: * returns:
97: * FAIL ->failure in x.25 link
98: * 0 -> ok
99: */
100: xwrdata(fp1, fn)
101: register FILE *fp1;
102: {
103: register int len, ret;
104: long bytes;
105: char bufr[XBUFSIZ];
106: char text[XBUFSIZ];
107: time_t ticks;
108:
109: bytes = 0L;
110: (void) millitick(); /* set msec timer */
111: while ((len = fread(bufr, sizeof (char), XBUFSIZ, fp1)) > 0) {
112: bytes += len;
113: ret = write(fn, bufr, len);
114: if (ret != len) {
115: return(FAIL);
116: }
117: if (len != XBUFSIZ)
118: break;
119: }
120: ret = write(fn, bufr, 0);
121: ticks = millitick();
122: (void) sprintf(text, "-> %ld / %ld.%.3d secs",
123: bytes, ticks / 1000, ticks % 1000);
124: DEBUG(4, "%s\n", text);
125: syslog(text);
126: return(0);
127: }
128:
129: /*
130: * read data from x.25 link and
131: * write into file
132: * fp2 -> file descriptor
133: * fn -> x.25 descriptor
134: * returns:
135: * 0 -> ok
136: * FAIL -> failure on x.25 link
137: */
138: xrddata(fn, fp2)
139: register FILE *fp2;
140: {
141: register int len;
142: long bytes;
143: char text[XBUFSIZ];
144: char bufr[XBUFSIZ];
145: time_t ticks;
146:
147: bytes = 0L;
148: (void) millitick(); /* set msec timer */
149: for (;;) {
150: len = xrdblk(bufr, XBUFSIZ, fn);
151: if (len < 0) {
152: return(FAIL);
153: }
154: bytes += len;
155: (void) fwrite(bufr, sizeof (char), len, fp2);
156: if (len < XBUFSIZ)
157: break;
158: }
159: ticks = millitick();
160: (void) sprintf(text, "<- %ld / %ld.%.3d secs",
161: bytes, ticks / 1000, ticks % 1000);
162: DEBUG(4, "%s\n", text);
163: syslog(text);
164: return(0);
165: }
166:
167: /*
168: * read blank from x.25 link
169: * reads are timed
170: * blk -> address of buffer
171: * len -> size to read
172: * fn -> x.25 descriptor
173: * returns:
174: * FAIL -> link error timeout on link
175: * i -> # of bytes read
176: */
177: xrdblk(blk, len, fn)
178: register char *blk;
179: {
180: register int i, ret;
181:
182: if(setjmp(Xfailbuf))
183: return(FAIL);
184:
185: for (i = 0; i < len; i += ret) {
186: (void) alarm(60);
187: if ((ret = read(fn, blk, len - i)) < 0) {
188: (void) alarm(0);
189: return(FAIL);
190: }
191: blk += ret;
192: if (ret == 0)
193: break;
194: }
195: (void) alarm(0);
196: return(i);
197: }
198: #endif X25
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.