Statistics
| Branch: | Revision:

grapes / som / net_helper.c @ c642734a

History | View | Annotate | Download (3.11 KB)

1 480921a6 Luca Abeni
#include <sys/types.h>
2
#include <sys/socket.h>
3
#include <netinet/in.h>
4
#include <arpa/inet.h>
5
#include <unistd.h>
6
#include <stdlib.h>
7
#include <stdio.h>
8
#include <string.h>
9
10 155319cd Luca Abeni
#include "net_helper.h"
11 480921a6 Luca Abeni
12 651ed37d Luca
struct nodeID {
13 480921a6 Luca Abeni
  struct sockaddr_in addr;
14 651ed37d Luca
  int fd;
15 480921a6 Luca Abeni
};
16
17 b576198c Luca Abeni
int wait4data(const struct nodeID *s, struct timeval tout)
18
{
19
  fd_set fds;
20
  int res;
21
22
  FD_ZERO(&fds);
23
  FD_SET(s->fd, &fds);
24
  res = select(s->fd + 1, &fds, NULL, NULL, &tout);
25
  if (FD_ISSET(s->fd, &fds)) {
26
    return 1;
27
  }
28
29
  return 0;
30
}
31
32
struct nodeID *create_node(const char *IPaddr, int port)
33 480921a6 Luca Abeni
{
34 651ed37d Luca
  struct nodeID *s;
35 480921a6 Luca Abeni
  int res;
36
37 651ed37d Luca
  s = malloc(sizeof(struct nodeID));
38
  memset(s, 0, sizeof(struct nodeID));
39 480921a6 Luca Abeni
  s->addr.sin_family = AF_INET;
40
  s->addr.sin_port = htons(port);
41
  res = inet_aton(IPaddr, &s->addr.sin_addr);
42
  if (res == 0) {
43
    free(s);
44
45 3bcb55e5 Luca
    return NULL;
46 480921a6 Luca Abeni
  }
47
48 651ed37d Luca
  s->fd = socket(AF_INET, SOCK_DGRAM, 0);
49
  if (s->fd < 0) {
50
    free(s);
51 480921a6 Luca Abeni
    
52 651ed37d Luca
    return NULL;
53
  }
54 b576198c Luca Abeni
55
  return s;
56
}
57
58
struct nodeID *net_helper_init(const char *my_addr, int port)
59
{
60
  int res;
61
  struct nodeID *myself;
62
63
  myself = create_node(my_addr, port);
64
  if (myself == NULL) {
65
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
66
  }
67
  fprintf(stderr, "My sock: %d\n", myself->fd);
68
69
  res = bind(myself->fd, (struct sockaddr *)&myself->addr, sizeof(struct sockaddr_in));
70 651ed37d Luca
  if (res < 0) {
71
    /* bind failed: not a local address... Just close the socket! */
72 b576198c Luca Abeni
    close(myself->fd);
73
    free(myself);
74
75
    return NULL;
76 480921a6 Luca Abeni
  }
77
78 b576198c Luca Abeni
  return myself;
79 480921a6 Luca Abeni
}
80
81 b576198c Luca Abeni
int send_to_peer(const struct nodeID *from, const struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size)
82 651ed37d Luca
{
83
  return sendto(from->fd, buffer_ptr, buffer_size, 0,
84 e621cfdf Luca Abeni
                (const struct sockaddr *)&to->addr, sizeof(struct sockaddr_in));
85 480921a6 Luca Abeni
}
86
87 b576198c Luca Abeni
int recv_from_peer(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size)
88 480921a6 Luca Abeni
{
89
  int res;
90
  struct sockaddr_in raddr;
91
  socklen_t raddr_size = sizeof(struct sockaddr_in);
92
93 651ed37d Luca
  *remote = malloc(sizeof(struct nodeID));
94
  if (*remote == NULL) {
95
    return -1;
96
  }
97
  res = recvfrom(local->fd, buffer_ptr, buffer_size, 0, (struct sockaddr *)&raddr, &raddr_size);
98
  memcpy(&(*remote)->addr, &raddr, raddr_size);
99
  (*remote)->fd = -1;
100 4dbe13f0 Luca
  //fprintf(stderr, "Read %d from %s\n", res, inet_ntoa(raddr.sin_addr));
101
102 480921a6 Luca Abeni
  return res;
103
}
104
105 651ed37d Luca
const char *node_addr(const struct nodeID *s)
106 480921a6 Luca Abeni
{
107
  static char addr[256];
108
109
  sprintf(addr, "%s:%d", inet_ntoa(s->addr.sin_addr), ntohs(s->addr.sin_port));
110
111
  return addr;
112
}
113
114 651ed37d Luca
struct nodeID *nodeid_dup(const struct nodeID *s)
115 480921a6 Luca Abeni
{
116 651ed37d Luca
  struct nodeID *res;
117 480921a6 Luca Abeni
118 651ed37d Luca
  res = malloc(sizeof(struct nodeID));
119 480921a6 Luca Abeni
  if (res != NULL) {
120 651ed37d Luca
    memcpy(res, s, sizeof(struct nodeID));
121 480921a6 Luca Abeni
  }
122
123
  return res;
124
}
125 651ed37d Luca
int nodeid_equal(const struct nodeID *s1, const struct nodeID *s2)
126 480921a6 Luca Abeni
{
127 5891be7d Luca
  return (memcmp(&s1->addr, &s2->addr, sizeof(struct sockaddr_in)) == 0);
128 480921a6 Luca Abeni
}
129
130 651ed37d Luca
int nodeid_dump(uint8_t *b, const struct nodeID *s)
131 480921a6 Luca Abeni
{
132 651ed37d Luca
  memcpy(b, s, sizeof(struct nodeID));
133 480921a6 Luca Abeni
134 651ed37d Luca
  return sizeof(struct nodeID);
135 480921a6 Luca Abeni
}
136
137 651ed37d Luca
struct nodeID *nodeid_undump(const uint8_t *b, int *len)
138 480921a6 Luca Abeni
{
139 651ed37d Luca
  struct nodeID *res;
140
  res = malloc(sizeof(struct nodeID));
141 480921a6 Luca Abeni
  if (res != NULL) {
142 651ed37d Luca
    memcpy(res, b, sizeof(struct nodeID));
143 480921a6 Luca Abeni
  }
144 651ed37d Luca
  *len = sizeof(struct nodeID);
145 480921a6 Luca Abeni
146
  return res;
147
}