Statistics
| Branch: | Revision:

grapes / src / net_helper.c @ 534e126a

History | View | Annotate | Download (5.08 KB)

1
/*
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
#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
#include "net_helper.h"
18

    
19
struct nodeID {
20
  struct sockaddr_in addr;
21
  int fd;
22
};
23

    
24
int wait4data(const struct nodeID *s, struct timeval *tout, int *user_fds)
25
{
26
  fd_set fds;
27
  int i, res, max_fd;
28

    
29
  FD_ZERO(&fds);
30
  if (s) {
31
    max_fd = s->fd;
32
    FD_SET(s->fd, &fds);
33
  } else {
34
    max_fd = -1;
35
  }
36
  if (user_fds) {
37
    for (i = 0; user_fds[i] != -1; i++) {
38
      FD_SET(user_fds[i], &fds);
39
      if (user_fds[i] > max_fd) {
40
        max_fd = user_fds[i];
41
      }
42
    }
43
  }
44
  res = select(max_fd + 1, &fds, NULL, NULL, tout);
45
  if (res <= 0) {
46
    return res;
47
  }
48
  if (s && FD_ISSET(s->fd, &fds)) {
49
    return 1;
50
  }
51

    
52
  /* If execution arrives here, user_fds cannot be 0
53
     (an FD is ready, and it's not s->fd) */
54
  for (i = 0; user_fds[i] != -1; i++) {
55
    if (!FD_ISSET(user_fds[i], &fds)) {
56
      user_fds[i] = -2;
57
    }
58
  }
59

    
60
  return 2;
