Skip to content

Instantly share code, notes, and snippets.

@ralight
Created January 9, 2014 11:30
Show Gist options
  • Save ralight/8332757 to your computer and use it in GitHub Desktop.
Save ralight/8332757 to your computer and use it in GitHub Desktop.
Test libmosquitto SSL against howsmyssl.com
cd mosquitto/lib
cp somewhere/hows.c .
gcc hows.c net_mosq.c logging_mosq.c memory_mosq.c messages_mosq.c mosquitto.c read_handle.c read_handle_client.c read_handle_shared.c send_mosq.c send_client_mosq.c time_mosq.c tls_mosq.c util_mosq.c will_mosq.c -I. -I.. -DWITH_TLS -lrt -lssl -lcrypto -o hows
./hows
#include <sys/select.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include "mosquitto.h"
#include "mosquitto_internal.h"
#include "net_mosq.h"
int main(int argc, char *argv[])
{
struct mosquitto *mosq;
int sock;
char outbuf[1024];
char inbuf[4096];
int pos = 0;
int olen, len;
struct timeval timeout;
fd_set readfds, writefds;
int fdcount;
mosquitto_lib_init();
mosq = mosquitto_new(NULL, 1, NULL);
mosq->tls_insecure = true;
mosquitto_tls_set(mosq, NULL, "/etc/ssl/certs", NULL, NULL, NULL);
_mosquitto_socket_connect(mosq, "www.howsmyssl.com", 443, NULL, true);
snprintf(outbuf, 1024, "GET https://www.howsmyssl.com/a/check HTTP/1.1\n\n");
olen = strlen(outbuf);
while(1){
FD_ZERO(&readfds);
FD_SET(mosq->sock, &readfds);
FD_ZERO(&writefds);
if(pos+1 < olen){
FD_SET(mosq->sock, &writefds);
}
timeout.tv_sec = 1;
timeout.tv_usec = 0;
fdcount = select(mosq->sock+1, &readfds, &writefds, NULL, &timeout);
if(fdcount <= 0){
return 1;
}else{
if(FD_ISSET(mosq->sock, &readfds)){
memset(inbuf, 0, 4096);
len = _mosquitto_net_read(mosq, inbuf, 4096);
if(len > 0){
printf("%s", inbuf);
fflush(stdout);
}else if(pos == olen && len == 0){
return 0;
}
}
if(FD_ISSET(mosq->sock, &writefds)){
if(pos < olen){
len = _mosquitto_net_write(mosq, &outbuf[pos], strlen(outbuf)-pos);
if(len > 0){
pos += len;
}
}
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment