From c979dbefeb45b358c812b2be99d89c0e8d9b9b78 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 5 Mar 2009 12:26:22 -0800 Subject: [PATCH] socket-util: New function guess_netmask(). --- lib/socket-util.c | 12 +++++++++++- lib/socket-util.h | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/socket-util.c b/lib/socket-util.c index d8ae179b..d0ff0566 100644 --- a/lib/socket-util.c +++ b/lib/socket-util.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford +/* Copyright (c) 2008, 2009 The Board of Trustees of The Leland Stanford * Junior University * * We are making the OpenFlow specification and associated documentation @@ -305,3 +305,13 @@ get_unix_name_len(socklen_t sun_len) ? sun_len - offsetof(struct sockaddr_un, sun_path) : 0); } + +uint32_t +guess_netmask(uint32_t ip) +{ + ip = ntohl(ip); + return ((ip >> 31) == 0 ? htonl(0xff000000) /* Class A */ + : (ip >> 30) == 2 ? htonl(0xffff0000) /* Class B */ + : (ip >> 29) == 6 ? htonl(0xffffff00) /* Class C */ + : htonl(0)); /* ??? */ +} diff --git a/lib/socket-util.h b/lib/socket-util.h index 3816c3c4..fd38a494 100644 --- a/lib/socket-util.h +++ b/lib/socket-util.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford +/* Copyright (c) 2008, 2009 The Board of Trustees of The Leland Stanford * Junior University * * We are making the OpenFlow specification and associated documentation @@ -48,5 +48,6 @@ void drain_fd(int fd, size_t n_packets); int make_unix_socket(int style, bool nonblock, bool passcred, const char *bind_path, const char *connect_path); int get_unix_name_len(socklen_t sun_len); +uint32_t guess_netmask(uint32_t ip); #endif /* socket-util.h */ -- 2.30.2