2017-02-02 19:10:12 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include "libp2p/net/multistream.h"
|
|
|
|
|
|
|
|
int test_multistream_connect() {
|
2017-02-23 20:16:04 +00:00
|
|
|
int retVal = 0;
|
2017-02-02 19:10:12 +00:00
|
|
|
char* response;
|
|
|
|
size_t response_size;
|
|
|
|
|
2017-02-23 20:16:04 +00:00
|
|
|
struct Stream* stream = libp2p_net_multistream_connect("www.jmjatlanta.com", 4001);
|
|
|
|
if (stream == NULL)
|
2017-02-02 23:27:57 +00:00
|
|
|
goto exit;
|
|
|
|
|
|
|
|
retVal = 1;
|
|
|
|
|
|
|
|
exit:
|
2017-04-27 04:56:18 +00:00
|
|
|
if (stream != NULL) {
|
|
|
|
struct SessionContext ctx;
|
|
|
|
ctx.insecure_stream = stream;
|
2017-11-08 15:51:43 +00:00
|
|
|
stream->close(stream);
|
2017-04-27 04:56:18 +00:00
|
|
|
libp2p_net_multistream_stream_free(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
2017-02-02 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int test_multistream_get_list() {
|
2017-03-02 21:14:52 +00:00
|
|
|
int retVal = 0;
|
2017-10-23 14:01:03 +00:00
|
|
|
struct StreamMessage* response;
|
2017-03-02 21:14:52 +00:00
|
|
|
char* filtered = NULL;
|
2017-02-02 23:27:57 +00:00
|
|
|
|
2017-03-19 12:42:52 +00:00
|
|
|
struct SessionContext session;
|
2017-07-13 12:27:10 +00:00
|
|
|
session.insecure_stream = libp2p_net_multistream_connect("10.211.55.2", 4001);
|
2017-03-13 13:53:20 +00:00
|
|
|
|
2017-10-23 20:21:50 +00:00
|
|
|
if (session.insecure_stream == NULL)
|
2017-02-02 19:10:12 +00:00
|
|
|
goto exit;
|
|
|
|
|
2017-02-02 20:43:35 +00:00
|
|
|
// try to respond something, ls command
|
2017-10-23 14:47:54 +00:00
|
|
|
struct StreamMessage outgoing;
|
|
|
|
outgoing.data = (uint8_t*)"ls\n";
|
|
|
|
outgoing.data_size = 3;
|
|
|
|
if (libp2p_net_multistream_write(&session, &outgoing) <= 0)
|
2017-02-02 19:10:12 +00:00
|
|
|
goto exit;
|
|
|
|
|
|
|
|
// retrieve response
|
2017-10-23 14:01:03 +00:00
|
|
|
retVal = libp2p_net_multistream_read(&session, &response, 5);
|
2017-02-02 19:10:12 +00:00
|
|
|
if (retVal <= 0)
|
|
|
|
goto exit;
|
|
|
|
|
2017-10-23 14:01:03 +00:00
|
|
|
filtered = malloc(response->data_size + 1);
|
|
|
|
strncpy(filtered, (char*)response->data, response->data_size);
|
|
|
|
filtered[response->data_size] = 0;
|
2017-03-02 21:14:52 +00:00
|
|
|
|
|
|
|
fprintf(stdout, "Response from multistream ls: %s", (char*)filtered);
|
2017-02-02 23:27:57 +00:00
|
|
|
|
2017-02-02 19:10:12 +00:00
|
|
|
retVal = 1;
|
|
|
|
|
|
|
|
exit:
|
2017-04-27 04:56:18 +00:00
|
|
|
if (session.insecure_stream != NULL) {
|
2017-11-08 15:51:43 +00:00
|
|
|
session.insecure_stream->close(session.insecure_stream);
|
2017-04-27 04:56:18 +00:00
|
|
|
libp2p_net_multistream_stream_free(session.insecure_stream);
|
|
|
|
}
|
2017-10-23 14:01:03 +00:00
|
|
|
libp2p_stream_message_free(response);
|
2017-04-27 04:56:18 +00:00
|
|
|
if (filtered != NULL)
|
|
|
|
free(filtered);
|
2017-02-02 19:10:12 +00:00
|
|
|
|
|
|
|
return retVal > 0;
|
|
|
|
}
|