Statistics
| Branch: | Revision:

streamers / out-stream-udp.c @ 5c17ac4b

History | View | Annotate | Download (2.02 KB)

1
/*
2
 *  Copyright (c) 2010 Csaba Kiraly
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 */
6
#include <sys/socket.h>
7
#include <netinet/in.h>
8
#include <arpa/inet.h>
9
#include <unistd.h>
10
#include <stdint.h>
11
#include <limits.h>
12
#include <stdio.h>
13
#include <stdlib.h>
14
#include <strings.h>
15

    
16
#include "out-stream.h"
17
#include "dbg.h"
18
#include "payload.h"
19
#include "io_udp.h"
20

    
21
static int outfd = -1;
22
static char ip[16];
23
static int base_port;
24
static int ports;
25

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

    
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);
33
    return -1;
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
  }
51

    
52
  return 1;
53
}
54

    
55

    
56
int out_stream_init(const char *config)
57
{
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;
78
}
79

    
80

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

    
85
  bzero(&si_other, sizeof(si_other));
86
  si_other.sin_family = AF_INET;
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) {
89
     fprintf(stderr, " output socket: inet_aton() failed\n");
90
     return;
91
  }
92

    
93
  sendto(outfd, data + sizeof (struct io_udp_header), size - sizeof (struct io_udp_header), 0, (struct sockaddr *) &si_other, sizeof(si_other));
94
}