Index: server.cxx
--- server.cxx.orig	2012-01-26 12:02:46.000000000 +0100
+++ server.cxx	2015-01-04 13:26:19.058471708 +0100
@@ -2,6 +2,7 @@
 #include <cstring>
 #include <iostream>
 #include <cstdlib>   
+#include <cstdio>   
 
 #ifndef WIN32
 #include <sys/time.h>
@@ -23,7 +24,7 @@
 usage()
 {
    cerr << "Usage: " << endl
-        << " ./server [-v] [-h] [-h IP_Address] [-a IP_Address] [-p port] [-o port] [-m mediaport]" << endl
+        << " ./server [-v] [-h] [-h IP_Address] [-a IP_Address] [-p port] [-o port] [-b] [-m mediaport] [-P pidfile]" << endl
         << " " << endl
         << " If the IP addresses of your NIC are 10.0.1.150 and 10.0.1.151, run this program with" << endl
         << "    ./server -v  -h 10.0.1.150 -a 10.0.1.151" << endl
@@ -32,7 +33,7 @@
         << "  -a sets the secondary IP" << endl
         << "  -p sets the primary port and defaults to 3478" << endl
         << "  -o sets the secondary port and defaults to 3479" << endl
-        << "  -b makes the program run in the backgroud" << endl
+        << "  -b makes the program run in the background" << endl
         << "  -m sets up a STERN server starting at port m" << endl
         << "  -v runs in verbose mode" << endl
       // in makefile too
@@ -55,6 +56,7 @@
    StunAddress4 altAddr;
    bool verbose=false;
    bool background=false;
+   char *myPidFile = 0;
    
    myAddr.addr = 0;
    altAddr.addr = 0;
@@ -135,6 +137,16 @@
          }
          myMediaPort = UInt16(strtol( argv[arg], NULL, 10));
       }
+      else if ( !strcmp( argv[arg] , "-P" ) )
+      {
+         arg++;
+         if ( argc <= arg ) 
+         {
+            usage();
+            exit(-1);
+         }
+         myPidFile = argv[arg];
+      }
       else
       {
          usage();
@@ -213,6 +225,19 @@
 
    if (pid == 0) //child or not using background
    {
+      /* write a daemon pidfile */
+      if (myPidFile) {
+          pid_t pid;
+          FILE *fp;
+          pid = getpid();
+          if ((fp = fopen(myPidFile, "w")) == NULL) {
+              fprintf(stderr, "stund: Can't write pidfile '%s'", myPidFile);
+              exit(1);
+          }
+          fprintf(fp, "%ld\n", (long)pid);
+          fclose(fp);
+      }
+
       StunServerInfo info;
       bool ok = stunInitServer(info, myAddr, altAddr, myMediaPort, verbose);
       
Index: stun.cxx
--- stun.cxx.orig	2012-01-26 12:02:46.000000000 +0100
+++ stun.cxx	2015-01-04 13:27:02.559527010 +0100
@@ -673,7 +673,7 @@
       asm("rdtsc" : "=A" (tick));
 #elif defined (__SUNPRO_CC) || defined( __sparc__ )	
       tick = gethrtime();
-#elif defined(__MACH__)  || defined(__linux)
+#elif defined(__MACH__)  || defined(__linux) || defined(__FreeBSD__)
       int fd=open("/dev/random",O_RDONLY);
       read(fd,&tick,sizeof(tick));
       closesocket(fd);
