File: network.c

package info (click to toggle)
muroard 0.1.14-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 420 kB
  • ctags: 424
  • sloc: ansic: 3,645; sh: 505; makefile: 113
file content (393 lines) | stat: -rw-r--r-- 9,387 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//network.c:

/*
 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2013
 *
 *  This file is part of �RoarD,
 *  a sound server daemon for using the RoarAudio protocol.
 *  See README for details.
 *
 *  This file is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3
 *  or (at your option) any later version as published by
 *  the Free Software Foundation.
 *
 *  RoarAudio is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this software; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#include "muroard.h"
#ifndef __WIN32
#include <sched.h>
#endif

int muroard_network_init(void) {
#ifdef __WIN32
 WSADATA wsadata;

 WSAStartup(MAKEWORD(1,1) , &wsadata);
#endif
 return 0;
}

int muroard_network_free(void) {
 return muroard_network_prefree();
}

int muroard_network_prefree(void) {
#ifdef MUROARD_FEATURE_SOCKET_LISTEN
 if ( muroard_state_member(listen_socket) != MUROARD_HANDLE_INVALID )
  muroard_network_close(muroard_state_member(listen_socket));

  muroard_state_member(listen_socket) = MUROARD_HANDLE_INVALID;
#ifdef MUROARD_FEATURE_SOCKET_LISTEN_SOCKTYPE
  muroard_state_member(listen_type) = -1;
#endif
#endif

 return 0;
}

#ifdef MUROARD_FEATURE_SOCKET_UNIX
static inline int muroard_network_test_unix (const char * addr) {
 struct sockaddr_un   un;
 muroard_handle_t fh;
 int ret;

 if ( (fh = socket(AF_UNIX, SOCK_STREAM, 0)) == MUROARD_HANDLE_INVALID )
  return -1;

 un.sun_family = AF_UNIX;
 strncpy(un.sun_path, addr, sizeof(un.sun_path) - 1);

 if ( connect(fh, (struct sockaddr *)&un, sizeof(struct sockaddr_un)) == -1 ) {
  if ( errno == ECONNREFUSED ) {
   ret =  0;
  } else {
   ret = -1;
  }
 } else {
  ret = 1;
 }

 muroard_network_close(fh);

 return ret;
}
#endif

#ifdef MUROARD_FEATURE_SOCKET_LISTEN
int muroard_network_listen(int type, const char * addr, int port) {
#ifdef MUROARD_FEATURE_SOCKET_UNIX
 struct sockaddr_un   un;
#endif
#ifdef MUROARD_FEATURE_SOCKET_INET
 struct hostent     * he;
 struct sockaddr_in   in;
#endif
 muroard_handle_t fh;
#ifdef MUROARD_DEFAULT_LISTEN_CHMOD
 mode_t defmask;
#endif
#ifdef MUROARD_FEATURE_SOCKET_DECNET
 struct sockaddr_dn dn;
 size_t objlen;
#ifdef DSO_ACCEPTMODE
 int acceptmode;
#endif
#endif

 switch (type) {
#ifdef MUROARD_FEATURE_SOCKET_UNIX
  case MUROARD_NETWORK_TYPE_UNIX:
    if ( addr == NULL )
     addr = MUROARD_DEFAULT_LISTEN_ADDR_UNIX;

    if ( (fh = socket(AF_UNIX, SOCK_STREAM, 0)) == MUROARD_HANDLE_INVALID )
     return -1;

    un.sun_family = PF_UNIX;
    strncpy(un.sun_path, addr, sizeof(un.sun_path) - 1);

#ifdef MUROARD_DEFAULT_LISTEN_CHMOD
    defmask = umask(0000);

    if ( fchmod(fh, MUROARD_DEFAULT_LISTEN_CHMOD) == -1 ) {
     muroard_network_close(fh);
     return -1;
    }
#endif

    if ( bind(fh, (struct sockaddr *)&un, sizeof(struct sockaddr_un)) == -1 ) {
     if ( muroard_network_test_unix(addr) != 0 ) {
      muroard_network_close(fh);
      return -1;
     }

     if ( unlink(addr) == -1 ) {
      muroard_network_close(fh);
      return -1;
     }

     if ( bind(fh, (struct sockaddr *)&un, sizeof(struct sockaddr_un)) == -1 ) {
      muroard_network_close(fh);
      return -1;
     }
    }

#ifdef MUROARD_DEFAULT_LISTEN_CHMOD
    umask(defmask);
#endif

   break;
#endif
#ifdef MUROARD_FEATURE_SOCKET_INET
  case MUROARD_NETWORK_TYPE_INET:
    if ( addr == NULL )
     addr = MUROARD_DEFAULT_LISTEN_ADDR_INET;

    if ( (he = gethostbyname(addr)) == NULL ) {
     return -1;
    }

    if ( (fh = socket(AF_INET, SOCK_STREAM, 0)) == MUROARD_HANDLE_INVALID )
     return -1;

    memcpy((struct in_addr *)&in.sin_addr, he->h_addr, sizeof(struct in_addr));

    in.sin_family = AF_INET;
    in.sin_port   = htons(port);

    if ( bind(fh, (const struct sockaddr *)&in, sizeof(in)) == -1 ) {
     muroard_network_close(fh);
     return -1;
    }
   break;
#endif
#ifdef MUROARD_FEATURE_SOCKET_DECNET
  case MUROARD_NETWORK_TYPE_DECNET:
    if ( addr == NULL )
     addr = MUROARD_DEFAULT_LISTEN_ADDR_DECNET;

    // we only support empty node addresses here (bind globaly).
    if ( addr[0] != ':' || addr[1] != ':' )
     return -1;

    addr += 2;

    if ( (fh = socket(AF_DECnet, SOCK_SEQPACKET, DNPROTO_NSP)) == MUROARD_HANDLE_INVALID )
     return -1;

#ifdef DSO_ACCEPTMODE
    acceptmode = ACC_DEFER;
    setsockopt(fh, DNPROTO_NSP, DSO_ACCEPTMODE, &acceptmode, sizeof(acceptmode));
#endif

    muroard_memzero(&dn, sizeof(dn));
    dn.sdn_family    = AF_DECnet;
    dn.sdn_flags     = 0;
    dn.sdn_objnum    = 0;
    objlen = strlen(addr);
#if MUROAR_CODEC_PCM_S_LE == MUROAR_CODEC_PCM_S
    dn.sdn_objnamel  = objlen;
#elif MUROAR_CODEC_PCM_S_BE == MUROAR_CODEC_PCM_S
    if ( sizeof(unsigned short) == 2 ) {
     dn.sdn_objnamel = ((objlen & 0xFF00) >> 8) | ((objlen & 0x00FF) << 8);
    } else {
     return -1;
    }
#else
#error Unsupported Endianess.
#endif
    strcpy((char *)dn.sdn_objname, addr);

    bind(fh, (struct sockaddr *)&dn, sizeof(dn));
   break;
#endif
  default:
    return -1;
   break;
 }

 if ( listen(fh, 8) == -1 ) {
  muroard_network_close(fh);
 }

 muroard_state_member(listen_socket) = fh;
#ifdef MUROARD_FEATURE_SOCKET_LISTEN_SOCKTYPE
 muroard_state_member(listen_type) = type;
#endif

 return 0;
}
#endif

#ifndef __WIN32
int muroard_network_nonblock(muroard_handle_t fh, int reset) {
 int flags;

 if ( (flags = fcntl(fh, F_GETFL, 0)) == -1 )
  return -1;

 flags |= O_NONBLOCK;
 if (reset)
  flags -= O_NONBLOCK;

 return fcntl(fh, F_SETFL, flags);
}
#endif

int muroard_network_check(enum muroard_blocking blocking) {
 struct timeval tv;
 fd_set rfds;
 void * data;
 int ret;
 muroard_handle_t fh;
 int i;
 int max_fh = -1;

 switch (blocking) {
  case MUROARD_BLOCKING_NONE:
    tv.tv_sec  = 0;
    tv.tv_usec = 1;
   break;
  case MUROARD_BLOCKING_LONG:
    tv.tv_sec  = 1024;
    tv.tv_usec = 0;
   break;
 }

 FD_ZERO(&rfds);

#ifdef MUROARD_FEATURE_SOCKET_LISTEN
 if ( muroard_state_member(listen_socket) != MUROARD_HANDLE_INVALID ) {
  FD_SET(muroard_state_member(listen_socket), &rfds);
  max_fh = muroard_state_member(listen_socket);
 }
#endif

 for (i = 0; i < MUROARD_MAX_STREAMS; i++) {
  if ( muroard_stream_get_datadir(i) == STREAM_DATADIR_IN ) {
   if ( (fh = muroard_stream_get_sock(i)) != MUROARD_HANDLE_INVALID ) {
    FD_SET(fh, &rfds);
    if ( fh > max_fh )
     max_fh = fh;
   }
  }
 }

 for (i = 0; i < MUROARD_MAX_CLIENTS; i++) {
  if ( (fh = client_get_sock(i)) != MUROARD_HANDLE_INVALID ) {
   FD_SET(fh, &rfds);
   if ( fh > max_fh )
    max_fh = fh;
  }
 }

 if ( max_fh == -1 ) { // all IOs are gone, terminate the daemon
  muroard_state_member(alive) = 0;
  return 0;
 }

 ret = select(max_fh + 1, &rfds, NULL, NULL, &tv);

 if ( ret == -1 )
  return -1;

 if ( ret == 0 ) {
  for (i = 0; i < MUROARD_MAX_STREAMS; i++) {
   if ( muroard_stream_get_datadir(i) == STREAM_DATADIR_IN ) {
    if ( (data = muroard_stream_get_iobuf(i)) != NULL ) {
     muroard_memzero(data, muroard_state_member(abuffer_size));
    }
   }
  }
  return 0;
 }

#ifdef MUROARD_FEATURE_SOCKET_LISTEN
 if ( muroard_state_member(listen_socket) != MUROARD_HANDLE_INVALID ) {
  if ( FD_ISSET(muroard_state_member(listen_socket), &rfds) ) {
   fh = accept(muroard_state_member(listen_socket), NULL, NULL);
#ifdef MUROARD_FEATURE_SOCKET_DECNET
   if ( muroard_state_member(listen_type) == MUROARD_NETWORK_TYPE_DECNET )
    dnet_accept(fh, 0, NULL, 0);
#endif
   muroard_client_new(fh);
  }
 }
#endif

 for (i = 0; i < MUROARD_MAX_STREAMS; i++) {
  if ( muroard_stream_get_datadir(i) == STREAM_DATADIR_IN ) {
   if ( (fh = muroard_stream_get_sock(i)) != MUROARD_HANDLE_INVALID ) {
    if ( FD_ISSET(fh, &rfds) ) {
     muroard_stream_read(i);
    } else {
     if ( (data = muroard_stream_get_iobuf(i)) != NULL )
      muroard_memzero(data, muroard_state_member(abuffer_size));
    }
   }
  }
 }

 for (i = 0; i < MUROARD_MAX_CLIENTS; i++) {
  if ( (fh = client_get_sock(i)) != MUROARD_HANDLE_INVALID ) {
   if ( FD_ISSET(fh, &rfds) ) {
    muroard_client_handle(i);
   }
  }
 }

 return 0;
}

#ifdef MUROARD_FEATURE_CMD_PASSFH
#define _SCMR_CONTROLLEN (sizeof(struct cmsghdr) + sizeof(int))
muroard_handle_t muroard_network_recvfh(muroard_handle_t fh) {
 struct iovec     iov[1];
 struct msghdr    msg;
 char             cmptr_buf[_SCMR_CONTROLLEN];
 struct cmsghdr * cmptr = (struct cmsghdr *) cmptr_buf;
 char             localmes[1];

 iov[0].iov_base = localmes;
 iov[0].iov_len  = 1;
 msg.msg_iov     = iov;
 msg.msg_iovlen  = 1;
 msg.msg_name    = NULL;
 msg.msg_namelen = 0;

 msg.msg_control    = (caddr_t) cmptr;
 msg.msg_controllen = _SCMR_CONTROLLEN;

 if ( recvmsg(fh, &msg, 0) == -1 ) {
#ifndef __WIN32
  if ( errno == EAGAIN ) {
   sched_yield();
   if ( recvmsg(fh, &msg, 0) == -1 )
    return MUROARD_HANDLE_INVALID;
  } else {
   return MUROARD_HANDLE_INVALID;
  }
#else
  return MUROARD_HANDLE_INVALID;
#endif
 }

 if ( msg.msg_controllen != _SCMR_CONTROLLEN )
  return MUROARD_HANDLE_INVALID;

 return *(int *)CMSG_DATA(cmptr);
}
#endif

//ll