tool/CommandIp.cpp
author Philipp Weyer <pw@igh-essen.com>
Fri, 18 Aug 2017 12:30:16 +0200
changeset 2679 33b922ec1871
parent 2597 0e145bb05859
permissions -rw-r--r--
Fixed lib include paths
2597
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  $Id$
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  Copyright (C) 2006-2014  Florian Pose, Ingenieurgemeinschaft IgH
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  This file is part of the IgH EtherCAT Master.
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it and/or
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
 *  modify it under the terms of the GNU General Public License version 2, as
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  published by the Free Software Foundation.
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  Public License for more details.
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *  You should have received a copy of the GNU General Public License along
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *  with the IgH EtherCAT Master; if not, write to the Free Software
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *  ---
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *  The license mentioned above concerns the source code only. Using the
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *  EtherCAT technology and brand is only permitted in compliance with the
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 *  industrial property and similar rights of Beckhoff Automation GmbH.
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 *  vim: expandtab
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
 *
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
 ****************************************************************************/
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
#include <sys/types.h>
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
#include <sys/socket.h>
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
#include <netdb.h>
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#include <iostream>
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
#include <algorithm>
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
using namespace std;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
#include "CommandIp.h"
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
#include "MasterDevice.h"
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
/*****************************************************************************/
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
CommandIp::CommandIp():
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
    Command("ip", "Set EoE IP parameters.")
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
{
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
}
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
/*****************************************************************************/
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
string CommandIp::helpString(const string &binaryBaseName) const
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
{
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
    stringstream str;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
    str << binaryBaseName << " " << getName() << " [OPTIONS] <ARGS>" << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
        << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
        << getBriefDescription() << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
        << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
        << "This command requires a single slave to be selected." << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
        << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
        << "IP parameters can be appended as argument pairs:" << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
        << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
        << "  addr <IPv4>[/prefix]  IP address (optionally with" << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
        << "                        decimal subnet prefix)" << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
        << "  link <MAC>            Link-layer address (may contain" << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
        << "                        colons or hyphens)" << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
        << "  default <IPv4>        Default gateway" << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
        << "  dns <IPv4>            DNS server" << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
        << "  name <hostname>       Host name (max. 32 byte)" << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
        << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
        << "IPv4 adresses can be given either in dot notation or as" << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
        << "hostnames, which will be automatically resolved." << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
        << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
        << "Command-specific options:" << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
        << "  --alias    -a <alias>" << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
        << "  --position -p <pos>    Slave selection. See the help of" << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
        << "                         the 'slaves' command." << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
        << endl
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
        << numericInfo();
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
    return str.str();
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
}
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
/****************************************************************************/
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
void CommandIp::execute(const StringVector &args)
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
{
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
    if (args.size() <= 0) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
        return;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
    }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
    if (args.size() % 2) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
        stringstream err;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
        err << "'" << getName() << "' needs an even number of arguments!";
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
        throwInvalidUsageException(err);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
    }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
    ec_ioctl_slave_eoe_ip_t io = {};
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
    for (unsigned int argIdx = 0; argIdx < args.size(); argIdx += 2) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
        string arg = args[argIdx];
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
        string val = args[argIdx + 1];
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
        std::transform(arg.begin(), arg.end(), arg.begin(), ::tolower);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
        if (arg == "link") {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
            parseMac(io.mac_address, val);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
            io.mac_address_included = 1;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
        }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
        else if (arg == "addr") {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
            parseIpv4Prefix(&io, val);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
            io.ip_address_included = 1;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
        }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
        else if (arg == "default") {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
            resolveIpv4(&io.gateway, val);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
            io.gateway_included = 1;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
        }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
        else if (arg == "dns") {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
            resolveIpv4(&io.dns, val);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
            io.dns_included = 1;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
        }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
        else if (arg == "name") {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
            if (val.size() > EC_MAX_HOSTNAME_SIZE - 1) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
                stringstream err;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
                err << "Name too long!";
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
                throwInvalidUsageException(err);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
            }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
            unsigned int i;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
            for (i = 0; i < val.size(); i++) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
                io.name[i] = val[i];
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
            }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
            io.name[i] = 0;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
            io.name_included = 1;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
        }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
        else {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
            stringstream err;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
            err << "Unknown argument '" << args[argIdx] << "'!";
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
            throwInvalidUsageException(err);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
        }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
    }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
    MasterDevice m(getSingleMasterIndex());
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   143
    m.open(MasterDevice::ReadWrite);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
    SlaveList slaves = selectedSlaves(m);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
    if (slaves.size() != 1) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
        throwSingleSlaveRequired(slaves.size());
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
    }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
    io.slave_position = slaves.front().position;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
    // execute actual request
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
    try {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
        m.setIpParam(&io);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
    } catch (MasterDeviceException &e) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
        throw e;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
    }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   156
}
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   157
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   158
/*****************************************************************************/
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
void CommandIp::parseMac(unsigned char mac[EC_ETH_ALEN], const string &str)
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   161
{
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
    unsigned int pos = 0;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
    for (unsigned int i = 0; i < EC_ETH_ALEN; i++) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
        if (pos + 2 > str.size()) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   166
            stringstream err;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   167
            err << "Incomplete MAC address!";
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   168
            throwInvalidUsageException(err);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
        }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   171
        string byteStr = str.substr(pos, 2);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   172
        pos += 2;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   173
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
        stringstream s;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   175
        s << byteStr;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
        unsigned int byteValue;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   177
        s >> hex >> byteValue;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   178
        if (s.fail() || !s.eof() || byteValue > 0xff) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   179
            stringstream err;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   180
            err << "Invalid MAC address!";
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   181
            throwInvalidUsageException(err);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   182
        }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
        mac[i] = byteValue;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
        while (pos < str.size() && (str[pos] == ':' || str[pos] == '-')) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
            pos++;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
        }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
    }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
}
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   190
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   191
/*****************************************************************************/
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   192
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
void CommandIp::parseIpv4Prefix(ec_ioctl_slave_eoe_ip_t *io,
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
        const string &str)
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
{
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
    size_t pos = str.find('/');
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
    string host;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   198
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   199
    io->subnet_mask_included = pos != string::npos;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
    if (pos == string::npos) { // no prefix found
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
        host = str;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
    }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
    else {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
        host = str.substr(0, pos);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
        string prefixStr = str.substr(pos + 1, string::npos);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
        stringstream s;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   208
        s << prefixStr;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
        unsigned int prefix;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
        s >> prefix;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
        if (s.fail() || !s.eof() || prefix > 32) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
            stringstream err;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
            err << "Invalid prefix '" << prefixStr << "'!";
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
            throwInvalidUsageException(err);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
        }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
        uint32_t mask = 0;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   217
        for (unsigned int bit = 0; bit < prefix; bit++) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   218
            mask |= (1 << (31 - bit));
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   219
        }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
        io->subnet_mask = htonl(mask);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   221
    }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   223
    resolveIpv4(&io->ip_address, host);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   224
}
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   225
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   226
/*****************************************************************************/
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   227
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   228
void CommandIp::resolveIpv4(uint32_t *addr, const string &str)
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   229
{
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   230
    struct addrinfo hints = {};
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   231
    struct addrinfo *res;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   232
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   233
    hints.ai_family = AF_INET; // only IPv4
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   234
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   235
    int ret = getaddrinfo(str.c_str(), NULL, &hints, &res);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   236
    if (ret) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   237
        stringstream err;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   238
        err << "Lookup of '" << str << "' failed: "
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   239
            << gai_strerror(ret) << endl;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   240
        throwCommandException(err.str());
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   241
    }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   242
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   243
    if (!res) { // returned list is empty
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   244
        stringstream err;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   245
        err << "Lookup of '" << str << "' failed." << endl;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   246
        throwCommandException(err.str());
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   247
    }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   248
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   249
    sockaddr_in *sin = (sockaddr_in *) res->ai_addr;
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   250
    for (unsigned int i = 0; i < 4; i++) {
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   251
        ((unsigned char *) addr)[i] =
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   252
            ((unsigned char *) &sin->sin_addr.s_addr)[i];
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   253
    }
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   254
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
    freeaddrinfo(res);
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   256
}
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   257
0e145bb05859 Implemented EoE Set IP parameter request via command-line tool.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   258
/****************************************************************************/