-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinaclient.h
More file actions
101 lines (88 loc) · 2.97 KB
/
linaclient.h
File metadata and controls
101 lines (88 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef LINA_CLIENT_H
#define LINA_CLIENT_H
/* Enable POSIX.1-2001 features for addrinfo, getaddrinfo, etc. */
/* Note: This must be defined before including any system headers */
#define _POSIX_C_SOURCE 200112L
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <time.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/uio.h>
#include <errno.h>
#include <time.h>
#include <netdb.h>
typedef int SOCKET;
#define INVALID_SOCKET (SOCKET)(~0)
#define SOCKET_ERROR (ssize_t)(~0)
#endif
#include <memory.h>
#include <stdbool.h>
#include "crc32.h"
#define bool uint8_t
#define true 1
#define false 0
#define MAX_MSG_LEN 4096
// Header: status(1) + ilen(1) + dlen(4) + checksum(4) = 10 bytes (before identifier)
#define LINA_HEADER_BASE_LENGTH 10
enum LiNaFlags {
LINA_DELETE = 0xC0,
LINA_WRITE = 0x80,
LINA_AUTH = 0x60,
LINA_READ = 0x40,
LINA_COVER = 0x02,
LINA_COMPRESS = 0x01,
LINA_NONE = 0x00
};
typedef struct LiNaClient{
SOCKET sock;
struct sockaddr_in server;
char* server_address; // IP address or URL
int server_port;
// Token management
char* session_token;
uint64_t token_expires_at;
// Cached credentials for auto-refresh
char* cached_username;
char* cached_password;
// Auto-refresh settings
bool auto_refresh;
uint32_t refresh_buffer; // Buffer time in seconds before expiration
} LiNaClient;
bool _connect(LiNaClient* client);
bool _disconnect(LiNaClient* client);
// Token management functions
bool is_token_expired(LiNaClient* client);
bool refresh_token_if_needed(LiNaClient* client, char* error_msg, size_t msg_len);
void cache_credentials(LiNaClient* client, char* username, char* password);
void clear_cached_credentials(LiNaClient* client);
typedef struct LiNaResult {
bool status;
union payload{
char* data;
char* message;
} payload;
} LiNaResult;
typedef struct HandshakeResult {
bool status;
char* token;
uint64_t expires_at;
char* message;
} HandshakeResult;
LiNaResult lina_upload_file(LiNaClient* client, char* name, char* data, size_t data_len, uint8_t flags);
LiNaResult lina_download_file(LiNaClient* client, char* name);
LiNaResult lina_delete_file(LiNaClient* client, char* name);
HandshakeResult lina_handshake(LiNaClient* client, char* username, char* password, bool cache_credentials);
void lina_free_result(LiNaResult* result);
void lina_free_handshake_result(HandshakeResult* result);
// Client initialization and cleanup
LiNaClient* lina_client_init(const char* address, int port, bool auto_refresh, uint32_t refresh_buffer);
void lina_client_cleanup(LiNaClient* client);
// Utility functions
uint8_t* to_array(uint64_t value, uint8_t length, bool little_endian);
uint64_t to_long(char* data, uint8_t length, bool little_endian);
#endif