#ifndef __FS_PROTOCOL_H__ #define __FS_PROTOCOL_H__ #include #include #include #include #include #include #include #define DEFAULT_PORT 8785 #define func_send(structure) \ static inline int send_ ## structure (int s, structure * str) { \ if(write(s, str, sizeof(*str)) != sizeof(*str)) { \ return -1; \ } \ return 0; \ } #define func_read(structure) \ static inline int read_ ## structure (int s, structure * str) { \ int to_read = sizeof(*str); \ while(to_read) { \ int c = read(s, (void*)str + ( sizeof(*str) - to_read ), to_read); \ if(c <= 0) { \ return -1; \ } \ to_read -= c; \ } \ return 0; \ } \ static inline int read_ ## structure ## _nonblocking (int s, structure * str) { \ int to_read = sizeof(*str); \ int flags = MSG_DONTWAIT; \ while(to_read) { \ int c = recv(s, (void*)str + ( sizeof(*str) - to_read ), to_read, flags); \ if(c <= 0) { \ if ( errno != EAGAIN || to_read == sizeof(*str) ) \ { \ if ( errno != EAGAIN ) \ return -1; \ return 0; \ } \ flags = MSG_WAITALL; \ c = 0; \ } \ to_read -= c; \ } \ return 0; \ } // Comunication from Client to Server enum { CMD_NO_CMD = 0, /* reserved. */ CMD_READ_CONFIGFILE, CMD_LOAD_CONFIGFILE, CMD_REPORT_IFS, CMD_STREAM, CMD_RESTART, CMD_CLOSE, CMD_ENV, } FS_COMMAND; typedef struct __attribute__ ((__packed__)) { uint32_t Length; } fs_param_readconfig; typedef struct __attribute__ ((__packed__)) { int OpenDefault; char FileNameLength; } fs_param_loadconfig; typedef struct __attribute__ ((__packed__)) { uint32_t Packets; float Throughput; uint8_t End; } fs_param_stream; typedef struct __attribute__ ((__packed__)) { uint32_t NameLength; uint32_t ValueLength; } fs_param_env; typedef struct __attribute__ ((__packed__)) { uint16_t CommandID; union { fs_param_readconfig readconfig; fs_param_loadconfig loadconfig; fs_param_stream stream; fs_param_env env; } Params; } fs_command_t; // Comunication Server to Client enum { INF_DONE, INF_STREAM, INF_ERROR, INF_IFS, } FS_INFO; typedef struct __attribute__ ((__packed__)) { int MsgLen; } fs_param_error; typedef struct __attribute__ ((__packed__)) { int PresentMask; unsigned char eth1[4]; unsigned char eth2[4]; } fs_param_ifs; typedef struct __attribute__ ((__packed__)) { uint16_t InfoID; union { fs_param_stream stream; fs_param_error error; fs_param_ifs ifs; } Params; } fs_info_t; func_send(fs_command_t) func_read(fs_command_t) func_send(fs_info_t) func_read(fs_info_t) static inline int sendString(int socket, const char *s, int Len) { if(write(socket, s, Len + 1) != Len + 1) { return -1; } return 0; } static inline int readString(int socket, char **s, int Len) { int to_read = Len + 1; *s = malloc( (Len + 1)*sizeof(char) ); while(to_read) { int c = read(socket, (*s) + ( Len + 1 - to_read ), to_read); if(c <= 0) { return -1; } to_read -= c; } return 0; } #endif