Statistics
| Branch: | Revision:

grapes / src / net_helper.c @ 5033613a

History | View | Annotate | Download (4.94 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
  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
  }
39
  FD_SET(s->fd, &fds);
40
  res = select(max_fd + 1, &fds, NULL, NULL, tout);
41
  if (res <= 0) {
42
    return res;
43
  }
44
  if (FD_ISSET(s->fd, &fds)) {
45
    return 1;
46
  }
47

    
48
  /* 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
  return 2;
57
}
58

    
59
struct nodeID *create_node(const char *IPaddr, int port)
60
{
61
  struct nodeID *s;
62
  int res;
63

    
64
  s = malloc(sizeof(struct nodeID));
65
  memset(s, 0, sizeof(struct nodeID));
66
  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
    return NULL;
73
  }
74

    
75
  s->fd = -1;
76

    
77
  return s;
78
}
79

    
80
struct nodeID *net_helper_init(const char *my_addr, int port, const char *config)
81
{
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
  myself->fd =  socket(AF_INET, SOCK_DGRAM, 0);
90
  if (myself->fd < 0) {
91
    free(myself);
92
    
93
    return NULL;
94
  }
95
  fprintf(stderr, "My sock: %d\n", myself->fd);
96

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

    
103
    return NULL;
104
  }
105

    
106
  return myself;
107
}
108

    
109
void bind_msg_type (uint8_t msgtype)
110
{
111
}
112

    
113
int send_to_peer(const struct nodeID *from, struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size)
114
{
115
  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
}
143

    
144
int recv_from_peer(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size)
145
{
146
  int res, recv;
147
  struct sockaddr_in raddr;
148
  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

    
159
  *remote = malloc(sizeof(struct nodeID));
160
  if (*remote == NULL) {
161
    return -1;
162
  }
163

    
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
  (*remote)->fd = -1;
179

    
180
  return recv;
181
}
182

    
183
const char *node_addr(const struct nodeID *s)
184
{
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
struct nodeID *nodeid_dup(struct nodeID *s)
193
{
194
  struct nodeID *res;
195

    
196
  res = malloc(sizeof(struct nodeID));
197
  if (res != NULL) {
198
    memcpy(res, s, sizeof(struct nodeID));
199
  }
200

    
201
  return res;
202
}
203

    
204
int nodeid_equal(const struct nodeID *s1, const struct nodeID *s2)
205
{
206
  return (memcmp(&s1->addr, &s2->addr, sizeof(struct sockaddr_in)) == 0);
207
}
208

    
209
int nodeid_dump(uint8_t *b, const struct nodeID *s, size_t max_write_size)
210
{
211
  if (max_write_size < sizeof(struct sockaddr_in)) return -1;
212

    
213
  memcpy(b, &s->addr, sizeof(struct sockaddr_in));
214

    
215
  return sizeof(struct sockaddr_in);
216
}
217

    
218
struct nodeID *nodeid_undump(const uint8_t *b, int *len)
219
{
220
  struct nodeID *res;
221
  res = malloc(sizeof(struct nodeID));
222
  if (res != NULL) {
223
    memcpy(&res->addr, b, sizeof(struct sockaddr_in));
224
    res->fd = -1;
225
  }
226
  *len = sizeof(struct sockaddr_in);
227

    
228
  return res;
229
}
230

    
231
void nodeid_free(struct nodeID *s)
232
{
233
  free(s);
234
}
235

    
236
const char *node_ip(const struct nodeID *s)
237
{
238
  static char ip[64];
239

    
240
  sprintf(ip, "%s", inet_ntoa(s->addr.sin_addr));
241

    
242
  return ip;
243
}