2017-02-02 10:18:34 +00:00
|
|
|
#include "lib.h"
|
|
|
|
|
|
|
|
|
2017-02-05 11:08:02 +00:00
|
|
|
int debug(const char* tag, const char* msg){
|
2017-02-02 10:18:34 +00:00
|
|
|
|
2017-02-05 11:08:02 +00:00
|
|
|
/* (1) Get number of characters to add */
|
|
|
|
int diff = DEBUG_LEN - strlen(msg) - strlen(tag) - 3;
|
2017-02-02 10:18:34 +00:00
|
|
|
|
2017-02-05 11:08:02 +00:00
|
|
|
/* (2) print msg */
|
|
|
|
printf("[%s] %s", tag, msg);
|
|
|
|
|
|
|
|
/* (3) print missing characters */
|
|
|
|
for( ; diff >= 0 ; diff-- )
|
|
|
|
printf(".");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int xbind(const int port){
|
|
|
|
|
|
|
|
/* [0] Initialization
|
|
|
|
=========================================================*/
|
2017-02-07 22:17:17 +00:00
|
|
|
int xsocket, bound, clientsock;
|
2017-02-05 11:08:02 +00:00
|
|
|
static struct sockaddr_in addr;
|
|
|
|
|
|
|
|
|
|
|
|
/* [1] Create xsocket
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Create UPD xsocket */
|
|
|
|
DEBUG&& debug("xbind", "creating server socket");
|
2017-02-08 10:44:10 +00:00
|
|
|
xsocket = socket(AF_INET, SOCK_STREAM, 0);
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
/* (r2-) Manage error */
|
|
|
|
if( xsocket == -1 ){
|
|
|
|
DEBUG&& printf("error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG&& printf("done\n");
|
|
|
|
|
|
|
|
/* [2] Set xsocket address information
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Reset values */
|
|
|
|
bzero(&addr, sizeof(struct sockaddr_in));
|
|
|
|
|
|
|
|
/* (2) Set xsocket addr */
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Bind to port
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Bind and return -1 if error */
|
|
|
|
DEBUG&& debug("xbind", "binding socket to port");
|
2017-02-08 10:44:10 +00:00
|
|
|
bound = bind(xsocket, (struct sockaddr*)&addr, sizeof(addr));
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
/* (2) Manage error */
|
|
|
|
if( bound == -1 ){
|
|
|
|
DEBUG&& printf("error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG&& printf("done\n");
|
2017-02-07 22:17:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [4] Mark socket so it will listen for incoming co.
|
|
|
|
=========================================================*/
|
2017-02-08 10:44:10 +00:00
|
|
|
DEBUG&& debug("xbind", "listen for client");
|
|
|
|
|
2017-02-14 08:33:48 +00:00
|
|
|
if( listen(xsocket, maxClients) < 0 ){
|
2017-02-07 22:17:17 +00:00
|
|
|
DEBUG&& printf("error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG&& printf("done\n");
|
|
|
|
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
|
2017-02-07 22:17:17 +00:00
|
|
|
/* [5] Return xsocket
|
2017-02-05 11:08:02 +00:00
|
|
|
=========================================================*/
|
|
|
|
return xsocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-08 10:44:10 +00:00
|
|
|
int xlisten(const int xsocket, struct sockaddr_in* client){
|
2017-02-07 22:17:17 +00:00
|
|
|
|
|
|
|
/* [1] Initialization
|
|
|
|
=========================================================*/
|
|
|
|
unsigned int sock_len;
|
2017-02-08 10:44:10 +00:00
|
|
|
int clientsock;
|
2017-02-07 22:17:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [2] Wait for client
|
|
|
|
=========================================================*/
|
2017-02-08 10:44:10 +00:00
|
|
|
/* (1) Wait for incoming client connection */
|
2017-02-07 22:17:17 +00:00
|
|
|
DEBUG&& debug("xlisten", "listening for client");
|
2017-02-08 10:44:10 +00:00
|
|
|
clientsock = accept(xsocket, (struct sockaddr*) client, &sock_len);
|
2017-02-07 22:17:17 +00:00
|
|
|
|
2017-02-08 10:44:10 +00:00
|
|
|
/* (2) Manage errors */
|
|
|
|
|
2017-02-07 22:17:17 +00:00
|
|
|
if( clientsock == -1 ){
|
|
|
|
DEBUG&& printf("error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG&& printf("done\n");
|
|
|
|
|
2017-02-08 10:44:10 +00:00
|
|
|
return clientsock;
|
2017-02-07 22:17:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-05 14:31:44 +00:00
|
|
|
int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
/* [0] Initialization
|
|
|
|
=========================================================*/
|
2017-02-08 10:44:10 +00:00
|
|
|
int xsocket, bound, connected;
|
2017-02-05 11:08:02 +00:00
|
|
|
struct hostent *host; // data found by lookup
|
|
|
|
|
|
|
|
|
|
|
|
/* [1] Create xsocket
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Create UPD xsocket */
|
|
|
|
DEBUG&& debug("xconnect", "creating client socket");
|
2017-02-08 10:44:10 +00:00
|
|
|
xsocket = socket(AF_INET, SOCK_STREAM, 0);
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
/* (r2-) Manage error */
|
|
|
|
if( xsocket == -1 ){
|
|
|
|
DEBUG&& printf("error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG&& printf("done\n");
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Get information by hostname
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Process */
|
|
|
|
DEBUG&& debug("xconnect", "fetch info by hostname");
|
2017-02-08 10:44:10 +00:00
|
|
|
host = gethostbyname(hostname);
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
/* (2) Manage error */
|
|
|
|
if( host == NULL ){
|
|
|
|
DEBUG&& printf("error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG&& printf("found\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Set server useful information from fetched info
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Reset values */
|
|
|
|
DEBUG&& debug("xconnect", "building server info");
|
2017-02-08 10:44:10 +00:00
|
|
|
bzero(serv, sizeof(struct sockaddr_in));
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
/* (2) Set server info (ipv4, port) */
|
2017-02-05 14:31:44 +00:00
|
|
|
serv->sin_family = AF_INET;
|
|
|
|
serv->sin_port = htons(port);
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
/* (3) Copy server address */
|
2017-02-05 14:31:44 +00:00
|
|
|
memcpy( &serv->sin_addr.s_addr, host->h_addr, host->h_length);
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
DEBUG&& printf("built\n");
|
|
|
|
|
|
|
|
|
2017-02-08 10:44:10 +00:00
|
|
|
/* [4] Connect to server
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Try to connect to given server */
|
|
|
|
DEBUG&& debug("xconnect", "connecting to server");
|
|
|
|
connected = connect(xsocket, (struct sockaddr*) serv, sizeof(*serv));
|
|
|
|
|
|
|
|
/* (2) Manage error */
|
|
|
|
if( connected == -1 ){
|
|
|
|
DEBUG&& printf("error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG&& printf("done\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-05 11:08:02 +00:00
|
|
|
// return socket
|
|
|
|
return xsocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-08 10:44:10 +00:00
|
|
|
int xread(const int xsocket, char* buffer, int bufsize){
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
/* [1] Wait for message through xsocket
|
|
|
|
=========================================================*/
|
2017-02-08 10:44:10 +00:00
|
|
|
/* (1) Memory allocation */
|
2017-02-05 14:31:44 +00:00
|
|
|
buffer = (char*) realloc(buffer, sizeof(char) * bufsize );
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
/* (2) Listen */
|
2017-02-08 10:44:10 +00:00
|
|
|
DEBUG&& debug("xread", "waiting for data");
|
|
|
|
int read = recv(xsocket, buffer, bufsize, 0);
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
/* (3) Manage error */
|
|
|
|
if( read == -1 ){
|
|
|
|
DEBUG&& printf("error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG&& printf("received\n");
|
|
|
|
|
2017-02-08 10:44:10 +00:00
|
|
|
DEBUG&& printf("[xread:received] '%s'\n", buffer);
|
2017-02-05 14:31:44 +00:00
|
|
|
|
|
|
|
|
2017-02-05 11:08:02 +00:00
|
|
|
/* [2] Return number of read characters
|
|
|
|
=========================================================*/
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-08 10:44:10 +00:00
|
|
|
int xwrite(const int xsocket, char* buffer){
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
/* [1] Send buffer (message) to target
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Useful data */
|
2017-02-08 10:44:10 +00:00
|
|
|
unsigned int addr_len = sizeof(struct sockaddr_in);
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
/* (2) Send data */
|
2017-02-08 10:44:10 +00:00
|
|
|
DEBUG&& debug("xwrite", "sending data");
|
|
|
|
int sent = send(xsocket, buffer, strlen(buffer), 0);
|
2017-02-05 11:08:02 +00:00
|
|
|
|
|
|
|
/* (3) Manage error */
|
|
|
|
if( sent == -1 ){
|
|
|
|
DEBUG&& printf("error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG&& printf("sent\n");
|
|
|
|
|
|
|
|
|
2017-02-08 10:44:10 +00:00
|
|
|
DEBUG&& printf("[xwrite:sent] '%s'\n", buffer);
|
2017-02-05 14:31:44 +00:00
|
|
|
|
|
|
|
|
2017-02-05 11:08:02 +00:00
|
|
|
/* [2] Return number of sent characters
|
|
|
|
=========================================================*/
|
|
|
|
return sent;
|
|
|
|
}
|