working on tcpdserver: create 2 fifo file in /tmp/ipc/ for initialization the communication
This commit is contained in:
parent
a73ab192ea
commit
2090114f83
@ -63,3 +63,4 @@ int main(int argc, char ** argv) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -8,9 +8,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/stat.h> // mkfifo
|
||||
#include <linux/limits.h>
|
||||
|
||||
#define PORT 6000
|
||||
#define BUF_SIZE 1024
|
||||
#define TMPDIR "/tmp/ipc/"
|
||||
|
||||
int init_connection(void)
|
||||
{
|
||||
@ -67,38 +71,185 @@ int read_message(int sock, char *buffer)
|
||||
return n;
|
||||
}
|
||||
|
||||
void endConnection(int sock, int sfd) {
|
||||
close(sock);
|
||||
close(sfd);
|
||||
}
|
||||
|
||||
void printClientAddr(struct sockaddr_in *csin) {
|
||||
printf("%s\n", inet_ntoa(csin->sin_addr));
|
||||
printf("%u\n", ntohs(csin->sin_port));
|
||||
printf("New client\n");
|
||||
printf("IP Addr : %s\n", inet_ntoa(csin->sin_addr));
|
||||
printf("Port : %u\n", ntohs(csin->sin_port));
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char * argv[], char **env) {
|
||||
|
||||
int sock = init_connection();
|
||||
char buffer[BUF_SIZE];
|
||||
// char buffer[BUF_SIZE];
|
||||
|
||||
struct sockaddr_in csin = { 0 };
|
||||
socklen_t sinsize = sizeof csin;
|
||||
int csock = accept(sock, (struct sockaddr *)&csin, &sinsize);
|
||||
if(csock == -1)
|
||||
int sfd = accept(sock, (struct sockaddr *)&csin, &sinsize);
|
||||
if(sfd == -1)
|
||||
{
|
||||
perror("accept()");
|
||||
close(sock);
|
||||
exit(errno);
|
||||
}
|
||||
|
||||
//printf("new client\n");
|
||||
printClientAddr(&csin);
|
||||
printf("%d \n",getpid());
|
||||
|
||||
int n = read_message(csock, buffer);
|
||||
if (n == -1) {
|
||||
perror("read_message()");
|
||||
}else {
|
||||
printf("%s\n",buffer );
|
||||
pthread_t listenPid;
|
||||
p_data *p_d_listen = malloc (sizeof(p_data));
|
||||
//p_d_listen.c_sock = NULL;
|
||||
p_d_listen->sfd = sfd;
|
||||
|
||||
int ret = pthread_create( &listenPid, NULL, &listen_thread, (void *) p_d_listen);
|
||||
if (ret) {
|
||||
perror("pthread_create()");
|
||||
endConnection(sock, sfd);
|
||||
exit(errno);
|
||||
} else {
|
||||
printf("Creation of listen thread \n");
|
||||
}
|
||||
|
||||
pthread_join(listenPid, NULL);
|
||||
|
||||
endConnection(sock, sfd);
|
||||
|
||||
close(sock);
|
||||
close(csock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void * listen_thread(void * pdata) {
|
||||
p_data *pda = (p_data*) pdata;
|
||||
char buffer[BUF_SIZE];
|
||||
char *service;
|
||||
int version;
|
||||
|
||||
int n = read_message(pda->sfd, buffer);
|
||||
if (n == -1) {
|
||||
perror("read_message()");
|
||||
return NULL;
|
||||
}else {
|
||||
parseString(buffer, &service, &version);
|
||||
}
|
||||
// printf("%s\n", service);
|
||||
// printf("%d\n", version);
|
||||
/* TODO : service correspond au service que le client veut utiliser
|
||||
** il faut comparer service à un tableau qui contient les services
|
||||
** disponibles
|
||||
*/
|
||||
|
||||
// printf("%d\n", getpid());
|
||||
// printf("%zu\n", pthread_self());
|
||||
|
||||
// gets the service path, such as /tmp/ipc/pid-index-version-in/out
|
||||
char *pathname[2];
|
||||
inOutPathCreate(pathname, version);
|
||||
|
||||
printf("%s\n",pathname[0] );
|
||||
printf("%s\n",pathname[1] );
|
||||
|
||||
/*if(fifo_create(pathname[0]) != 0) {
|
||||
perror("fifo_create()");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(fifo_create(pathname[1]) != 0) {
|
||||
perror("fifo_create()");
|
||||
return NULL;
|
||||
}*/
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void parseString(char * buf, char ** service, int *version) {
|
||||
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;
|
||||
}
|
||||
|
||||
void inOutPathCreate(char ** pathname, int version) {
|
||||
pathname[0] = malloc(sizeof PATH_MAX);
|
||||
pathname[1] = malloc(sizeof PATH_MAX);
|
||||
|
||||
int length = snprintf( NULL, 0, "%d", version );
|
||||
char* str = malloc( length + 2 );
|
||||
snprintf( str, length + 1, "%d", version );
|
||||
|
||||
int length2 = snprintf( NULL, 0, "%zu", pthread_self() );
|
||||
char * str2 = malloc( length2 + 1 );
|
||||
snprintf( str2, length2 + 1, "%zu", pthread_self() );
|
||||
|
||||
int length3 = snprintf( NULL, 0, "%d", 1 );
|
||||
char* str3 = malloc( length3 + 1 );
|
||||
snprintf( str3, length3 + 1, "%d", 1 );
|
||||
|
||||
strcat(pathname[0], TMPDIR);
|
||||
strcat(pathname[0], str2);
|
||||
strcat(pathname[0], "-");
|
||||
strcat(pathname[0], str3);
|
||||
strcat(pathname[0], "-");
|
||||
strcat(pathname[0], str);
|
||||
strcat(pathname[0], "-in");
|
||||
|
||||
strcat(pathname[1], TMPDIR);
|
||||
strcat(pathname[1], str2);
|
||||
strcat(pathname[1], "-");
|
||||
strcat(pathname[1], str3);
|
||||
strcat(pathname[1], "-");
|
||||
strcat(pathname[1], str);
|
||||
strcat(pathname[1], "-out");
|
||||
}
|
||||
|
||||
|
||||
|
@ -6,11 +6,20 @@
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
typedef struct {
|
||||
struct sockaddr_in c_sock;
|
||||
int sfd;
|
||||
} p_data;
|
||||
|
||||
int initConnection ();
|
||||
void endConnection (int sock, int csock);
|
||||
void printClientAddr (struct sockaddr_in *csin);
|
||||
void write_message(int sock, const char *buffer);
|
||||
int read_message(int sock, char *buffer);
|
||||
void * listen_thread(void * p_data);
|
||||
void parseString(char * buf, char ** service, int *version);
|
||||
void inOutPathCreate(char ** pathname, int version);
|
||||
int fifo_create (char * path);
|
||||
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user