server/: Introduce accessor functions for reading and writing port numbers.
[tripe] / server / servutil.c
index ed73e6d..b6fbf5b 100644 (file)
 
 octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
 
-/*----- Main code ---------------------------------------------------------*/
+/*----- Sequence numbers --------------------------------------------------*/
+
+/* --- @seq_reset@ --- *
+ *
+ * Arguments:  @seqwin *s@ = sequence-checking window
+ *
+ * Returns:    ---
+ *
+ * Use:                Resets a sequence number window.
+ */
+
+void seq_reset(seqwin *s) { s->seq = 0; s->win = 0; }
+
+/* --- @seq_check@ --- *
+ *
+ * Arguments:  @seqwin *s@ = sequence-checking window
+ *             @uint32 q@ = sequence number to check
+ *             @const char *service@ = service to report message from
+ *
+ * Returns:    Zero on success, nonzero if the sequence number was bad.
+ *
+ * Use:                Checks a sequence number against the window, updating things
+ *             as necessary.
+ */
+
+int seq_check(seqwin *s, uint32 q, const char *service)
+{
+  uint32 qbit;
+  uint32 n;
+
+  if (q < s->seq) {
+    a_warn(service, "replay", "old-sequence", A_END);
+    return (-1);
+  }
+  if (q >= s->seq + SEQ_WINSZ) {
+    n = q - (s->seq + SEQ_WINSZ - 1);
+    if (n < SEQ_WINSZ)
+      s->win >>= n;
+    else
+      s->win = 0;
+    s->seq += n;
+  }
+  qbit = 1 << (q - s->seq);
+  if (s->win & qbit) {
+    a_warn(service, "replay", "duplicated-sequence", A_END);
+    return (-1);
+  }
+  s->win |= qbit;
+  return (0);
+}
+
+/*----- Random odds and sods ----------------------------------------------*/
 
 /* --- @timestr@ --- *
  *
@@ -70,53 +121,69 @@ int mystrieq(const char *x, const char *y)
   }
 }
 
-/* --- @seq_reset@ --- *
+/*----- Address handling --------------------------------------------------*/
+
+const struct addrfam aftab[] = {
+#define DEF(af) { AF_##af, #af },
+  ADDRFAM(DEF)
+#undef DEF
+};
+
+/* --- @afix@ --- *
  *
- * Arguments:  @seqwin *s@ = sequence-checking window
+ * Arguments:  @int af@ = an address family code
  *
- * Returns:    ---
+ * Returns:    The index of the address family's record in @aftab@, or @-1@.
+ */
+
+int afix(int af)
+{
+  int i;
+
+  for (i = 0; i < NADDRFAM; i++)
+    if (af == aftab[i].af) return (i);
+  return (-1);
+}
+
+/* --- @addrsz@ --- *
  *
- * Use:                Resets a sequence number window.
+ * Arguments:  @const addr *a@ = a network address
+ *
+ * Returns:    The size of the address, for passing into the sockets API.
  */
 
-void seq_reset(seqwin *s) { s->seq = 0; s->win = 0; }
+socklen_t addrsz(const addr *a)
+{
+  switch (a->sa.sa_family) {
+    case AF_INET: return (sizeof(a->sin));
+    default: abort();
+  }
+}
 
-/* --- @seq_check@ --- *
+/* --- @getport@, @setport@ --- *
  *
- * Arguments:  @seqwin *s@ = sequence-checking window
- *             @uint32 q@ = sequence number to check
- *             @const char *service@ = service to report message from
+ * Arguments:  @addr *a@ = a network address
+ *             @unsigned port@ = port number to set
  *
- * Returns:    Zero on success, nonzero if the sequence number was bad.
+ * Returns:    ---
  *
- * Use:                Checks a sequence number against the window, updating things
- *             as necessary.
+ * Use:                Retrieves or sets the port number in an address structure.
  */
 
-int seq_check(seqwin *s, uint32 q, const char *service)
+unsigned getport(addr *a)
 {
-  uint32 qbit;
-  uint32 n;
-
-  if (q < s->seq) {
-    a_warn(service, "replay", "old-sequence", A_END);
-    return (-1);
-  }
-  if (q >= s->seq + SEQ_WINSZ) {
-    n = q - (s->seq + SEQ_WINSZ - 1);
-    if (n < SEQ_WINSZ)
-      s->win >>= n;
-    else
-      s->win = 0;
-    s->seq += n;
+  switch (a->sa.sa_family) {
+    case AF_INET: return (ntohs(a->sin.sin_port)); break;
+    default: abort();
   }
-  qbit = 1 << (q - s->seq);
-  if (s->win & qbit) {
-    a_warn(service, "replay", "duplicated-sequence", A_END);
-    return (-1);
+}
+
+void setport(addr *a, unsigned port)
+{
+  switch (a->sa.sa_family) {
+    case AF_INET: a->sin.sin_port = htons(port); break;
+    default: abort();
   }
-  s->win |= qbit;
-  return (0);
 }
 
 /*----- That's all, folks -------------------------------------------------*/