Refactor + cleanup + lib:shortcode:debug - transmission error (pointer malloc-ed in function ??) [TO FIX]

This commit is contained in:
xdrm-brackets 2017-02-05 12:08:02 +01:00
parent 8c1274871a
commit 440cfcbe39
9 changed files with 230 additions and 96 deletions

BIN
client

Binary file not shown.

View File

@ -7,20 +7,16 @@ int main(int argc, char* argv[]){
/* [1] Initialisation /* [1] Initialisation
=========================================================*/ =========================================================*/
/* (1) Socket information */ /* (1) Socket information */
// Server information
struct hostent *server_host;
// Server socket address // Server socket address
static struct sockaddr_in server_addr; static struct sockaddr_in server_addr;
// Server socket address length
socklen_t sock_len;
// Local socket // Local socket
int sock; int sock;
/* (2) Misc. information */ /* (2) Misc. information */
// Response // Response
char *msg = "some message"; char *msg = "some message\0";
// Reception buffer // Reception buffer
char buffer[BUFSIZE]; char buffer[BUFSIZE] = {'\0'};
// Will contain the response // Will contain the response
char *response; char *response;
// Will contain the length of received/sent data // Will contain the length of received/sent data
@ -30,8 +26,7 @@ int main(int argc, char* argv[]){
/* [2] UPD socket creation /* [2] UPD socket creation
=========================================================*/ =========================================================*/
/* (1) Socket initialization */ sock = xconnect("localhost", 4000, (struct sockaddr*) &server_addr);
sock = socket(AF_INET, SOCK_DGRAM, 0);
/* (1-) Manage error */ /* (1-) Manage error */
if( sock == -1 ){ if( sock == -1 ){
@ -39,41 +34,11 @@ int main(int argc, char* argv[]){
exit(1); exit(1);
} }
/* (2) Get host information */
server_host = gethostbyname("localhost");
/* (2-) Manage error */
if( server_host == NULL ){
perror("erreur adresse serveur");
exit(1);
}
/* (3) Create destination socket */
bzero(&server_addr, sizeof(struct sockaddr_in)); // clean @server_addr struct before updating
server_addr.sin_family = AF_INET; // set server addr family (ipv4)
server_addr.sin_port = htons(4000); // set server port
memcpy( &server_addr.sin_addr.s_addr, // ) set host ipv4 addr
/* ( */ server_host->h_addr, // )
/* ( */ server_host->h_length );
/* [3] Send message /* [3] Send message
=========================================================*/ =========================================================*/
// calculate server id struct size /* (1) Send message to server */
sock_len = sizeof(struct sockaddr_in); nb_bytes = xsend(sock, msg, (struct sockaddr*) &server_addr);
/* (1) Send message to server (identified by <sockaddr_in>) */
nb_bytes = sendto( sock, // ) socket
/* ( */ msg, // ) buffer
/* ( */ strlen(msg) + 1, // ) buffer size
/* ( */ 0, // ) flags
/* ( */ (struct sockaddr*) &server_addr, // ) server identifier
/* ( */ sock_len ); // identifier size
/* (2) Check if send succesfully */ /* (2) Check if send succesfully */
if( nb_bytes == -1 ){ if( nb_bytes == -1 ){
@ -88,12 +53,8 @@ int main(int argc, char* argv[]){
/* [4] Wait for response /* [4] Wait for response
=========================================================*/ =========================================================*/
nb_bytes = recvfrom( sock, // ) socket /* (1) Wait for response */
/* ( */ buffer, // ) receive buffer nb_bytes = xlisten(sock, (struct sockaddr*) &server_addr, buffer, BUFSIZE);
/* ( */ BUFSIZE, // ) buffer size
/* ( */ 0, // ) flags
/* ( */ (struct sockaddr*) &server_addr, // ) server identifier
/* ( */ &sock_len ); // identifier size
/* (2) Check if received successfully (and consistently) */ /* (2) Check if received successfully (and consistently) */
if( nb_bytes == -1 ){ if( nb_bytes == -1 ){
@ -102,7 +63,7 @@ int main(int argc, char* argv[]){
} }
/* (3) Copy response into var */ /* (3) Copy response into var */
response = (char *) malloc(nb_bytes * sizeof(char)); response = (char*) malloc(nb_bytes * sizeof(char));
memcpy(response, buffer, nb_bytes); memcpy(response, buffer, nb_bytes);
/* (4) log */ /* (4) log */

BIN
client.o

Binary file not shown.

202
lib.c
View File

@ -1,6 +1,206 @@
#include "lib.h" #include "lib.h"
int debug(const char* tag, const char* msg){
/* (1) Get number of characters to add */
int diff = DEBUG_LEN - strlen(msg) - strlen(tag) - 3;
/* (2) print msg */
printf("[%s] %s", tag, msg);
/* (3) print missing characters */
for( ; diff >= 0 ; diff-- )
printf(".");
return 1;
}
int createUPDSocket(int port){ return 1; }
int xbind(const int port){
/* [0] Initialization
=========================================================*/
int xsocket, bound;
static struct sockaddr_in addr;
/* [1] Create xsocket
=========================================================*/
/* (1) Create UPD xsocket */
xsocket = socket(AF_INET, SOCK_DGRAM, 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] Return xsocket
=========================================================*/
return xsocket;
}
int xconnect(const char* hostname, const int port, struct sockaddr* serv){
/* [0] Initialization
=========================================================*/
int xsocket, bound;
struct hostent *host; // data found by lookup
static struct sockaddr_in* serv_in;
serv_in = (void*) serv;
/* [1] Create xsocket
=========================================================*/
/* (1) Create UPD xsocket */
xsocket = socket(AF_INET, SOCK_DGRAM, 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_in->sin_family = AF_INET;
serv_in->sin_port = htons(port);
/* (3) Copy server address */
memcpy( &serv_in->sin_addr.s_addr, host->h_addr, host->h_length);
DEBUG&& printf("built\n");
// return socket
return xsocket;
}
int xlisten(const int xsocket, const struct sockaddr *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*) malloc( sizeof(char) * bufsize );
/* (2) Listen */
DEBUG&& debug("xlisten", "waiting for data");
int read = recvfrom(xsocket, buffer, bufsize, 0, (struct sockaddr *) client, &sock_len);
/* (3) Manage error */
if( read == -1 ){
DEBUG&& printf("error\n");
return -1;
}
DEBUG&& printf("received\n");
/* [2] Return number of read characters
=========================================================*/
return read;
}
int xsend(const int xsocket, char* buffer, struct sockaddr* target){
/* [1] Send buffer (message) to target
=========================================================*/
/* (1) Useful data */
socklen_t addr_len = sizeof(struct sockaddr_in);
/* (2) Send data */
DEBUG&& debug("xsend", "sending data");
int sent = sendto(xsocket, buffer, strlen(buffer), 0, target, addr_len);
/* (3) Manage error */
if( sent == -1 ){
DEBUG&& printf("error\n");
return -1;
}
DEBUG&& printf("sent\n");
/* [2] Return number of sent characters
=========================================================*/
return sent;
}

28
lib.h
View File

@ -7,23 +7,31 @@
// debug flags // debug flags
# define DEBUG 1 #define DEBUG 0x1
#define DEBUG_LEN 40
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
/************************************************ /************************************************
**** Signatures **** **** Signatures ****
************************************************/ ************************************************/
/* CREATES A UDP SOCKET int debug(const char* tag, const char* msg);
*
* @port<int> Port to use (if 0: random)
*
* @return socket<socket> Socket descriptor or -1 if error
*
*/
int createUPDSocket(int port);
int xbind(const int port);
int xconnect(const char* hostname, const int port, struct sockaddr* serv);
int xlisten(const int xsocket, const struct sockaddr *client, char* buffer, int bufsize);
int xsend(const int xsocket, char* buffer, struct sockaddr* target);
#endif #endif

BIN
lib.o

Binary file not shown.

BIN
server

Binary file not shown.

View File

@ -7,14 +7,10 @@ int main(int argc, char* argv[]){
/* [1] Initialisation /* [1] Initialisation
=========================================================*/ =========================================================*/
/* (1) Socket info */ /* (1) Socket info */
// Server (local)
static struct sockaddr_in addr_server;
// Client // Client
static struct sockaddr_in addr_client; static struct sockaddr_in addr_client;
// Client identifier // Client identifier
struct hostent *host_client = NULL; struct hostent *host_client = NULL;
// Socket size
socklen_t sock_len;
char client_ip[20]; char client_ip[20];
// Socket // Socket
int sock; int sock;
@ -35,7 +31,7 @@ int main(int argc, char* argv[]){
/* [2] Create socket /* [2] Create socket
=========================================================*/ =========================================================*/
/* (1) Create socket */ /* (1) Create socket */
sock = socket(AF_INET, SOCK_DGRAM, 0); // ipv4, udp, flags? sock = xbind(4000);
/* (1-) Manage error */ /* (1-) Manage error */
if( sock == -1 ){ if( sock == -1 ){
@ -43,37 +39,11 @@ int main(int argc, char* argv[]){
exit(1); exit(1);
} }
/* (2) Set socket information */
bzero(&addr_server, sizeof(struct sockaddr_in));
addr_server.sin_family = AF_INET; // ipv4
addr_server.sin_port = htons(4000); // port: 4000
addr_server.sin_addr.s_addr = htonl(INADDR_ANY); //
/* (3) Bind socket to local port */
int bounded = bind( sock, // ) socket
/* ( */ (struct sockaddr*)&addr_server, // ) filled with address
/* ( */ sizeof(addr_server) ); // length of struct
/* (3-) Manage error */
if( bounded == -1 ){
perror("erreur bind");
exit(1);
}
/* [3] Wait for client message /* [3] Wait for client message
=========================================================*/ =========================================================*/
// set socket length buf_len = xlisten(sock, (struct sockaddr*) &addr_client, buffer, BUFSIZE);
sock_len = sizeof(struct sockaddr_in);
/* (1) Wait for message */
buf_len = recvfrom(sock, // ) socket
/* ( */ buffer, // ) buffer
/* ( */ BUFSIZE, // ) buffer length
/* ( */ 0, // ) flags
/* ( */ (struct sockaddr *)&addr_client, // ) client addr (filled)
/* ( */ &sock_len );
/* (1-) Manage error */ /* (1-) Manage error */
if( buf_len == -1 ){ if( buf_len == -1 ){
@ -105,12 +75,7 @@ int main(int argc, char* argv[]){
/* [4] Send response /* [4] Send response
=========================================================*/ =========================================================*/
/* (1) Send response */ /* (1) Send response */
buf_len = sendto(sock, // ) socket xsend(sock, response, (struct sockaddr*)&addr_client);
/* ( */ response, // ) response
/* ( */ strlen(response)+1, // ) response length
/* ( */ 0, // ) flags
/* ( */ (struct sockaddr*) &addr_client, // ) client identifier
/* ( */ sock_len ); // identifier length
/* (1-) Manage error */ /* (1-) Manage error */
if( buf_len == -1 ){ if( buf_len == -1 ){

BIN
server.o

Binary file not shown.