Add transaction support to Set Lan Cmd and Set Channel Access Cmd

* Ipmid will cache single setting to memory by Set Lan cmd
* Add Set Channel Access cmd to ipmid
* Set Channel Access cmd is used to apply the previous cached settings

TODO: parse the request data of Set Channel Access cmd if needed.

Resolves openbmc/openbmc#444

Change-Id: Ia7ac57a551731920ffe69dad5c69c995521fed02
Signed-off-by: Nan Li <william.bjlinan@hotmail.com>
diff --git a/apphandler.cpp b/apphandler.cpp
index 9d94b1b..431cbf0 100644
--- a/apphandler.cpp
+++ b/apphandler.cpp
@@ -7,9 +7,15 @@
 #include <systemd/sd-bus.h>
 #include <mapper.h>
 #include <array>
+#include <arpa/inet.h>
+#include "transporthandler.h"
 
 extern sd_bus *bus;
 
+constexpr auto app_obj = "/org/openbmc/NetworkManager/Interface";
+constexpr auto app_ifc = "org.openbmc.NetworkManager";
+constexpr auto app_nwinterface = "eth0";
+
 void register_netfn_app_functions() __attribute__((constructor));
 
 // Offset in get device id command.
@@ -535,6 +541,117 @@
     return rc;
 }
 
+extern struct channel_config_t channel_config;
+
+ipmi_ret_t ipmi_set_channel_access(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
+                             ipmi_request_t request, ipmi_response_t response,
+                             ipmi_data_len_t data_len, ipmi_context_t context)
+{
+    ipmi_ret_t rc = IPMI_CC_OK;
+
+    sd_bus *bus = ipmid_get_sd_bus_connection();
+    sd_bus_message *reply = nullptr;
+    sd_bus_error error = SD_BUS_ERROR_NULL;
+    int r = 0;
+    char *app = nullptr;
+    int family = 0;
+    unsigned char prefixlen = 0;
+    char* ipaddr = nullptr;
+    uint32_t mask = 0xFFFFFFFF;
+    char* gateway = nullptr;
+    char tmp_netmask[INET_ADDRSTRLEN];
+
+    // Todo: parse the request data if needed.
+
+    // Using Set Channel cmd to apply changes of Set Lan Cmd.
+
+    r = mapper_get_service(bus, app_obj, &app);
+    if (r < 0) {
+        fprintf(stderr, "Failed to get %s bus name: %s\n",
+                app_obj, strerror(-r));
+        rc = IPMI_CC_UNSPECIFIED_ERROR;
+        goto finish;
+    }
+
+    r = sd_bus_call_method(bus, app, app_obj, app_ifc, "GetAddress4", &error,
+                            &reply, "s", app_nwinterface);
+    if (r < 0) {
+        fprintf(stderr, "Failed to call Get Method: %s\n", strerror(-r));
+        rc = IPMI_CC_UNSPECIFIED_ERROR;
+        goto finish;
+    }
+
+    r = sd_bus_message_read(reply, "iyss",
+                            &family, &prefixlen, &ipaddr, &gateway);
+    if (r < 0) {
+        fprintf(stderr, "Failed to get a response: %s\n", strerror(-r));
+        rc = IPMI_CC_RESPONSE_ERROR;
+        goto finish;
+    }
+
+    printf("N/W data from Cache: %s:%s:%s\n",
+            channel_config.new_ipaddr.c_str(),
+            channel_config.new_netmask.c_str(),
+            channel_config.new_gateway.c_str());
+
+    if(channel_config.new_ipaddr.empty()) {
+        channel_config.new_ipaddr.assign(ipaddr);
+    }
+
+    if(channel_config.new_netmask.empty()) {
+        mask = htonl(mask<<(32-prefixlen));
+        uint8_t* p = (uint8_t*)&mask;
+
+        snprintf(tmp_netmask, INET_ADDRSTRLEN, "%d.%d.%d.%d",
+            *p, *(p+1), *(p+2), *(p+3));
+        channel_config.new_netmask.assign(tmp_netmask);
+    }
+
+    if(channel_config.new_gateway.empty()) {
+        channel_config.new_gateway.assign(gateway);
+    }
+
+    printf("N/W data from HW %s:%d:%s:%s\n",
+            family==AF_INET?"IPv4":"IPv6", prefixlen, ipaddr,gateway);
+    printf("N/W data from Cache: %s:%s:%s\n",
+            channel_config.new_ipaddr.c_str(),
+            channel_config.new_netmask.c_str(),
+            channel_config.new_gateway.c_str());
+
+    r = sd_bus_call_method(bus,            // On the System Bus
+                            app,            // Service to contact
+                            app_obj,            // Object path
+                            app_ifc,            // Interface name
+                            "SetAddress4",  // Method to be called
+                            &error,         // object to return error
+                            &reply,         // Response message on success
+                            "ssss",         // input message (Interface,
+                                            // IP Address, Netmask, Gateway)
+                            app_nwinterface,    // eth0
+                            channel_config.new_ipaddr.c_str(),
+                            channel_config.new_netmask.c_str(),
+                            channel_config.new_gateway.c_str());
+    if(r < 0) {
+        fprintf(stderr, "Failed to set network data %s:%s:%s %s\n",
+                channel_config.new_ipaddr.c_str(),
+                channel_config.new_netmask.c_str(),
+                channel_config.new_gateway.c_str(),
+                error.message);
+        rc = IPMI_CC_UNSPECIFIED_ERROR;
+    }
+
+    channel_config.new_ipaddr.clear();
+    channel_config.new_netmask.clear();
+    channel_config.new_gateway.clear();
+
+finish:
+    sd_bus_error_free(&error);
+    reply = sd_bus_message_unref(reply);
+    free(app);
+
+    return rc;
+}
+
 // ATTENTION: This ipmi function is very hardcoded on purpose
 // OpenBMC does not fully support IPMI.  This command is useful
 // to have around because it enables testing of interfaces with
@@ -644,6 +761,10 @@
     printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_MSG_FLAGS);
     ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_MSG_FLAGS, NULL, ipmi_app_get_msg_flags);
 
+    printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
+                                            IPMI_CMD_SET_CHAN_ACCESS);
+    ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_CHAN_ACCESS, NULL,
+                                            ipmi_set_channel_access);
 
     printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_CHAN_INFO);
     ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHAN_INFO, NULL, ipmi_app_channel_info);