Statistics
| Branch: | Revision:

grapes / src / net_helper.c @ ef8002fd

History | View | Annotate | Download (4.94 KB)

1 8ab58ec7 Luca Abeni
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *  Copyright (c) 2010 Csaba Kiraly
4
 *
5
 *  This is free software; see lgpl-2.1.txt
6
 */
7
8 480921a6 Luca Abeni
#include <sys/types.h>
9
#include <sys/socket.h>
10
#include <netinet/in.h>
11
#include <arpa/inet.h>
12
#include <unistd.h>
13
#include <stdlib.h>
14
#include <stdio.h>
15
#include <string.h>
16
17 155319cd Luca Abeni
#include "net_helper.h"
18 480921a6 Luca Abeni
19 651ed37d Luca
struct nodeID {
20 480921a6 Luca Abeni
  struct sockaddr_in addr;
21 651ed37d Luca
  int fd;
22 480921a6 Luca Abeni
};
23
24 0cb8677a Luca Abeni
int wait4data(const struct nodeID *s, struct timeval *tout, int *user_fds)
25 b576198c Luca Abeni
{
26
  fd_set fds;
27 0cb8677a Luca Abeni
  int i, res, max_fd;
28 b576198c Luca Abeni
29
  FD_ZERO(&fds);
30 0cb8677a Luca Abeni
  max_fd = s->fd;
31
  if (user_fds) {
32
    for (i = 0; user_fds[i] != -1; i++) {
33
      FD_SET(user_fds[i], &fds);
34
      if (user_fds[i] > max_fd) {
35
        max_fd = user_fds[i];
36
      }
37
    }
38 92bdbfcd Luca Abeni
  }
39 0cb8677a Luca Abeni
  FD_SET(s->fd, &fds);
40
  res = select(max_fd + 1, &fds, NULL, NULL, tout);
41 92bdbfcd Luca Abeni
  if (res <= 0) {
42
    return res;
43
  }
44 0cb8677a Luca Abeni
  if (FD_ISSET(s->fd, &fds)) {
45 b576198c Luca Abeni
    return 1;
46
  }
47
48 0cb8677a Luca Abeni
  /* If execution arrives here, user_fds cannot be 0
49
     (an FD is ready, and it's not s->fd) */
50
  for (i = 0; user_fds[i] != -1; i++) {
51
    if (!FD_ISSET(user_fds[i], &fds)) {
52
      user_fds[i] = -2;
53
    }
54
  }
55
56 92bdbfcd Luca Abeni
  return 2;
57 b576198c Luca Abeni
}
58
59
struct nodeID *create_node(const char *IPaddr, int port)
60 480921a6 Luca Abeni
{
61 651ed37d Luca
  struct nodeID *s;
62 480921a6 Luca Abeni
  int res;
63
64 651ed37d Luca
  s = malloc(sizeof(struct nodeID));
65
  memset(s, 0, sizeof(struct nodeID));
66 480921a6 Luca Abeni
  s->addr.sin_family = AF_INET;
67
  s->addr.sin_port = htons(port);
68
  res = inet_aton(IPaddr, &s->addr.sin_addr);
69
  if (res == 0) {
70
    free(s);
71
72 3bcb55e5 Luca
    return NULL;
73 480921a6 Luca Abeni
  }
74
75 b7790a94 Luca Abeni
  s->fd = -1;
76 b576198c Luca Abeni
77
  return s;
78
}
79
80 c919f1bf Csaba Kiraly
struct nodeID *net_helper_init(const char *my_addr, int port, const char *config)
81 b576198c Luca Abeni
{
82
  int res;
83
  struct nodeID *myself;
84
85
  myself = create_node(my_addr, port);
86
  if (myself == NULL) {
87
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
88
  }
89 b7790a94 Luca Abeni
  myself->fd =  socket(AF_INET, SOCK_DGRAM, 0);
90
  if (myself->fd < 0) {
91
    free(myself);
92
    
93
    return NULL;
94
  }
95 b576198c Luca Abeni
  fprintf(stderr, "My sock: %d\n", myself->fd);
96
97
  res = bind(myself->fd, (struct sockaddr *)&myself->addr, sizeof(struct sockaddr_in));
98 651ed37d Luca
  if (res < 0) {
99
    /* bind failed: not a local address... Just close the socket! */
100 b576198c Luca Abeni
    close(myself->fd);
101
    free(myself);
102
103
    return NULL;
104 480921a6 Luca Abeni
  }
105
106 b576198c Luca Abeni
  return myself;
107 480921a6 Luca Abeni
}
108
109 458b1d7e Luca Abeni
void bind_msg_type (uint8_t msgtype)
110
{
111
}
112
113 5ada309a Luca Abeni
int send_to_peer(const struct nodeID *from, struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size)
114 651ed37d Luca
{
115 41f12636 Luca Abeni
  static struct msghdr msg;
116
  static uint8_t my_hdr;
117
  struct iovec iov[2];
118
  int res;
119
120
  iov[0].iov_base = &my_hdr;
121
  iov[0].iov_len = 1;
122
  msg.msg_name = &to->addr;
123
  msg.msg_namelen = sizeof(struct sockaddr_in);
124
  msg.msg_iovlen = 2;
125
  msg.msg_iov = iov;
126
  
127
  do {
128
    iov[1].iov_base = buffer_ptr;
129
    if (buffer_size > 1024 * 60) {
130
      iov[1].iov_len = 1024 * 60;
131
      my_hdr = 0;
132
    } else {
133
      iov[1].iov_len = buffer_size;
134
      my_hdr = 1;
135
    }
136
    buffer_size -= iov[1].iov_len;
137
    buffer_ptr += iov[1].iov_len;
138
    res = sendmsg(from->fd, &msg, 0);
139
  } while (buffer_size > 0);
140
141
  return res;
142 480921a6 Luca Abeni
}
143
144 b576198c Luca Abeni
int recv_from_peer(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size)
145 480921a6 Luca Abeni
{
146 41f12636 Luca Abeni
  int res, recv;
147 480921a6 Luca Abeni
  struct sockaddr_in raddr;
148 41f12636 Luca Abeni
  static struct msghdr msg;
149
  static uint8_t my_hdr;
150
  struct iovec iov[2];
151
152
  iov[0].iov_base = &my_hdr;
153
  iov[0].iov_len = 1;
154
  msg.msg_name = &raddr;
155
  msg.msg_namelen = sizeof(struct sockaddr_in);
156
  msg.msg_iovlen = 2;
157
  msg.msg_iov = iov;
158 480921a6 Luca Abeni
159 651ed37d Luca
  *remote = malloc(sizeof(struct nodeID));
160
  if (*remote == NULL) {
161
    return -1;
162
  }
163 41f12636 Luca Abeni
164
  recv = 0;
165
  do {
166
    iov[1].iov_base = buffer_ptr;
167
    if (buffer_size > 1024 * 60) {
168
      iov[1].iov_len = 1024 * 60;
169
    } else {
170
      iov[1].iov_len = buffer_size;
171
    }
172
    buffer_size -= iov[1].iov_len;
173
    buffer_ptr += iov[1].iov_len;
174
    res = recvmsg(local->fd, &msg, 0);
175
    recv += (res - 1);
176
  } while ((my_hdr == 0) && (buffer_size > 0));
177
  memcpy(&(*remote)->addr, &raddr, msg.msg_namelen);
178 651ed37d Luca
  (*remote)->fd = -1;
179 4dbe13f0 Luca
180 41f12636 Luca Abeni
  return recv;
181 480921a6 Luca Abeni
}
182
183 651ed37d Luca
const char *node_addr(const struct nodeID *s)
184 480921a6 Luca Abeni
{
185
  static char addr[256];
186
187
  sprintf(addr, "%s:%d", inet_ntoa(s->addr.sin_addr), ntohs(s->addr.sin_port));
188
189
  return addr;
190
}
191
192 5ada309a Luca Abeni
struct nodeID *nodeid_dup(struct nodeID *s)
193 480921a6 Luca Abeni
{
194 651ed37d Luca
  struct nodeID *res;
195 480921a6 Luca Abeni
196 651ed37d Luca
  res = malloc(sizeof(struct nodeID));
197 480921a6 Luca Abeni
  if (res != NULL) {
198 651ed37d Luca
    memcpy(res, s, sizeof(struct nodeID));
199 480921a6 Luca Abeni
  }
200
201
  return res;
202
}
203 2edb3e55 Luca Abeni
204 651ed37d Luca
int nodeid_equal(const struct nodeID *s1, const struct nodeID *s2)
205 480921a6 Luca Abeni
{
206 5891be7d Luca
  return (memcmp(&s1->addr, &s2->addr, sizeof(struct sockaddr_in)) == 0);
207 480921a6 Luca Abeni
}
208
209 20903c9c Csaba Kiraly
int nodeid_dump(uint8_t *b, const struct nodeID *s, size_t max_write_size)
210 480921a6 Luca Abeni
{
211 20903c9c Csaba Kiraly
  if (max_write_size < sizeof(struct sockaddr_in)) return -1;
212
213 4abf6307 Csaba Kiraly
  memcpy(b, &s->addr, sizeof(struct sockaddr_in));
214 480921a6 Luca Abeni
215 4abf6307 Csaba Kiraly
  return sizeof(struct sockaddr_in);
216 480921a6 Luca Abeni
}
217
218 651ed37d Luca
struct nodeID *nodeid_undump(const uint8_t *b, int *len)
219 480921a6 Luca Abeni
{
220 651ed37d Luca
  struct nodeID *res;
221
  res = malloc(sizeof(struct nodeID));
222 480921a6 Luca Abeni
  if (res != NULL) {
223 4abf6307 Csaba Kiraly
    memcpy(&res->addr, b, sizeof(struct sockaddr_in));
224 31806e09 Luca Abeni
    res->fd = -1;
225 480921a6 Luca Abeni
  }
226 4abf6307 Csaba Kiraly
  *len = sizeof(struct sockaddr_in);
227 480921a6 Luca Abeni
228
  return res;
229
}
230 678c612d Luca Abeni
231
void nodeid_free(struct nodeID *s)
232
{
233
  free(s);
234
}
235
236 e55fe1f8 Marco Biazzini
const char *node_ip(const struct nodeID *s)
237
{
238 2edb3e55 Luca Abeni
  static char ip[64];
239 e55fe1f8 Marco Biazzini
240 2edb3e55 Luca Abeni
  sprintf(ip, "%s", inet_ntoa(s->addr.sin_addr));
241 e55fe1f8 Marco Biazzini
242 2edb3e55 Luca Abeni
  return ip;
243 e55fe1f8 Marco Biazzini
}