[ipxe-devel] How to determine if IP belongs to specified network range (IPXE script)?

Robert Socha socha at socha.it
Sun Oct 11 10:23:07 UTC 2015


Hello,

First - IPXE is great!
Thank you for your so good job.

I 'm looking for a way to determine whether the IP number belongs to a
particular network (from a IPXE script).

For example in pseudo code:

if ( IP in 192.168.1.0/24) && goto dosomething

I needed a ASAP solution so I created a simple patch (attached below).

Is this a correct solution? Is there any way to do so with the existing
IPXE commands ?

Regards

diff --git a/src/core/exec.c b/src/core/exec.c
index 2c2ade0..cb10d28 100644
--- a/src/core/exec.c
+++ b/src/core/exec.c
@@ -41,6 +41,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 #include <ipxe/process.h>
 #include <ipxe/nap.h>
 #include <ipxe/shell.h>
+#include <ipxe/in.h>

 /** @file
  *
@@ -603,3 +604,48 @@ struct command sleep_command __command = {
        .name = "sleep",
        .exec = sleep_exec,
 };
+
+/** "isnetwork" options */
+struct isnetwork_options {};
+
+/** "isnetwork" option list */
+static struct option_descriptor isnetwork_opts[] = {};
+
+/** "isnetwork" command descriptor */
+static struct command_descriptor isnetwork_cmd =
+       COMMAND_DESC ( struct isnetwork_options, isnetwork_opts, 3, 3,
+                      "<source> <network> <netmask>" );
+
+/**
+ * "isnetwork" command
+ *
+ * @v argc             Argument count
+ * @v argv             Argument list
+ * @ret rc             Return status code
+ */
+static int isnetwork_exec ( int argc, char **argv ) {
+       struct isnetwork_options opts;
+       struct in_addr source,network,netmask;
+       int rc;
+
+       /* Parse options */
+       if ( ( rc = parse_options ( argc, argv, &isnetwork_cmd, &opts )
) != 0 )
+               return rc;
+
+       if ( inet_aton(argv[optind],&source) == 0 )
+               return -EINVAL;
+
+       if ( inet_aton(argv[optind+1],&network) == 0 )
+               return -EINVAL;
+
+       if ( inet_aton(argv[optind+2],&netmask) == 0 )
+               return -EINVAL;
+
+       return  !( ( source.s_addr & netmask.s_addr ) == network.s_addr );
+}
+
+/** "isnetwork" command */
+struct command isnetwork_command __command = {
+       .name = "isnetwork",
+       .exec = isnetwork_exec,
+};

-- 
rjs


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3781 bytes
Desc: Kryptograficzna sygnatura S/MIME
URL: <http://lists.ipxe.org/pipermail/ipxe-devel/attachments/20151011/22f7d6cc/attachment.p7s>


More information about the ipxe-devel mailing list