chiark / gitweb /
Actual forwarder program compiles apparently ok.
[userv-utils.git] / ipif / utils.c
diff --git a/ipif/utils.c b/ipif/utils.c
new file mode 100644 (file)
index 0000000..c215390
--- /dev/null
@@ -0,0 +1,142 @@
+/*
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "forwarder.h"
+
+const char *const *argv;
+char programid[SYS_NMLN+sizeof(PROGRAM)+3];
+
+void arg_assert_fail(const char *msg) {
+  fprintf(stderr, PROGRAM ": argument error (!`%s')\n",msg);
+  exit(12);
+}
+
+void sysfail(const char *msg) {
+  fprintf(stderr, "%s: fatal system error: %s: %s\n", programid, msg, strerror(errno));
+  exit(8);
+}
+
+void fail(const char *msg) {
+  fprintf(stderr, "%s: fatal error: %s\n", programid, msg);
+  exit(4);
+}
+
+void sysdiag(const char *msg) {
+  fprintf(stderr, "%s: system/network error: %s: %s\n", programid, msg, strerror(errno));
+}
+
+void diag(const char *msg) {
+  fprintf(stderr, "%s: %s\n", programid, msg);
+}
+
+time_t now(void) {
+  time_t r;
+  if (time(&r) == (time_t)-1) sysfail("get time of day");
+  return r;
+}
+
+void *xmalloc(size_t sz) {
+  void *r;
+  r= malloc(sz);
+  if (!r) sysfail("allocate memory");
+  return r;
+}
+
+void write_must(int fd, const void *p_in, int sz, const char *what) {
+  const unsigned char *p= p_in;
+  int r;
+  
+  while (sz) {
+    r= write(fd,p,sz);
+    if (r<0) {
+      if (errno == EINTR) continue;
+      else sysfail(what);
+    }
+    assert(r && r <= sz);
+    p += r;
+    sz -= r;
+  }
+}
+
+void read_must(int fd, void *p_in, int sz, const char *what) {
+  unsigned char *p= p_in;
+  int r;
+  
+  while (sz) {
+    r= read(fd,p,sz);
+    if (r<0) {
+      if (errno == EINTR) continue;
+      else sysfail(what);
+    }
+    if (r==0) fail(what);
+    assert(r <= sz);
+    p += r;
+    sz -= r;
+  }
+}
+
+void get_random(void *ptr, size_t sz) {
+  static FILE *randfile;
+
+  size_t r;
+
+  if (!randfile) {
+    randfile= fopen("/dev/urandom","rb");
+    if (!randfile && errno==ENOENT) randfile= fopen("/dev/random","rb");
+    if (!randfile) sysfail("open random number generator");
+  }
+
+  r= fread(ptr,1,sz,randfile);
+  if (r == sz) return;
+  (ferror(randfile) ? sysfail : fail)("cannot read random number generator");
+}
+
+const char *getarg_string(void) {
+  const char *arg;
+  
+  arg= *++argv;
+  arg_assert(arg);
+  return arg;
+}
+
+unsigned long getarg_ulong(void) {
+  char *ep;
+  unsigned long ul;
+  
+  ul= strtoul(getarg_string(),&ep,0);
+  arg_assert(!*ep);
+  return ul;
+}
+
+void *buf_append(struct buffer *buf, size_t amount) {
+  void *p;
+
+  p= buf->start + buf->size;
+  buf->size += amount;
+  return p;
+}
+  
+void *buf_prepend(struct buffer *buf, size_t amount) {
+  buf->size += amount;
+  return buf->start -= amount;
+}
+
+void *buf_unappend(struct buffer *buf, size_t amount) {
+  if (buf->size < amount) return 0;
+  return buf->start + (buf->size -= amount);
+}
+
+void *buf_unprepend(struct buffer *buf, size_t amount) {
+  void *p;
+
+  p= buf->start;
+  buf->start += amount;
+  buf->size -= amount;
+  return p;
+}