This repository was archived by the owner on Nov 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_udp.c
More file actions
97 lines (78 loc) · 2.51 KB
/
util_udp.c
File metadata and controls
97 lines (78 loc) · 2.51 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
/**
* \file
*
* Este arquivo, juntamente com o seu .h relacionado, possui funcionalidades
* utilitarias para se trabalhar com os arquivos de imagens disponiveis
*
* @author Rafael B. Januzi (rjanuzi@gmail.com)
* @date 06/11/2016
*
*/
#include <util_udp.h>
bool utilUdp_sendUdpMsg(const uint8_t* msg, uint16_t len, const char* hostIp, int destPort, int srcPort)
{
struct sockaddr_in si_other, si_me;
int s, i, slen=sizeof(si_other);
uint8_t buf[UTIL_UDP_BUFFLEN];
if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
{
printf("\n[ERROR]: util_udp.utilUdp_sendUdpMsg - Fail to open socket.");
goto err;
}
memset( (char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(destPort);
memset( (char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(srcPort);
if (inet_aton(hostIp, &si_other.sin_addr)==0)
{
printf("\n[ERROR]: util_udp.utilUdp_sendUdpMsg - inet_aton() failed.");
goto err;
}
if( bind(s, (__CONST_SOCKADDR_ARG) &si_me, (socklen_t) sizeof(si_me)) == -1)
{
printf("\n[ERROR]: utilUdp.utilUdp_sendUdpMsg - Can't bind the socket.\n");
printf("Trying again in 10 seconds...\n");
sleep(10);
}
sprintf(buf, "%s", msg);
if( sendto(s, (const void*) &msg[0], (size_t) len, 0, (__CONST_SOCKADDR_ARG)&si_other, (socklen_t)slen) == -1)
{
printf("\n[ERROR]: util_udp.utilUdp_sendUdpMsg - Sending fail.");
goto err;
}
close(s);
return true;
err:
close(s);
return false;
}
bool utilUdp_receiveUdpMsg(uint8_t* buf, int destPort)
{
struct sockaddr_in si_other, si_me;
int s, slen=sizeof(si_other);
while( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1 )
{
printf("\n[ERROR]: utilUdp_receiveUdpMsg - Can't open the socket.\n");
printf("Trying again in 10 seconds...\n");
sleep(10);
}
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(destPort);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if( bind(s, (__CONST_SOCKADDR_ARG) &si_me, (socklen_t) sizeof(si_me)) == -1)
{
printf("[ERROR]: utilUdp_receiveUdpMsg - Can't bind the socket.\n");
printf("Trying again in 10 seconds...\n");
sleep(10);
}
if( recvfrom(s, (void *__restrict) buf, (size_t) UTIL_UDP_BUFFLEN, 0, (__SOCKADDR_ARG) &si_other, (socklen_t *__restrict) &slen) == -1)
{
printf("[ERROR]: utilUdp_receiveUdpMsg - Fail to receive packet.\n");
}
close(s);
// printf("[LOG]: utilUdp_receiveUdpMsg - Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
return true;
}