Initial commit of multicast test sender/listener
authorKai Blin <kai@samba.org>
Sun, 15 May 2011 09:21:18 +0000 (11:21 +0200)
committerKai Blin <kai@samba.org>
Sun, 15 May 2011 09:21:18 +0000 (11:21 +0200)
.gitignore [new file with mode: 0644]
Makefile [new file with mode: 0644]
listener.c [new file with mode: 0644]
sender.c [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..85d0aaf
--- /dev/null
@@ -0,0 +1,4 @@
+*.o
+*.swp
+listener
+sender
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..2db1596
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,13 @@
+all: listener sender
+
+listener: listener.o
+       gcc -o listener listener.o
+
+sender: sender.o
+       gcc -o sender sender.o
+
+.c.o:
+       gcc -c -Wall -o $@ $<
+
+clean:
+       rm -f listener sender *.o
diff --git a/listener.c b/listener.c
new file mode 100644 (file)
index 0000000..9f77842
--- /dev/null
@@ -0,0 +1,50 @@
+/* Multicast test, listener */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int main(int argc, char **argv)
+{
+    int sock6;
+    struct sockaddr_in6 name6;
+    ssize_t len6 = sizeof(name6);
+    char buf[1024];
+    ssize_t msg_len;
+
+    sock6 = socket(AF_INET6, SOCK_DGRAM, 0);
+    if (sock6 == -1) {
+        perror("opening v6 listening socket");
+        exit(1);
+    }
+
+    memset(&name6, 0, len6);
+
+    name6.sin6_family = PF_INET6;
+    name6.sin6_addr = in6addr_any;
+    name6.sin6_port = htons(5355);
+
+    if (bind(sock6, (struct sockaddr *) &name6, len6) == -1) {
+        perror("binding to v6 socket");
+        exit(1);
+    }
+
+    msg_len = read(sock6, buf, 1024);
+
+    if (msg_len == -1) {
+        perror("receiving data");
+        exit(1);
+    }
+
+    buf[msg_len] = '\0';
+
+    printf("--> %s\n", buf);
+    close(sock6);
+    return 0;
+}
+
diff --git a/sender.c b/sender.c
new file mode 100644 (file)
index 0000000..c9fe972
--- /dev/null
+++ b/sender.c
@@ -0,0 +1,65 @@
+/* Multicast test, sender */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#define DATA "Mu..mu..multicast!"
+
+int main(int argc, char **argv)
+{
+    int err;
+    int sock6;
+    struct addrinfo hints, *ai, *pai;
+
+    if (argc < 2) {
+        fprintf(stderr, "Usage: %s <host>\n", argv[0]);
+        exit(2);
+    }
+
+    memset(&hints, 0, sizeof(struct addrinfo));
+    hints.ai_family = AF_INET6;
+    hints.ai_socktype = SOCK_DGRAM;
+    hints.ai_flags = 0;
+    hints.ai_protocol = 0;
+
+    err = getaddrinfo(argv[1], "5355", &hints, &ai);
+    if (err != 0) {
+        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
+        exit(1);
+    }
+
+    for (pai = ai; pai != NULL; pai = pai->ai_next) {
+        sock6 = socket(pai->ai_family, pai->ai_socktype, pai->ai_protocol);
+        if (sock6 == -1) {
+            continue;
+        }
+
+        if (connect(sock6, pai->ai_addr, pai->ai_addrlen) != -1) {
+            /* success, end loop */
+            break;
+        }
+
+        close(sock6);
+    }
+
+    if (pai == NULL) {
+        fprintf(stderr, "Could not connect to %s\n", argv[1]);
+        exit(1);
+    }
+
+    freeaddrinfo(ai);
+
+    if (write(sock6, DATA, strlen(DATA) + 1) != strlen(DATA) + 1) {
+        perror("writing " DATA);
+        exit(1);
+    }
+
+    close(sock6);
+    return 0;
+}
+