kinetic-c  v0.12.0
Seagate Kinetic Protocol Client Library for C
discovery.c
Go to the documentation of this file.
1 /*
2 * kinetic-c
3 * Copyright (C) 2015 Seagate Technology.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20 
21 #include <stdio.h>
22 
23 #include <err.h>
24 #include <errno.h>
25 
26 #include <netinet/in.h>
27 
28 #include "kinetic_client.h"
29 #include "socket99.h"
30 #include "json.h"
31 
32 static int discover_service(void);
33 
34 
35 //------------------------------------------------------------------------------
36 // Main Entry Point Definition
37 int main(int argc, char** argv)
38 {
39  // TODO: CLI args?
40  (void)argc;
41  (void)argv;
42  return discover_service();
43 }
44 
45 
46 //------------------------------------------------------------------------------
47 // Service discovery
48 
49 static int discover_service(void) {
50  int v_true = 1;
51  socket99_config cfg = {
52  .host = INADDR_ANY,
53  .port = KINETIC_PORT,
54  .server = true,
55  .datagram = true,
56  .sockopts = {
57  {/*SOL_SOCKET,*/ SO_BROADCAST, &v_true, sizeof(v_true)},
58  },
59  };
60  socket99_result res;
61 
62  if (!socket99_open(&cfg, &res)) {
63  errno = res.saved_errno;
64  printf("res %d, %d\n", res.status, res.getaddrinfo_error);
65  if (res.status == SOCKET99_ERROR_GETADDRINFO) {
66  fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res.getaddrinfo_error));
67  return 1;
68  }
69  err(1, "socket99_open");
70  return 1;
71  }
72 
73  int one = 1;
74  if (0 != setsockopt(res.fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one))) {
75  err(1, "setsockopt");
76  }
77 
78  char buf[1024];
79  struct sockaddr_storage client_addr;
80  socklen_t addr_len = sizeof(client_addr);
81 
82  /* TODO:
83  * + nonblocking, with max timeout
84  * + set up multicast on 239.1.2.3
85  * + if we receive any info, print it */
86 
87  for (;;) {
88  ssize_t received = recvfrom(res.fd, buf, sizeof(buf), 0,
89  (struct sockaddr *)&client_addr, &addr_len);
90 
91  if (received > 0) {
92  buf[received] = '\0';
93  printf("Got: '%s'\n", buf);
94  /* TODO: sink into json, print decoded data. */
95  }
96  }
97 
98  return 0;
99 }
static int discover_service(void)
Definition: discovery.c:49
#define KINETIC_PORT
Default kinetic port.
Definition: kinetic_types.h:40
int main(int argc, char **argv)
Definition: discovery.c:37