Statistics
| Branch: | Revision:

grapes / som / net_helper.c @ 458b1d7e

History | View | Annotate | Download (3.24 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
void bind_msg_type (uint8_t msgtype)
82
{
83
}
84

    
85
int send_to_peer(const struct nodeID *from, const struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size)
86
{
87
  return sendto(from->fd, buffer_ptr, buffer_size, 0,
88
                (const struct sockaddr *)&to->addr, sizeof(struct sockaddr_in));
89
}
90

    
91
int recv_from_peer(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size)
92
{
93
  int res;
94
  struct sockaddr_in raddr;
95
  socklen_t raddr_size = sizeof(struct sockaddr_in);
96

    
97
  *remote = malloc(sizeof(struct nodeID));
98
  if (*remote == NULL) {
99
    return -1;
100
  }
101
  res = recvfrom(local->fd, buffer_ptr, buffer_size, 0, (struct sockaddr *)&raddr, &raddr_size);
102
  memcpy(&(*remote)->addr, &raddr, raddr_size);
103
  (*remote)->fd = -1;
104
  //fprintf(stderr, "Read %d from %s\n", res, inet_ntoa(raddr.sin_addr));
105

    
106
  return res;
107
}
108

    
109
const char *node_addr(const struct nodeID *s)
110
{
111
  static char addr[256];
112

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

    
115
  return addr;
116
}
117

    
118
struct nodeID *nodeid_dup(const struct nodeID *s)
119
{
120
  struct nodeID *res;
121

    
122
  res = malloc(sizeof(struct nodeID));
123
  if (res != NULL) {
124
    memcpy(res, s, sizeof(struct nodeID));
125
  }
126

    
127
  return res;
128
}
129
int nodeid_equal(const struct nodeID *s1, const struct nodeID *s2)
130
{
131
  return (memcmp(&s1->addr, &s2->addr, sizeof(struct sockaddr_in)) == 0);
132
}
133

    
134
int nodeid_dump(uint8_t *b, const struct nodeID *s)
135
{
136
  memcpy(b, &s->addr, sizeof(struct sockaddr_in));
137

    
138
  return sizeof(struct sockaddr_in);
139
}
140

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

    
150
  return res;
151
}
152

    
153
void nodeid_free(struct nodeID *s)
154
{
155
  free(s);
156
}
157