[syslinux:pathbased] pxe: move port allocation to a separate file; use dynamic port for DNS

syslinux-bot for H. Peter Anvin hpa at zytor.com
Sun Mar 7 21:09:11 PST 2010


Commit-ID:  d907fc879e20e3a4a2094b9b30c8badda6a8995b
Gitweb:     http://syslinux.zytor.com/commit/d907fc879e20e3a4a2094b9b30c8badda6a8995b
Author:     H. Peter Anvin <hpa at zytor.com>
AuthorDate: Sun, 7 Mar 2010 21:05:51 -0800
Committer:  H. Peter Anvin <hpa at zytor.com>
CommitDate: Sun, 7 Mar 2010 21:05:51 -0800

pxe: move port allocation to a separate file; use dynamic port for DNS

Move all local port number allocation to a separate file, and use that
API to allocate a dynamic port number for DNS queries.

Signed-off-by: H. Peter Anvin <hpa at zytor.com>


---
 core/fs/pxe/portnum.c |   68 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 68 insertions(+), 0 deletions(-)

diff --git a/core/fs/pxe/portnum.c b/core/fs/pxe/portnum.c
new file mode 100644
index 0000000..19af0cd
--- /dev/null
+++ b/core/fs/pxe/portnum.c
@@ -0,0 +1,68 @@
+/* ----------------------------------------------------------------------- *
+ *   
+ *   Copyright 2010 Intel Corporation; author: H. Peter Anvin
+ *
+ *   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, Inc., 51 Franklin St, Fifth Floor,
+ *   Boston MA 02110-1301, USA; either version 2 of the License, or
+ *   (at your option) any later version; incorporated herein by reference.
+ *
+ * ----------------------------------------------------------------------- */
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <netinet/in.h>
+#include "pxe.h"
+
+/* Port number bitmap - port numbers 49152 (0xc000) to 57343 (0xefff) */
+#define PORT_NUMBER_BASE	49152
+#define PORT_NUMBER_COUNT	8192 /* Power of 2, please */
+static uint32_t port_number_bitmap[PORT_NUMBER_COUNT/32];
+static uint16_t first_port_number /* = 0 */;
+
+/*
+ * Bitmap functions
+ */
+static bool test_bit(const uint32_t *bitmap, int32_t index)
+{
+    uint8_t st;
+    asm("btl %2,%1 ; setc %0" : "=qm" (st) : "m" (*bitmap), "r" (index));
+    return st;
+}
+
+static void set_bit(uint32_t *bitmap, int32_t index)
+{
+    asm volatile("btsl %1,%0" : "+m" (*bitmap) : "r" (index) : "memory");
+}
+
+static void clr_bit(uint32_t *bitmap, int32_t index)
+{
+    asm volatile("btcl %1,%0" : "+m" (*bitmap) : "r" (index) : "memory");
+}
+
+/*
+ * Get and free a port number (host byte order)
+ */
+uint16_t get_port(void)
+{
+    uint16_t port;
+
+    do {
+	port = first_port_number++;
+	first_port_number &= PORT_NUMBER_COUNT - 1;
+    } while (test_bit(port_number_bitmap, port));
+
+    set_bit(port_number_bitmap, port);
+    return htons(port + PORT_NUMBER_BASE);
+}
+
+void free_port(uint16_t port)
+{
+    port = ntohs(port) - PORT_NUMBER_BASE;
+
+    if (port >= PORT_NUMBER_COUNT)
+	return;
+
+    clr_bit(port_number_bitmap, port);
+}



More information about the Syslinux-commits mailing list