File: mp_xmit.c

package info (click to toggle)
p3nfs 5.3-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 596 kB
  • ctags: 825
  • sloc: ansic: 7,434; makefile: 126; sh: 40
file content (371 lines) | stat: -rw-r--r-- 7,206 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * Protocol:
 *
 * new: PREFIX byte + old:
 * old:
 *
 * Host sends command length (1 byte) + command
 * A command consists of:
 *  bcpl filename + 0 byte + 
 *  command specific data (0-6 bytes) + command type (1 byte)
 * Command specific data:
 * - CREATE/SETATTR: attributes (NYI)
 * - RENAME: strlen of the second bcpl string
 * - READ/WRITE: 4 byte offset, 2 byte length
 * - GETVERS: filename[0] == 'C' and return > 1 then crc is used: 
 *     - READ/WRITE: after the data 2 byte crc follows.
 *
 * Answer:
 * new: PREFIX byte + old:
 * old:
 * - status of operation (0 for success)
 * - Thereafter, if return is 0 and op is
 *   - GETATTR, a 12 byte attribute structure is sent
 *   - READ,    the data is sent (+crc).
 *   - READDIR, a list of cstrings are sent, the last is of length 0
 *   - GETDEVS, a list of (cstring + 14 bytes), the last ...
 *   - WRITE, when crc is used, if crc is ok byte 0 is returned.
 *
 *
 * In short:
 *
 * Command:
 * - PREFIX 0x80, omitted if old protocol is used
 * - command length
 * - namelen
 * - name
 * - 0x00
 * - request data (0-6 bytes)
 * - request type
 * 
 *
 * Answer:
 * - PREFIX 0x80, omitted if old protocol is used
 * - status (0 == OK)
 * - data
 *
 */

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#ifdef _IBMR2
# include <sys/select.h>
#endif
#include <termios.h>	/* for tcdrain() */
#if defined(__SVR4) || defined(__GLIBC__)
#include <poll.h>
#include <unistd.h>
#include <string.h>
#endif
#include "nfs_prot.h"
#include "mp.h"
#include "config.h"

/* Since the psion is little endian, we have to do some conversion */
/* Note: this should work for little endian hosts too */

#include <sys/errno.h>
extern int errno;
#if !defined(__NetBSD__) && !defined(__GLIBC__)
extern char *sys_errlist[];
#endif

void
short2pstr(l, s)
  unsigned int l;
  unsigned char *s;
{
  s[0] = l & 0xff;
  s[1] = (l & 0xff00) >> 8;
}

void
long2pstr(l, s)
  unsigned int l;
  unsigned char *s;
{
  s[0] = l & 0xff;
  s[1] = (l & 0xff00) >> 8;
  s[2] = (l & 0xff0000) >> 16;
  s[3] = (l & 0xff000000) >> 24;
}

unsigned int
pstr2long(s)
  unsigned char *s;
{
  int l = s[0];

  l |= s[1] <<  8; l |= s[2] << 16;
  return l | (s[3] << 24);
}


int
senddata(p, len)
  char *p;
  int len;
{
  int ret;
  int lerr;

  if(debug) printf("\t-> psion\n");
  psion_alive = fd_is_still_alive(psionfd,1);
  if(!psion_alive)
    {
      if(debug) printf("Senddata: Not alive...\n");
      return EOF;
    }

  lerr = 0;
  if(debug > 1) printf("\tSending: %#x, %d\n", (unsigned int)p, len);
  while((ret = write(psionfd, p, len)) < len)
    {
      if(debug > 1)
	fprintf(stderr, "\t\twrite(%d, %#x, %d) = %d\n",
	        psionfd, (unsigned int)p, len, ret);
      if(ret > 0)
	{
	  p += ret;
	  len -= ret;
	  lerr = 0;
	  continue;
	}
      if(ret < 0 && errno != EAGAIN)
        {
	  perror("p3nfsd: Write");
	  return 1;
	}

      lerr++;
      if(lerr > 1)
	{
	  if(debug)
	    fprintf(stderr, "\twrite=%d after flush/select\n", ret);
	  return 1;
	}

#ifdef NO_WRITE_SELECT

      if(debug > 1)
	fprintf(stderr, "\t\tdraining serial line\n");
      if(tcdrain(psionfd))
	perror("p3nfsd: tcdrain");

#else

      {
	fd_set writefd;
	struct timeval to;

	FD_ZERO(&writefd); FD_SET(psionfd, &writefd);
	to.tv_sec = 5; to.tv_usec = 0; 
	if((ret = select(psionfd+1, 0, &writefd, 0, &to)) <= 0)
	  {
	    if(debug)
	      fprintf(stderr, "p3nfsd: writeselect returned %d\n", ret);
	    return 1; 
	  }
      }

#endif
    }
  if(debug > 1)
    fprintf(stderr, "\t\twrite(%d, %#x, %d) = %d\n", psionfd, (unsigned int)p, len, ret);

#if !defined(linux)
  /*
   * tcdrain is broken on my linux version, strace reports 
   * ioctl(3, TCSBRK, 0x1) = 0
   */
#endif
  if(tcdrain(psionfd))
    perror("p3nfsd: tcdrain");
  return 0;
}

