Statistics
| Branch: | Revision:

grapes / som / net_helper.c @ 3bcb55e5

History | View | Annotate | Download (2.61 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
struct nodeID *create_socket(const char *IPaddr, int port)
18
{
19
  struct nodeID *s;
20
  int res;
21

    
22
  s = malloc(sizeof(struct nodeID));
23
  memset(s, 0, sizeof(struct nodeID));
24
  s->addr.sin_family = AF_INET;
25
  s->addr.sin_port = htons(port);
26
  res = inet_aton(IPaddr, &s->addr.sin_addr);
27
  if (res == 0) {
28
    free(s);
29

    
30
    return NULL;
31
  }
32

    
33
  s->fd = socket(AF_INET, SOCK_DGRAM, 0);
34
  if (s->fd < 0) {
35
    free(s);
36
    
37
    return NULL;
38
  }
39
fprintf(stderr, "My sock: %d\n", s->fd);
40
  res = bind(s->fd, (struct sockaddr *)&s->addr, sizeof(struct sockaddr_in));
41
  if (res < 0) {
42
    /* bind failed: not a local address... Just close the socket! */
43
    close(s->fd);
44
    s->fd = -1;
45
  }
46

    
47
  return s;
48
}
49

    
50
int send_data(const struct nodeID *from, const struct nodeID *to, const uint8_t *buffer_ptr, int buffer_size)
51
{
52
  return sendto(from->fd, buffer_ptr, buffer_size, 0,
53
                (struct sockaddr *)&to->addr, sizeof(struct sockaddr_in));
54
}
55

    
56
int recv_data(const struct nodeID *local, struct nodeID **remote, uint8_t *buffer_ptr, int buffer_size)
57
{
58
  int res;
59
  struct sockaddr_in raddr;
60
  socklen_t raddr_size = sizeof(struct sockaddr_in);
61

    
62
  *remote = malloc(sizeof(struct nodeID));
63
  if (*remote == NULL) {
64
    return -1;
65
  }
66
  res = recvfrom(local->fd, buffer_ptr, buffer_size, 0, (struct sockaddr *)&raddr, &raddr_size);
67
  memcpy(&(*remote)->addr, &raddr, raddr_size);
68
  (*remote)->fd = -1;
69
fprintf(stderr, "Read %d from %s\n", res, inet_ntoa(raddr.sin_addr));
70
  return res;
71
}
72

    
73
const char *node_addr(const struct nodeID *s)
74
{
75
  static char addr[256];
76

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

    
79
  return addr;
80
}
81

    
82
int getFD(const struct nodeID *n)
83
{
84
  return n->fd;
85
}
86

    
87
struct nodeID *nodeid_dup(const struct nodeID *s)
88
{
89
  struct nodeID *res;
90

    
91
  res = malloc(sizeof(struct nodeID));
92
  if (res != NULL) {
93
    memcpy(res, s, sizeof(struct nodeID));
94
  }
95

    
96
  return res;
97
}
98
int nodeid_equal(const struct nodeID *s1, const struct nodeID *s2)
99
{
100
  return (memcmp(&s1->addr, &s2->addr, sizeof(struct sockaddr_in)) == 0);
101
}
102

    
103
int nodeid_dump(uint8_t *b, const struct nodeID *s)
104
{
105
  memcpy(b, s, sizeof(struct nodeID));
106

    
107
  return sizeof(struct nodeID);
108
}
109

    
110
struct nodeID *nodeid_undump(const uint8_t *b, int *len)
111
{
112
  struct nodeID *res;
113
  res = malloc(sizeof(struct nodeID));
114
  if (res != NULL) {
115
    memcpy(res, b, sizeof(struct nodeID));
116
  }
117
  *len = sizeof(struct nodeID);
118

    
119
  return res;
120
}