r4763 - in developers/werner: . owping
werner at docs.openmoko.org
werner at docs.openmoko.org
Fri Nov 7 02:15:34 CET 2008
Author: werner
Date: 2008-11-07 02:15:33 +0100 (Fri, 07 Nov 2008)
New Revision: 4763
Added:
developers/werner/owping/
developers/werner/owping/Makefile
developers/werner/owping/owping.c
developers/werner/owping/owping.h
developers/werner/owping/rx.c
developers/werner/owping/tx.c
Log:
One-way ping (UDP) to detect the sources of latency and jitter.
Added: developers/werner/owping/Makefile
===================================================================
--- developers/werner/owping/Makefile (rev 0)
+++ developers/werner/owping/Makefile 2008-11-07 01:15:33 UTC (rev 4763)
@@ -0,0 +1,34 @@
+CC=arm-angstrom-linux-gnueabi-gcc
+
+CFLAGS=-Wall -Wshadow -g -O
+
+PREFIX=/usr
+
+NAME=owping
+OBJS=owping.o rx.o tx.o
+
+.PHONY: all install uninstall clean depend spotless
+
+all: $(NAME)
+
+$(NAME): $(OBJS)
+
+install: $(NAME)
+ install -D $(NAME) $(PREFIX)/bin/$(NAME)
+
+uninstall:
+ rm -f $(PREFIX)/bin/$(NAME)
+
+depend:
+ $(CPP) $(CFLAGS) -MM -MG *.c >.depend || \
+ { rm -f .depend; exit 1; }
+
+ifeq (.depend,$(wildcard .depend))
+include .depend
+endif
+
+clean:
+ rm -f $(OBJS) .depend
+
+spotless: clean
+ rm -f $(NAME)
Added: developers/werner/owping/owping.c
===================================================================
--- developers/werner/owping/owping.c (rev 0)
+++ developers/werner/owping/owping.c 2008-11-07 01:15:33 UTC (rev 4763)
@@ -0,0 +1,52 @@
+/*
+ * owping.c - One-way ping (UDP)
+ *
+ * Copyright (C) 2008 by OpenMoko, Inc.
+ * Written by Werner Almesberger <werner at openmoko.org>
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <arpa/inet.h>
+
+#include "owping.h"
+
+
+#define PORT 23867
+
+
+static void usage(const char *name)
+{
+ fprintf(stderr, "usage: %s [ip_address]\n", name);
+ exit(1);
+}
+
+
+int main(int argc, char **argv)
+{
+ struct sockaddr_in addr;
+
+ switch (argc) {
+ case 1:
+ rx(PORT);
+ break;
+ case 2:
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = inet_addr(argv[1]);
+ addr.sin_port = htons(PORT);
+ if (addr.sin_addr.s_addr == (in_addr_t) -1)
+ usage(*argv);
+ tx(addr, 100);
+ break;
+ default:
+ usage(*argv);
+ }
+ return 0;
+}
Added: developers/werner/owping/owping.h
===================================================================
--- developers/werner/owping/owping.h (rev 0)
+++ developers/werner/owping/owping.h 2008-11-07 01:15:33 UTC (rev 4763)
@@ -0,0 +1,32 @@
+/*
+ * owping.h - One-way ping (UDP)
+ *
+ * Copyright (C) 2008 by OpenMoko, Inc.
+ * Written by Werner Almesberger <werner at openmoko.org>
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+
+#ifndef OWPING_H
+#define OWPING_H
+
+#include <stdint.h>
+#include <arpa/inet.h>
+
+
+struct owping {
+ uint32_t seq;
+ uint32_t tv_sec;
+ uint32_t tv_usec;
+};
+
+
+void tx(struct sockaddr_in addr, int num);
+void rx(int port);
+
+#endif /* OWPING_H */
Added: developers/werner/owping/rx.c
===================================================================
--- developers/werner/owping/rx.c (rev 0)
+++ developers/werner/owping/rx.c 2008-11-07 01:15:33 UTC (rev 4763)
@@ -0,0 +1,92 @@
+/*
+ * rx.c - One-way ping (UDP) receiver
+ *
+ * Copyright (C) 2008 by OpenMoko, Inc.
+ * Written by Werner Almesberger <werner at openmoko.org>
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/time.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <arpa/inet.h>
+
+#include "owping.h"
+
+
+static void delta(struct timeval t0, struct timeval t1)
+{
+ long us;
+
+ us = t1.tv_usec-t0.tv_usec;
+ us += (t1.tv_sec-t0.tv_sec)*1000000;
+ printf(" %ld.%03ldms", us/1000, us % 1000);
+}
+
+
+void rx(int port)
+{
+ struct sockaddr_in addr;
+ int s;
+
+ s = socket(PF_INET, SOCK_DGRAM, 0);
+ if (s < 0) {
+ perror("socket");
+ exit(1);
+ }
+
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = INADDR_ANY;
+ addr.sin_port = htons(port);
+ if (bind(s, (const struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ perror("bind");
+ exit(1);
+ }
+
+ // setsockopt timestamp
+
+ while (1) {
+ struct owping buf;
+ socklen_t addr_len;
+ ssize_t got;
+ struct timeval t_src, t_itf, t_user;
+
+ addr_len = sizeof(addr);
+ got = recvfrom(s, &buf, sizeof(buf), 0,
+ (struct sockaddr *) &addr, &addr_len);
+ if (got < 0) {
+ perror("recvmsg");
+ exit(1);
+ }
+ if (got == 1)
+ return;
+ if (got != sizeof(buf)) {
+ fprintf(stderr, "bad read: expected %u, got %u\n",
+ (unsigned) sizeof(buf), (unsigned) got);
+ exit(1);
+ }
+ if (gettimeofday(&t_user, NULL) < 0) {
+ perror("gettimeofday");
+ exit(1);
+ }
+ if (ioctl(s, SIOCGSTAMP, &t_itf) < 0) {
+ perror("ioctl(SIOCGSTAMP)");
+ exit(1);
+ }
+ printf("seq=%u from %s:", (unsigned) ntohl(buf.seq),
+ inet_ntoa(addr.sin_addr));
+ t_src.tv_sec = ntohl(buf.tv_sec);
+ t_src.tv_usec = ntohl(buf.tv_usec);
+ delta(t_src, t_itf);
+ delta(t_src, t_user);
+ putchar('\n');
+ }
+}
Added: developers/werner/owping/tx.c
===================================================================
--- developers/werner/owping/tx.c (rev 0)
+++ developers/werner/owping/tx.c 2008-11-07 01:15:33 UTC (rev 4763)
@@ -0,0 +1,76 @@
+/*
+ * tx.c - One-way ping (UDP) sender
+ *
+ * Copyright (C) 2008 by OpenMoko, Inc.
+ * Written by Werner Almesberger <werner at openmoko.org>
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+
+#include "owping.h"
+
+
+void tx(struct sockaddr_in addr, int num)
+{
+ ssize_t sent;
+ int s, seq;
+
+ s = socket(PF_INET, SOCK_DGRAM, 0);
+ if (s < 0) {
+ perror("socket");
+ exit(1);
+ }
+
+ if (connect(s, (const struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ perror("connect");
+ exit(1);
+ }
+
+ for (seq = 0; seq != num; seq++) {
+ struct owping buf;
+ struct timeval now;
+
+ if (seq)
+ sleep(1);
+ buf.seq = htonl(seq);
+ if (gettimeofday(&now, NULL) < 0) {
+ perror("gettimeofday");
+ exit(1);
+ }
+ buf.tv_sec = htonl(now.tv_sec);
+ buf.tv_usec = htonl(now.tv_usec);
+
+ sent = write(s, &buf, sizeof(buf));
+ if (sent < 0) {
+ perror("write");
+ exit(1);
+ }
+ if (sent != sizeof(buf)) {
+ fprintf(stderr, "bad write: %u < %u\n", (unsigned) sent,
+ (unsigned) sizeof(buf));
+ exit(1);
+ }
+ }
+
+ sent = write(s, "", 1);
+ if (sent < 0) {
+ perror("write");
+ exit(1);
+ }
+ if (sent != 1) {
+ fprintf(stderr, "bad write: %u < 1\n", (unsigned) sent);
+ exit(1);
+ }
+}
More information about the commitlog
mailing list