Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper function to validate interface name length #931

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ common_libswsscommon_la_SOURCES = \
common/zmqserver.cpp \
common/asyncdbupdater.cpp \
common/redis_table_waiter.cpp \
common/interface.h \
common/c-api/util.cpp \
common/c-api/dbconnector.cpp \
common/c-api/consumerstatetable.cpp \
Expand Down
19 changes: 19 additions & 0 deletions common/interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __INTERFACE__
#define __INTERFACE__

#include <string>
#include <net/if.h>

namespace swss
{

const size_t IFACE_NAME_MAX_LEN = IFNAMSIZ - 1;

bool isInterfaceNameValid(const std::string &ifaceName)
{
return !ifaceName.empty() && (ifaceName.length() < IFNAMSIZ);
}

}

#endif
4 changes: 3 additions & 1 deletion pyext/swsscommon.i
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include "zmqproducerstatetable.h"
#include <memory>
#include <functional>
#include "interface.h"
%}

%include <std_string.i>
Expand Down Expand Up @@ -282,6 +283,7 @@ T castSelectableObj(swss::Selectable *temp)
%include "zmqserver.h"
%include "zmqclient.h"
%include "zmqconsumerstatetable.h"
%include "interface.h"

%extend swss::DBConnector {
%template(hgetall) hgetall<std::map<std::string, std::string>>;
Expand All @@ -296,7 +298,7 @@ T castSelectableObj(swss::Selectable *temp)
%include "table.h"
#ifdef ENABLE_YANG_MODULES
%include "decoratortable.h"
#endif
#endif
%clear std::vector<std::string> &keys;
%clear std::vector<std::string> &ops;
%clear std::vector<std::vector<std::pair<std::string, std::string>>> &fvss;
Expand Down
8 changes: 8 additions & 0 deletions tests/test_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from swsscommon import swsscommon

def test_is_interface_name_valid():
invalid_interface_name = "TooLongInterfaceName"
assert not swsscommon.is_interface_name_valid(invalid_interface_name)

validInterfaceName = "OkInterfaceName"
assert swsscommon.is_interface_name_valid(validInterfaceName)
Loading