-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsender_json_bind_interface.c
84 lines (72 loc) · 2.18 KB
/
sender_json_bind_interface.c
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
#include <stdio.h>
#include <stdlib.h>
#include "../src/unyte_sender.h"
#define MTU 1500
struct buffer_to_send
{
char *buffer;
uint buffer_len;
};
struct buffer_to_send *read_json_file(uint bytes)
{
struct buffer_to_send *bf = (struct buffer_to_send *)malloc(sizeof(struct buffer_to_send));
bf->buffer = (char *)malloc(bytes);
bf->buffer_len = bytes;
FILE *fptr;
int filename_size = 24;
if (bytes > 999)
{
filename_size = 25;
}
char *filename = (char *)malloc(filename_size);
snprintf(filename, filename_size, "resources/json-%d.json", bytes);
if ((fptr = fopen(filename, "r")) == NULL)
{
printf("Error! opening file");
free(filename);
// Program exits if the file pointer returns NULL.
exit(1);
}
free(filename);
fread(bf->buffer, bytes, 1, fptr);
fclose(fptr);
return bf;
}
int main(int argc, char *argv[])
{
if (argc != 4)
{
printf("Error: arguments not valid\n");
printf("Usage: ./sender_json_bind_interface <interface_name> <ip> <port>\n");
exit(1);
}
// Initialize collector options
unyte_sender_options_t options = {0};
options.interface = argv[1];
options.address = argv[2];
options.port = argv[3];
options.default_mtu = MTU;
printf("Init sender on %s:%s | mtu: %d\n", options.address, options.port, MTU);
struct unyte_sender_socket *sender_sk = unyte_start_sender(&options);
struct buffer_to_send *bf_send = read_json_file(200); // resources/json-200.json
// struct buffer_to_send *bf_send = read_json_file(8950); // resources/json-8950.json
unyte_message_t *message = (unyte_message_t *)malloc(sizeof(unyte_message_t));
message->buffer = bf_send->buffer;
message->buffer_len = bf_send->buffer_len;
// UDP-notif
message->version = 0;
message->space = UNYTE_SPACE_STANDARD;
message->media_type = UNYTE_MEDIATYPE_YANG_JSON;
message->observation_domain_id = 1000;
message->message_id = 2147483669;
message->used_mtu = 0; // use default configured
message->options = NULL; // no custom options sent
message->options_len = 0;
unyte_send(sender_sk, message);
// Freeing message and socket
free(message);
free(bf_send->buffer);
free(bf_send);
free_sender_socket(sender_sk);
return 0;
}