Skip to content

Commit

Permalink
Network errors are printed correctly on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Mar 7, 2016
1 parent a79412e commit abc2f3f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Broker:
- Reconnecting client with clean session set to false doesn't start with mid=1
again.
- Will topic isn't truncated by one byte when using a mount_point any more.
- Network errors are printed correctly on Windows.

Client library:
- Fix the case where a message received just before the keepalive timer
Expand Down
36 changes: 23 additions & 13 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ static int tls_ex_index_listener = -1;
extern unsigned int g_socket_connections;
#endif


static void net__print_error(int log, const char *format_str)
{
#ifdef WIN32
char *buf;

FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, WSAGetLastError(), LANG_NEUTRAL, &buf, 0, NULL);

_mosquitto_log_printf(NULL, log, format_str, buf);
LocalFree(buf);
#else
char buf[256];

strerror_r(errno, buf, 256);
_mosquitto_log_printf(NULL, log, format_str, buf);
#endif
}


int mqtt3_socket_accept(struct mosquitto_db *db, mosq_sock_t listensock)
{
int i;
Expand Down Expand Up @@ -335,7 +355,6 @@ int mqtt3_socket_listen(struct _mqtt3_listener *listener)
X509_STORE *store;
X509_LOOKUP *lookup;
#endif
char buf[256];

if(!listener) return MOSQ_ERR_INVAL;

Expand All @@ -361,8 +380,7 @@ int mqtt3_socket_listen(struct _mqtt3_listener *listener)

sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(sock == INVALID_SOCKET){
strerror_r(errno, buf, 256);
_mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: %s", buf);
net__print_error(MOSQ_LOG_WARNING, "Warning: %s");
continue;
}
listener->sock_count++;
Expand All @@ -385,21 +403,13 @@ int mqtt3_socket_listen(struct _mqtt3_listener *listener)
}

if(bind(sock, rp->ai_addr, rp->ai_addrlen) == -1){
#ifdef WIN32
errno = WSAGetLastError();
#endif
strerror_r(errno, buf, 256);
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: %s", buf);
net__print_error(MOSQ_LOG_ERR, "Error: %s");
COMPAT_CLOSE(sock);
return 1;
}

if(listen(sock, 100) == -1){
#ifdef WIN32
errno = WSAGetLastError();
#endif
strerror_r(errno, buf, 256);
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: %s", buf);
net__print_error(MOSQ_LOG_ERR, "Error: %s");
COMPAT_CLOSE(sock);
return 1;
}
Expand Down

0 comments on commit abc2f3f

Please sign in to comment.