Statistics
| Branch: | Tag: | Revision:

mongoose / examples / mcast_udp_server / mcast_udp_server.c @ eaef5bd1

History | View | Annotate | Download (2.87 KB)

1
// Copyright (c) 2016 Cesanta Software Limited
2
// All rights reserved
3
//
4
// This software is dual-licensed: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License version 2 as
6
// published by the Free Software Foundation. For the terms of this
7
// license, see <http://www.gnu.org/licenses/>.
8
//
9
// You are free to use this software under the terms of the GNU General
10
// Public License, but WITHOUT ANY WARRANTY; without even the implied
11
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
// See the GNU General Public License for more details.
13
//
14
// Alternatively, you can license this software under a commercial
15
// license, as set out in <https://www.cesanta.com/license>.
16
//
17
// This demo shows how to configure a mongoose UDP connection to receive
18
// multicast traffic.
19

    
20
#include <netinet/in.h>
21

    
22
#include "mongoose.h"
23

    
24
#define MCAST_GROUP "224.0.22.1"
25
#define DEFAULT_PORT "1234"
26

    
27
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
28
  struct mbuf *io = &nc->recv_mbuf;
29
  (void) p;
30
  switch (ev) {
31
    case MG_EV_POLL: {
32
      const char *peer;
33
      peer = inet_ntoa(nc->sa.sin.sin_addr);
34
      printf("nc->sa: %s %d\n", peer, ntohs(nc->sa.sin.sin_port));
35

    
36
      break;
37
    }
38
    case MG_EV_RECV:
39
      printf("Received (%zu bytes): '%.*s'\n", io->len, (int) io->len, io->buf);
40
      mbuf_remove(io, io->len);
41
      nc->flags |= MG_F_SEND_AND_CLOSE;
42
      break;
43
    default:
44
      break;
45
  }
46
}
47

    
48
void usage(char **argv) {
49
  fprintf(stderr, "%s: -i address_of_mcast_interface [ -g %s ] [ -p %s ]\n",
50
          argv[0], MCAST_GROUP, DEFAULT_PORT);
51
  exit(1);
52
}
53

    
54
int main(int argc, char **argv) {
55
  struct mg_mgr mgr;
56
  const char *port = "1234";
57
  struct mg_connection *nc;
58
  struct ip_mreq group;
59
  const char *interface = NULL;
60
  const char *mcast_group = MCAST_GROUP;
61
  int i;
62

    
63
  /* Parse command line arguments */
64
  for (i = 1; i < argc; i++) {
65
    // IP address of the interface where to join a multicast group.
66
    if (strcmp(argv[i], "-i") == 0) {
67
      interface = argv[++i];
68
    } else if (strcmp(argv[i], "-g") == 0) {
69
      mcast_group = argv[++i];
70
    } else if (strcmp(argv[i], "-p") == 0) {
71
      port = argv[++i];
72
    }
73
  }
74

    
75
  if (interface == NULL) {
76
    usage(argv);
77
  }
78

    
79
  mg_mgr_init(&mgr, NULL);
80
  {
81
    char listen[256];
82
    sprintf(listen, "udp://%s", port);
83
    nc = mg_bind(&mgr, listen, ev_handler);
84
  }
85

    
86
  if (nc == NULL) {
87
    perror("cannot bind\n");
88
    exit(1);
89
  }
90

    
91
  group.imr_multiaddr.s_addr = inet_addr(mcast_group);
92
  group.imr_interface.s_addr = inet_addr(interface);
93
  if (setsockopt(nc->sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &group,
94
                 sizeof(group)) < 0) {
95
    perror("Adding multicast group error");
96
    exit(1);
97
  }
98

    
99
  printf("Starting mcast server on port %s listening to group %s\n", port,
100
         mcast_group);
101
  for (;;) {
102
    mg_mgr_poll(&mgr, 1000);
103
  }
104
  mg_mgr_free(&mgr);
105

    
106
  return 0;
107
}