2016-09-16 00:09:04 +02:00
|
|
|
#include "tcpdserver.h"
|
2016-09-18 01:03:37 +02:00
|
|
|
#include "../lib/communication.h"
|
2016-09-18 00:17:47 +02:00
|
|
|
|
2016-09-16 00:09:04 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
2016-09-16 15:22:14 +02:00
|
|
|
#include <string.h>
|
2016-09-16 21:41:23 +02:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <sys/stat.h> // mkfifo
|
|
|
|
#include <linux/limits.h>
|
2016-09-16 00:09:04 +02:00
|
|
|
|
|
|
|
#define PORT 6000
|
2016-09-16 15:22:14 +02:00
|
|
|
#define BUF_SIZE 1024
|
2016-09-16 21:41:23 +02:00
|
|
|
#define TMPDIR "/tmp/ipc/"
|
2016-09-16 00:09:04 +02:00
|
|
|
|
|
|
|
int init_connection(void)
|
|
|
|
{
|
|
|
|
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
struct sockaddr_in sin = { 0 };
|
|
|
|
|
|
|
|
if(sock == -1)
|
|
|
|
{
|
|
|
|
perror("socket()");
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
sin.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
sin.sin_port = htons(PORT);
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
|
|
|
|
if(bind(sock,(struct sockaddr *) &sin, sizeof sin) == -1)
|
|
|
|
{
|
|
|
|
perror("bind()");
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(listen(sock, 5) == -1)
|
|
|
|
{
|
|
|
|
perror("listen()");
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2016-09-16 15:22:14 +02:00
|
|
|
void write_message(int sock, const char *buffer)
|
|
|
|
{
|
|
|
|
if(send(sock, buffer, strlen(buffer), 0) < 0)
|
|
|
|
{
|
|
|
|
perror("send()");
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
}
|
2016-09-16 00:09:04 +02:00
|
|
|
|
2016-09-16 15:22:14 +02:00
|
|
|
int read_message(int sock, char *buffer)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
if((n = recv(sock, buffer, BUF_SIZE - 1, 0)) < 0)
|
|
|
|
{
|
|
|
|
perror("recv()");
|
|
|
|
/* if recv error we disonnect the client */
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[n] = 0;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2016-09-16 21:41:23 +02:00
|
|
|
void endConnection(int sock, int sfd) {
|
|
|
|
close(sock);
|
|
|
|
close(sfd);
|
|
|
|
}
|
|
|
|
|
2016-09-16 15:22:14 +02:00
|
|
|
void printClientAddr(struct sockaddr_in *csin) {
|
2016-09-16 21:41:23 +02:00
|
|
|
printf("New client\n");
|
|
|
|
printf("IP Addr : %s\n", inet_ntoa(csin->sin_addr));
|
|
|
|
printf("Port : %u\n", ntohs(csin->sin_port));
|
2016-09-16 15:22:14 +02:00
|
|
|
}
|
2016-09-16 00:09:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char * argv[], char **env) {
|
|
|
|
|
|
|
|
int sock = init_connection();
|
2016-09-16 21:41:23 +02:00
|
|
|
// char buffer[BUF_SIZE];
|
2016-09-16 00:09:04 +02:00
|
|
|
|
|
|
|
struct sockaddr_in csin = { 0 };
|
|
|
|
socklen_t sinsize = sizeof csin;
|
2016-09-16 21:41:23 +02:00
|
|
|
int sfd = accept(sock, (struct sockaddr *)&csin, &sinsize);
|
|
|
|
if(sfd == -1)
|
2016-09-16 00:09:04 +02:00
|
|
|
{
|
|
|
|
perror("accept()");
|
2016-09-16 21:41:23 +02:00
|
|
|
close(sock);
|
2016-09-16 00:09:04 +02:00
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
|
2016-09-16 15:22:14 +02:00
|
|
|
printClientAddr(&csin);
|
2016-09-16 21:41:23 +02:00
|
|
|
printf("%d \n",getpid());
|
|
|
|
|
|
|
|
pthread_t listenPid;
|
|
|
|
p_data *p_d_listen = malloc (sizeof(p_data));
|
|
|
|
//p_d_listen.c_sock = NULL;
|
|
|
|
p_d_listen->sfd = sfd;
|
2016-09-21 20:38:04 +02:00
|
|
|
p_d_listen->index = 0;
|
2016-09-16 21:41:23 +02:00
|
|
|
|
2016-09-21 20:38:04 +02:00
|
|
|
int ret = pthread_create( &listenPid, NULL, &service_thread, (void *) p_d_listen);
|
2016-09-16 21:41:23 +02:00
|
|
|
if (ret) {
|
|
|
|
perror("pthread_create()");
|
|
|
|
endConnection(sock, sfd);
|
|
|
|
exit(errno);
|
|
|
|
} else {
|
|
|
|
printf("Creation of listen thread \n");
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_join(listenPid, NULL);
|
|
|
|
|
|
|
|
endConnection(sock, sfd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-21 20:38:04 +02:00
|
|
|
void * service_thread(void * pdata) {
|
2016-09-16 21:41:23 +02:00
|
|
|
p_data *pda = (p_data*) pdata;
|
|
|
|
char buffer[BUF_SIZE];
|
|
|
|
char *service;
|
|
|
|
int version;
|
2016-09-21 20:38:04 +02:00
|
|
|
int clientSock = pda->sfd;
|
2016-09-16 15:22:14 +02:00
|
|
|
|
2016-09-21 20:38:04 +02:00
|
|
|
if (read_message(clientSock, buffer) == -1) {
|
2016-09-16 15:22:14 +02:00
|
|
|
perror("read_message()");
|
2016-09-16 21:41:23 +02:00
|
|
|
return NULL;
|
2016-09-16 15:22:14 +02:00
|
|
|
}else {
|
2016-09-18 00:17:47 +02:00
|
|
|
parseServiceVersion(buffer, &service, &version);
|
2016-09-16 21:41:23 +02:00
|
|
|
}
|
2016-09-18 00:17:47 +02:00
|
|
|
|
2016-09-16 21:41:23 +02:00
|
|
|
/* TODO : service correspond au service que le client veut utiliser
|
|
|
|
** il faut comparer service à un tableau qui contient les services
|
|
|
|
** disponibles
|
|
|
|
*/
|
|
|
|
|
2016-09-21 20:38:04 +02:00
|
|
|
//path service
|
|
|
|
char * servicePath = malloc(sizeof PATH_MAX);
|
|
|
|
strcat(servicePath, TMPDIR);
|
|
|
|
strcat(servicePath, service);
|
|
|
|
|
|
|
|
//pid index version
|
|
|
|
char * piv = malloc(sizeof PATH_MAX);
|
|
|
|
makePivMessage(&piv, getpid(), pda->index, version);
|
|
|
|
printf("piv : %s\n",piv );
|
|
|
|
|
|
|
|
//write pid index version in T/I/S of service
|
|
|
|
int ret = file_write(servicePath, piv, strlen(piv));
|
|
|
|
if(ret == 0) {
|
|
|
|
perror("file_write()");
|
|
|
|
free(servicePath);
|
|
|
|
free(piv);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-09-16 21:41:23 +02:00
|
|
|
|
|
|
|
// gets the service path, such as /tmp/ipc/pid-index-version-in/out
|
|
|
|
char *pathname[2];
|
2016-09-21 20:38:04 +02:00
|
|
|
inOutPathCreate(pathname, pda->index, version);
|
2016-09-16 21:41:23 +02:00
|
|
|
|
2016-09-21 20:38:04 +02:00
|
|
|
// printf("pathname 1: %s\n",pathname[0] );
|
|
|
|
// printf("pathname 2: %s\n",pathname[1] );
|
2016-09-16 21:41:23 +02:00
|
|
|
|
2016-09-21 20:38:04 +02:00
|
|
|
//create in out files
|
|
|
|
if(fifo_create(pathname[0]) != 0) {
|
2016-09-16 21:41:23 +02:00
|
|
|
perror("fifo_create()");
|
|
|
|
return NULL;
|
2016-09-16 15:22:14 +02:00
|
|
|
}
|
|
|
|
|
2016-09-16 21:41:23 +02:00
|
|
|
if(fifo_create(pathname[1]) != 0) {
|
|
|
|
perror("fifo_create()");
|
|
|
|
return NULL;
|
2016-09-18 00:17:47 +02:00
|
|
|
}
|
|
|
|
|
2016-09-21 20:38:04 +02:00
|
|
|
printf("pathname[0] : %s\n", pathname[0]);
|
|
|
|
//open -in fifo file
|
|
|
|
int fdin = open (pathname[0], O_RDWR);
|
|
|
|
if (fdin <= 0) {
|
|
|
|
printf("open: fd < 0\n");
|
|
|
|
perror ("open()");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-09-18 00:17:47 +02:00
|
|
|
|
2016-09-21 20:38:04 +02:00
|
|
|
//utilisation du select() pour surveiller la socket du client et fichier in
|
|
|
|
fd_set rdfs;
|
|
|
|
int max = clientSock > fdin ? clientSock : fdin;
|
|
|
|
max = max > STDIN_FILENO ? max : STDIN_FILENO;
|
2016-09-18 00:17:47 +02:00
|
|
|
|
2016-09-21 20:38:04 +02:00
|
|
|
while(1) {
|
|
|
|
FD_ZERO(&rdfs);
|
2016-09-18 00:17:47 +02:00
|
|
|
|
2016-09-21 20:38:04 +02:00
|
|
|
/* add STDIN_FILENO */
|
|
|
|
FD_SET(STDIN_FILENO, &rdfs);
|
|
|
|
|
|
|
|
//add client's socket
|
|
|
|
FD_SET(clientSock, &rdfs);
|
|
|
|
|
|
|
|
//add in file
|
|
|
|
FD_SET(fdin, &rdfs);
|
|
|
|
|
|
|
|
if(select(max + 1, &rdfs, NULL, NULL, NULL) == -1)
|
|
|
|
{
|
|
|
|
perror("select()");
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* something from standard input : i.e keyboard */
|
|
|
|
if(FD_ISSET(STDIN_FILENO, &rdfs))
|
|
|
|
{
|
|
|
|
/* stop process when type on keyboard */
|
|
|
|
break;
|
|
|
|
}else if (FD_ISSET(clientSock, &rdfs)) {
|
|
|
|
if(read_message(clientSock, buffer) > 0) {
|
|
|
|
printf("message : %s\n",buffer );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*if(file_write(pathname[1], buffer, strlen(buffer)) < 0) {
|
|
|
|
perror("file_write");
|
|
|
|
}*/
|
|
|
|
}else {
|
|
|
|
if(read(fdin, &buffer, BUF_SIZE) < 0) {
|
|
|
|
perror("read()");
|
|
|
|
}
|
|
|
|
printf("file in : %s\n", buffer );
|
2016-09-18 00:17:47 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-21 20:38:04 +02:00
|
|
|
free(servicePath);
|
|
|
|
free(piv);
|
2016-09-18 00:17:47 +02:00
|
|
|
|
2016-09-16 21:41:23 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:17:47 +02:00
|
|
|
void parseServiceVersion(char * buf, char ** service, int *version) {
|
2016-09-16 21:41:23 +02:00
|
|
|
char *token = NULL, *saveptr = NULL;
|
|
|
|
char *str = NULL;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (str = buf, i = 1; ; str = NULL, i++) {
|
|
|
|
token = strtok_r(str, " ", &saveptr);
|
|
|
|
if (token == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i == 1) {
|
|
|
|
*service = token;
|
|
|
|
}
|
|
|
|
else if (i == 2) {
|
|
|
|
*version = strtoul(token, NULL, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int fifo_create (char * path)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if ((ret = mkfifo (path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) {
|
|
|
|
switch (errno) {
|
|
|
|
case EACCES :
|
|
|
|
printf ("file %s : EACCES\n", path);
|
|
|
|
return 1;
|
|
|
|
case EEXIST :
|
|
|
|
printf ("file %s : EEXIST\n", path);
|
|
|
|
break;
|
|
|
|
case ENAMETOOLONG :
|
|
|
|
printf ("file %s : ENAMETOOLONG\n", path);
|
|
|
|
return 2;
|
|
|
|
case ENOENT :
|
|
|
|
printf ("file %s : ENOENT\n", path);
|
|
|
|
return 3;
|
|
|
|
case ENOSPC :
|
|
|
|
printf ("file %s : ENOSPC\n", path);
|
|
|
|
return 4;
|
|
|
|
case ENOTDIR :
|
|
|
|
printf ("file %s : ENOTDIR\n", path);
|
|
|
|
return 5;
|
|
|
|
case EROFS :
|
|
|
|
printf ("file %s : EROFS\n", path);
|
|
|
|
return 6;
|
|
|
|
default :
|
|
|
|
printf ("err file %s unknown\n", path);
|
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-21 20:38:04 +02:00
|
|
|
void inOutPathCreate(char ** pathname, int index, int version) {
|
2016-09-16 21:41:23 +02:00
|
|
|
pathname[0] = malloc(sizeof PATH_MAX);
|
|
|
|
pathname[1] = malloc(sizeof PATH_MAX);
|
|
|
|
|
|
|
|
int length = snprintf( NULL, 0, "%d", version );
|
2016-09-21 20:38:04 +02:00
|
|
|
char* versionStr = malloc( length + 2 );
|
|
|
|
snprintf( versionStr, length + 1, "%d", version );
|
2016-09-16 21:41:23 +02:00
|
|
|
|
2016-09-21 20:38:04 +02:00
|
|
|
int length2 = snprintf( NULL, 0, "%d", getpid() );
|
|
|
|
char * pidprocess = malloc( length2 + 1 );
|
|
|
|
snprintf( pidprocess, length2 + 1, "%d", getpid() );
|
2016-09-16 21:41:23 +02:00
|
|
|
|
2016-09-21 20:38:04 +02:00
|
|
|
int length3 = snprintf( NULL, 0, "%d", index );
|
|
|
|
char* indexStr = malloc( length3 + 1 );
|
|
|
|
snprintf( indexStr, length3 + 1, "%d", index );
|
2016-09-16 21:41:23 +02:00
|
|
|
|
|
|
|
strcat(pathname[0], TMPDIR);
|
2016-09-21 20:38:04 +02:00
|
|
|
strcat(pathname[0], pidprocess);
|
2016-09-16 21:41:23 +02:00
|
|
|
strcat(pathname[0], "-");
|
2016-09-21 20:38:04 +02:00
|
|
|
strcat(pathname[0], indexStr);
|
2016-09-16 21:41:23 +02:00
|
|
|
strcat(pathname[0], "-");
|
2016-09-21 20:38:04 +02:00
|
|
|
strcat(pathname[0], versionStr);
|
2016-09-16 21:41:23 +02:00
|
|
|
strcat(pathname[0], "-in");
|
|
|
|
|
|
|
|
strcat(pathname[1], TMPDIR);
|
2016-09-21 20:38:04 +02:00
|
|
|
strcat(pathname[1], pidprocess);
|
2016-09-16 21:41:23 +02:00
|
|
|
strcat(pathname[1], "-");
|
2016-09-21 20:38:04 +02:00
|
|
|
strcat(pathname[1], indexStr);
|
2016-09-16 21:41:23 +02:00
|
|
|
strcat(pathname[1], "-");
|
2016-09-21 20:38:04 +02:00
|
|
|
strcat(pathname[1], versionStr);
|
2016-09-16 21:41:23 +02:00
|
|
|
strcat(pathname[1], "-out");
|
|
|
|
}
|
|
|
|
|
2016-09-21 20:38:04 +02:00
|
|
|
void makePivMessage (char ** piv, int pid, int index, int version) {
|
|
|
|
int length1 = snprintf( NULL, 0, "%d", getpid() );
|
|
|
|
char * pidprocess = malloc( length1 + 1 );
|
|
|
|
snprintf( pidprocess, length1 + 1, "%d", getpid() );
|
|
|
|
|
|
|
|
int length2 = snprintf( NULL, 0, "%d", index );
|
|
|
|
char* indexStr = malloc( length2 + 1 );
|
|
|
|
snprintf( indexStr, length2 + 1, "%d", index );
|
|
|
|
|
|
|
|
int length3 = snprintf( NULL, 0, "%d", version );
|
|
|
|
char* versionStr = malloc( length3 + 2 );
|
|
|
|
snprintf( versionStr, length3 + 1, "%d", version );
|
|
|
|
|
|
|
|
strcat(*piv, pidprocess);
|
|
|
|
strcat(*piv, " ");
|
|
|
|
strcat(*piv, indexStr);
|
|
|
|
strcat(*piv, " ");
|
|
|
|
strcat(*piv, versionStr);
|
|
|
|
}
|
|
|
|
|
2016-09-16 00:09:04 +02:00
|
|
|
|