Skip to content

Commit

Permalink
Run bun run clang-format using LLVM 18.1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Electroid authored and github-actions[bot] committed Sep 25, 2024
1 parent 55d2b43 commit c63c6db
Show file tree
Hide file tree
Showing 13 changed files with 2,507 additions and 2,279 deletions.
1,539 changes: 808 additions & 731 deletions packages/bun-usockets/src/bsd.c

Large diffs are not rendered by default.

293 changes: 150 additions & 143 deletions packages/bun-usockets/src/crypto/sni_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@
* limitations under the License.
*/

/* This Server Name Indication hostname tree is written in C++ but could be ported to C.
* Overall it looks like crap, but has no memory allocations in fast path and is O(log n). */
/* This Server Name Indication hostname tree is written in C++ but could be
* ported to C. Overall it looks like crap, but has no memory allocations in
* fast path and is O(log n). */

#ifndef SNI_TREE_H
#define SNI_TREE_H

#ifndef LIBUS_NO_SSL

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <memory>
#include <string_view>
#include <cstring>
#include <cstdlib>
#include <algorithm>

/* We only handle a maximum of 10 labels per hostname */
#define MAX_LABELS 10
Expand All @@ -37,180 +38,186 @@
thread_local void (*sni_free_cb)(void *);

struct sni_node {
/* Empty nodes must always hold null */
void *user = nullptr;
std::map<std::string_view, std::unique_ptr<sni_node>> children;

~sni_node() {
for (auto &p : children) {
/* The data of our string_views are managed by malloc */
free((void *) p.first.data());

/* Call destructor passed to sni_free only if we hold data.
* This is important since sni_remove does not have sni_free_cb set */
if (p.second.get()->user) {
sni_free_cb(p.second.get()->user);
}
}
/* Empty nodes must always hold null */
void *user = nullptr;
std::map<std::string_view, std::unique_ptr<sni_node>> children;

~sni_node() {
for (auto &p : children) {
/* The data of our string_views are managed by malloc */
free((void *)p.first.data());

/* Call destructor passed to sni_free only if we hold data.
* This is important since sni_remove does not have sni_free_cb set */
if (p.second.get()->user) {
sni_free_cb(p.second.get()->user);
}
}
}
};

// this can only delete ONE single node, but may cull "empty nodes with null as data"
void *removeUser(struct sni_node *root, unsigned int label, std::string_view *labels, unsigned int numLabels) {

/* If we are in the bottom (past bottom by one), there is nothing to remove */
if (label == numLabels) {
void *user = root->user;
/* Mark us for culling on the way up */
root->user = nullptr;
return user;
}

/* Is this label a child of root? */
auto it = root->children.find(labels[label]);
if (it == root->children.end()) {
/* We cannot continue */
return nullptr;
}

void *removedUser = removeUser(it->second.get(), label + 1, labels, numLabels);

/* On the way back up, we may cull empty nodes with no children.
* This ends up being where we remove all nodes */
if (it->second.get()->children.empty() && it->second.get()->user == nullptr) {

/* The data of our string_views are managed by malloc */
free((void *) it->first.data());

/* This can only happen with user set to null, otherwise we use sni_free_cb which is unset by sni_remove */
root->children.erase(it);
}

return removedUser;
// this can only delete ONE single node, but may cull "empty nodes with null as
// data"
void *removeUser(struct sni_node *root, unsigned int label,
std::string_view *labels, unsigned int numLabels) {

/* If we are in the bottom (past bottom by one), there is nothing to remove */
if (label == numLabels) {
void *user = root->user;
/* Mark us for culling on the way up */
root->user = nullptr;
return user;
}

/* Is this label a child of root? */
auto it = root->children.find(labels[label]);
if (it == root->children.end()) {
/* We cannot continue */
return nullptr;
}

void *removedUser =
removeUser(it->second.get(), label + 1, labels, numLabels);

/* On the way back up, we may cull empty nodes with no children.
* This ends up being where we remove all nodes */
if (it->second.get()->children.empty() && it->second.get()->user == nullptr) {

/* The data of our string_views are managed by malloc */
free((void *)it->first.data());

/* This can only happen with user set to null, otherwise we use sni_free_cb
* which is unset by sni_remove */
root->children.erase(it);
}

return removedUser;
}

void *getUser(struct sni_node *root, unsigned int label, std::string_view *labels, unsigned int numLabels) {
void *getUser(struct sni_node *root, unsigned int label,
std::string_view *labels, unsigned int numLabels) {

/* Do we have labels to match? Otherwise, return where we stand */
if (label == numLabels) {
return root->user;
}
/* Do we have labels to match? Otherwise, return where we stand */
if (label == numLabels) {
return root->user;
}

/* Try and match by our label */
auto it = root->children.find(labels[label]);
if (it != root->children.end()) {
void *user = getUser(it->second.get(), label + 1, labels, numLabels);
if (user) {
return user;
}
/* Try and match by our label */
auto it = root->children.find(labels[label]);
if (it != root->children.end()) {
void *user = getUser(it->second.get(), label + 1, labels, numLabels);
if (user) {
return user;
}
}

/* Try and match by wildcard */
it = root->children.find("*");
if (it == root->children.end()) {
/* Matching has failed for both label and wildcard */
return nullptr;
}
/* Try and match by wildcard */
it = root->children.find("*");
if (it == root->children.end()) {
/* Matching has failed for both label and wildcard */
return nullptr;
}

/* We matched by wildcard */
return getUser(it->second.get(), label + 1, labels, numLabels);
/* We matched by wildcard */
return getUser(it->second.get(), label + 1, labels, numLabels);
}

extern "C" {

void *sni_new() {
return new sni_node;
}

void sni_free(void *sni, void (*cb)(void *)) {
/* We want to run this callback for every remaining name */
sni_free_cb = cb;

delete (sni_node *) sni;
}

/* Returns non-null if this name already exists */
int sni_add(void *sni, const char *hostname, void *user) {
struct sni_node *root = (struct sni_node *) sni;
void *sni_new() { return new sni_node; }

/* Traverse all labels in hostname */
for (std::string_view view(hostname, strlen(hostname)), label;
view.length(); view.remove_prefix(std::min(view.length(), label.length() + 1))) {
/* Label is the token separated by dot */
label = view.substr(0, view.find('.', 0));
void sni_free(void *sni, void (*cb)(void *)) {
/* We want to run this callback for every remaining name */
sni_free_cb = cb;

auto it = root->children.find(label);
if (it == root->children.end()) {
/* Duplicate this label for our kept string_view of it */
void *labelString = malloc(label.length());
memcpy(labelString, label.data(), label.length());
delete (sni_node *)sni;
}

it = root->children.emplace(std::string_view((char *) labelString, label.length()),
std::make_unique<sni_node>()).first; // NOLINT(clang-analyzer-unix.Malloc)
}
/* Returns non-null if this name already exists */
int sni_add(void *sni, const char *hostname, void *user) {
struct sni_node *root = (struct sni_node *)sni;

root = it->second.get();
}
/* Traverse all labels in hostname */
for (std::string_view view(hostname, strlen(hostname)), label; view.length();
view.remove_prefix(std::min(view.length(), label.length() + 1))) {
/* Label is the token separated by dot */
label = view.substr(0, view.find('.', 0));

/* We must never add multiple contexts for the same name, as that would overwrite and leak */
if (root->user) {
return 1;
}
auto it = root->children.find(label);
if (it == root->children.end()) {
/* Duplicate this label for our kept string_view of it */
void *labelString = malloc(label.length());
memcpy(labelString, label.data(), label.length());

it = root->children
.emplace(std::string_view((char *)labelString, label.length()),
std::make_unique<sni_node>())
.first; // NOLINT(clang-analyzer-unix.Malloc)
}

root->user = user;
root = it->second.get();
}

return 0;
}
/* We must never add multiple contexts for the same name, as that would
* overwrite and leak */
if (root->user) {
return 1;
}

/* Removes the exact match. Wildcards are treated as the verbatim asterisk char, not as an actual wildcard */
void *sni_remove(void *sni, const char *hostname) {
struct sni_node *root = (struct sni_node *) sni;
root->user = user;

/* I guess 10 labels is an okay limit */
std::string_view labels[10];
unsigned int numLabels = 0;
return 0;
}

/* We traverse all labels first of all */
for (std::string_view view(hostname, strlen(hostname)), label;
view.length(); view.remove_prefix(std::min(view.length(), label.length() + 1))) {
/* Label is the token separated by dot */
label = view.substr(0, view.find('.', 0));
/* Removes the exact match. Wildcards are treated as the verbatim asterisk char,
* not as an actual wildcard */
void *sni_remove(void *sni, const char *hostname) {
struct sni_node *root = (struct sni_node *)sni;

/* Anything longer than 10 labels is forbidden */
if (numLabels == 10) {
return nullptr;
}
/* I guess 10 labels is an okay limit */
std::string_view labels[10];
unsigned int numLabels = 0;

labels[numLabels++] = label;
}
/* We traverse all labels first of all */
for (std::string_view view(hostname, strlen(hostname)), label; view.length();
view.remove_prefix(std::min(view.length(), label.length() + 1))) {
/* Label is the token separated by dot */
label = view.substr(0, view.find('.', 0));

return removeUser(root, 0, labels, numLabels);
/* Anything longer than 10 labels is forbidden */
if (numLabels == 10) {
return nullptr;
}

void *sni_find(void *sni, const char *hostname) {
struct sni_node *root = (struct sni_node *) sni;
labels[numLabels++] = label;
}

/* I guess 10 labels is an okay limit */
std::string_view labels[10];
unsigned int numLabels = 0;
return removeUser(root, 0, labels, numLabels);
}

/* We traverse all labels first of all */
for (std::string_view view(hostname, strlen(hostname)), label;
view.length(); view.remove_prefix(std::min(view.length(), label.length() + 1))) {
/* Label is the token separated by dot */
label = view.substr(0, view.find('.', 0));
void *sni_find(void *sni, const char *hostname) {
struct sni_node *root = (struct sni_node *)sni;

/* Anything longer than 10 labels is forbidden */
if (numLabels == 10) {
return nullptr;
}
/* I guess 10 labels is an okay limit */
std::string_view labels[10];
unsigned int numLabels = 0;

labels[numLabels++] = label;
}
/* We traverse all labels first of all */
for (std::string_view view(hostname, strlen(hostname)), label; view.length();
view.remove_prefix(std::min(view.length(), label.length() + 1))) {
/* Label is the token separated by dot */
label = view.substr(0, view.find('.', 0));

return getUser(root, 0, labels, numLabels);
/* Anything longer than 10 labels is forbidden */
if (numLabels == 10) {
return nullptr;
}

labels[numLabels++] = label;
}

return getUser(root, 0, labels, numLabels);
}
}

#endif
Expand Down
Loading

0 comments on commit c63c6db

Please sign in to comment.