Statistics
| Branch: | Revision:

grapes / som / TopologyManager / ncast_proto.c @ 8ab58ec7

History | View | Annotate | Download (2.22 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6

    
7
#include <stdint.h>
8
#include <stdlib.h>
9
#include <stdio.h>
10

    
11
#include "net_helper.h"
12
#include "nccache.h"
13
#include "proto.h"
14
#include "ncast_proto.h"
15
#include "msg_types.h"
16

    
17
static struct cache_entry *myEntry;
18

    
19
int ncast_payload_fill(uint8_t *payload, int size, struct cache_entry *c, struct nodeID *snot)
20
{
21
  int i;
22
  uint8_t *p = payload;
23

    
24
  p += entry_dump(p, myEntry, 0);
25
  for (i = 0; nodeid(c, i); i++) {
26
    if (!nodeid_equal(nodeid(c, i), snot)) {
27
      if (p - payload > size - 32 /* FIXME */) {
28
        fprintf(stderr, "too many entries!\n");
29
        return -1;
30
      }
31
      p += entry_dump(p, c, i);
32
    }
33
  }
34

    
35
  return p - payload;
36
}
37

    
38
int ncast_reply(const uint8_t *payload, int psize, struct cache_entry *local_cache)
39
{
40
  uint8_t pkt[1500];
41
  struct ncast_header *h = (struct ncast_header *)pkt;
42
  struct cache_entry *c = entries_undump(payload, psize);
43
  int len, res;
44
  struct nodeID *dst;
45

    
46
#if 0
47
  n = psize / sizeof(struct cache_entry);
48
  if (n * sizeof(struct cache_entry) != psize) {
49
    fprintf(stderr, "Wrong number of elems %d (%d / %d)!!!\n", n, psize, sizeof(struct cache_entry));
50
    return -1;
51
  }
52
#endif
53
  dst = nodeid(c, 0);
54
  h->protocol = MSG_TYPE_TOPOLOGY;
55
  h->type = NCAST_REPLY;
56
  len = ncast_payload_fill(pkt + sizeof(struct ncast_header), 1500 - sizeof(struct ncast_header), local_cache, dst);
57

    
58
  res = send_to_peer(nodeid(myEntry, 0), dst, pkt, sizeof(struct ncast_header) + len);
59
  cache_free(c);
60
  return res;
61
}
62

    
63
int ncast_query_peer(struct cache_entry *local_cache, struct nodeID *dst)
64
{
65
  uint8_t pkt[1500];
66
  struct ncast_header *h = (struct ncast_header *)pkt;
67
  int len;
68

    
69
  h->protocol = MSG_TYPE_TOPOLOGY;
70
  h->type = NCAST_QUERY;
71
  len = ncast_payload_fill(pkt + sizeof(struct ncast_header), 1500 - sizeof(struct ncast_header), local_cache, dst);
72
  return send_to_peer(nodeid(myEntry, 0), dst, pkt, sizeof(struct ncast_header) + len);
73
}
74

    
75
int ncast_query(struct cache_entry *local_cache)
76
{
77
  struct nodeID *dst;
78

    
79
  dst = rand_peer(local_cache);
80
  if (dst == NULL) {
81
    return 0;
82
  }
83
  return ncast_query_peer(local_cache, dst);
84
}
85

    
86
int ncast_proto_init(struct nodeID *s)
87
{
88
  myEntry = cache_init(2);
89
  cache_add(myEntry, s);
90

    
91
  return 0;
92
}