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
33NetCtl::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
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
72NetCtl::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 ALIGN(sizeof(u32)) 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}
Result
Result codes.
Definition Application.h:54
ArgumentParser & parser()
Get program arguments parser.
void setDescription(const String &desc)
Set program description.
Result registerPositional(const char *name, const char *description, Size count=1)
Register a positional argument.
static const String toString(const Address address)
Convert address to string.
Definition Ethernet.cpp:76
FileSystemClient provides a simple interface to a FileSystemServer.
FileSystemMount * getFileSystems(Size &numberOfMounts) const
Get file system mounts table.
static const String toString(const Address address)
Convert address to string.
Definition IPV4.cpp:82
u32 Address
IP-address.
Definition IPV4.h:47
Result showDevice(const char *deviceName)
Output device information.
Definition NetCtl.cpp:72
virtual Result initialize()
Initialize the application.
Definition NetCtl.cpp:44
NetCtl(int argc, char **argv)
Class constructor.
Definition NetCtl.cpp:33
virtual Result exec()
Execute the application event loop.
Definition NetCtl.cpp:50
virtual ~NetCtl()
Class destructor.
Definition NetCtl.cpp:40
POSIX-compatible application.
Abstraction of strings.
Definition String.h:42
C 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
C int open(const char *path, int oflag,...)
Open file relative to directory file descriptor.
Definition open.cpp:26
C int close(int fildes)
Close a file descriptor.
Definition close.cpp:22
#define O_RDONLY
Open for reading only.
Definition fcntl.h:81
C int printf(const char *format,...)
Output a formatted string to standard output.
Definition printf.cpp:22
C ssize_t read(int fildes, void *buf, size_t nbyte)
Read from a file.
Definition read.cpp:22
unsigned int u32
Unsigned 32-bit number.
Definition Types.h:53
unsigned int Size
Any sane size indicator cannot go negative.
Definition Types.h:128
#define ALIGN(n)
Aligns a symbol at the given boundary.
Definition Macros.h:167
#define DEBUG(msg)
Output a debug message to standard output.
Definition Log.h:89
Ethernet network address.
Definition Ethernet.h:53
Represents a mounted filesystem.