File: network6.c

package info (click to toggle)
poc-streamer 0.4.2-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,000 kB
  • sloc: ansic: 8,782; makefile: 307; ruby: 152; perl: 135; yacc: 115; lex: 36; sh: 30
file content (290 lines) | stat: -rw-r--r-- 6,128 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*C
  (c) 2005 bl0rg.net
**/

#ifdef WITH_IPV6

#include "conf.h"

#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>

#include "network.h"
#include "pack.h"

/*M
  \emph{Create an ipv6 UDP socket.}

  Returns the filedescriptor of the socket, or -1 on error.
**/
static int net_udp6_socket(struct sockaddr_in6 *saddr,
                           unsigned short port,
                           unsigned int hops) {
  assert(saddr != NULL);
  
  /*M
    Create UDP socket.
  **/
  int msock;
  if ((msock = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) {
    perror("socket");
    return -1;
  }

  /*M
    Set socket to reuse addresses.
  **/
  int on = 1;
  if (setsockopt(msock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
    perror("setsockopt");
    goto error;
  }

  saddr->sin6_family = AF_INET6;
  saddr->sin6_port   = htons(port);

  /*M
    If the address is a multicast address, set the TTL and turn on
    multicast loop so the local host can receive the UDP packets.
  **/
  if (IN6_IS_ADDR_MULTICAST(&saddr->sin6_addr)) {
    unsigned int loop = 1;
    if ((setsockopt(msock,
                    IPPROTO_IPV6,
                    IPV6_MULTICAST_HOPS,
                    &hops, sizeof(hops)) < 0) ||
        (setsockopt(msock,
                    IPPROTO_IPV6,
                    IPV6_MULTICAST_LOOP,
                    &loop, sizeof(loop)) < 0)) {
      perror("setsockopt");
      goto error;
    }
  }

  return msock;

 error:
  if (close(msock) < 0)
    perror("close");
  return -1;
}

/*M
  \emph{Create a ipv6 sending UDP socket.}

  Connects the created UDP socket to hostname. Returns the
  filedescriptor of the socket, or -1 on error.
**/
int net_udp6_send_socket(char *hostname,
                         unsigned short port,
                         unsigned int hops) {
  /*M
    Get hostname address.
  **/
  struct hostent *host;
  if (NULL == (host = gethostbyname2(hostname, AF_INET6))) {
    perror("gethostbyname");
    return -1;
  }

  /*M
    Init sockaddr structure.
  **/
  struct sockaddr_in6 saddr;
  memcpy(&saddr.sin6_addr, host->h_addr_list[0], (size_t)host->h_length);

  /*M
    Create udp socket.
  **/
  int msock;
  if ((msock = net_udp6_socket(&saddr, port, hops)) < 0)
    return -1;

  /*M
    Connect to hostname.
  **/
  if (connect(msock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
    perror("connect");
    goto error;
  }

  return msock;

 error:
  if (close(msock) < 0)
    perror("close");
  return -1;
}

/*M
  \emph{Create a receiving ipv6 UDP socket.}

  Binds the created UDP socket to hostname, and adds multicast
  membership if hostname is a multicast hostname. Returns the
  filedescriptor of the socket, or -1 on error.
**/
int net_udp6_recv_socket(char *hostname,
                         unsigned short port) {
  /*M
    Get hostname address.
  **/
  struct hostent *host;
  if (NULL == (host = gethostbyname2(hostname, AF_INET6))) {
    perror("gethostbyname");
    return -1;
  }

  /*M
    Initialize sockaddr structure.
  **/
  struct sockaddr_in6 addr;
  memcpy(&addr.sin6_addr, host->h_addr_list[0], (size_t)host->h_length);

  /*M
    Create udp socket.
  **/
  int msock;
  if ((msock = net_udp6_socket(&addr, port, 1)) < 0)
    return -1;

  /*M
    Bind to hostname.
  **/
  if (bind(msock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("bind");
    goto error;
  }

  /*M
    Add multicast membership if address is a multicast address.
  **/
  if (IN6_IS_ADDR_MULTICAST(&addr.sin6_addr)) {
    struct ipv6_mreq mreq;
    memcpy(&mreq.ipv6mr_multiaddr,
           &addr.sin6_addr,
           sizeof(addr.sin6_addr));
    mreq.ipv6mr_interface = 0;

    if (setsockopt(msock,
                   IPPROTO_IPV6,
                   IPV6_JOIN_GROUP,
                   &mreq, sizeof(mreq)) < 0) {
      perror("setsockopt");
      goto error;
    }
  }

  return msock;

 error:
  if (close(msock) < 0)
    perror("close");
  return -1;
}

/*M
  \emph{Create a TCP v6 socket and set it non to nonblocking IO.}
**/
int net_tcp6_nonblock_socket(void) {
  int s;

  s = socket(AF_INET6, SOCK_STREAM, 0);
  if (s == -1)
    return -1;
  if (net_tcp6_socket_nonblock(s) == -1) {
    close(s);
    return -1;
  }
  
  return s;
}

int net_tcp6_socket_nonblock(int s) {
  if (fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK) == -1)
    return -1;

  return 0;
}

/*M
  \emph{Bind a socket to an IPV4 adress and port.}
**/
int net_tcp6_bind(int s, unsigned char ip[16], unsigned short port) {
  struct sockaddr_in6 sa;

  memset(&sa, 0, sizeof(sa));

  sa.sin6_family = AF_INET6;

  unsigned char *ptr = (unsigned char *)&sa.sin6_port;
  UINT16_PACK(ptr, port);
  memcpy(&sa.sin6_addr, ip, 16);
  
  return bind(s, (struct sockaddr *)&sa, sizeof(sa));
}

int net_tcp6_bind_reuse(int s,
                        unsigned char ip[16],
                        unsigned short port) {
  int opt = 1;
  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  return net_tcp6_bind(s, ip, port);
}

int net_tcp6_listen_socket(char *hostname, unsigned short port) {
  /*M
    Get hostname address.
  **/
  struct hostent *host;
  if (NULL == (host = gethostbyname2(hostname, AF_INET6))) {
    perror("gethostbyname");
    return -1;
  }

  if (host->h_length != 16) {
    perror("gethostbyname");
    return -1;
  }

  int sock;
  if (((sock = net_tcp6_nonblock_socket()) < 0) ||
      (net_tcp6_bind_reuse(sock, host->h_addr_list[0], port) < 0) ||
      (listen(sock, 16) < 0)) {
    return -1;
  }

  return sock;
}

int net_tcp6_accept_socket(int s,
                           unsigned char ip[16],
                           unsigned short *port) {
  struct sockaddr_in6 sa;
  int len = sizeof(sa);
  int fd;

  fd = accept(s, (struct sockaddr *)&sa, &len);
  if (fd == -1)
    return -1;

  memcpy(ip, (unsigned char *)&sa.sin6_addr, 16);
  unsigned char *ptr = (unsigned char *)&sa.sin6_port;
  *port = UINT16_UNPACK(ptr);

  return fd;
}

#endif

/*M
**/