Skip to content

Commit

Permalink
Fix nasa#93, Refactor and resolve cast align warning
Browse files Browse the repository at this point in the history
  • Loading branch information
skliper committed Aug 31, 2020
1 parent 5e879b9 commit 2e084c3
Showing 1 changed file with 35 additions and 55 deletions.
90 changes: 35 additions & 55 deletions Subsystems/cmdUtil/SendUdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,35 @@
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** Udp packet send routine
*/

/*
* Udp packet send routine
*/

#include "SendUdp.h"

#ifdef WIN32
#pragma warning(disable : 4786)
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
typedef int socklen_t;
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SOCKET int
#define closesocket(fd) close(fd)
#endif

/*
** SendUdp
*/
int SendUdp(char *hostname, char *portNum, unsigned char *packetData, int packetSize)
{
SOCKET sd;
int rc;
int port;
int errcode;
unsigned int i;
struct sockaddr_in cliAddr;
struct addrinfo hints;
struct addrinfo * result;

#ifdef WIN32
WSADATA wsaData;
WSAStartup(WINSOCK_VERSION, &wsaData);
#endif
int sd;
int rc;
int port;
unsigned int i;
struct addrinfo hints;
struct addrinfo *result;
struct addrinfo *rp;
char hbuf[NI_MAXHOST] = {0};

if (hostname == NULL)
{
Expand All @@ -78,44 +63,41 @@ int SendUdp(char *hostname, char *portNum, unsigned char *packetData, int packet
/*
**Criteria for selecting socket address
*/
memset(&hints, 0, sizeof(struct addrinfo));
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; /*IPv4*/
hints.ai_socktype = SOCK_DGRAM; /*Datagram socket*/
hints.ai_flags = AI_CANONNAME;
hints.ai_protocol = 0; /*Any Protocol*/

errcode = getaddrinfo(hostname, portNum, &hints, &result);
if (errcode != 0)
rc = getaddrinfo(hostname, portNum, &hints, &result);
if (rc != 0)
{
return -3;
}

printf("sending data to '%s' (IP : %s); port %d\n", result->ai_canonname,
inet_ntoa(((struct sockaddr_in *)result->ai_addr)->sin_addr), port);
/* Loop through potential addresses until sucessful socket/connect */
for (rp = result; rp != NULL; rp = rp->ai_next)
{
sd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);

if (sd == -1)
continue;

/*
** Create Socket
*/
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (connect(sd, rp->ai_addr, rp->ai_addrlen) != -1)
break; /* Success */

close(sd);
}

if (sd < 0)
if (rp == NULL)
{
return -4;
}

/*
** bind any port
*/
cliAddr.sin_family = AF_INET;
cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliAddr.sin_port = htons(0);
if (getnameinfo(rp->ai_addr, rp->ai_addrlen, hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST));
printf("sending data to '%s' (IP : %s); port %d\n", rp->ai_canonname, hbuf, port);

rc = bind(sd, (struct sockaddr *)&cliAddr, sizeof(cliAddr));
if (rc < 0)
{
printf("%s: cannot bind port\n", portNum);
return -5;
}
freeaddrinfo(result);

printf("Data to send:\n");
i = 0;
Expand All @@ -132,16 +114,14 @@ int SendUdp(char *hostname, char *portNum, unsigned char *packetData, int packet
/*
** send the event
*/
rc = sendto(sd, (char *)packetData, packetSize, 0, result->ai_addr, result->ai_addrlen);
rc = send(sd, (char *)packetData, packetSize, 0);

if (rc < 0)
close(sd);

if (rc != packetSize)
{
freeaddrinfo(result);
closesocket(sd);
return -6;
}

freeaddrinfo(result);
closesocket(sd);
return 0;
}

0 comments on commit 2e084c3

Please sign in to comment.