Files
progettoso/lib/api/api.c

129 lines
2.5 KiB
C
Raw Normal View History

2022-04-21 18:56:47 +02:00
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/time.h>
2022-04-16 21:21:04 +02:00
#include <api.h>
2022-04-21 18:56:47 +02:00
#define SOCKNAMEMAX 256 // should be at least 108
// -----------------------------------------------------------------------------
/* variabili globali */
int fd_skt; // descriptor of the socket
char socketName[SOCKNAMEMAX] = ""; // name of the socket
2022-04-16 21:21:04 +02:00
// -----------------------------------------------------------------------------
/* funzioni ausiliarie */
// -----------------------------------------------------------------------------
int openConnection(const char* sockname, int msec, const struct timespec abstime){
2022-04-21 18:56:47 +02:00
if(!sockname){
errno = EINVAL;
return -1;
}
struct sockaddr_un sa;
struct timespec ts;
ts.tv_sec = msec/1000;
ts.tv_nsec = (msec % 1000) * 1000000;
struct timeval t1, t2; // for gettimeofday
long long start, end; // to calculate the time elapsed
long long elapsedTime;
// select socket
strncpy(sa.sun_path, sockname, sizeof(sa.sun_path));
sa.sun_family = AF_UNIX;
if ((fd_skt = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
errno = EINVAL;
perror("socket");
return -1;
}
if(gettimeofday(&t1, NULL) != 0) {
perror("gettimeofday");
return -1;
}
start = (long long) (t1.tv_sec * 1000000000 + t1.tv_usec * 1000);
while (connect(fd_skt, (struct sockaddr*) &sa, sizeof(sa)) == -1) {
gettimeofday(&t2, NULL);
end = (long long) t2.tv_sec * 1000000000 + t2.tv_usec * 1000;
elapsedTime = (end - start);
// return if more time has elapsed than allowed
if (elapsedTime > (abstime.tv_sec * 1000000000) + abstime.tv_nsec) {
errno = ETIMEDOUT;
return -1;
}
nanosleep(&ts, &ts);
}
strncpy(socketName, sockname, sizeof(socketName));
2022-04-16 21:21:04 +02:00
return 1;
}
int closeConnection(const char* sockname){
return 1;
}
int openFile(const char* pathname, int flags){
return 1;
}
int readFile(const char* pathname, void** buf, size_t* size){
return 1;
}
int readNFiles(int N, const char* dirname){
return 1;
}
int writeFile(const char* pathname, const char* dirname){
return 1;
}
int appendToFile(const char* pathname, void* buf, size_t size, const char* dirname){
return 1;
}
int lockFile(const char* pathname){
return 1;
}
int unlockFile(const char* pathname){
return 1;
}
int closeFile(const char *pathname){
return 1;
}
int removeFile(const char* pathname){
return 1;
}
int setDirectory(char* Dir, int rw){
return 1;
}
void printInfo(int p){
return;
}