Skip to content

Commit 3f6c283

Browse files
committed
refactor to avoid 'transport' struct
Replace 'transport' struct, which would have to be versioned, with a simple getter API of `get(key, &ptr)` signature.
1 parent 8dc4a69 commit 3f6c283

File tree

2 files changed

+68
-31
lines changed

2 files changed

+68
-31
lines changed

agent/csec_metadata.c

+48-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 New Relic Corporation. All rights reserved.
2+
* Copyright 2024 New Relic Corporation. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

@@ -14,24 +14,60 @@
1414
#include "php_compat.h"
1515
#include "php_newrelic.h"
1616

17-
int nr_php_csec_get_metadata(nr_php_csec_metadata_t* csec_metadata) {
18-
if (NULL == csec_metadata) {
17+
int nr_php_csec_get_metadata(const nr_php_csec_metadata_key_t key, void** p) {
18+
const char* value = NULL;
19+
20+
if (NULL == p) {
1921
return -1;
2022
}
2123

2224
if (NULL == NRPRG(app)) {
2325
return -2;
2426
}
2527

26-
csec_metadata->high_security = NRPRG(app)->info.high_security;
27-
csec_metadata->entity_name = nr_strdup(nr_app_get_entity_name(NRPRG(app)));
28-
csec_metadata->entity_type = nr_strdup(nr_app_get_entity_type(NRPRG(app)));
29-
csec_metadata->entity_guid = nr_strdup(nr_app_get_entity_guid(NRPRG(app)));
30-
csec_metadata->host_name = nr_strdup(nr_app_get_host_name(NRPRG(app)));
31-
csec_metadata->agent_run_id = nr_strdup(NRPRG(app)->agent_run_id);
32-
csec_metadata->account_id = nr_strdup(NRPRG(app)->account_id);
33-
csec_metadata->license = nr_strdup(NRPRG(license).value);
34-
csec_metadata->plicense = nr_strdup(NRPRG(app)->plicense);
28+
switch (key) {
29+
case NR_PHP_CSEC_METADATA_HIGH_SECURITY:
30+
*p = nr_zalloc(sizeof(int));
31+
if (NULL == *p) {
32+
return -3;
33+
}
34+
*((int*)*p) = NRPRG(app)->info.high_security;
35+
return 0;
36+
case NR_PHP_CSEC_METADATA_ENTITY_NAME:
37+
value = nr_app_get_entity_name(NRPRG(app));
38+
break;
39+
case NR_PHP_CSEC_METADATA_ENTITY_TYPE:
40+
value = nr_app_get_entity_type(NRPRG(app));
41+
break;
42+
case NR_PHP_CSEC_METADATA_ENTITY_GUID:
43+
value = nr_app_get_entity_guid(NRPRG(app));
44+
break;
45+
case NR_PHP_CSEC_METADATA_HOST_NAME:
46+
value = nr_app_get_host_name(NRPRG(app));
47+
break;
48+
case NR_PHP_CSEC_METADATA_AGENT_RUN_ID:
49+
value = NRPRG(app)->agent_run_id;
50+
break;
51+
case NR_PHP_CSEC_METADATA_ACCOUNT_ID:
52+
value = NRPRG(app)->account_id;
53+
break;
54+
case NR_PHP_CSEC_METADATA_LICENSE:
55+
value = NRPRG(license).value;
56+
break;
57+
case NR_PHP_CSEC_METADATA_PLICENSE:
58+
value = NRPRG(app)->plicense;
59+
break;
60+
default:
61+
return -4;
62+
}
3563

64+
if (NULL == value) {
65+
return -5;
66+
}
67+
68+
*p = nr_strdup(value);
69+
if (NULL == *p) {
70+
return -3;
71+
}
3672
return 0;
3773
}

agent/csec_metadata.h

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
11
/*
2-
* Copyright 2020 New Relic Corporation. All rights reserved.
2+
* Copyright 2024 New Relic Corporation. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

66
#ifndef CSEC_METADATA_H
77
#define CSEC_METADATA_H
88

9-
typedef struct _nr_php_csec_metadata_t {
10-
int high_security; /* Indicates if high security been set locally for this
11-
application */
12-
char* license; /* License key provided */
13-
char* plicense; /* Printable license (abbreviated for security) */
14-
char* host_name; /* Local host name reported to the daemon */
15-
char* entity_name; /* Entity name related to this application */
16-
char* entity_type; /* Entity type */
17-
char* account_id; /* Security : Added for getting account id */
18-
char* entity_guid; /* Entity guid related to this application */
19-
char* agent_run_id; /* The collector's agent run ID; assigned from the
20-
New Relic backend */
21-
} nr_php_csec_metadata_t;
9+
typedef enum {
10+
NR_PHP_CSEC_METADATA_HIGH_SECURITY = 1,
11+
NR_PHP_CSEC_METADATA_ENTITY_NAME,
12+
NR_PHP_CSEC_METADATA_ENTITY_TYPE,
13+
NR_PHP_CSEC_METADATA_ENTITY_GUID,
14+
NR_PHP_CSEC_METADATA_HOST_NAME,
15+
NR_PHP_CSEC_METADATA_AGENT_RUN_ID,
16+
NR_PHP_CSEC_METADATA_ACCOUNT_ID,
17+
NR_PHP_CSEC_METADATA_LICENSE,
18+
NR_PHP_CSEC_METADATA_PLICENSE
19+
} nr_php_csec_metadata_key_t;
2220

2321
/*
24-
* Purpose : Return app meta data by populating nr_php_csec_metadata_t
25-
* structure. The caller is responsible for freeing the memory
26-
* allocated for the strings in the structure.
22+
* Purpose : Copy requested app meta data into allocated *value.
23+
* The caller is responsible for freeing the memory
24+
* allocated.
2725
*
2826
* Params : Pointer to a nr_php_csec_metadata_t structure
2927
*
3028
* Returns : 0 for success
3129
* -1 for invalid input
3230
* -2 for invalid internal state
31+
* -3 for inability to allocate memory
32+
* -4 for invalid metadata key
33+
* -5 for inability to retrieve metadata value
3334
*/
34-
extern int nr_php_csec_get_metadata(nr_php_csec_metadata_t*);
35-
typedef int (*nr_php_csec_get_metadata_t)(nr_php_csec_metadata_t*);
35+
extern int nr_php_csec_get_metadata(const nr_php_csec_metadata_key_t k, void** value);
36+
typedef int (*nr_php_csec_get_metadata_t)(const nr_php_csec_metadata_key_t k, void** value);
3637
#define NR_PHP_CSEC_GET_METADATA "nr_php_csec_get_metadata"
3738
#endif

0 commit comments

Comments
 (0)