Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When connecting, try all addresses and not one #1

Open
wants to merge 3 commits into
base: upstream
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 23 additions & 30 deletions MQTTClient-C/src/linux/MQTTLinux.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,49 +115,42 @@ void NetworkInit(Network* n)

int NetworkConnect(Network* n, char* addr, int port)
{
int type = SOCK_STREAM;
struct sockaddr_in address;
int rc = -1;
sa_family_t family = AF_INET;
struct addrinfo *result = NULL;
struct addrinfo hints = {0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL, NULL};
struct addrinfo hints = {.ai_socktype = SOCK_STREAM};
struct timeval tv = {.tv_sec = 3};

if ((rc = getaddrinfo(addr, NULL, &hints, &result)) == 0)
if (getaddrinfo(addr, NULL, &hints, &result) == 0)
{
struct addrinfo* res = result;
struct addrinfo* res;

/* prefer ip4 addresses */
while (res)
for (res = result; res; res = res->ai_next)
{
if (res->ai_family == AF_INET)
{
result = res;
break;
((struct sockaddr_in*)(res->ai_addr))->sin_port = htons(port);
else if (res->ai_family == AF_INET6)
((struct sockaddr_in6*)(res->ai_addr))->sin6_port = htons(port);
else
continue;

n->my_socket = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (n->my_socket != -1) {
setsockopt(n->my_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));
rc = connect(n->my_socket, res->ai_addr, res->ai_addrlen);
if (rc == 0) {
tv.tv_sec = 0;
setsockopt(n->my_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));
break;
}

close(n->my_socket);
n->my_socket = -1;
}
res = res->ai_next;
}

if (result->ai_family == AF_INET)
{
address.sin_port = htons(port);
address.sin_family = family = AF_INET;
address.sin_addr = ((struct sockaddr_in*)(result->ai_addr))->sin_addr;
}
else
rc = -1;

freeaddrinfo(result);
}

if (rc == 0)
{
n->my_socket = socket(family, type, 0);
if (n->my_socket != -1)
rc = connect(n->my_socket, (struct sockaddr*)&address, sizeof(address));
else
rc = -1;
}

return rc;
}

Expand Down
6 changes: 3 additions & 3 deletions MQTTPacket/samples/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,20 @@ int main(int argc, char *argv[])
int buflen = sizeof(buf);
int len = 0;
char *host = "m2m.eclipse.org";
int port = 1883;
char *port = "1883";

stop_init();
if (argc > 1)
host = argv[1];

if (argc > 2)
port = atoi(argv[2]);
port = argv[2];

mysock = transport_open(host, port);
if(mysock < 0)
return mysock;

printf("Sending to hostname %s port %d\n", host, port);
printf("Sending to hostname %s port %s\n", host, port);

data.clientID.cstring = "me";
data.keepAliveInterval = KEEPALIVE_INTERVAL;
Expand Down
6 changes: 3 additions & 3 deletions MQTTPacket/samples/ping_nb.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ int main(int argc, char *argv[])
int buflen = sizeof(buf);
int len = 0;
char *host = "m2m.eclipse.org";
int port = 1883;
char *port = "1883";
MQTTTransport mytransport;
int state;

Expand All @@ -83,13 +83,13 @@ int main(int argc, char *argv[])
host = argv[1];

if (argc > 2)
port = atoi(argv[2]);
port = argv[2];

mysock = transport_open(host, port);
if(mysock < 0)
return mysock;

printf("Sending to hostname %s port %d\n", host, port);
printf("Sending to hostname %s port %s\n", host, port);

mytransport.sck = &mysock;
mytransport.getfn = transport_getdatanb;
Expand Down
6 changes: 3 additions & 3 deletions MQTTPacket/samples/pub0sub1.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@ int main(int argc, char *argv[])
int payloadlen = strlen(payload);
int len = 0;
char *host = "m2m.eclipse.org";
int port = 1883;
char *port = "1883";

stop_init();
if (argc > 1)
host = argv[1];

if (argc > 2)
port = atoi(argv[2]);
port = argv[2];

mysock = transport_open(host, port);
if(mysock < 0)
return mysock;

printf("Sending to hostname %s port %d\n", host, port);
printf("Sending to hostname %s port %s\n", host, port);

data.clientID.cstring = "me";
data.keepAliveInterval = 20;
Expand Down
6 changes: 3 additions & 3 deletions MQTTPacket/samples/pub0sub1_nb.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ int main(int argc, char *argv[])
int payloadlen = strlen(payload);
int len = 0;
char *host = "m2m.eclipse.org";
int port = 1883;
char *port = "1883";
MQTTTransport mytransport;

