Skip to content

Commit

Permalink
Apply upstream changes to client utilities (#287)
Browse files Browse the repository at this point in the history
- Convert CHECK_RETURN_IF_FALSE() to RET_CHECK().

- Canonicalize source with clang-format.

Signed-off-by: Derek G Foster <[email protected]>
  • Loading branch information
ffoulkes authored Sep 24, 2023
1 parent 049134f commit d2d21fd
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 125 deletions.
57 changes: 27 additions & 30 deletions clients/gnmi-ctl/gnmi_ctl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

#include "absl/cleanup/cleanup.h"
#include "gflags/gflags.h"
#include "gnmi_ctl_utils.h"
#include "gnmi/gnmi.grpc.pb.h"
#include "gnmi_ctl_utils.h"
#include "grpcpp/grpcpp.h"
#include "grpcpp/security/credentials.h"
#include "grpcpp/security/tls_credentials_options.h"
Expand All @@ -26,7 +26,7 @@
#include "stratum/lib/utils.h"

DEFINE_bool(grpc_use_insecure_mode, false,
"grpc communication channel in insecure mode");
"grpc communication channel in insecure mode");
DECLARE_bool(grpc_use_insecure_mode);

#define DEFAULT_CERTS_DIR "/usr/share/stratum/certs/"
Expand Down Expand Up @@ -171,7 +171,7 @@ ::gnmi::GetRequest build_gnmi_get_req(std::string path) {
::gnmi::SetRequest build_gnmi_set_req(std::string path, std::string val) {
::gnmi::SetRequest req;
::gnmi::Update* update;
char *check;
char* check;

if (FLAGS_replace) {
update = req.add_replace();
Expand Down Expand Up @@ -220,8 +220,8 @@ ::gnmi::SubscribeRequest build_gnmi_sub_sample_req(
}

bool extract_interface_node(char** path, char* node_path, bool* vhost_dev) {
char* key = NULL;
char* value = NULL;
char* key = NULL;
char* value = NULL;
int found_node = 0;

while (client_parse_key_value(path, &key, &value)) {
Expand All @@ -242,36 +242,35 @@ bool extract_interface_node(char** path, char* node_path, bool* vhost_dev) {
}
if (strcmp(key, FLAGS_name_key.c_str()) == 0) {
// Hardcoded length of "[name=]/"
snprintf(node_path + strlen(node_path), strlen(value) + 9,
"[name=%s]/", value);
snprintf(node_path + strlen(node_path), strlen(value) + 9, "[name=%s]/",
value);
found_node += 1;
}
if (found_node == 2)
return 0;
if (found_node == 2) return 0;
}
return -1;
}

void traverse_params(char** path, char* node_path, char* config_value,
bool& flag) {
char* key = NULL;
char* value = NULL;
char* key = NULL;
char* value = NULL;

if (client_parse_key_value(path, &key, &value)) {
if ((value != NULL) && value[0] != '\0') {
// This should be executed for a <key=value> pair, specifically for
// SET operation.
snprintf(node_path + strlen(node_path),
strlen(FLAGS_subtree_config.c_str()) + strlen(key) + 2,
"%s/%s", FLAGS_subtree_config.c_str(), key);
strlen(FLAGS_subtree_config.c_str()) + strlen(key) + 2, "%s/%s",
FLAGS_subtree_config.c_str(), key);
strcpy(config_value, value);
return;
} else if (key != NULL && key[0] != '\0') {
// This should be executed for a <key>, specifically for
// GET operation.
snprintf(node_path + strlen(node_path),
strlen(FLAGS_subtree_config.c_str()) + strlen(key) + 2,
"%s/%s", FLAGS_subtree_config.c_str(), key);
strlen(FLAGS_subtree_config.c_str()) + strlen(key) + 2, "%s/%s",
FLAGS_subtree_config.c_str(), key);
return;
}
}
Expand All @@ -283,7 +282,6 @@ ::grpc::ClientReaderWriterInterface<
::gnmi::SubscribeRequest, ::gnmi::SubscribeResponse>* stream_reader_writer;

::util::Status Main(int argc, char** argv) {

// Default certificate file location for TLS-mode
FLAGS_ca_cert_file = DEFAULT_CERTS_DIR "ca.crt";
FLAGS_server_key_file = DEFAULT_CERTS_DIR "stratum.key";
Expand All @@ -297,7 +295,7 @@ ::util::Status Main(int argc, char** argv) {
::gflags::SetUsageMessage(kUsage);
InitGoogle(argv[0], &argc, &argv, true);
stratum::InitStratumLogging();

bool vhost_device = false;
if (argc < 2) {
std::cout << kUsage << std::endl;
Expand All @@ -311,9 +309,9 @@ ::util::Status Main(int argc, char** argv) {
RETURN_IF_ERROR(
CreatePipeForSignalHandling(&pipe_read_fd_, &pipe_write_fd_));
}
CHECK_RETURN_IF_FALSE(std::signal(SIGINT, HandleSignal) != SIG_ERR);
RET_CHECK(std::signal(SIGINT, HandleSignal) != SIG_ERR);
pthread_t context_cancel_tid;
CHECK_RETURN_IF_FALSE(pthread_create(&context_cancel_tid, nullptr,
RET_CHECK(pthread_create(&context_cancel_tid, nullptr,
ContextCancelThreadFunc, nullptr) == 0);
auto cleaner = absl::MakeCleanup([&context_cancel_tid, &ctx] {
int signal = SIGINT;
Expand All @@ -330,11 +328,11 @@ ::util::Status Main(int argc, char** argv) {
std::shared_ptr<::grpc::Channel> channel;
if (FLAGS_grpc_use_insecure_mode) {
std::shared_ptr<::grpc::ChannelCredentials> channel_credentials =
::grpc::InsecureChannelCredentials();
::grpc::InsecureChannelCredentials();
channel = ::grpc::CreateChannel(FLAGS_grpc_addr, channel_credentials);
} else {
ASSIGN_OR_RETURN(auto credentials_manager,
CredentialsManager::CreateInstance(true));
CredentialsManager::CreateInstance(true));
channel = ::grpc::CreateChannel(
FLAGS_grpc_addr,
credentials_manager->GenerateExternalFacingClientCredentials());
Expand All @@ -357,7 +355,7 @@ ::util::Status Main(int argc, char** argv) {
return ::util::OkStatus();
}

char *path = argv[2];
char* path = argv[2];
char buffer[MAX_STR_LENGTH];
bool params = true;

Expand All @@ -380,7 +378,7 @@ ::util::Status Main(int argc, char** argv) {
RETURN_IF_GRPC_ERROR(stub->Get(&ctx, req, &resp));
PRINT_MSG(resp, "Get Response from Server");
} else if (cmd == "set") {
while(params) {
while (params) {
char path1[MAX_STR_LENGTH] = {0};
char config_value[MAX_STR_LENGTH] = {0};

Expand All @@ -389,8 +387,9 @@ ::util::Status Main(int argc, char** argv) {
// If device is 'virtual-device' and port type is 'link', consider it a
// 'vhost' type.
if (((strcmp(config_value, "link") == 0) ||
(strcmp(config_value, "LINK") == 0)) && vhost_device) {
strcpy(config_value, "vhost");
(strcmp(config_value, "LINK") == 0)) &&
vhost_device) {
strcpy(config_value, "vhost");
}
if (params) {
auto stub = ::gnmi::gNMI::NewStub(channel);
Expand All @@ -414,7 +413,7 @@ ::util::Status Main(int argc, char** argv) {
auto stream_reader_writer_ptr = stub->Subscribe(&ctx);
stream_reader_writer = stream_reader_writer_ptr.get();
::gnmi::SubscribeRequest req = build_gnmi_sub_onchange_req(path);
// CHECK_RETURN_IF_FALSE(stream_reader_writer->Write(req))
// RET_CHECK(stream_reader_writer->Write(req))
// << "Cannot write request.";
stream_reader_writer->Write(req);
::gnmi::SubscribeResponse resp;
Expand All @@ -429,7 +428,7 @@ ::util::Status Main(int argc, char** argv) {
stream_reader_writer = stream_reader_writer_ptr.get();
::gnmi::SubscribeRequest req =
build_gnmi_sub_sample_req(path, FLAGS_interval);
// CHECK_RETURN_IF_FALSE(stream_reader_writer->Write(req))
// RET_CHECK(stream_reader_writer->Write(req))
// << "Cannot write request.";
stream_reader_writer->Write(req);
::gnmi::SubscribeResponse resp;
Expand All @@ -446,6 +445,4 @@ ::util::Status Main(int argc, char** argv) {

} // namespace gnmi

int main(int argc, char** argv) {
return gnmi::Main(argc, argv).error_code();
}
int main(int argc, char** argv) { return gnmi::Main(argc, argv).error_code(); }
174 changes: 86 additions & 88 deletions clients/gnmi-ctl/gnmi_ctl_utils.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
* Copyright (c) 2022 Intel Corporation.
* Copyright (c) 2022-2023 Intel Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,35 +22,34 @@
#include <stdbool.h>
#include <string.h>

static size_t parse_value(const char *s, const char *delimiters)
{
size_t n = 0;
static size_t parse_value(const char* s, const char* delimiters) {
size_t n = 0;

/* Iterate until we reach a delimiter.
*
* strchr(s, '\0') returns s+strlen(s), so this test handles the null
* terminator at the end of 's'. */
while (!strchr(delimiters, s[n])) {
if (s[n] == '(') {
int level = 0;
do {
switch (s[n]) {
case '\0':
return n;
case '(':
level++;
break;
case ')':
level--;
break;
}
n++;
} while (level > 0);
} else {
n++;
/* Iterate until we reach a delimiter.
*
* strchr(s, '\0') returns s+strlen(s), so this test handles the null
* terminator at the end of 's'. */
while (!strchr(delimiters, s[n])) {
if (s[n] == '(') {
int level = 0;
do {
switch (s[n]) {
case '\0':
return n;
case '(':
level++;
break;
case ')':
level--;
break;
}
n++;
} while (level > 0);
} else {
n++;
}
return n;
}
return n;
}

/* Parses a key or a key-value pair from '*stringp'.
Expand All @@ -63,64 +62,63 @@ static size_t parse_value(const char *s, const char *delimiters)
*
* If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
* NULL and returns false. */
bool client_parse_key_value(char **stringp, char **keyp, char **valuep)
{
/* Skip white space and delimiters. If that brings us to the end of the
* input string, we are done and there are no more key-value pairs. */
*stringp += strspn(*stringp, ", \t\r\n");
if (**stringp == '\0') {
*keyp = *valuep = NULL;
return false;
}
bool client_parse_key_value(char** stringp, char** keyp, char** valuep) {
/* Skip white space and delimiters. If that brings us to the end of the
* input string, we are done and there are no more key-value pairs. */
*stringp += strspn(*stringp, ", \t\r\n");
if (**stringp == '\0') {
*keyp = *valuep = NULL;
return false;
}

/* Extract the key and the delimiter that ends the key-value pair or begins
* the value. Advance the input position past the key and delimiter. */
char *key = *stringp;
size_t key_len = strcspn(key, ":=(, \t\r\n");
char key_delim = key[key_len];
key[key_len] = '\0';
*stringp += key_len + (key_delim != '\0');
/* Extract the key and the delimiter that ends the key-value pair or begins
* the value. Advance the input position past the key and delimiter. */
char* key = *stringp;
size_t key_len = strcspn(key, ":=(, \t\r\n");
char key_delim = key[key_len];
key[key_len] = '\0';
*stringp += key_len + (key_delim != '\0');

/* Figure out what delimiter ends the value:
*
* - If key_delim is ":" or "=", the value extends until white space
* or a comma.
*
* - If key_delim is "(", the value extends until ")".
*
* If there is no value, we are done. */
const char *value_delims;
if (key_delim == ':' || key_delim == '=') {
value_delims = ", \t\r\n";
} else if (key_delim == '(') {
value_delims = ")";
} else {
*keyp = key;
*valuep = key + key_len; /* Empty string. */
return true;
}
/* Figure out what delimiter ends the value:
*
* - If key_delim is ":" or "=", the value extends until white space
* or a comma.
*
* - If key_delim is "(", the value extends until ")".
*
* If there is no value, we are done. */
const char* value_delims;
if (key_delim == ':' || key_delim == '=') {
value_delims = ", \t\r\n";
} else if (key_delim == '(') {
value_delims = ")";
} else {
*keyp = key;
*valuep = key + key_len; /* Empty string. */
return true;
}

/* Extract the value. Advance the input position past the value and
* delimiter. */
char *value = *stringp;
size_t value_len = parse_value(value, value_delims);
char value_delim = value[value_len];
/* Extract the value. Advance the input position past the value and
* delimiter. */
char* value = *stringp;
size_t value_len = parse_value(value, value_delims);
char value_delim = value[value_len];

/* Handle the special case if the value is of the form "(x)->y".
* After parsing, 'valuep' will be pointing to - "x)->y".
* */
if (key_delim == '(' && value[value_len] == ')' &&
value[value_len + 1] == '-' && value[value_len + 2] == '>') {
value_delims = ", \t\r\n";
value_len += parse_value(&value[value_len], value_delims);
value_delim = value[value_len];
}
value[value_len] = '\0';
*stringp += value_len + (value_delim != '\0');
/* Handle the special case if the value is of the form "(x)->y".
* After parsing, 'valuep' will be pointing to - "x)->y".
* */
if (key_delim == '(' && value[value_len] == ')' &&
value[value_len + 1] == '-' && value[value_len + 2] == '>') {
value_delims = ", \t\r\n";
value_len += parse_value(&value[value_len], value_delims);
value_delim = value[value_len];
}
value[value_len] = '\0';
*stringp += value_len + (value_delim != '\0');

*keyp = key;
*valuep = value;
return true;
*keyp = key;
*valuep = value;
return true;
}

/* Copies 'src' to 'dst'. Reads no more than 'size - 1' bytes from 'src'.
Expand All @@ -133,13 +131,13 @@ bool client_parse_key_value(char **stringp, char **keyp, char **valuep)
* memset(dst, '\0', size);
* client_strlcpy(dst, src, size);
*
* (Thus, client_strzcpy() is similar to strncpy() without some of the pitfalls.)
* (Thus, client_strzcpy() is similar to strncpy() without some of the
* pitfalls.)
*/
void client_strzcpy(char *dst, const char *src, size_t size)
{
if (size > 0) {
size_t len = strnlen(src, size - 1);
memcpy(dst, src, len);
memset(dst + len, '\0', size - len);
}
void client_strzcpy(char* dst, const char* src, size_t size) {
if (size > 0) {
size_t len = strnlen(src, size - 1);
memcpy(dst, src, len);
memset(dst + len, '\0', size - len);
}
}
Loading

0 comments on commit d2d21fd

Please sign in to comment.