Skip to content

Commit 8948e55

Browse files
committed
util: move functions into private header
These function are only used internally, thus move it to private and thus reduce the API size and dependencies a bit further. Signed-off-by: Daniel Wagner <[email protected]>
1 parent f31826d commit 8948e55

5 files changed

Lines changed: 156 additions & 158 deletions

File tree

libnvme/src/libnvme.ld

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ LIBNVME_2_0 {
121121
nvme_insert_tls_key;
122122
nvme_insert_tls_key_compat;
123123
nvme_insert_tls_key_versioned;
124-
nvme_ipaddrs_eq;
125124
nvme_lookup_key;
126125
nvme_lookup_keyring;
127126
nvme_mi_admin_admin_passthru;

libnvme/src/nvme/private.h

Lines changed: 154 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
*
66
* Authors: Hannes Reinecke <[email protected]>
77
*/
8-
9-
#ifndef _LIBNVME_PRIVATE_H
10-
#define _LIBNVME_PRIVATE_H
8+
#pragma once
119

1210
#include <ccan/list/list.h>
1311
#include <poll.h>
@@ -584,4 +582,156 @@ static inline char *xstrdup(const char *s)
584582
*/
585583
const struct ifaddrs *nvme_getifaddrs(struct nvme_global_ctx *ctx);
586584

587-
#endif /* _LIBNVME_PRIVATE_H */
585+
/**
586+
* nvme_ipaddrs_eq - Check if 2 IP addresses are equal.
587+
* @addr1: IP address (can be IPv4 or IPv6)
588+
* @addr2: IP address (can be IPv4 or IPv6)
589+
*
590+
* Return: true if addr1 == addr2. false otherwise.
591+
*/
592+
bool nvme_ipaddrs_eq(const char *addr1, const char *addr2);
593+
594+
/**
595+
* nvme_iface_matching_addr - Get interface matching @addr
596+
* @iface_list: Interface list returned by getifaddrs()
597+
* @addr: Address to match
598+
*
599+
* Parse the interface list pointed to by @iface_list looking
600+
* for the interface that has @addr as one of its assigned
601+
* addresses.
602+
*
603+
* Return: The name of the interface that owns @addr or NULL.
604+
*/
605+
const char *nvme_iface_matching_addr(const struct ifaddrs *iface_list,
606+
const char *addr);
607+
608+
/**
609+
* nvme_iface_primary_addr_matches - Check that interface's primary
610+
* address matches
611+
* @iface_list: Interface list returned by getifaddrs()
612+
* @iface: Interface to match
613+
* @addr: Address to match
614+
*
615+
* Parse the interface list pointed to by @iface_list and looking for
616+
* interface @iface. The get its primary address and check if it matches
617+
* @addr.
618+
*
619+
* Return: true if a match is found, false otherwise.
620+
*/
621+
bool nvme_iface_primary_addr_matches(const struct ifaddrs *iface_list,
622+
const char *iface, const char *addr);
623+
624+
int hostname2traddr(struct nvme_global_ctx *ctx, const char *traddr,
625+
char **hostname);
626+
627+
/**
628+
* get_entity_name - Get Entity Name (ENAME).
629+
* @buffer: The buffer where the ENAME will be saved as an ASCII string.
630+
* @bufsz: The size of @buffer.
631+
*
632+
* Per TP8010, ENAME is defined as the name associated with the host (i.e.
633+
* hostname).
634+
*
635+
* Return: Number of characters copied to @buffer.
636+
*/
637+
size_t get_entity_name(char *buffer, size_t bufsz);
638+
639+
/**
640+
* get_entity_version - Get Entity Version (EVER).
641+
* @buffer: The buffer where the EVER will be saved as an ASCII string.
642+
* @bufsz: The size of @buffer.
643+
*
644+
* EVER is defined as the operating system name and version as an ASCII
645+
* string. This function reads different files from the file system and
646+
* builds a string as follows: [os type] [os release] [distro release]
647+
*
648+
* E.g. "Linux 5.17.0-rc1 SLES 15.4"
649+
*
650+
* Return: Number of characters copied to @buffer.
651+
*/
652+
size_t get_entity_version(char *buffer, size_t bufsz);
653+
654+
655+
/**
656+
* startswith - Checks that a string starts with a given prefix.
657+
* @s: The string to check
658+
* @prefix: A string that @s could be starting with
659+
*
660+
* Return: If @s starts with @prefix, then return a pointer within @s at
661+
* the first character after the matched @prefix. NULL otherwise.
662+
*/
663+
char *startswith(const char *s, const char *prefix);
664+
665+
/**
666+
* kv_strip - Strip blanks from key value string
667+
* @kv: The key-value string to strip
668+
*
669+
* Strip leading/trailing blanks as well as trailing comments from the
670+
* Key=Value string pointed to by @kv.
671+
*
672+
* Return: A pointer to the stripped string. Note that the original string,
673+
* @kv, gets modified.
674+
*/
675+
char *kv_strip(char *kv);
676+
677+
/**
678+
* kv_keymatch - Look for key in key value string
679+
* @kv: The key=value string to search for the presence of @key
680+
* @key: The key to look for
681+
*
682+
* Look for @key in the Key=Value pair pointed to by @k and return a
683+
* pointer to the Value if @key is found.
684+
*
685+
* Check if @kv starts with @key. If it does then make sure that we
686+
* have a whole-word match on the @key, and if we do, return a pointer
687+
* to the first character of value (i.e. skip leading spaces, tabs,
688+
* and equal sign)
689+
*
690+
* Return: A pointer to the first character of "value" if a match is found.
691+
* NULL otherwise.
692+
*/
693+
char *kv_keymatch(const char *kv, const char *key);
694+
695+
#define __round_mask(val, mult) ((__typeof__(val))((mult)-1))
696+
697+
/**
698+
* round_up - Round a value @val to the next multiple specified by @mult.
699+
* @val: Value to round
700+
* @mult: Multiple to round to.
701+
*
702+
* usage: int x = round_up(13, sizeof(__u32)); // 13 -> 16
703+
*/
704+
#define round_up(val, mult) ((((val)-1) | __round_mask((val), (mult)))+1)
705+
706+
/**
707+
* nvmf_exat_len() - Return length rounded up by 4
708+
* @val_len: Value length
709+
*
710+
* Return the size in bytes, rounded to a multiple of 4 (e.g., size of
711+
* __u32), of the buffer needed to hold the exat value of size
712+
* @val_len.
713+
*
714+
* Return: Length rounded up by 4
715+
*/
716+
static inline __u16 nvmf_exat_len(size_t val_len)
717+
{
718+
return (__u16)round_up(val_len, sizeof(__u32));
719+
}
720+
721+
/**
722+
* nvmf_exat_size - Return min aligned size to hold value
723+
* @val_len: This is the length of the data to be copied to the "exatval"
724+
* field of a "struct nvmf_ext_attr".
725+
*
726+
* Return the size of the "struct nvmf_ext_attr" needed to hold
727+
* a value of size @val_len.
728+
*
729+
* Return: The size in bytes, rounded to a multiple of 4 (i.e. size of
730+
* __u32), of the "struct nvmf_ext_attr" required to hold a string of
731+
* length @val_len.
732+
*/
733+
static inline __u16 nvmf_exat_size(size_t val_len)
734+
{
735+
return (__u16)(sizeof(struct nvmf_ext_attr) + nvmf_exat_len(val_len));
736+
}
737+

libnvme/src/nvme/util.h

Lines changed: 0 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
*/
99
#pragma once
1010

11-
#include <ifaddrs.h>
12-
1311
#include <nvme/types.h>
1412

1513
/**
@@ -149,121 +147,6 @@ const char *nvme_errno_to_string(int err);
149147
*/
150148
const char *nvme_strerror(int err);
151149

152-
struct nvme_root;
153-
struct nvme_global_ctx;
154-
155-
int hostname2traddr(struct nvme_global_ctx *ctx, const char *traddr, char **hostname);
156-
157-
/**
158-
* get_entity_name - Get Entity Name (ENAME).
159-
* @buffer: The buffer where the ENAME will be saved as an ASCII string.
160-
* @bufsz: The size of @buffer.
161-
*
162-
* Per TP8010, ENAME is defined as the name associated with the host (i.e.
163-
* hostname).
164-
*
165-
* Return: Number of characters copied to @buffer.
166-
*/
167-
size_t get_entity_name(char *buffer, size_t bufsz);
168-
169-
/**
170-
* get_entity_version - Get Entity Version (EVER).
171-
* @buffer: The buffer where the EVER will be saved as an ASCII string.
172-
* @bufsz: The size of @buffer.
173-
*
174-
* EVER is defined as the operating system name and version as an ASCII
175-
* string. This function reads different files from the file system and
176-
* builds a string as follows: [os type] [os release] [distro release]
177-
*
178-
* E.g. "Linux 5.17.0-rc1 SLES 15.4"
179-
*
180-
* Return: Number of characters copied to @buffer.
181-
*/
182-
size_t get_entity_version(char *buffer, size_t bufsz);
183-
184-
/**
185-
* kv_strip - Strip blanks from key value string
186-
* @kv: The key-value string to strip
187-
*
188-
* Strip leading/trailing blanks as well as trailing comments from the
189-
* Key=Value string pointed to by @kv.
190-
*
191-
* Return: A pointer to the stripped string. Note that the original string,
192-
* @kv, gets modified.
193-
*/
194-
char *kv_strip(char *kv);
195-
196-
/**
197-
* kv_keymatch - Look for key in key value string
198-
* @kv: The key=value string to search for the presence of @key
199-
* @key: The key to look for
200-
*
201-
* Look for @key in the Key=Value pair pointed to by @k and return a
202-
* pointer to the Value if @key is found.
203-
*
204-
* Check if @kv starts with @key. If it does then make sure that we
205-
* have a whole-word match on the @key, and if we do, return a pointer
206-
* to the first character of value (i.e. skip leading spaces, tabs,
207-
* and equal sign)
208-
*
209-
* Return: A pointer to the first character of "value" if a match is found.
210-
* NULL otherwise.
211-
*/
212-
char *kv_keymatch(const char *kv, const char *key);
213-
214-
/**
215-
* startswith - Checks that a string starts with a given prefix.
216-
* @s: The string to check
217-
* @prefix: A string that @s could be starting with
218-
*
219-
* Return: If @s starts with @prefix, then return a pointer within @s at
220-
* the first character after the matched @prefix. NULL otherwise.
221-
*/
222-
char *startswith(const char *s, const char *prefix);
223-
224-
#define __round_mask(val, mult) ((__typeof__(val))((mult)-1))
225-
226-
/**
227-
* round_up - Round a value @val to the next multiple specified by @mult.
228-
* @val: Value to round
229-
* @mult: Multiple to round to.
230-
*
231-
* usage: int x = round_up(13, sizeof(__u32)); // 13 -> 16
232-
*/
233-
#define round_up(val, mult) ((((val)-1) | __round_mask((val), (mult)))+1)
234-
235-
/**
236-
* nvmf_exat_len() - Return length rounded up by 4
237-
* @val_len: Value length
238-
*
239-
* Return the size in bytes, rounded to a multiple of 4 (e.g., size of
240-
* __u32), of the buffer needed to hold the exat value of size
241-
* @val_len.
242-
*
243-
* Return: Length rounded up by 4
244-
*/
245-
static inline __u16 nvmf_exat_len(size_t val_len)
246-
{
247-
return (__u16)round_up(val_len, sizeof(__u32));
248-
}
249-
250-
/**
251-
* nvmf_exat_size - Return min aligned size to hold value
252-
* @val_len: This is the length of the data to be copied to the "exatval"
253-
* field of a "struct nvmf_ext_attr".
254-
*
255-
* Return the size of the "struct nvmf_ext_attr" needed to hold
256-
* a value of size @val_len.
257-
*
258-
* Return: The size in bytes, rounded to a multiple of 4 (i.e. size of
259-
* __u32), of the "struct nvmf_ext_attr" required to hold a string of
260-
* length @val_len.
261-
*/
262-
static inline __u16 nvmf_exat_size(size_t val_len)
263-
{
264-
return (__u16)(sizeof(struct nvmf_ext_attr) + nvmf_exat_len(val_len));
265-
}
266-
267150
/**
268151
* nvmf_exat_ptr_next - Increment @p to the next element in the array.
269152
* @p: Pointer to an element of an array of "struct nvmf_ext_attr".
@@ -335,38 +218,3 @@ int nvme_uuid_random(unsigned char uuid[NVME_UUID_LEN]);
335218
*/
336219
int nvme_uuid_find(struct nvme_id_uuid_list *uuid_list, const unsigned char uuid[NVME_UUID_LEN]);
337220

338-
/**
339-
* nvme_ipaddrs_eq - Check if 2 IP addresses are equal.
340-
* @addr1: IP address (can be IPv4 or IPv6)
341-
* @addr2: IP address (can be IPv4 or IPv6)
342-
*
343-
* Return: true if addr1 == addr2. false otherwise.
344-
*/
345-
bool nvme_ipaddrs_eq(const char *addr1, const char *addr2);
346-
347-
/**
348-
* nvme_iface_matching_addr - Get interface matching @addr
349-
* @iface_list: Interface list returned by getifaddrs()
350-
* @addr: Address to match
351-
*
352-
* Parse the interface list pointed to by @iface_list looking
353-
* for the interface that has @addr as one of its assigned
354-
* addresses.
355-
*
356-
* Return: The name of the interface that owns @addr or NULL.
357-
*/
358-
const char *nvme_iface_matching_addr(const struct ifaddrs *iface_list, const char *addr);
359-
360-
/**
361-
* nvme_iface_primary_addr_matches - Check that interface's primary address matches
362-
* @iface_list: Interface list returned by getifaddrs()
363-
* @iface: Interface to match
364-
* @addr: Address to match
365-
*
366-
* Parse the interface list pointed to by @iface_list and looking for
367-
* interface @iface. The get its primary address and check if it matches
368-
* @addr.
369-
*
370-
* Return: true if a match is found, false otherwise.
371-
*/
372-
bool nvme_iface_primary_addr_matches(const struct ifaddrs *iface_list, const char *iface, const char *addr);

libnvme/test/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ if conf.get('HAVE_NETDB')
143143
dependencies: [
144144
config_dep,
145145
ccan_dep,
146-
libnvme_dep,
146+
libnvme_test_dep,
147147
],
148148
)
149149
test('libnvme - util', test_util)

libnvme/test/test-util.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string.h>
2222

2323
#include <libnvme.h>
24+
#include <nvme/private.h>
2425

2526
static size_t safe_strlen(const char *p) {
2627
return p ? strlen(p) : strlen("null");

0 commit comments

Comments
 (0)