Statistics
| Branch: | Revision:

grapes / som / net_helper.c @ 1d82dff0

History | View | Annotate | Download (3.14 KB)

1
#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
#include "net_helper.h"
11

    
12
struct nodeID {
13
  struct sockaddr_in addr;
14
  int fd;
15
};
16

    
17
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
{
34
  struct nodeID *s;
35
  int res;
36

    
37
  s = malloc(sizeof(struct nodeID));
38
  memset(s, 0, sizeof(struct nodeID));
39
  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
    return NULL;
46
  }
47

    
48
  s->fd = socket(AF_INET, SOCK_DGRAM, 0);
49
  if (s->fd < 0) {
50
    free(s);
51
    
52
    return NULL;
53
  }
54

    
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
  if (res < 0) {
71
    /* bind failed: not a local address... Just close the socket! */
72
    close(myself->fd);
73
    free(myself);
74

    
75
    return NULL;
76
  }
77

    
78
  return myself;
79
}
80

    
81
int send_to_peer(const struct nodeID *from, const struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size)
82
{
83
  return sendto(from->fd, buffer_ptr, buffer_size, 0,
84
                (const struct sockaddr *)&to->addr, sizeof(struct sockaddr_in));
85
}
86

    
87
int recv_from_peer(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size)
88
{
89
  int res;
90
  struct sockaddr_in raddr;
91
  socklen_t raddr_size = sizeof(struct sockaddr_in);
92

    
93
  *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
  //fprintf(stderr, "Read %d from %s\n", res, inet_ntoa(raddr.sin_addr));
101

    
102
  return res;
103
}
104

    
105
const char *node_addr(const struct nodeID *s)
106
{
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
struct nodeID *nodeid_dup(const struct nodeID *s)
115
{
116
  struct nodeID *res;
117

    
118
  res = malloc(sizeof(struct nodeID));
119
  if (res != NULL) {
120
    memcpy(res, s, sizeof(struct nodeID));
121
  }
122

    
123
  return res;
124
}
125
int nodeid_equal(const struct nodeID *s1, const struct nodeID *s2)
126
{
127
  return (memcmp(&s1->addr, &s2->addr, sizeof(struct sockaddr_in)) == 0);
128
}
129

    
130
int nodeid_dump(uint8_t *b, const struct nodeID *s)
131
{
132
  memcpy(b, &s->addr, sizeof(struct sockaddr_in));
133

    
134
  return sizeof(struct sockaddr_in);
135
}
136

    
137
struct nodeID *nodeid_undump(const uint8_t *b, int *len)
138
{
139
  struct nodeID *res;
140
  res = malloc(sizeof(struct nodeID));
141
  if (res != NULL) {
142
    memcpy(&res->addr, b, sizeof(struct sockaddr_in));
143
  }
144
  *len = sizeof(struct sockaddr_in);
145

    
146
  return res;
147
}