2019-06-12 15:02:43 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include "../src/ipc.h"
|
|
|
|
#include "../src/utils.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tcp connection to localhost port argv[1]
|
|
|
|
* message 1: pong
|
|
|
|
* message 2: echo with libipc format
|
|
|
|
*
|
|
|
|
* [messagetype | len | usertype | payload ]
|
|
|
|
* 1 B | 4 B | 1 B | ]
|
|
|
|
*/
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
int connection (char *ipstr, int port)
|
2019-06-12 15:02:43 +02:00
|
|
|
{
|
|
|
|
int sockfd;
|
2019-07-27 15:48:10 +02:00
|
|
|
SECURE_DECLARATION (struct sockaddr_in, server);
|
2019-06-12 15:02:43 +02:00
|
|
|
socklen_t addrlen;
|
|
|
|
|
|
|
|
// socket factory
|
2020-01-01 12:11:34 +01:00
|
|
|
T_PERROR_Q (((sockfd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1), "socket creation", EXIT_FAILURE);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
|
|
|
// init remote addr structure and other params
|
|
|
|
server.sin_family = AF_INET;
|
2020-01-01 12:11:34 +01:00
|
|
|
server.sin_port = htons (port);
|
|
|
|
addrlen = sizeof (struct sockaddr_in);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
|
|
|
// get addr from command line and convert it
|
2020-01-01 12:11:34 +01:00
|
|
|
T_PERROR_Q ((inet_pton (AF_INET, ipstr, &server.sin_addr) <= 0), "inet_pton", EXIT_FAILURE);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
printf ("Trying to connect to the remote host\n");
|
2019-07-27 15:48:10 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
T_PERROR_Q ((connect (sockfd, (struct sockaddr *)&server, addrlen) == -1), "connection", EXIT_FAILURE);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
printf ("Connection OK\n");
|
2019-06-12 15:02:43 +02:00
|
|
|
|
|
|
|
return sockfd;
|
|
|
|
}
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
void send_receive (int sockfd)
|
|
|
|
{
|
2019-07-27 15:48:10 +02:00
|
|
|
SECURE_BUFFER_DECLARATION (unsigned char, buf, BUFSIZ);
|
|
|
|
int paylen;
|
2019-06-12 15:02:43 +02:00
|
|
|
|
|
|
|
// first, send service name "pong"
|
|
|
|
// send string
|
2020-01-01 12:11:34 +01:00
|
|
|
T_PERROR_Q ((send (sockfd, "pong", 4, 0) == -1), "sending a message", EXIT_FAILURE);
|
2019-06-12 15:02:43 +02:00
|
|
|
printf ("message 'pong' sent\n");
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
T_PERROR_Q (((paylen = recv (sockfd, buf, BUFSIZ, 0)) <= 0), "cannot connect to networkd", EXIT_FAILURE);
|
2019-07-27 15:48:10 +02:00
|
|
|
print_hexa ("should be 'OK'", buf, paylen);
|
2019-06-12 15:02:43 +02:00
|
|
|
memset (buf, 0, BUFSIZ);
|
|
|
|
|
|
|
|
// 2 | 6 | 0 | "coucou"
|
|
|
|
// 1 B | 4 B | 1 | 6 B
|
2020-01-01 12:11:34 +01:00
|
|
|
ipc_message_raw_serialize ((char *)buf, MSG_TYPE_DATA, 42, "coucou", 6);
|
2020-07-04 19:02:43 +02:00
|
|
|
printf("\n");
|
|
|
|
print_hexa ("message to send", buf, 12);
|
|
|
|
printf("\n");
|
2019-07-27 15:48:10 +02:00
|
|
|
// sleep (1);
|
2020-01-01 12:11:34 +01:00
|
|
|
T_PERROR_Q ((send (sockfd, buf, 12, 0) == -1), "sending a message", EXIT_FAILURE);
|
2019-06-12 15:02:43 +02:00
|
|
|
printf ("message 'coucou' sent\n");
|
|
|
|
memset (buf, 0, BUFSIZ);
|
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
// receiving a message
|
2020-01-01 12:11:34 +01:00
|
|
|
T_PERROR_Q (((paylen = recv (sockfd, buf, BUFSIZ, 0)) < 0), "receiving a message", EXIT_FAILURE);
|
2020-07-04 19:02:43 +02:00
|
|
|
printf("\n");
|
2019-07-27 15:48:10 +02:00
|
|
|
|
|
|
|
if (paylen == 0) {
|
2019-06-12 15:02:43 +02:00
|
|
|
fprintf (stderr, "error: disconnection from the server\n");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
print_hexa ("RECEIVED MESSAGE", buf, paylen);
|
2019-06-12 15:02:43 +02:00
|
|
|
}
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
int main (int argc, char *argv[])
|
2019-06-12 15:02:43 +02:00
|
|
|
{
|
2019-07-27 15:48:10 +02:00
|
|
|
char *ipstr = "127.0.0.1";
|
2019-06-12 15:02:43 +02:00
|
|
|
int port = 9000;
|
2019-07-27 15:48:10 +02:00
|
|
|
if (argc == 2) {
|
2019-06-12 15:02:43 +02:00
|
|
|
port = atoi (argv[1]);
|
2020-01-01 12:11:34 +01:00
|
|
|
} else if (argc == 3) {
|
2019-07-27 15:48:10 +02:00
|
|
|
ipstr = argv[1];
|
|
|
|
port = atoi (argv[2]);
|
|
|
|
}
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
int sockfd = connection (ipstr, port);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
|
|
|
send_receive (sockfd);
|
|
|
|
|
2020-07-04 19:02:43 +02:00
|
|
|
printf ("\nDisconnection\n");
|
2019-06-12 15:02:43 +02:00
|
|
|
|
|
|
|
// close the socket
|
2020-01-01 12:11:34 +01:00
|
|
|
close (sockfd);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|