sysdis-tp/lib.c

271 lines
5.5 KiB
C
Raw Normal View History

2017-02-02 10:18:34 +00:00
#include "lib.h"
int debug(const char* tag, const char* msg){
2017-02-02 10:18:34 +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
/* (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
=========================================================*/
int xsocket, bound, clientsock;
static struct sockaddr_in addr;
/* [1] Create xsocket
=========================================================*/
/* (1) Create UPD xsocket */
xsocket = socket(AF_INET, SOCK_STREAM, 0);
DEBUG&& debug("xbind", "creating server socket");
/* (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 */
bound = bind(xsocket, (struct sockaddr*)&addr, sizeof(addr));
DEBUG&& debug("xbind", "binding socket to port");
/* (2) Manage error */
if( bound == -1 ){
DEBUG&& printf("error\n");
return -1;
}
DEBUG&& printf("done\n");
/* [4] Mark socket so it will listen for incoming co.
=========================================================*/
DEBUG&& debug("xbind", "make socket listen for co");
if( listen(xsocket, 1) < 0 ){
DEBUG&& printf("error\n");
return -1;
}
DEBUG&& printf("done\n");
/* [5] Return xsocket
=========================================================*/
return xsocket;
}
int xlisten(const int xsocket, struct sockaddr_in* client, char* buffer, int bufsize){
/* [1] Initialization
=========================================================*/
unsigned int sock_len;
int clientsock, bytes;
char buf[bufsize] = {'\0'};
/* [2] Wait for client
=========================================================*/
clientsock = accept(xsocket, (struct sockaddr*) client, &sock_len);
DEBUG&& debug("xlisten", "listening for client");
if( clientsock == -1 ){
DEBUG&& printf("error\n");
return -1;
}
DEBUG&& printf("done\n");
/* [3] Receive client data
=========================================================*/
// while there is chunks to read
do{
/* (1) Receive buffer */
bytes = recv(clientsock, buf, bufsize);
/* (2) Copy to target */
buffer = realloc(buffer, (strlen(buffer)+strlen(buf)) * sizeof(char );
strcat(buffer, buf);
}while( bytes > 0 );
}
int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
/* [0] Initialization
=========================================================*/
int xsocket, bound;
struct hostent *host; // data found by lookup
/* [1] Create xsocket
=========================================================*/
/* (1) Create UPD xsocket */
xsocket = socket(AF_INET, SOCK_STREAM, 0);
DEBUG&& debug("xconnect", "creating client socket");
/* (r2-) Manage error */
if( xsocket == -1 ){
DEBUG&& printf("error\n");
return -1;
}
DEBUG&& printf("done\n");
/* [2] Get information by hostname
=========================================================*/
/* (1) Process */
host = gethostbyname(hostname);
DEBUG&& debug("xconnect", "fetch info by hostname");
/* (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 */
bzero(serv, sizeof(struct sockaddr_in));
DEBUG&& debug("xconnect", "building server info");
/* (2) Set server info (ipv4, port) */
serv->sin_family = AF_INET;
serv->sin_port = htons(port);
/* (3) Copy server address */
memcpy( &serv->sin_addr.s_addr, host->h_addr, host->h_length);
DEBUG&& printf("built\n");
// return socket
return xsocket;
}
int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize){
/* [1] Wait for message through xsocket
=========================================================*/
/* (1) Useful data + memory allocation */
socklen_t sock_len = sizeof(struct sockaddr_in);
buffer = (char*) realloc(buffer, sizeof(char) * bufsize );
/* (2) Listen */
DEBUG&& debug("xlisten", "waiting for data");
int read = listen(xsocket, 1);
/* (3) Manage error */
if( read == -1 ){
DEBUG&& printf("error\n");
return -1;
}
DEBUG&& printf("received\n");
printf("[xlisten:received] '%s'\n", buffer);
/* [2] Return number of read characters
=========================================================*/
return read;
}
int xsend(const int xsocket, char* buffer, struct sockaddr_in* target){
/* [1] Send buffer (message) to target
=========================================================*/
/* (1) Useful data */
size_t addr_len = sizeof(struct sockaddr_in);
/* (2) Send data */
DEBUG&& debug("xsend", "sending data");
int sent = sendto(xsocket, buffer, strlen(buffer)+1, 0, (struct sockaddr*) target, addr_len);
/* (3) Manage error */
if( sent == -1 ){
DEBUG&& printf("error\n");
return -1;
}
DEBUG&& printf("sent\n");
printf("[xsend:sent] '%s'\n", buffer);
/* [2] Return number of sent characters
=========================================================*/
return sent;
}