use parseInt, avoid strndup
This commit is contained in:
+25
-24
@@ -22,6 +22,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "device.h"
|
#include "device.h"
|
||||||
|
#include "data.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@@ -45,31 +46,31 @@ Device::~Device()
|
|||||||
Device* Device::create(const char* name, const bool checkDevice, const bool readonly,
|
Device* Device::create(const char* name, const bool checkDevice, const bool readonly,
|
||||||
void (*logRawFunc)(const unsigned char byte, bool received))
|
void (*logRawFunc)(const unsigned char byte, bool received))
|
||||||
{
|
{
|
||||||
if (strchr(name, '/') == NULL) {
|
if (strchr(name, '/') == NULL && strchr(name, ':') != NULL) {
|
||||||
char* pos = strchr((char*)name, ':');
|
char* dup = strdup(name);
|
||||||
if (pos != NULL) {
|
char* pos = strchr(dup, ':');
|
||||||
char* end = NULL;
|
result_t result = RESULT_OK;
|
||||||
unsigned long int port = strtoul(pos+1, &end, 10);
|
unsigned long int port = parseInt(pos+1, 10, 1, 65535, result);
|
||||||
if (end == NULL || end == pos+1 || *end != 0 || port < 1 || port > 65535) {
|
if (result!=RESULT_OK) {
|
||||||
return NULL; // invalid port
|
free(dup);
|
||||||
}
|
return NULL; // invalid port
|
||||||
struct sockaddr_in address;
|
|
||||||
memset((char*)&address, 0, sizeof(address));
|
|
||||||
|
|
||||||
char* host = strndup(name, pos-name);
|
|
||||||
if (inet_aton(host, &address.sin_addr) == 0) {
|
|
||||||
struct hostent* h = gethostbyname(host);
|
|
||||||
if (h == NULL) {
|
|
||||||
free(host);
|
|
||||||
return NULL; // invalid host
|
|
||||||
}
|
|
||||||
memcpy(&address.sin_addr, h->h_addr_list[0], h->h_length);
|
|
||||||
}
|
|
||||||
free(host);
|
|
||||||
address.sin_family = AF_INET;
|
|
||||||
address.sin_port = htons((uint16_t)port);
|
|
||||||
return new NetworkDevice(name, address, readonly, logRawFunc);
|
|
||||||
}
|
}
|
||||||
|
struct sockaddr_in address;
|
||||||
|
memset((char*)&address, 0, sizeof(address));
|
||||||
|
|
||||||
|
*pos = 0;
|
||||||
|
if (inet_aton(dup, &address.sin_addr) == 0) {
|
||||||
|
struct hostent* h = gethostbyname(dup);
|
||||||
|
if (h == NULL) {
|
||||||
|
free(dup);
|
||||||
|
return NULL; // invalid host
|
||||||
|
}
|
||||||
|
memcpy(&address.sin_addr, h->h_addr_list[0], h->h_length);
|
||||||
|
}
|
||||||
|
free(dup);
|
||||||
|
address.sin_family = AF_INET;
|
||||||
|
address.sin_port = htons((uint16_t)port);
|
||||||
|
return new NetworkDevice(name, address, readonly, logRawFunc);
|
||||||
}
|
}
|
||||||
return new SerialDevice(name, checkDevice, readonly, logRawFunc);
|
return new SerialDevice(name, checkDevice, readonly, logRawFunc);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user