FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 02-13-2009, 12:01 AM
David Cantrell
 
Default Correct netmask conversion in iface_netmask2prefix()

Found by Hans de Goede <hdegoede@redhat.com>. The netmask to
prefix conversion code was not correct. Adapted this patch from
a patch Hans made to mkinitrd.
---
isys/iface.c | 12 +++++++-----
1 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/isys/iface.c b/isys/iface.c
index eb3a20b..2a67817 100644
--- a/isys/iface.c
+++ b/isys/iface.c
@@ -300,7 +300,8 @@ struct in_addr *iface_prefix2netmask(int prefix) {
* Convert an IPv4 netmask to an IPv4 CIDR prefix. Return -1 on failure.
*/
int iface_netmask2prefix(struct in_addr *netmask) {
- int ret = -1;
+ int prefix = 32;
+ unsigned int s_addr;
struct in_addr mask;

if (netmask == NULL) {
@@ -308,13 +309,14 @@ int iface_netmask2prefix(struct in_addr *netmask) {
}

memcpy(&mask, netmask, sizeof(struct in_addr));
+ s_addr = ntohl(mask.s_addr);

- while (mask.s_addr != 0) {
- mask.s_addr = mask.s_addr >> 1;
- ret++;
+ while (!(s_addr & 1)) {
+ s_addr >>= 1;
+ prefix--;
}

- return ret;
+ return prefix;
}

/*
--
1.6.1.3

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
 
Old 02-13-2009, 06:46 AM
Hans de Goede
 
Default Correct netmask conversion in iface_netmask2prefix()

David Cantrell wrote:

Found by Hans de Goede <hdegoede@redhat.com>. The netmask to
prefix conversion code was not correct. Adapted this patch from
a patch Hans made to mkinitrd.
---
isys/iface.c | 12 +++++++-----
1 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/isys/iface.c b/isys/iface.c
index eb3a20b..2a67817 100644
--- a/isys/iface.c
+++ b/isys/iface.c
@@ -300,7 +300,8 @@ struct in_addr *iface_prefix2netmask(int prefix) {
* Convert an IPv4 netmask to an IPv4 CIDR prefix. Return -1 on failure.
*/
int iface_netmask2prefix(struct in_addr *netmask) {
- int ret = -1;
+ int prefix = 32;
+ unsigned int s_addr;
struct in_addr mask;

if (netmask == NULL) {

@@ -308,13 +309,14 @@ int iface_netmask2prefix(struct in_addr *netmask) {
}

memcpy(&mask, netmask, sizeof(struct in_addr));

+ s_addr = ntohl(mask.s_addr);

- while (mask.s_addr != 0) {

- mask.s_addr = mask.s_addr >> 1;
- ret++;
+ while (!(s_addr & 1)) {
+ s_addr >>= 1;
+ prefix--;
}

- return ret;

+ return prefix;
}

/*


The local copy "netmask" of the passed in in_addr struct is no longer needed as
we no longer make any changes to the passed in argument. Also if you want to
make sure functions do not changes things they get passed by a ptr, you should
make the pointer const, so make the prototype:

int iface_netmask2prefix(const struct in_addr *netmask);

This will make the compiler check you do not accidentally still write to the
memory pointed to, and indicates that this function will not changes the passed
in struct to other programmers looking at the code and / or using the function.


Regards,

Hans

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
 
Old 02-16-2009, 10:45 PM
David Cantrell
 
Default Correct netmask conversion in iface_netmask2prefix()

Hans de Goede wrote:



David Cantrell wrote:

Found by Hans de Goede <hdegoede@redhat.com>. The netmask to
prefix conversion code was not correct. Adapted this patch from
a patch Hans made to mkinitrd.
---
isys/iface.c | 12 +++++++-----
1 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/isys/iface.c b/isys/iface.c
index eb3a20b..2a67817 100644
--- a/isys/iface.c
+++ b/isys/iface.c
@@ -300,7 +300,8 @@ struct in_addr *iface_prefix2netmask(int prefix) {
* Convert an IPv4 netmask to an IPv4 CIDR prefix. Return -1 on
failure.

*/
int iface_netmask2prefix(struct in_addr *netmask) {
- int ret = -1;
+ int prefix = 32;
+ unsigned int s_addr;
struct in_addr mask;

if (netmask == NULL) {

@@ -308,13 +309,14 @@ int iface_netmask2prefix(struct in_addr *netmask) {
}

memcpy(&mask, netmask, sizeof(struct in_addr));

+ s_addr = ntohl(mask.s_addr);

- while (mask.s_addr != 0) {

- mask.s_addr = mask.s_addr >> 1;
- ret++;
+ while (!(s_addr & 1)) {
+ s_addr >>= 1;
+ prefix--;
}

- return ret;

+ return prefix;
}

/*


The local copy "netmask" of the passed in in_addr struct is no longer
needed as we no longer make any changes to the passed in argument. Also
if you want to make sure functions do not changes things they get passed
by a ptr, you should make the pointer const, so make the prototype:

int iface_netmask2prefix(const struct in_addr *netmask);

This will make the compiler check you do not accidentally still write to
the memory pointed to, and indicates that this function will not changes
the passed in struct to other programmers looking at the code and / or
using the function.




Good point. I could be making better use of const, that's for sure.

But, even better than that, netmask2prefix() is no longer used, so I've
sent a patch to remove it.


--
David Cantrell <dcantrell@redhat.com>
Red Hat / Honolulu, HI

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list
 

Thread Tools




All times are GMT. The time now is 03:03 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org