-
Notifications
You must be signed in to change notification settings - Fork 0
/
connections.c
148 lines (119 loc) · 3.64 KB
/
connections.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* membox Progetto del corso di LSO 2017/2018
*
* Dipartimento di Informatica Università di Pisa
* Docenti: Prencipe, Torquati
*
*/
/**
* @file connections.c
* @author Jacopo Massa 543870 \n( <mailto:jacopomassa97@gmail.com> )
* @brief Implementazione delle funzioni del file connections.h
* @copyright **Si dichiara che il contenuto di questo file è in ogni sua parte opera
originale dell'autore**
* @see connections.h
*/
#define _POSIX_C_SOURCE 200809L
#include <utility.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <string.h>
#include <message.h>
#include <connections.h>
int openConnection(char* path, unsigned int ntimes, unsigned int secs)
{
int sfd;
struct sockaddr_un sa;
if(path == NULL || ntimes < 1 || secs < 1)
return -1;
SYSCALL( sfd = socket(AF_UNIX,SOCK_STREAM,0) , -1, "socket in openConnection");
memset(&sa,'0', sizeof(sa));
sa.sun_family = AF_UNIX;
strncpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
for(int i=0; i<ntimes; i++)
{
if((connect(sfd,(struct sockaddr*)&sa,sizeof(sa))) == -1)
{
if (errno == ENOENT)
sleep(secs); /* sock non esiste */
else
return -1;
}
else
{
printf("connected on %d\n",sfd);
return sfd;
}
}
return -1;
}
int readHeader(long connfd, message_hdr_t *hdr)
{
if(connfd < 0 || hdr == NULL)
return 0;
ssize_t n_read;
n_read = readn((int)connfd,&hdr->op,sizeof(op_t),"op read in readHeader");
EQZERO(n_read);
n_read = readn((int)connfd,hdr->sender,MAX_NAME_LENGTH+1,"sender read in readHeader");
EQZERO(n_read);
return 1;
}
int readData(long fd, message_data_t *data)
{
if(fd < 0 || data == NULL)
return 0;
ssize_t n_read;
n_read = readn((int)fd,data->hdr.receiver,MAX_NAME_LENGTH+1,"receiver read in readData");
EQZERO(n_read);
n_read = readn((int)fd,&data->hdr.len, sizeof(unsigned int),"length read in readData");
if(data->hdr.len == 0) return 1; // il buffer potrebbe essere vuoto, ma va bene comunque
SYSCALL(data->buf = (char*) malloc(sizeof(char)*data->hdr.len),NULL,"malloc buffer in readData");
n_read = readn((int)fd,data->buf,data->hdr.len,"buf read in readData");
EQZERO(n_read);
return 1;
}
int readMsg(long fd, message_t *msg)
{
if(fd < 0 || msg == NULL)
return 0;
ssize_t res_hdr, res_data;
res_hdr = readHeader(fd,&msg->hdr);
res_data = readData(fd,&msg->data);
return res_hdr && res_data;
}
int sendHeader(long fd, message_hdr_t *msg)
{
if(fd < 0 || msg == NULL)
return 0;
ssize_t n_write;
n_write = writen((int)fd,&msg->op, sizeof(unsigned int),"op write in sendHeader");
EQZERO(n_write);
n_write = writen((int)fd,msg->sender, MAX_NAME_LENGTH+1,"sender write in sendHeader");
EQZERO(n_write);
return 1;
}
int sendData(long fd, message_data_t *msg)
{
if(fd < 0 || msg == NULL)
return 0;
ssize_t n_write;
n_write = writen((int)fd,msg->hdr.receiver, MAX_NAME_LENGTH+1,"receiver write in sendData");
EQZERO(n_write);
n_write = writen((int)fd,&msg->hdr.len, sizeof(unsigned int),"length write in sendData");
if(msg->hdr.len == 0) return 1;
n_write = writen(fd,msg->buf, msg->hdr.len,"buf write in sendData");
EQZERO(n_write);
return 1;
}
int sendRequest(long fd, message_t *msg)
{
if(fd < 0 || msg == NULL)
return 0;
ssize_t res_hdr, res_data;
res_hdr = sendHeader(fd,&msg->hdr);
res_data = sendData(fd,&msg->data);
return res_hdr && res_data;
}