#define TOUT 5

static int
getbyte()
{
  static unsigned char readbuf[256];
  static int idx = 0, len = 0;
  int err;

  if(idx == len)
    {
      for(;;)
        {
#ifdef __SVR4
          struct pollfd readfd;

	  errno = 0;
	  readfd.fd = psionfd;
	  readfd.events = POLLIN;
	  err = poll(&readfd, 1, TOUT * 1000);
#else
          fd_set readfd;
          struct timeval to;

	  errno = 0;
	  to.tv_sec = TOUT; to.tv_usec = 0; 
	  FD_ZERO(&readfd); FD_SET(psionfd, &readfd);
	  err = select(psionfd+1, &readfd, 0, 0, &to);
#endif
	  if (err <= 0)
	    {
	      if(errno == EINTR)
	        continue;
	      fprintf(stderr, "p3nfsd: select: %s\n",
	              err < 0 ? sys_errlist[errno] : "timeout" );
	      return EOF;
	    }
	  break;
	}

      idx = 0;
      if(debug > 1) { printf("\t\tFilling read buffer..."); fflush(stdout); }
      len = read(psionfd, readbuf, sizeof(readbuf));
      if(debug > 1) printf("got %d bytes\n", len);
      if(debug > 2) 
        {
	  int i;

	  for(i = 0; i < len; i++)
	    {
	      printf("%02x ", readbuf[i]);
	      if((i & 15) == 15)
	        printf("\n");
	    }
	  printf("\n");
	}
      if(len <= 0)
        {
	  perror("p3nfsd: read"); len = 0;
	  return EOF;
	}
    }
  return readbuf[idx++];
}

int
sendcmd(cmd, fname, rest, restlen)
  char *fname, *rest;
  int cmd, restlen;
{
  int l;
  unsigned char mbuf[255], *str;

  if(old_nfsc)
    str = mbuf;
  else
    {
      str = mbuf+1;
      mbuf[0] = PREFIX;
    }

  str[1] = l = strlen(fname);
  if(l + restlen + 4 > 254)
    {
      printf("ERROR: filename too long (%d)\n", l);
      return 1;
    }

  bcopy(fname, str+2, l+1);	/* include trailing \0 */
  l += 3;
  if(restlen)
    {
      bcopy(rest, str+l, restlen);
      l += restlen;
    }
  str[l++] = cmd;
  *str = l-1;

  if(debug > 1)
    printf("\tSendcmd %d ('%s'+%d) %d bytes\n", cmd, fname, restlen, l);
  return senddata((char *)mbuf, l + (old_nfsc ? 0 : 1));
}

int
getcount(str, num)
  unsigned char *str; int num;
{
  int c;

  if(debug > 1) {printf("\tGoing to read %d bytes...",num);fflush(stdout);}
  while(num-- > 0)
    if((c = getbyte()) == EOF)
      {
	if(debug) printf("Bad connection to psion... (%d)\n", c);
	return -53;
      }
    else
      *str++ = c;
  if(debug > 1) printf("\tRead ok\n");
  return 0;
}

int
getstr(str)
  char *str;
{
  int c;
  unsigned char *s = (unsigned char *)str;

  if(debug > 1) {printf("\tGoing to read string...");fflush(stdout);}
  for(;;)
    {
      if((c = getbyte()) == EOF)
        {
	  if(debug) printf("Bad connection to psion... received EOF\n");
	  return 1;
	}
      *s++ = c;
      if(c == 0)
        {
	  if(debug > 1) printf(" got string (%s)\n", str);
	  return 0;
	}
    }
}

/* Old protocol: 1 byte, new Protocol 2 bytes */
int
getanswer()
{
  int i;

  i = getbyte();
  if(debug > 1) printf("Answer:(1) %#x\n", i);
  if(old_nfsc)
    return i;

  while(i != EOF)
    {
      if(i == PREFIX)
        {
	  i = getbyte();
	  if(debug > 1) printf("Answer:(1) %#x\n", i);
	  break;
	}
      shell_feed(i); /* this byte *has to be* input for our shell */
      i = getbyte();
      if(debug > 1) printf("Answer:(3) %#x\n", i);
    }
  return i;
}

int
sendop(cmd, fname, rest, restlen)
  char *fname, *rest;
  int cmd, restlen;
{
  int i = EOF;

  if((i = sendcmd(cmd, fname, rest, restlen)) == 0)
    i = getanswer();
  if(debug > 1) printf("\tReceived: (op %d) %d\n", cmd, i);
  return i;
}