FreeNOS
NetCtl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <MemoryBlock.h>
25 #include <NetworkClient.h>
26 #include <NetworkSocket.h>
27 #include <IPV4.h>
28 #include <ICMP.h>
29 #include <Ethernet.h>
30 #include <FileSystemClient.h>
31 #include "NetCtl.h"
32 
33 NetCtl::NetCtl(int argc, char **argv)
34  : POSIXApplication(argc, argv)
35 {
36  parser().setDescription("control network devices");
37  parser().registerPositional("ARGS", "optional key=value arguments", 0);
38 }
39 
41 {
42 }
43 
45 {
46  DEBUG("");
47  return Success;
48 }
49 
51 {
52  Size numberOfMounts = 0;
53 
54  DEBUG("");
55 
56  // Make a list of network devices
57  // Get a list of mounts
58  FileSystemClient filesystem;
59  FileSystemMount *mounts = filesystem.getFileSystems(numberOfMounts);
60 
61  // Find closest matching device
62  for (Size i = 0; i < numberOfMounts; i++)
63  {
64  if (mounts[i].path[0] && strncmp(mounts[i].path, "/network/", 9) == 0)
65  {
66  showDevice(mounts[i].path + 9);
67  }
68  }
69  return Success;
70 }
71 
72 NetCtl::Result NetCtl::showDevice(const char *deviceName)
73 {
74  DEBUG("");
75 
76  String ipv4, ether, out;
77  ether << "/network/" << deviceName << "/ethernet/address";
78  ipv4 << "/network/" << deviceName << "/ipv4/address";
79  out << deviceName << " ipv4 ";
80 
81  // read the ipv4/address file
82  int fd = open(*ipv4, O_RDONLY);
83  int r;
84 
85  if (fd != -1)
86  {
87  IPV4::Address ipAddr;
88 
89  r = read(fd, &ipAddr, sizeof(ipAddr));
90  if (r != -1)
91  {
92  out << IPV4::toString(ipAddr);
93  }
94  close(fd);
95  }
96  out << " ether ";
97 
98  fd = open(*ether, O_RDONLY);
99  if (fd != -1)
100  {
101  Ethernet::Address etherAddress;
102 
103  r = read(fd, &etherAddress, sizeof(etherAddress));
104  if (r != -1)
105  {
106  out << Ethernet::toString(etherAddress);
107  }
108  close(fd);
109  }
110 
111  printf("%s\r\n", *out);
112  return Success;
113 }
strncmp
int strncmp(const char *dest, const char *src, size_t count)
Compare two strings, by only a maximum number of bytes.
Definition: strncmp.cpp:20
fcntl.h
string.h
FileSystemMount
Represents a mounted filesystem.
Definition: FileSystemMount.h:35
String
Abstraction of strings.
Definition: String.h:41
Ethernet.h
FileSystemClient
FileSystemClient provides a simple interface to a FileSystemServer.
Definition: FileSystemClient.h:42
POSIXApplication
POSIX-compatible application.
Definition: POSIXApplication.h:35
Application::Success
@ Success
Definition: Application.h:55
MemoryBlock.h
open
int open(const char *path, int oflag,...)
Open file relative to directory file descriptor.
Definition: open.cpp:26
ArgumentParser::setDescription
void setDescription(const String &desc)
Set program description.
Definition: ArgumentParser.cpp:95
read
ssize_t read(int fildes, void *buf, size_t nbyte)
Read from a file.
Definition: read.cpp:22
NetCtl::exec
virtual Result exec()
Execute the application event loop.
Definition: NetCtl.cpp:50
DEBUG
#define DEBUG(msg)
Output a debug message to standard output.
Definition: Log.h:89
Ethernet::Address
Ethernet network address.
Definition: Ethernet.h:52
printf
int printf(const char *format,...)
Output a formatted string to standard output.
Definition: printf.cpp:22
close
int close(int fildes)
Close a file descriptor.
Definition: close.cpp:22
NetCtl::NetCtl
NetCtl(int argc, char **argv)
Class constructor.
Definition: NetCtl.cpp:33
stdio.h
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
Application::Result
Result
Result codes.
Definition: Application.h:53
FileSystemClient::getFileSystems
FileSystemMount * getFileSystems(Size &numberOfMounts) const
Get file system mounts table.
Definition: FileSystemClient.cpp:344
ICMP.h
IPV4::Address
u32 Address
IP-address.
Definition: IPV4.h:47
IPV4.h
ArgumentParser::registerPositional
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
Definition: ArgumentParser.cpp:119
IPV4::toString
static const String toString(const Address address)
Convert address to string.
Definition: IPV4.cpp:82
unistd.h
NetCtl::showDevice
Result showDevice(const char *deviceName)
Output device information.
Definition: NetCtl.cpp:72
Application::parser
ArgumentParser & parser()
Get program arguments parser.
Definition: Application.cpp:102
FileSystemClient.h
Ethernet::toString
static const String toString(const Address address)
Convert address to string.
Definition: Ethernet.cpp:76
NetCtl::~NetCtl
virtual ~NetCtl()
Class destructor.
Definition: NetCtl.cpp:40
NetCtl::initialize
virtual Result initialize()
Initialize the application.
Definition: NetCtl.cpp:44
stdlib.h
NetworkClient.h
NetCtl.h
O_RDONLY
#define O_RDONLY
Open for reading only.
Definition: fcntl.h:81
NetworkSocket.h
errno.h