Skip to content

Commit 110339e

Browse files
Martin Belangerigaw
authored andcommitted
tree: Use nvme_ipaddrs_eq() to compare IP addresses
Signed-off-by: Martin Belanger <[email protected]>
1 parent df86025 commit 110339e

1 file changed

Lines changed: 39 additions & 5 deletions

File tree

src/nvme/tree.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,34 @@ static int nvme_ctrl_scan_namespace(nvme_root_t r, struct nvme_ctrl *c,
4848
char *name);
4949
static int nvme_ctrl_scan_path(nvme_root_t r, struct nvme_ctrl *c, char *name);
5050

51+
/**
52+
* Compare two C strings and handle NULL pointers gracefully.
53+
* Return true if both pointers are equal (including both set to NULL).
54+
* Return false if one and only one of the two pointers is NULL.
55+
* Perform string comparisong only if both pointers are not NULL and
56+
* return true if both strings are the same, false otherwise.
57+
*/
58+
static bool streq0(const char *s1, const char *s2)
59+
{
60+
if (s1 == s2)
61+
return true;
62+
if (!s1 || !s2)
63+
return false;
64+
return !strcmp(s1, s2);
65+
}
66+
67+
/**
68+
* Same as streq0() but ignore the case of the characters.
69+
*/
70+
static bool streqcase0(const char *s1, const char *s2)
71+
{
72+
if (s1 == s2)
73+
return true;
74+
if (!s1 || !s2)
75+
return false;
76+
return !strcasecmp(s1, s2);
77+
}
78+
5179
static inline void nvme_free_dirents(struct dirent **d, int i)
5280
{
5381
while (i-- > 0)
@@ -1111,22 +1139,28 @@ nvme_ctrl_t __nvme_lookup_ctrl(nvme_subsystem_t s, const char *transport,
11111139

11121140
{
11131141
struct nvme_ctrl *c;
1142+
bool (*addreq)(const char *, const char *);
1143+
1144+
if (!strcmp(transport, "tcp") || !strcmp(transport, "rdma"))
1145+
addreq = nvme_ipaddrs_eq; /* IP address compare for TCP/RDMA */
1146+
else
1147+
addreq = streqcase0; /* Case-insensitive for FC (n/a for loop) */
11141148

11151149
c = p ? nvme_subsystem_next_ctrl(s, p) : nvme_subsystem_first_ctrl(s);
11161150
for (; c != NULL; c = nvme_subsystem_next_ctrl(s, c)) {
1117-
if (strcmp(c->transport, transport))
1151+
if (!streq0(c->transport, transport))
11181152
continue;
11191153
if (traddr && c->traddr &&
1120-
strcasecmp(c->traddr, traddr))
1154+
!addreq(c->traddr, traddr))
11211155
continue;
11221156
if (host_traddr && c->cfg.host_traddr &&
1123-
strcmp(c->cfg.host_traddr, host_traddr))
1157+
!addreq(c->cfg.host_traddr, host_traddr))
11241158
continue;
11251159
if (host_iface && c->cfg.host_iface &&
1126-
strcmp(c->cfg.host_iface, host_iface))
1160+
!streq0(c->cfg.host_iface, host_iface))
11271161
continue;
11281162
if (trsvcid && c->trsvcid &&
1129-
strcmp(c->trsvcid, trsvcid))
1163+
!streq0(c->trsvcid, trsvcid))
11301164
continue;
11311165
return c;
11321166
}

0 commit comments

Comments
 (0)