Statistics
| Branch: | Revision:

grapes / som / net_helper.c @ 8ab58ec7

History | View | Annotate | Download (3.27 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)
25
{
26
  fd_set fds;
27
  int res;
28

    
29
  FD_ZERO(&fds);
30
  FD_SET(s->fd, &fds);
31
  res = select(s->fd + 1, &fds, NULL, NULL, &tout);
32
  if (FD_ISSET(s->fd, &fds)) {
33
    return 1;
34
  }
35

    
36
  return 0;
37
}
38

    
39
struct nodeID *create_node(const char *IPaddr, int port)
40
{
41
  struct nodeID *s;
42
  int res;
43

    
44
  s = malloc(sizeof(struct nodeID));
45
  memset(s, 0, sizeof(struct nodeID));
46
  s->addr.sin_family = AF_INET;
47
  s->addr.sin_port = htons(port);
48
  res = inet_aton(IPaddr, &s->addr.sin_addr);
49
  if (res == 0) {
50
    free(s);
51

    
52
    return NULL;
53
  }
54

    
55
  s->fd = socket(AF_INET, SOCK_DGRAM, 0);
56
  if (s->fd < 0) {
57
    free(s);
58
    
59
    return NULL;
60
  }
61

    
62
  return s;
63
}
64

    
65
struct nodeID *net_helper_init(const char *my_addr, int port)
66
{
67
  int res;
68
  struct nodeID *myself;
69

    
70
  myself = create_node(my_addr, port);
71
  if (myself == NULL) {
72
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
73
  }
74
  fprintf(stderr, "My sock: %d\n", myself->fd);
75

    
76
  res = bind(myself->fd, (struct sockaddr *)&myself->addr, sizeof(struct sockaddr_in));
77
  if (res < 0) {
78
    /* bind failed: not a local address... Just close the socket! */
79
    close(myself->fd);
80
    free(myself);
81

    
82
    return NULL;
83
  }
84

    
85
  return myself;
86
}
87

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

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

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

    
109
  return res;
110
}
111

    
112
const char *node_addr(const struct nodeID *s)
113
{
114
  static char addr[256];
115

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

    
118
  return addr;
119
}
120

    
121
struct nodeID *nodeid_dup(const struct nodeID *s)
122
{
123
  struct nodeID *res;
124

    
125
  res = malloc(sizeof(struct nodeID));
126
  if (res != NULL) {
127
    memcpy(res, s, sizeof(struct nodeID));
128
  }
129

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

    
137
int nodeid_dump(uint8_t *b, const struct nodeID *s)
138
{
139
  memcpy(b, &s->addr, sizeof(struct sockaddr_in));
140

    
141
  return sizeof(struct sockaddr_in);
142
}
143

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

    
153
  return res;
154
}