Statistics
| Branch: | Revision:

grapes / src / net_helper-win32.c @ 5033613a

History | View | Annotate | Download (4.77 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 <winsock2.h>
9
#include <sys/types.h>
10
#include <unistd.h>
11
#include <stdlib.h>
12
#include <stdio.h>
13
#include <string.h>
14

    
15
#include "net_helper.h"
16

    
17
struct nodeID {
18
  struct sockaddr_in addr;
19
  int fd;
20
  uint8_t *buff;
21
};
22

    
23
static int inet_aton(const char *cp, struct in_addr *addr)
24
{
25
    if( cp==NULL || addr==NULL )
26
    {
27
        return(0);
28
    }
29

    
30
    addr->s_addr = inet_addr(cp);
31
    return (addr->s_addr == INADDR_NONE) ? 0 : 1;
32
}
33

    
34
int wait4data(const struct nodeID *s, struct timeval *tout, int *user_fds)
35
{
36
  fd_set fds;
37
  int i, res, max_fd;
38

    
39
  FD_ZERO(&fds);
40
  max_fd = s->fd;
41
  if (user_fds) {
42
    for (i = 0; user_fds[i] != -1; i++) {
43
      FD_SET(user_fds[i], &fds);
44
      if (user_fds[i] > max_fd) {
45
        max_fd = user_fds[i];
46
      }
47
    }
48
  }
49
  FD_SET(s->fd, &fds);
50
  res = select(max_fd + 1, &fds, NULL, NULL, tout);
51
  if (res <= 0) {
52
    return res;
53
  }
54
  if (FD_ISSET(s->fd, &fds)) {
55
    return 1;
56
  }
57

    
58
  /* If execution arrives here, user_fds cannot be 0
59
     (an FD is ready, and it's not s->fd) */
60
  for (i = 0; user_fds[i] != -1; i++) {
61
    if (!FD_ISSET(user_fds[i], &fds)) {
62
      user_fds[i] = -2;
63
    }
64
  }
65

    
66
  return 2;
67
}
68

    
69
struct nodeID *create_node(const char *IPaddr, int port)
70
{
71
  struct nodeID *s;
72
  int res;
73

    
74
  s = malloc(sizeof(struct nodeID));
75
  memset(s, 0, sizeof(struct nodeID));
76
  s->addr.sin_family = AF_INET;
77
  s->addr.sin_port = htons(port);
78
  res = inet_aton(IPaddr, &s->addr.sin_addr);
79
  if (res == 0) {
80
    free(s);
81

    
82
    return NULL;
83
  }
84
  s->buff = malloc(60 * 1024 + 1);
85

    
86
  s->fd = -1;
87

    
88
  return s;
89
}
90

    
91
struct nodeID *net_helper_init(const char *my_addr, int port, const char *config)
92
{
93
  int res;
94
  struct nodeID *myself;
95

    
96
  myself = create_node(my_addr, port);
97
  if (myself == NULL) {
98
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
99
  }
100
  myself->fd =  socket(AF_INET, SOCK_DGRAM, 0);
101
  if (myself->fd < 0) {
102
    free(myself);
103
    
104
    return NULL;
105
  }
106
  fprintf(stderr, "My sock: %d\n", myself->fd);
107

    
108
  res = bind(myself->fd, (struct sockaddr *)&myself->addr, sizeof(struct sockaddr_in));
109
  if (res < 0) {
110
    /* bind failed: not a local address... Just close the socket! */
111
    close(myself->fd);
112
    free(myself);
113

    
114
    return NULL;
115
  }
116

    
117
  return myself;
118
}
119

    
120
void bind_msg_type (uint8_t msgtype)
121
{
122
}
123

    
124
int send_to_peer(const struct nodeID *from, struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size)
125
{
126
  int res, len;
127

    
128
  do {
129
    if (buffer_size > 1024 * 60) {
130
      len = 1024 * 60;
131
      from->buff[0] = 0;
132
    } else {
133
      len = buffer_size;
134
      from->buff[0] = 1;
135
    }
136
    memcpy(from->buff + 1, buffer_ptr, len);
137
    buffer_size -= len;
138
    buffer_ptr += len;
139
    res = sendto(from->fd, from->buff, len + 1, 0, &to->addr, sizeof(struct sockaddr_in));
140
  } while (buffer_size > 0);
141

    
142
  return res;
143
}
144

    
145
int recv_from_peer(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size)
146
{
147
  int res, recv, len, addrlen;
148
  struct sockaddr_in raddr;
149

    
150
  *remote = malloc(sizeof(struct nodeID));
151
  if (*remote == NULL) {
152
    return -1;
153
  }
154

    
155
  recv = 0;
156
  do {
157
    if (buffer_size > 1024 * 60) {
158
      len = 1024 * 60;
159
    } else {
160
      len = buffer_size;
161
    }
162
    addrlen = sizeof(struct sockaddr_in);
163
    res = recvfrom(local->fd, local->buff, len, 0, &raddr, &addrlen);
164
    memcpy(buffer_ptr, local->buff + 1, res - 1);
165
    buffer_size -= (res - 1);
166
    buffer_ptr += (res - 1);
167
    recv += (res - 1);
168
  } while ((local->buff[0] == 0) && (buffer_size > 0));
169
  memcpy(&(*remote)->addr, &raddr, addrlen);
170
  (*remote)->fd = -1;
171

    
172
  return recv;
173
}
174

    
175
const char *node_addr(const struct nodeID *s)
176
{
177
  static char addr[256];
178

    
179
  sprintf(addr, "%s:%d", inet_ntoa(s->addr.sin_addr), ntohs(s->addr.sin_port));
180

    
181
  return addr;
182
}
183

    
184
struct nodeID *nodeid_dup(struct nodeID *s)
185
{
186
  struct nodeID *res;
187

    
188
  res = malloc(sizeof(struct nodeID));
189
  if (res != NULL) {
190
    memcpy(res, s, sizeof(struct nodeID));
191
  }
192

    
193
  return res;
194
}
195

    
196
int nodeid_equal(const struct nodeID *s1, const struct nodeID *s2)
197
{
198
  return (memcmp(&s1->addr, &s2->addr, sizeof(struct sockaddr_in)) == 0);
199
}
200

    
201
int nodeid_dump(uint8_t *b, const struct nodeID *s, size_t max_write_size)
202
{
203
  if (max_write_size < sizeof(struct sockaddr_in)) return -1;
204

    
205
  memcpy(b, &s->addr, sizeof(struct sockaddr_in));
206

    
207
  return sizeof(struct sockaddr_in);
208
}
209

    
210
struct nodeID *nodeid_undump(const uint8_t *b, int *len)
211
{
212
  struct nodeID *res;
213
  res = malloc(sizeof(struct nodeID));
214
  if (res != NULL) {
215
    memcpy(&res->addr, b, sizeof(struct sockaddr_in));
216
    res->fd = -1;
217
  }
218
  *len = sizeof(struct sockaddr_in);
219

    
220
  return res;
221
}
222

    
223
void nodeid_free(struct nodeID *s)
224
{
225
  free(s);
226
}
227

    
228
const char *node_ip(const struct nodeID *s)
229
{
230
  static char ip[64];
231

    
232
  sprintf(ip, "%s", inet_ntoa(s->addr.sin_addr));
233

    
234
  return ip;
235
}