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

Feature/typepayload #200

Merged
merged 19 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add cmd connect
  • Loading branch information
angelskieglazki committed Apr 5, 2021
commit d5741db635a75fa9d90deda2abeedaa9e38e532d
44 changes: 32 additions & 12 deletions src/commands_creator.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,19 @@ uint8_t* createCmdConnectRPacketUdp(ksnetEvMgrClass *event_manager, size_t *size
packet->ip_counts = 0;
packet->port = event_manager->kc->port;

const char* type = teoGetAppType(event_manager);

size_t hid_len;
host_info_data *hid = teoGetHostInfo(event_manager, &hid_len);

if(hid != NULL) {
char* full_type = teoGetFullAppTypeFromHostInfo(hid);
strncpy(packet->type, full_type, sizeof(packet->type));
free(hid);
} else {
strncpy(packet->type, type, sizeof(packet->type));
}
char *full_type = teoGetFullAppTypeFromHostInfo(hid);
strncpy(packet->type, full_type, sizeof(packet->type));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь как-то хочется проверять, что full_type влезает целиком в packet->type
А что делать, если не влезает? Выкидывать лишние типы? Писать, сколько влезет?
Может тогда ограничить размер full_type? Или неограниченный размер для packet->type.

free(hid);
free(full_type);


size_t ptr = 0;

// Fill data with IPs and Port
for(int i=0; i < len; i++) {
for(int i = 0; i < len; i++) {
if(ip_is_private(ips[i])) {
int ip_len = strlen(ips[i]) + 1;
memcpy(packet->ips + ptr, ips[i], ip_len);
Expand All @@ -52,11 +48,35 @@ uint8_t* createCmdConnectRPacketUdp(ksnetEvMgrClass *event_manager, size_t *size


uint8_t* createCmdConnectRPacketTcp(ksnetEvMgrClass *event_manager, size_t *size_out) {
(void)event_manager;
// Create data with empty list of local IPs and port
uint8_t* packet = malloc(sizeof(uint8_t));
uint8_t *packet = malloc(sizeof(uint8_t));
uint8_t *num = (uint8_t *) packet; // Pointer to number of IPs
*size_out = sizeof(uint8_t); // Pointer (to first IP)
*num = 0; // Number of IPs

return packet;
return (uint8_t*)packet;
}


uint8_t* createCmdConnectPacket(ksnetEvMgrClass *event_manager, char *name, char *addr, uint32_t port, size_t *size_out) {
size_t hid_len;
host_info_data *hid = teoGetHostInfo(event_manager, &hid_len);

char *full_type = teoGetFullAppTypeFromHostInfo(hid);

*size_out = strlen(name) + 1 + strlen(full_type) + 1 + strlen(addr) + 1 + sizeof(uint32_t);
uint8_t *packet = malloc(*size_out);
memset(packet, '\0', *size_out);

size_t ptr = 0;
memcpy(packet, name, strlen(name)); ptr = strlen(name) + 1;
memcpy(packet + ptr, addr, strlen(addr)); ptr += strlen(addr) + 1;
memcpy(packet + ptr, full_type, strlen(full_type)); ptr += strlen(full_type) + 1;
*((uint32_t *)(packet + ptr)) = port;

free(hid);
free(full_type);

return (uint8_t *)packet;
}
10 changes: 2 additions & 8 deletions src/commands_creator.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,10 @@ typedef struct connect_r_packet {
char ips[];
} connect_r_packet_t;

typedef struct connect_packet {
uint32_t port; ///< Peer port
char *name; ///< Peer name
char *addr; ///< Peer IP address
} connect_packet_t;

#pragma pack(pop)
#pragma pack(pop)

uint8_t* createCmdConnectRPacketUdp(ksnetEvMgrClass *event_manager, size_t *size_out);
uint8_t* createCmdConnectRPacketTcp(ksnetEvMgrClass *event_manager, size_t *size_out);
uint8_t* createCmdConnectPacket(ksnetEvMgrClass *event_manager, size_t *size_out);
uint8_t* createCmdConnectPacket(ksnetEvMgrClass *event_manager, char *name, char *addr, uint32_t port, size_t *size_out);

#endif