-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.h
104 lines (76 loc) · 2.84 KB
/
conn.h
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
/* conn.h
written by Fabio Ferrero
*/
#include <stdio.h>
#include <stdlib.h> // exit();
#include <unistd.h> // close();
#include <time.h>
#include <errno.h> // errno
#include <string.h>
#include <fcntl.h> // open();
#include <stdint.h> // uint32_t
#include <sys/stat.h> // files info
#include <sys/types.h> // pthread_x_t
#include <sys/wait.h> // wait();
#include <signal.h> // signal();
#include <netinet/in.h> // Address conversion
#include <rpc/xdr.h> // XDR
//#define TRUE 1
//#define FALSE 0
#define TCP 0
#define UDP 1
#define DATAGRAM_LEN 1024
#define NO_TIMEOUT 0
#define TIMEOUT_EXPIRED (errno == EAGAIN || errno == EWOULDBLOCK)
typedef struct connection {
char address[46];
int port;
int sock;
} Connection;
typedef struct host {
char address[46];
int port;
int sock;
} Host;
/* NOTE: all funciton return: 0 for SUCCESS and -1 for ERROR */
void fatal_err(char *message);
void report_err(char *message);
/***** SERVER *****/
/* Returns an host used to acceptConn() on TCP or recvfromHost() on UDP */
Host prepareServer(int port, int protocol); // EXIT on failure
/***** TCP SERVER *****/
Connection acceptConn(Host server); //TODO by reference and return value
/***** TCP CLIENT *****/
Connection conn_connect(char * address, int port); // EXIT on failure
/***** TCP *****/
int conn_close(Connection conn);
int conn_sends(Connection conn, char * string);
int conn_sendn(Connection conn, void * data, int datalen);
int conn_sendfile(Connection conn, int fd, int file_size);
int conn_sendfile_tokenized(Connection conn, int fd, int file_size, int tokenlen);
int conn_recvs(Connection conn, char * string, int str_len, char * terminator);
int conn_recvn(Connection conn, void * data, int datalen);
int conn_recvfile(Connection conn, int fd, int file_size);
int conn_recvfile_tokenized(Connection conn, int fd, int file_size, int tokenlen);
int conn_setTimeout(Connection conn, int timeout);
/***** UDP CLIENT *****/
Host Host_init(char * address, int port); // EXIT on failure
/***** UDP *****/
int closeHost(Host host);
int sendstoHost(char * string, Host host);
int sendntoHost(void * data, int datalen, Host host);
int recvsfromHost(char * string, int str_len, Host * host, int timeout);
int recvnfromHost(void * data, int datalen, Host * host, int timeout);
/***** XDR *****/
int xdr_sendfile(XDR * xdrOut, int fd, int file_size);
int xdr_recvfile(XDR * xdrOut, int fd, int file_size);
/** UTILITIES **/
int checkaddress(char * address);
int checkport(char * port); // Returns the port number or -1
int readline(char * string, int str_len); // Returns the string lenght
int writen(int fd, void * buffer, int nbyte);
int readn(int fd, void * buffer, int nbyte);
/** DNS resolve **/
char * getAddressByName(char * url, char * ip); // Returns the IP or NULL
char * nextAddress(char * ip); // Returns the IP or NULL
void useIPv6(int trueOrFalse);