File: client.c

package info (click to toggle)
rpld 1.8beta1-8
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 840 kB
  • ctags: 627
  • sloc: ansic: 2,230; asm: 434; yacc: 225; makefile: 171; sh: 40
file content (426 lines) | stat: -rw-r--r-- 8,024 bytes parent folder | download | duplicates (3)
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*************************************************
*     rpld - an IBM style RIPL server            *
*************************************************/

/* Copyright (c) 1999,2000,2001 James McKenzie.
 *                      All rights reserved
 * Copyright (c) 1998,2000,2001 Christopher Lightfoot.
 *                      All rights reserved
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENCE file which can be found at the top level of
 * the rpld distribution.
 *
 * IBM is a trademark of IBM corp.
 *
 */


static char rcsid[] = "$Id: client.c,v 1.34 2001/11/01 15:30:29 root Exp $";

/*
 * $Log: client.c,v $
 * Revision 1.34  2001/11/01 15:30:29  root
 * #
 *
 * Revision 1.33  2001/11/01 15:28:23  root
 * #
 *
 * Revision 1.32  2001/11/01 15:26:45  root
 * #
 *
 * Revision 1.31  2001/11/01 15:26:29  root
 * #
 *
 * Revision 1.30  2001/11/01 15:23:59  root
 * #
 *
 * Revision 1.29  2000/09/26 04:06:07  root
 * #
 *
 * Revision 1.28  2000/09/26 03:48:23  root
 * #
 *
 * Revision 1.27  2000/09/26 02:32:46  root
 * #
 *
 * Revision 1.26  2000/09/26 02:31:38  root
 * #
 *
 * Revision 1.25  2000/09/26 01:41:22  root
 * #
 *
 * Revision 1.24  2000/09/26 01:41:20  root
 * #
 *
 * Revision 1.23  2000/09/26 01:39:17  root
 * #
 *
 * Revision 1.22  2000/09/26 01:03:19  root
 * #
 *
 * Revision 1.21  2000/07/29 23:25:58  root
 * #
 *
 * Revision 1.20  2000/07/29 23:25:25  root
 * #
 *
 * Revision 1.19  2000/07/23 19:14:19  root
 * #
 *
 * Revision 1.18  2000/07/23 19:07:49  root
 * #
 *
 * Revision 1.17  2000/07/17 10:49:20  root
 * #
 *
 * Revision 1.16  2000/07/17 10:45:38  root
 * #
 *
 * Revision 1.15  2000/07/17 10:43:54  root
 * #
 *
 * Revision 1.14  2000/07/17 10:43:34  root
 * #
 *
 * Revision 1.13  2000/07/16 14:22:06  root
 * #
 *
 * Revision 1.12  2000/07/16 14:05:28  root
 * #
 *
 * Revision 1.11  2000/07/16 13:18:10  root
 * #
 *
 * Revision 1.1  2000/07/16 13:16:33  root
 * #
 *
 * Revision 1.10  1999/09/14 17:12:38  root
 * #
 *
 * Revision 1.9  1999/09/14 17:12:05  root
 * #
 *
 * Revision 1.8  1999/09/13 11:17:35  root
 * \#
 *
 * Revision 1.7  1999/09/13 11:05:27  root
 * \#
 *
 * Revision 1.6  1999/09/13 11:04:13  root
 * \#
 *
 */

#include "project.h"

struct client *clients = NULL;

void
cache_locally (struct clfile *f)
{
  int fd;
  int len;

  fd = open (f->path, O_RDONLY);

  if (fd < 0)
    {
      syslog (LOG_ERR, "can't open %s:%m", f->path);
      return;
    }

  len = f->length;

  if (len < 0)
    {
      len = lseek (fd, 0, SEEK_END);
      lseek (fd, 0, SEEK_SET);
    }

  f->data = malloc (len);
  f->length = len;

  lseek (fd, f->offset, SEEK_SET);
  read (fd, f->data, len);

  f->offset = 0;

  close (fd);

  free (f->path);
  f->path = NULL;
}

/* Cache the last client */
struct client *
find_client_by_mac (unsigned char *mac)
{
  struct client *pc = NULL, *c = clients;

/* We need to match clients by a (possibly partial) MAC address; however
 * this struct client is also used to transmit to them, so we need to return
 * a struct client containing a _real_ MAC address; this is a first and
 * pretty ugly implementation of this idea.
 */
  while (c)
    {
      if (memcmp (mac, c->mac, c->partial_mac_len) == 0)
        {
          /* if partial match, create new struct client */
          if (c->partial_mac_len != ETH_ALEN)
            {
              pc = (struct client *) malloc (sizeof (struct client));
              memcpy (pc, c, sizeof (struct client));

              pc->next = c->next;
              c->next = clients;
              clients = c;

              return pc;
            }

          /* else reorder the tree for speed, and return the found client */
          if (pc)
            {                   /*Short circuit for next time */
              pc->next = c->next;
              c->next = clients;
              clients = c;
            }
          return (c);
        }

      pc = c;
      c = c->next;
    }

  return (NULL);
}

