Skip to content

Commit

Permalink
Merge pull request #133 from nasa/integration-candidate
Browse files Browse the repository at this point in the history
Integration Candidate: 2020-09-02
  • Loading branch information
yammajamma authored Sep 9, 2020
2 parents 06a4747 + 5265076 commit aa82078
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 57 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ See [Guide-GroundSystem.md](https://github.com/nasa/cFS-GroundSystem/blob/master

## Version History

### Development Build: 2.2.0-rc1+dev8

- Replaces old code that caused a cast-align warning when strict. Refactored and removed unnecessary code while also following recommended model for getaddrinfo. Removed old windows support/defines/etc (likely not tested for years, no longer supported).
- Reduce the size of the strncpy so that it ensures there's a null byte at the end of the string buffer.
- See <https://github.com/nasa/cFS-GroundSystem/pull/133>

### Development Build: 2.2.0+dev2

- Fixes multiple typos
Expand Down
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[1025] = {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;
}
2 changes: 1 addition & 1 deletion Subsystems/cmdUtil/cmdUtil.c
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ int main(int argc, char *argv[])
}

/* Copy the data over (zero fills) */
strncpy(sbuf, &tail[1], sizeof(sbuf));
strncpy(sbuf, &tail[1], sizeof(sbuf) - 1);
CopyData(cmd.Packet, &startbyte, sbuf, tempull);

/* Reset tail so it doesn't trigger error */
Expand Down
2 changes: 1 addition & 1 deletion _version.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#

# Development Build Macro Definitions
_cFS_GrndSys_build_number = 2
_cFS_GrndSys_build_number = 8
_cFS_GrndSys_build_baseline = "v2.2.0-rc1"

# Version Number Definitions
Expand Down

0 comments on commit aa82078

Please sign in to comment.