Skip to content

Commit

Permalink
Implement get/set IAID, DUID, DnsServers
Browse files Browse the repository at this point in the history
Change-Id: Ib3d8702dabfe49585eeb7f4be1de0ac84fa15f84
  • Loading branch information
Vinay Kulkarni authored and michellew-vmware committed May 18, 2016
1 parent bfba53c commit f5ed980
Show file tree
Hide file tree
Showing 11 changed files with 753 additions and 6 deletions.
9 changes: 9 additions & 0 deletions include/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef __CONSTANTS_H__
#define __CONSTANTS_H__

#define MAX_DUID_SIZE 128


#endif /* __CONSTANTS_H__ */


40 changes: 38 additions & 2 deletions include/public/netmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,49 @@ free_interface(

uint32_t
ifup(
const char* pszInterfaceName
const char *pszInterfaceName
);

uint32_t
ifdown(
const char* pszInterfaceName
const char * pszInterfaceName
);

int
set_iaid(
const char *pszInterfaceName,
const uint32_t iaid
);

int
get_iaid(
const char *pszInterfaceName,
uint32_t *iaid
);

int
set_duid(
const char *pszInterfaceName,
const char *pszDuid
);

int
get_duid(
const char *pszInterfaceName,
char *pszDuid
);

int
set_dns_servers(
const char *pszInterfaceName,
const char *pszDnsServers
);

int
get_dns_servers(
const char *pszInterfaceName,
char *pszDnsServers
);

#endif /* __NETMGR_H__ */

3 changes: 2 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ AM_CFLAGS += $(GLIB_CFLAGS)

libnetmgr_la_SOURCES = \
iniparser.c \
netmgr.c
netmgr.c \
utils.c

libnetmgr_la_LIBADD = \
@top_builddir@/common/libcommon.la \
Expand Down
12 changes: 12 additions & 0 deletions src/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@

#define IsNullOrEmptyString(str) (!(str) || !(*str))

#ifndef __DEFINES_H__
#define __DEFINES_H__


#define MAX_LINE 128

#define SYSTEMD_PATH "/etc/systemd/"
#define SYSTEMD_NET_PATH "/etc/systemd/network/"


#define bail_on_error(errcode) \
if (errcode) { \
goto error; \
}

#endif /* __DEFINES_H__ */
3 changes: 2 additions & 1 deletion src/includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
#include <sys/ioctl.h>
#include <net/if.h>

#include <constants.h>
#include <netmgmtsys.h>


#include <netmgr.h>
#include <iniparser.h>

#include "../common/prototypes.h"
#include "defines.h"
#include "structs.h"
#include "prototypes.h"
#include "utils.h"

7 changes: 7 additions & 0 deletions src/iniparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ ini_cfg_read(

if (!fgets(buffer, sizeof(buffer), fp))
{
if (feof(fp))
{
break;
}
err = errno;
bail_on_error(err);
}
Expand Down Expand Up @@ -292,6 +296,9 @@ ini_cfg_find_sections(
bail_on_error(err);
}

*pdwNumSections = 0;
*pppSections = NULL;

for (pCursor = pConfig->pSection; pCursor; pCursor = pCursor->pNext)
{
if (!strcmp(pCursor->pszName, pszName))
Expand Down
232 changes: 230 additions & 2 deletions src/netmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ free_interface(

uint32_t
ifup(
const char* pszInterfaceName
const char *pszInterfaceName
)
{
uint32_t err = 0;
Expand All @@ -131,7 +131,7 @@ ifup(

uint32_t
ifdown(
const char* pszInterfaceName
const char *pszInterfaceName
)
{
uint32_t err = 0;
Expand All @@ -148,3 +148,231 @@ ifdown(
goto cleanup;
}

int
set_iaid(
const char *pszInterfaceName,
const uint32_t iaid
)
{
uint32_t err = 0;
char cfgFileName[MAX_LINE];
const char szSectionName[] = "Link";
const char szKey[] = "IAID";
char szValue[MAX_LINE] = "";

if (!pszInterfaceName)
{
err = EINVAL;
bail_on_error(err);
}

sprintf(cfgFileName, "%s10-%s.network", SYSTEMD_NET_PATH, pszInterfaceName);
sprintf(szValue, "%u", iaid);

if (iaid > 0)
{
err = set_key_value(cfgFileName, szSectionName, szKey, szValue, 0);
}
else
{
err = set_key_value(cfgFileName, szSectionName, szKey, NULL, 0);
}

bail_on_error(err);

error:
return err;
}

int
get_iaid(
const char *pszInterfaceName,
uint32_t *iaid
)
{
uint32_t err = 0;
char cfgFileName[MAX_LINE];
const char szSectionName[] = "Link";
const char szKey[] = "IAID";
char szIaid[MAX_LINE];

if (!pszInterfaceName)
{
err = EINVAL;
bail_on_error(err);
}

sprintf(cfgFileName, "%s10-%s.network", SYSTEMD_NET_PATH, pszInterfaceName);

err = get_key_value(cfgFileName, szSectionName, szKey, szIaid);
bail_on_error(err);

sscanf(szIaid, "%u", iaid);

error:
return err;
}

int
set_duid(
const char *pszInterfaceName,
const char *pszDuid
)
{
uint32_t err = 0;
char cfgFileName[MAX_LINE];
const char szSectionName[] = "DUID";
const char szKey[] = "RawData";

if (pszInterfaceName != NULL)
{
/* TODO: Add support */
err = ENOTSUP;
bail_on_error(err);
}
else
{
sprintf(cfgFileName, "%snetworkd.conf", SYSTEMD_PATH);
}

if (strlen(pszDuid) == 0)
{
err = set_key_value(cfgFileName, szSectionName, szKey, NULL, 0);
}
else
{
err = set_key_value(cfgFileName, szSectionName, szKey, pszDuid, 0);
}
bail_on_error(err);

error:
return err;
}

int
get_duid(
const char *pszInterfaceName,
char *pszDuid
)
{

uint32_t err = 0;
char cfgFileName[MAX_LINE];
const char szSectionName[] = "DUID";
const char szKey[] = "RawData";

if (pszInterfaceName != NULL)
{
/* TODO: Add support */
err = ENOTSUP;
bail_on_error(err);
}
else
{
sprintf(cfgFileName, "%snetworkd.conf", SYSTEMD_PATH);
}

err = get_key_value(cfgFileName, szSectionName, szKey, pszDuid);
bail_on_error(err);

error:
return err;
}

int
set_dns_servers(
const char *pszInterfaceName,
const char *pszDnsServers
)
{
uint32_t err = 0;
char cfgFileName[MAX_LINE];
char szSectionName[MAX_LINE];
char szKey[MAX_LINE] = "DNS";
char szValue[MAX_LINE];
DIR *dirFile = NULL;
struct dirent *hFile;

if (pszInterfaceName != NULL)
{
sprintf(cfgFileName, "%s10-%s.network", SYSTEMD_NET_PATH, pszInterfaceName);
sprintf(szSectionName, "Network");
}
else
{
sprintf(cfgFileName, "%sresolved.conf", SYSTEMD_PATH);
sprintf(szSectionName, "Resolve");
}

if (strlen(pszDnsServers) == 0)
{
sprintf(szValue, "true");
err = set_key_value(cfgFileName, szSectionName, szKey, NULL, 0);
}
else
{
sprintf(szValue, "false");
err = set_key_value(cfgFileName, szSectionName, szKey, pszDnsServers, 0);
}
bail_on_error(err);

/* For each .network file - set 'UseDNS=false' */
if (pszInterfaceName == NULL)
{
dirFile = opendir(SYSTEMD_NET_PATH);
if (dirFile != NULL)
{
errno = 0;
sprintf(szSectionName, "DHCP");
sprintf(szKey, "UseDNS");
while ((hFile = readdir(dirFile)) != NULL)
{
if (!strcmp(hFile->d_name, ".")) continue;
if (!strcmp(hFile->d_name, "..")) continue;
if (hFile->d_name[0] == '.') continue;
if (strstr(hFile->d_name, ".network"))
{
sprintf(cfgFileName, "%s%s", SYSTEMD_NET_PATH, hFile->d_name);
err = set_key_value(cfgFileName, szSectionName, szKey, szValue, 0);
bail_on_error(err);
}
}
}
}

error:
if (dirFile != NULL)
{
closedir(dirFile);
}
return err;
}

int
get_dns_servers(
const char *pszInterfaceName,
char *pszDnsServers
)
{
uint32_t err = 0;
char cfgFileName[MAX_LINE];
char szSectionName[MAX_LINE];
char szKey[MAX_LINE] = "DNS";

if (pszInterfaceName != NULL)
{
sprintf(cfgFileName, "%s10-%s.network", SYSTEMD_NET_PATH, pszInterfaceName);
sprintf(szSectionName, "Network");
}
else
{
sprintf(cfgFileName, "%sresolved.conf", SYSTEMD_PATH);
sprintf(szSectionName, "Resolve");
}

err = get_key_value(cfgFileName, szSectionName, szKey, pszDnsServers);
bail_on_error(err);

error:
return err;
}
Loading

0 comments on commit f5ed980

Please sign in to comment.