Revision 5c17ac4b

View differences:

out-stream-udp.c
8 8
#include <arpa/inet.h>
9 9
#include <unistd.h>
10 10
#include <stdint.h>
11
#include <limits.h>
11 12
#include <stdio.h>
12 13
#include <stdlib.h>
13 14
#include <strings.h>
......
18 19
#include "io_udp.h"
19 20

  
20 21
static int outfd = -1;
21
#define PLAYER_IP "127.0.0.1"
22
#define PLAYER_PORT 32500
22
static char ip[16];
23
static int base_port;
24
static int ports;
23 25

  
24
int output_stream_init()
26
static int config_parse(const char *config)
25 27
{
26
  int fd;
28
  int res, i;
27 29

  
28
  fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
29
  if (fd < 0) {
30
    fprintf(stderr, "output socket: can't open\n");
30
  res = sscanf(config, "udp://%15[0-9.]:%d%n", ip, base_port, &i);
31
  if (res < 2) {
32
    fprintf(stderr,"error parsing output config: %s\n", config);
31 33
    return -1;
32 34
  }
35
  if (*(config + i) == 0) {
36
    ports = INT_MAX;
37
  } else {
38
    int max_port;
39
    res = sscanf(config + i, "-%d", &max_port);
40
    if (res == 1) {
41
      ports = max_port - base_port + 1;
42
      if (ports <=0) {
43
        fprintf(stderr,"error parsing output config: negative port range %s\n", config + i);
44
        return -2;
45
      }
46
    } else {
47
      fprintf(stderr,"error parsing output config: bad port range %s\n", config + i);
48
      return -3;
49
    }
50
  }
33 51

  
34
  return fd;
52
  return 1;
35 53
}
36 54

  
55

  
37 56
int out_stream_init(const char *config)
38 57
{
39
  return 1;
58
  int res;
59

  
60
  if (!config) {
61
    fprintf(stderr, "udp output not configured\n");
62
    return -1;
63
  }
64

  
65
  outfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
66
  if (outfd < 0) {
67
    fprintf(stderr, "output socket: can't open\n");
68
    return -2;
69
  }
70

  
71
  res = config_parse(config);
72
  if (res < 0) {
73
    close(outfd);
74
    return -3;
75
  }
76

  
77
  return outfd;
40 78
}
41 79

  
80

  
42 81
void chunk_write(int id, const uint8_t *data, int size)
43 82
{
44 83
  struct sockaddr_in si_other;
45 84

  
46
  if (outfd < 0) {
47
    outfd = output_stream_init();
48
    if (outfd < 0) {
49
      fprintf(stderr, "Error: can't open output socket\n");
50
      exit(1);
51
    }
52
  }
53

  
54 85
  bzero(&si_other, sizeof(si_other));
55 86
  si_other.sin_family = AF_INET;
56
  si_other.sin_port = htons((PLAYER_PORT + ((const struct io_udp_header*)data)->portdiff));
57
  if (inet_aton(PLAYER_IP, &si_other.sin_addr) == 0) {
87
  si_other.sin_port = htons((base_port + ((const struct io_udp_header*)data)->portdiff));
88
  if (inet_aton(ip, &si_other.sin_addr) == 0) {
58 89
     fprintf(stderr, " output socket: inet_aton() failed\n");
59 90
     return;
60 91
  }

Also available in: Unified diff