Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Bluetooth base structure in lib. #1607

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 39 additions & 22 deletions lib/libbluetooth/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,42 @@ struct hostent *
bt_gethostent(void)
{
char *p, *cp, **q;
int retry_count = 0; // Retry counter to prevent infinite loops

if (hostf == NULL)
hostf = fopen(_PATH_BT_HOSTS, "r");

if (hostf == NULL) {
h_errno = NETDB_INTERNAL;
perror("fopen"); // Log the error
return (NULL);
}
again:
if ((p = fgets(buf, sizeof(buf), hostf)) == NULL) {
h_errno = HOST_NOT_FOUND;

while (retry_count < 100) { // Limit retries to 100
if ((p = fgets(buf, sizeof(buf), hostf)) == NULL) {
h_errno = HOST_NOT_FOUND;
return (NULL);
}
if (*p == '#')
continue; // Skip comments
if ((cp = strpbrk(p, "#\n")) == NULL)
continue; // Skip invalid lines
*cp = 0;
if ((cp = strpbrk(p, " \t")) == NULL)
continue; // Skip invalid lines
*cp++ = 0;
if (bt_aton(p, &host_addr) == 0)
continue; // Skip invalid addresses

// Process valid entry
break;
}

if (retry_count >= 100) {
h_errno = NO_RECOVERY;
return (NULL);
}
if (*p == '#')
goto again;
if ((cp = strpbrk(p, "#\n")) == NULL)
goto again;
*cp = 0;
if ((cp = strpbrk(p, " \t")) == NULL)
goto again;
*cp++ = 0;
if (bt_aton(p, &host_addr) == 0)
goto again;

host_addr_ptrs[0] = (char *) &host_addr;
host_addr_ptrs[1] = NULL;
host.h_addr_list = host_addr_ptrs;
Expand Down Expand Up @@ -215,6 +228,7 @@ bt_getprotoent(void)

if (protof == NULL)
return (NULL);

again:
if ((p = fgets(buf, sizeof(buf), protof)) == NULL)
return (NULL);
Expand Down Expand Up @@ -268,34 +282,35 @@ bt_endprotoent(void)
if (protof != NULL) {
(void) fclose(protof);
protof = NULL;
proto_stayopen = 0; // Reset stayopen flag
}
}

char const *
bt_ntoa(bdaddr_t const *ba, char *str)
{
static char buffer[24];
static char buffer[24];

if (str == NULL)
str = buffer;

sprintf(str, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
ba->b[5], ba->b[4], ba->b[3], ba->b[2], ba->b[1], ba->b[0]);
snprintf(str, 24, "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
ba->b[5], ba->b[4], ba->b[3], ba->b[2], ba->b[1], ba->b[0]);

return (str);
}

int
bt_aton(char const *str, bdaddr_t *ba)
{
int i, b;
char *end = NULL;
int i, b;
char *end = NULL; // Initialize to NULL

memset(ba, 0, sizeof(*ba));

for (i = 5, end = strchr(str, ':');
i > 0 && *str != '\0' && end != NULL;
i --, str = end + 1, end = strchr(str, ':')) {
i--, str = end + 1, end = strchr(str, ':')) {
switch (end - str) {
case 1:
b = bt_hex_nibble(str[0]);
Expand All @@ -309,7 +324,7 @@ bt_aton(char const *str, bdaddr_t *ba)
b = -1;
break;
}

if (b < 0)
return (0);

Expand Down Expand Up @@ -344,7 +359,10 @@ bt_aton(char const *str, bdaddr_t *ba)
static int
bt_hex_byte(char const *str)
{
int n1, n2;
if (str == NULL || strlen(str) < 2) // Validate input
return (-1);

int n1, n2;

if ((n1 = bt_hex_nibble(str[0])) < 0)
return (-1);
Expand All @@ -369,4 +387,3 @@ bt_hex_nibble(char nibble)

return (-1);
}

8 changes: 7 additions & 1 deletion lib/libbluetooth/bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include <errno.h>
#include <netdb.h>
#include <string.h> // Ensure bcmp is available
#include <bitstring.h>

#include <netgraph/ng_message.h>
Expand Down Expand Up @@ -224,7 +225,12 @@ bdaddr_copy(bdaddr_t *d, const bdaddr_t *s)
d->b[5] = s->b[5];
}

/*
* Internal utility functions (used by bt_aton and bt_ntoa)
*/
static int bt_hex_byte(char const *str);
static int bt_hex_nibble(char nibble);

__END_DECLS

#endif /* ndef _BLUETOOTH_H_ */