1 /*****************************************************************************
3 * main function to process ipv4 packets
6 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ****************************************************************************/
21 #include <dhcp/pkt4.h>
22 #include <log/logger.h>
23 #include <log/macros.h>
24 #include <log/message_initializer.h>
25 #include "library_common.h"
26 #include <curl/curl.h>
29 using namespace isc::dhcp;
30 using namespace isc::hooks;
32 using namespace isc::log;
33 namespace pt = boost::property_tree;
36 isc::log::Logger logger("sdnc-notify-logger");
37 const char* log_messages[] = {
38 "SNL_BASE", "message: %1",
39 "SNL_PKT_SEND", "Outgoing packet: \n%1",
40 "SNL_CURL_RECEIVED", "Received: \n%1",
41 "SNL_CURL_FAILED", "Unable to receive data from %1",
45 /// Initializer for log messages.
46 const MessageInitializer message_initializer(log_messages);
49 int pkt4_send(CalloutHandle& handle) {
54 struct curl_slist *list=NULL;
55 bool perform_updates = 0;
61 FILE *response_memfile;
72 Pkt4Ptr response4_ptr;
75 // Boost / json related variables.
78 boost::optional<std::string> siaddr_json_field;
81 handle.getArgument("response4", response4_ptr);
82 hwaddr_ptr = response4_ptr->getHWAddr();
83 isc::asiolink::IOAddress new_yiaddr(response4_ptr->getYiaddr());
84 hwaddr = hwaddr_ptr->toText(false);
85 yiaddr = new_yiaddr.toText();
86 msg_name = response4_ptr->getName();
87 /* POST string for DMaaP */
88 post_data = "{\n\"macaddr\":\"" + hwaddr + "\",\n\"yiaddr\":\"" + yiaddr + "\",\n\"msg_name\":\"" + msg_name + "\"\n}";
89 final_url = json_params[0] ;
90 LOG_DEBUG(logger, 0, "SNL_BASE").arg(final_url);
91 LOG_DEBUG(logger, 0, "SNL_BASE").arg(yiaddr);
92 LOG_DEBUG(logger, 0, "SNL_BASE").arg(post_data);
95 if ( msg_name == "DHCPACK")
98 if (!perform_updates) {
99 LOG_WARN(logger, "SNL_BASE").arg("Nothing to update.");
103 curl_global_init(CURL_GLOBAL_DEFAULT);
104 curl = curl_easy_init();
106 curl_global_cleanup();
107 LOG_ERROR(logger, "SNL_BASE").arg("Could not initialize curl");
111 list = curl_slist_append(list, "Content-type: application/json");
112 list = curl_slist_append(list, "Accept: application/json");
114 curl_easy_cleanup(curl);
115 curl_global_cleanup();
116 LOG_ERROR(logger, "SNL_BASE").arg("Could not create curl slist.");
120 response_memfile = open_memstream (&bp, &size);
121 if (response_memfile == NULL) {
122 curl_easy_cleanup(curl);
123 curl_global_cleanup();
124 LOG_ERROR(logger, "SNL_BASE").arg("Could not create memfile.");
129 curl_opt_res += curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
130 curl_opt_res += curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
132 // Default curl timeout is 300 seconds
133 curl_opt_res += curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1L);
134 curl_opt_res += curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1L);
135 curl_opt_res += curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response_memfile);
136 curl_opt_res += curl_easy_setopt(curl, CURLOPT_URL, (final_url).c_str());
137 curl_opt_res += curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1L );
138 curl_opt_res += curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data.c_str() );
139 curl_opt_res += curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
141 #ifdef SKIP_PEER_VERIFICATION
142 curl_opt_res += curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
145 #ifdef SKIP_HOSTNAME_VERIFICATION
146 curl_opt_res += curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
148 if (curl_opt_res > 0) {
149 fclose(response_memfile);
151 curl_easy_cleanup(curl);
152 curl_global_cleanup();
153 LOG_ERROR(logger, "SNL_BASE").arg("Error setting curl options.");
156 res = curl_easy_perform(curl);
157 /* Check for errors */
158 if(res != CURLE_OK) {
159 fclose(response_memfile);
161 curl_easy_cleanup(curl);
162 curl_global_cleanup();
163 LOG_ERROR(logger, "SNL_CURL_FAILED").arg(ss.str());
166 // make bp available for reading.
167 fclose(response_memfile);
168 curl_easy_cleanup(curl);
169 curl_global_cleanup();
172 LOG_DEBUG(logger, 0, "SNL_CURL_RECEIVED").arg(ss.str());
173 // Load the json file in this ptree
176 LOG_DEBUG(logger, 0, "SNL_PKT_SEND").arg(response4_ptr->toText());