void
client_calc_offsets (struct client *c)
{
  struct clfile *f;
  int bn = 0;
  int len;
  int fd;
  int blocks;

  f = c->files;

  while (f)
    {

      f->sblock = bn;

      if (f->path)
        {
          fd = open (f->path, O_RDONLY);
          if (fd < 0)
            {
              syslog (LOG_ERR, "can't open %s:%m", f->path);
              return;
            }
          len = f->length;
          if (len < 0)
            {
              len = lseek (fd, 0, SEEK_END);
              len -= f->offset;
            }
          close (fd);
        }
      else
        {
          len = f->length;
        }

#ifdef DEBUG
      printf ("%s is %d bytes long gives", f->path, len);
#endif
      blocks = len / (c->blocklen);
      len = len - (blocks * c->blocklen);

      if (len == 0)
        blocks--;

      bn += blocks;
      f->eblock = bn;

#ifdef DEBUG
      printf (" %d blocks [%d,%d]\n", blocks, f->sblock, f->eblock);
#endif

      bn++;

      f = f->next;

    }

}


#define NOTINRANGE(l,v,h) (((v)<(l)) || ((v)>(h)))
void
client_get_block (struct client *c, struct rpl_packet *p)
{
  struct clfile *f;
  int toread;
  int offset;

  f = c->file;

  p->datalen = 0;               /* Some buggy clients request
                                   * blocks after the end when
                                   * they get confused */

  if ((!f) || (NOTINRANGE (f->sblock, c->blocknum, f->eblock)))
    {
      if ((f) && (f->f))
        {
          fclose (f->f);
          f->f = NULL;
        }
      f = c->files;
      while (f && (f->eblock < c->blocknum))
        f = f->next;

      if (!f)
        {
          syslog (LOG_ERR, "this shouldn't happen oops!");
          return;
        }

      if (f->path)
        {
          f->f = fopen (f->path, "r");
          if (!f->f)
            {
              c->file = NULL;

              syslog (LOG_ERR, "failed to open %s ", f->path);
              return;
            }

        }

      c->file = f;
    }


  offset = (c->blocklen) * (c->blocknum - f->sblock);

  if (f->length > 0)
    {
      toread = f->length - offset;
      if (toread > c->blocklen)
        toread = c->blocklen;
    }
  else
    {
      toread = c->blocklen;
    }

  if (f->path)
    {                           /* A real file */
      fseek (f->f, offset + f->offset, SEEK_SET);
      p->datalen = fread (p->data, 1, toread, f->f);
      if ((p->datalen != toread) && (c->blocknum != f->eblock))
        {
          syslog (LOG_ERR, "short read on %s", f->path);
        }
    }
  else
    {                           /*cached */
      bcopy (f->data + offset, p->data, toread);
      p->datalen = toread;
    }

  p->addr.load = f->load_addr + offset;

}

int
client_last_block (struct client *c)
{
  struct clfile *f;
  f = c->file;

  if ((!f) || (NOTINRANGE (f->sblock, c->blocknum, f->eblock)))
    {
      if ((f) && (f->f))
        {
          fclose (f->f);
          f->f = NULL;
        }
      c->file = NULL;
      f = c->files;
      while (f && (f->eblock < c->blocknum))
        f = f->next;

      if (!f)
        {
          syslog (LOG_ERR, "this shouldn't happen oops!");
          return 1;             /*Infact past the last block */
        }
    }

  if (!(f->next) && (f->eblock == c->blocknum))
    {
      return 1;
    }


  return 0;
}

void
client_flush_cache (struct client *c)
{

  struct clfile *f;
  f = c->file;

  if ((f) && (f->f))
    {
      fclose (f->f);
      f->f = NULL;
      c->file = NULL;
    }

}

clients_check_status ()
{
  struct client *c = clients;

  downloading = 0;
  pacing = 0;

  while (c)
    {
      if ((c->state == ST_FILEDATA) && (!c->nospew))
        {
          downloading++;
          if (pacing < c->pacing)
            pacing = c->pacing;
        }
      c = c->next;
    }

}

client_dispatch (struct llcdrv *lld)
{
  struct client *c = clients;

  while (c)
    {
      if ((c->state == ST_FILEDATA) && (!c->nospew))
        {
          file_data_frame (lld, c);
        }
      c = c->next;

    }

}