61
}
62

    
63
struct nodeID *create_node(const char *IPaddr, int port)
64
{
65
  struct nodeID *s;
66
  int res;
67

    
68
  s = malloc(sizeof(struct nodeID));
69
  memset(s, 0, sizeof(struct nodeID));
70
  s->addr.sin_family = AF_INET;
71
  s->addr.sin_port = htons(port);
72
  res = inet_aton(IPaddr, &s->addr.sin_addr);
73
  if (res == 0) {
74
    free(s);
75

    
76
    return NULL;
77
  }
78

    
79
  s->fd = -1;
80

    
81
  return s;
82
}
83

    
84
struct nodeID *net_helper_init(const char *my_addr, int port, const char *config)
85
{
86
  int res;
87
  struct nodeID *myself;
88

    
89
  myself = create_node(my_addr, port);
90
  if (myself == NULL) {
91
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
92
  }
93
  myself->fd =  socket(AF_INET, SOCK_DGRAM, 0);
94
  if (myself->fd < 0) {
95
    free(myself);
96
    
97
    return NULL;
98
  }
99
  fprintf(stderr, "My sock: %d\n", myself->fd);
100

    
101
  res = bind(myself->fd, (struct sockaddr *)&myself->addr, sizeof(struct sockaddr_in));
102
  if (res < 0) {
103
    /* bind failed: not a local address... Just close the socket! */
104
    close(myself->fd);
105
    free(myself);
106

    
107
    return NULL;
108
  }
109

    
110
  return myself;
111
}
112

    
113
void bind_msg_type (uint8_t msgtype)
114
{
115
}
116

    
117
int send_to_peer(const struct nodeID *from, struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size)
118
{
119
  struct msghdr msg = {0};
120
  uint8_t my_hdr;
121
  struct iovec iov[2];
122
  int res;
123

    
124
  iov[0].iov_base = &my_hdr;
125
  iov[0].iov_len = 1;
126
  msg.msg_name = &to->addr;
127
  msg.msg_namelen = sizeof(struct sockaddr_in);
128
  msg.msg_iovlen = 2;
129
  msg.msg_iov = iov;
130
  
131
  do {
132
    iov[1].iov_base = buffer_ptr;
133
    if (buffer_size > 1024 * 60) {
134
      iov[1].iov_len = 1024 * 60;
135
      my_hdr = 0;
136
    } else {
137
      iov[1].iov_len = buffer_size;
138
      my_hdr = 1;
139
    }
140
    buffer_size -= iov[1].iov_len;
141
    buffer_ptr += iov[1].iov_len;
142
    res = sendmsg(from->fd, &msg, 0);
143
  } while (buffer_size > 0);
144

    
145
  return res;
146
}
147

    
148
int recv_from_peer(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size)
149
{
150
  int res, recv;
151
  struct sockaddr_in raddr;
152
  struct msghdr msg = {0};
153
  uint8_t my_hdr;
154
  struct iovec iov[2];
155

    
156
  iov[0].iov_base = &my_hdr;
157
  iov[0].iov_len = 1;
158
  msg.msg_name = &raddr;
159
  msg.msg_namelen = sizeof(struct sockaddr_in);
160
  msg.msg_iovlen = 2;
161
  msg.msg_iov = iov;
162

    
163
  *remote = malloc(sizeof(struct nodeID));
164
  if (*remote == NULL) {
165
    return -1;
166
  }
167

    
168
  recv = 0;
169
  do {
170
    iov[1].iov_base = buffer_ptr;
171
    if (buffer_size > 1024 * 60) {
172
      iov[1].iov_len = 1024 * 60;
173
    } else {
174
      iov[1].iov_len = buffer_size;
175
    }
176
    buffer_size -= iov[1].iov_len;
177
    buffer_ptr += iov[1].iov_len;
178
    res = recvmsg(local->fd, &msg, 0);
179
    recv += (res - 1);
180
  } while ((my_hdr == 0) && (buffer_size > 0));
181
  memcpy(&(*remote)->addr, &raddr, msg.msg_namelen);
182
  (*remote)->fd = -1;
183

    
184
  return recv;
185
}
186

    
187
int node_addr(const struct nodeID *s, char *addr, int len)
188
{
189
  int n;
190

    
191
  if (!inet_ntop(AF_INET, &(s->addr.sin_addr), addr, len)) {
192
    return -1;
193
  }
194
  n = snprintf(addr + strlen(addr), len - strlen(addr) - 1, ":%d", ntohs(s->addr.sin_port));
195

    
196
  return n;
197
}
198

    
199
struct nodeID *nodeid_dup(struct nodeID *s)
200
{
201
  struct nodeID *res;
202

    
203
  res = malloc(sizeof(struct nodeID));
204
  if (res != NULL) {
205
    memcpy(res, s, sizeof(struct nodeID));
206
  }
207

    
208
  return res;
209
}
210

    
211
int nodeid_equal(const struct nodeID *s1, const struct nodeID *s2)
212
{
213
  return (memcmp(&s1->addr, &s2->addr, sizeof(struct sockaddr_in)) == 0);
214
}
215

    
216
int nodeid_dump(uint8_t *b, const struct nodeID *s, size_t max_write_size)
217
{
218
  if (max_write_size < sizeof(struct sockaddr_in)) return -1;
219

    
220
  memcpy(b, &s->addr, sizeof(struct sockaddr_in));
221

    
222
  return sizeof(struct sockaddr_in);
223
}
224

    
225
struct nodeID *nodeid_undump(const uint8_t *b, int *len)
226
{
227
  struct nodeID *res;
228
  res = malloc(sizeof(struct nodeID));
229
  if (res != NULL) {
230
    memcpy(&res->addr, b, sizeof(struct sockaddr_in));
231
    res->fd = -1;
232
  }
233
  *len = sizeof(struct sockaddr_in);
234

    
235
  return res;
236
}
237

    
238
void nodeid_free(struct nodeID *s)
239
{
240
  free(s);
241
}
242

    
243
int node_ip(const struct nodeID *s, char *ip, int len)
244
{
245
  if (inet_ntop(AF_INET, &(s->addr.sin_addr), ip, len) == 0) {
246
    return -1;
247
  }
248

    
249
  return 1;
250
}