23
23
#include "SendUdp.h"
24
24
25
25
#ifdef WIN32
26
- #pragma warning (disable: 4786)
27
- #include <winsock2.h>
28
- #include <ws2tcpip.h>
29
- #include <windows.h>
30
- typedef int socklen_t ;
26
+ #pragma warning(disable : 4786)
27
+ #include <winsock2.h>
28
+ #include <ws2tcpip.h>
29
+ #include <windows.h>
30
+ typedef int socklen_t ;
31
31
#else
32
- #include <sys/types.h>
33
- #include <sys/socket.h>
34
- #include <arpa/inet.h>
35
- #include <netdb.h>
36
- #include <unistd.h>
37
- #include <ctype.h>
38
- #include <stdio.h>
39
- #include <string.h>
40
- #include <stdlib.h>
41
- #define SOCKET int
42
- #define closesocket (fd ) close(fd)
32
+ #include <sys/types.h>
33
+ #include <sys/socket.h>
34
+ #include <arpa/inet.h>
35
+ #include <netdb.h>
36
+ #include <unistd.h>
37
+ #include <ctype.h>
38
+ #include <stdio.h>
39
+ #include <string.h>
40
+ #include <stdlib.h>
41
+ #define SOCKET int
42
+ #define closesocket (fd ) close(fd)
43
43
#endif
44
44
45
45
/*
46
46
** SendUdp
47
47
*/
48
- int SendUdp (char * hostname , char * portNum , char * packetData , int packetSize ) {
49
- SOCKET sd ;
50
- int rc ;
51
- int port ;
52
- int errcode ;
53
- unsigned int i ;
54
- struct sockaddr_in cliAddr ;
55
- struct addrinfo hints ;
56
- struct addrinfo * result ;
57
-
58
- #ifdef WIN32
59
- WSADATA wsaData ;
60
- WSAStartup (WINSOCK_VERSION , & wsaData );
61
- #endif
62
-
63
- if (hostname == NULL ) {
48
+ int SendUdp (char * hostname , char * portNum , unsigned char * packetData , int packetSize )
49
+ {
50
+ SOCKET sd ;
51
+ int rc ;
52
+ int port ;
53
+ int errcode ;
54
+ unsigned int i ;
55
+ struct sockaddr_in cliAddr ;
56
+ struct addrinfo hints ;
57
+ struct addrinfo * result ;
58
+
59
+ #ifdef WIN32
60
+ WSADATA wsaData ;
61
+ WSAStartup (WINSOCK_VERSION , & wsaData );
62
+ #endif
63
+
64
+ if (hostname == NULL )
65
+ {
64
66
return -1 ;
65
67
}
66
-
68
+
67
69
/*
68
70
** Check port
69
71
*/
70
72
port = atoi (portNum );
71
- if (port == -1 ) {
73
+ if (port == -1 )
74
+ {
72
75
return -2 ;
73
76
}
74
-
77
+
75
78
/*
76
- **Criteria for selecting socket address
79
+ **Criteria for selecting socket address
77
80
*/
78
81
memset (& hints , 0 , sizeof (struct addrinfo ));
79
- hints .ai_family = AF_INET ; /*IPv4*/
82
+ hints .ai_family = AF_INET ; /*IPv4*/
80
83
hints .ai_socktype = SOCK_DGRAM ; /*Datagram socket*/
81
- hints .ai_flags = AI_CANONNAME ;
84
+ hints .ai_flags = AI_CANONNAME ;
82
85
hints .ai_protocol = 0 ; /*Any Protocol*/
83
-
86
+
84
87
errcode = getaddrinfo (hostname , portNum , & hints , & result );
85
- if (errcode != 0 ) {
88
+ if (errcode != 0 )
89
+ {
86
90
return -3 ;
87
91
}
88
92
89
93
printf ("sending data to '%s' (IP : %s); port %d\n" , result -> ai_canonname ,
90
- inet_ntoa (((struct sockaddr_in * )result -> ai_addr )-> sin_addr ), port );
94
+ inet_ntoa (((struct sockaddr_in * )result -> ai_addr )-> sin_addr ), port );
91
95
92
96
/*
93
97
** Create Socket
94
98
*/
95
99
sd = socket (AF_INET , SOCK_DGRAM , 0 );
96
100
97
- if (sd < 0 ){
101
+ if (sd < 0 )
102
+ {
98
103
return -4 ;
99
104
}
100
105
101
106
/*
102
107
** bind any port
103
108
*/
104
- cliAddr .sin_family = AF_INET ;
109
+ cliAddr .sin_family = AF_INET ;
105
110
cliAddr .sin_addr .s_addr = htonl (INADDR_ANY );
106
- cliAddr .sin_port = htons (0 );
111
+ cliAddr .sin_port = htons (0 );
107
112
108
- rc = bind (sd , (struct sockaddr * ) & cliAddr , sizeof (cliAddr ));
109
- if (rc < 0 ) {
113
+ rc = bind (sd , (struct sockaddr * )& cliAddr , sizeof (cliAddr ));
114
+ if (rc < 0 )
115
+ {
110
116
printf ("%s: cannot bind port\n" , portNum );
111
117
return -5 ;
112
118
}
113
119
114
120
printf ("Data to send:\n" );
115
121
i = 0 ;
116
- while (i < packetSize ) {
122
+ while (i < packetSize )
123
+ {
117
124
printf ("0x%02X " , packetData [i ] & 0xFF );
118
- if (++ i % 8 == 0 ) {
125
+ if (++ i % 8 == 0 )
126
+ {
119
127
puts ("" );
120
128
}
121
129
}
@@ -124,11 +132,10 @@ int SendUdp(char *hostname, char *portNum, char *packetData, int packetSize) {
124
132
/*
125
133
** send the event
126
134
*/
127
- rc = sendto (sd , (char * )packetData , packetSize , 0 ,
128
- result -> ai_addr , result -> ai_addrlen );
129
-
135
+ rc = sendto (sd , (char * )packetData , packetSize , 0 , result -> ai_addr , result -> ai_addrlen );
130
136
131
- if (rc < 0 ) {
137
+ if (rc < 0 )
138
+ {
132
139
freeaddrinfo (result );
133
140
closesocket (sd );
134
141
return -6 ;
0 commit comments