summaryrefslogtreecommitdiff
path: root/nodeapi.c
diff options
context:
space:
mode:
authorDave Hibberd <hibby@debian.org>2024-11-05 21:03:15 +0000
committerDave Hibberd <hibby@debian.org>2024-11-05 21:03:15 +0000
commit424c0bd3e14d3ddda6c2d945b6465d51c462c76b (patch)
tree80b44cbe3f5fc19de1c057e37c761a95a0518a44 /nodeapi.c
parenteb0d36d3f67be13de74023adb6d30362d47805fe (diff)
New upstream version 6.0.24.49upstream/6.0.24.49
Diffstat (limited to 'nodeapi.c')
-rw-r--r--nodeapi.c136
1 files changed, 102 insertions, 34 deletions
diff --git a/nodeapi.c b/nodeapi.c
index c5070e7..8ac9c3c 100644
--- a/nodeapi.c
+++ b/nodeapi.c
@@ -22,6 +22,20 @@ typedef struct Token {
struct Token* next;
} Token;
+typedef struct API
+{
+ char *URL;
+ int URLLen;
+ int (* APIRoutine)();
+ int Auth;
+} API;
+
+// Auth defines
+
+#define AuthNone 0
+#define AuthUser 1
+#define Auth BBSUser 2
+#define AuthSysop 4
// Function prototypes
void handle_request(SOCKET client_socket, char * request, char * response);
@@ -36,12 +50,12 @@ void add_token_to_list(Token* token);
Token* find_token(const char* token);
Token* generate_token();
-int sendPortList(char * response, char * token,int Flags);
-int sendNodeList(char * response, char * token,int Flags);
-int sendUserList(char * response, char * token,int Flags);
-int sendInfo(char * response, char * token, int Flags);
-int sendLinks(char * response, char * token, int Flags);
-int sendPortMHList(char * response, char * token, int param);
+int sendPortList(char * response, char * token, char * Rest);
+int sendNodeList(char * response, char * token, char * Rest);
+int sendUserList(char * response, char * token, char * Rest);
+int sendInfo(char * response, char * token, char * Rest);
+int sendLinks(char * response, char * token, char * Rest);
+int sendPortMHList(char * response, char * token, char * Rest);
void BuildPortMH(char * MHJSON, struct PORTCONTROL * PORT);
DllExport struct PORTCONTROL * APIENTRY GetPortTableEntryFromSlot(int portslot);
@@ -49,6 +63,21 @@ DllExport struct PORTCONTROL * APIENTRY GetPortTableEntryFromSlot(int portslot);
// Token list
Token* token_list = NULL;
+
+struct API APIList[] =
+{
+ "/api/ports", 10, sendPortList, 0,
+ "/api/nodes", 10, sendNodeList, 0,
+ "/api/info", 9, sendInfo, 0,
+ "/api/links", 10, sendLinks, 0,
+ "/api/users", 10, sendUserList, 0,
+ "/api/mheard", 11, sendPortMHList, 0
+};
+
+int APICount = sizeof(APIList) / sizeof(struct API);
+
+
+
int xx()
{
while (1)
@@ -67,7 +96,7 @@ int APIProcessHTTPMessage(char * response, char * Method, char * URL, char * req
const char * auth_header = "Authorization: Bearer ";
char * token_begin = strstr(request, auth_header);
char token[TOKEN_SIZE + 1]= "";
- int Flags = 0;
+ int Flags = 0, n;
// Node Flags isn't currently used
@@ -94,7 +123,7 @@ int APIProcessHTTPMessage(char * response, char * Method, char * URL, char * req
}
else
{
- // Token must be first param of URL
+ // There may be a token as first param, but if auth not needed may be misisng
Tok = strlop(URL, '?');
param = strlop(Tok, '&');
@@ -105,19 +134,34 @@ int APIProcessHTTPMessage(char * response, char * Method, char * URL, char * req
strcpy(token, Tok);
}
+ else param = Tok;
}
remove_expired_tokens(); // Tidy up
// Check if the request is for token generation
+ if (strcmp(Method, "OPTIONS") == 0)
+ {
+ // CORS Request
+
+ char Resp[] =
+ "HTTP/1.1 200 OK\r\n"
+ "Access-Control-Allow-Origin: *\r\n"
+ "Access-Control-Allow-Methods: POST, GET, OPTIONS\r\n"
+ "Access-Control-Allow-Headers: authorization";
+
+ return send_http_response(response, Resp);
+ }
+
if (strcmp(Method, "GET") != 0)
return send_http_response(response, "403 (Bad Method)");
if (_stricmp(URL, "/api/request_token") == 0)
return request_token(response);
- if (token[0] == 0)
+/*
+if (token[0] == 0)
{
// Extract the token from the request (assuming it's present in the request headers)
if (token_begin == NULL)
@@ -137,20 +181,26 @@ int APIProcessHTTPMessage(char * response, char * Method, char * URL, char * req
return send_http_response(response, "401 Unauthorized");
}
+ */
+
// Determine the requested API endpoint
- if (_stricmp(URL, "/api/ports") == 0)
- return sendPortList(response, token, Flags);
- else if (_stricmp(URL, "/api/nodes") == 0)
- return sendNodeList(response, token, Flags);
- else if (_stricmp(URL, "/api/users") == 0)
- return sendUserList(response, token, Flags);
- else if (_stricmp(URL, "/api/info") == 0)
- return sendInfo(response, token, Flags);
- else if (_stricmp(URL, "/api/links") == 0)
- return sendLinks(response, token, Flags);
- else if (strstr(URL, "/api/mheardport") != 0)
- return sendPortMHList(response, token, atoi(param));
+ for (n = 0; n < APICount; n++)
+ {
+ struct API * APIEntry;
+ char * rest;
+
+ APIEntry = &APIList[n];
+
+ if (_memicmp(URL, APIEntry->URL, APIEntry->URLLen) == 0)
+ {
+ rest = &request[4 + APIEntry->URLLen]; // Anything following?
+
+ if (rest[0] == ' ' || rest[0] == '/' || rest[0] == '?')
+ return APIEntry->APIRoutine(response, token, rest);
+ }
+
+ }
return send_http_response(response, "401 Invalid API Call");
}
@@ -294,7 +344,7 @@ int send_http_response(char * response, const char* msg)
extern int MasterPort[MAXBPQPORTS+1]; // Pointer to first BPQ port for a specific MPSK or UZ7HO host
-int sendPortList(char * response, char * token, int Flags)
+int sendPortList(char * response, char * token, char * param)
{
char * Array = 0;
int ArrayLen = 0;
@@ -506,7 +556,7 @@ extern struct DEST_LIST * DESTS; // NODE LIST
extern int DEST_LIST_LEN;
-int sendNodeList(char * response, char * token, int Flags)
+int sendNodeList(char * response, char * token, char * param)
{
int ArrayPtr = 0;
@@ -610,16 +660,16 @@ int sendNodeList(char * response, char * token, int Flags)
}
-int sendUserList(char * response, char * token, int Flags)
+int sendUserList(char * response, char * token, char * param)
{
int ArrayPtr = 0;
int n = MAXCIRCUITS;
TRANSPORTENTRY * L4 = L4TABLE;
- TRANSPORTENTRY * Partner;
+// TRANSPORTENTRY * Partner;
int MaxLinks = MAXLINKS;
char State[12] = "", Type[12] = "Uplink";
char LHS[50] = "", MID[10] = "", RHS[50] = "";
- char Line[100];
+// char Line[100];
char Normcall[10];
int len;
@@ -634,7 +684,7 @@ int sendUserList(char * response, char * token, int Flags)
len = ConvFromAX25(L4->L4USER, Normcall);
Normcall[len] = 0;
- ArrayPtr += sprintf(&response[ArrayPtr], " {\"Call\", \"%s\"},\r\n", Normcall);
+ ArrayPtr += sprintf(&response[ArrayPtr], " {\"Call\": \"%s\"},\r\n", Normcall);
L4++;
}
}
@@ -656,7 +706,7 @@ extern char MYALIASLOPPED[];
extern char TextVerstring[];
extern char LOCATOR[];
-int sendInfo(char * response, char * token, int Flags)
+int sendInfo(char * response, char * token, char * param)
{
char call[10];
@@ -669,7 +719,7 @@ int sendInfo(char * response, char * token, int Flags)
return strlen(response);
}
-int sendLinks(char * response, char * token, int Flags)
+int sendLinks(char * response, char * token, char * param)
{
struct _LINKTABLE * Links = LINKS;
int MaxLinks = MAXLINKS;
@@ -734,19 +784,37 @@ int sendLinks(char * response, char * token, int Flags)
return ReplyLen;
}
-int sendPortMHList(char * response, char * token, int param)
+int sendPortMHList(char * response, char * token, char * param)
{
- struct PORTCONTROL * PORTVEC = GetPortTableEntryFromPortNum(param);
+ struct PORTCONTROL * PORTVEC ;
+ int n;
+ int port = 0;
+ if (param[0] = '?' || param[0] == '/')
+ port = atoi(&param[1]);
+
+ PORTVEC = GetPortTableEntryFromPortNum(port);
response[0] = 0;
if (PORTVEC == 0)
return send_http_response(response, "401 Invalid API Call");
- BuildPortMH( response, PORTVEC );
- response[ strlen(response)-3 ] = '\0'; // remove ,\r\n
+ n = sprintf(response,"{\"mheard\":[\r\n");
+
+ BuildPortMH(&response[n], PORTVEC );
+
+ if (response[n] == 0) // No entries
+ {
+ response[strlen(response) - 2] = '\0'; // remove \r\n
+ strcat(response, "]}\r\n");
+ }
+ else
+ {
+ response[strlen(response)-3 ] = '\0'; // remove ,\r\n
+ strcat(response, "\r\n]}\r\n");
// printf("MH for port %d:\r\n%s\r\n", PORTVEC->PORTNUMBER, response);
- return strlen(response);
+ }
+ return strlen(response);
}