stop_init();
if (argc > 1)
host = argv[1];

if (argc > 2)
port = atoi(argv[2]);
port = argv[2];

mysock = transport_open(host, port);
if(mysock < 0)
return mysock;

printf("Sending to hostname %s port %d\n", host, port);
printf("Sending to hostname %s port %s\n", host, port);

mytransport.sck = &mysock;
mytransport.getfn = transport_getdatanb;
Expand Down
6 changes: 3 additions & 3 deletions MQTTPacket/samples/qos0pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ int main(int argc, char *argv[])
int payloadlen = strlen(payload);
int len = 0;
char *host = "m2m.eclipse.org";
int port = 1883;
char *port = "1883";

if (argc > 1)
host = argv[1];

if (argc > 2)
port = atoi(argv[2]);
port = argv[2];

mysock = transport_open(host,port);
if(mysock < 0)
return mysock;

printf("Sending to hostname %s port %d\n", host, port);
printf("Sending to hostname %s port %s\n", host, port);

data.clientID.cstring = "me";
data.keepAliveInterval = 20;
Expand Down
78 changes: 20 additions & 58 deletions MQTTPacket/samples/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,90 +106,52 @@ return >=0 for a socket descriptor, <0 for an error code
@todo Basically moved from the sample without changes, should accomodate same usage for 'sock' for clarity,
removing indirections
*/
int transport_open(char* addr, int port)
int transport_open(char* addr, char *port)
{
int* sock = &mysock;
int type = SOCK_STREAM;
struct sockaddr_in address;
#if defined(AF_INET6)
struct sockaddr_in6 address6;
#endif
int rc = -1;
#if defined(WIN32)
short family;
#else
sa_family_t family = AF_INET;
#endif
struct addrinfo *result = NULL;
struct addrinfo hints = {0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL, NULL};
static struct timeval tv;
struct addrinfo hints = {.ai_socktype = SOCK_STREAM, .ai_flags = AI_NUMERICSERV};
struct timeval tv = {.tv_sec = 3};

*sock = -1;
if (addr[0] == '[')
++addr;

if ((rc = getaddrinfo(addr, NULL, &hints, &result)) == 0)
if (getaddrinfo(addr, port, &hints, &result) == 0)
{
struct addrinfo* res = result;

/* prefer ip4 addresses */
while (res)
{
if (res->ai_family == AF_INET)
{
result = res;
break;
}
res = res->ai_next;
}

#if defined(AF_INET6)
if (result->ai_family == AF_INET6)
{
address6.sin6_port = htons(port);
address6.sin6_family = family = AF_INET6;
address6.sin6_addr = ((struct sockaddr_in6*)(result->ai_addr))->sin6_addr;
}
else
#endif
if (result->ai_family == AF_INET)
for (res = result; res; res = res->ai_next)
{
address.sin_port = htons(port);
address.sin_family = family = AF_INET;
address.sin_addr = ((struct sockaddr_in*)(result->ai_addr))->sin_addr;
}
else
rc = -1;

freeaddrinfo(result);
}
*sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (*sock == -1)
continue;

if (rc == 0)
{
*sock = socket(family, type, 0);
if (*sock != -1)
{
#if defined(NOSIGPIPE)
int opt = 1;

if (setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&opt, sizeof(opt)) != 0)
Log(TRACE_MIN, -1, "Could not set SO_NOSIGPIPE for socket %d", *sock);
#endif

if (family == AF_INET)
rc = connect(*sock, (struct sockaddr*)&address, sizeof(address));
#if defined(AF_INET6)
else
rc = connect(*sock, (struct sockaddr*)&address6, sizeof(address6));
#endif
setsockopt(mysock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));
if (connect(*sock, res->ai_addr, res->ai_addrlen) == 0)
break;

close(*sock);
*sock = -1;
}

freeaddrinfo(result);
}

if (mysock == INVALID_SOCKET)
return rc;
return mysock;

tv.tv_sec = 1; /* 1 second Timeout */
tv.tv_usec = 0;
setsockopt(mysock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
tv.tv_sec = 0;
setsockopt(mysock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));
return mysock;
}

Expand Down
2 changes: 1 addition & 1 deletion MQTTPacket/samples/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
int transport_sendPacketBuffer(int sock, unsigned char* buf, int buflen);
int transport_getdata(unsigned char* buf, int count);
int transport_getdatanb(void *sck, unsigned char* buf, int count);
int transport_open(char* host, int port);
int transport_open(char* host, char* port);
int transport_close(int sock);