1 /***************************************************************************
2 protocol.c - Class for a protocol
4 begin : Sun Mar 12 2006
5 copyright : (C) 2006 by Yannick Lecaillez, Avi Alkalay
6 email : sizon5@gmail.com, avi@unix.sh
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the BSD License (revised). *
14 ***************************************************************************/
16 /***************************************************************************
18 * Class for messages, to be passed over a protocol line. *
20 ***************************************************************************/
37 #ifdef HAVE_SYS_TYPES_H
38 #include <sys/types.h>
57 /* Magic: Magic number for protocol header. Elektra written like l33t ;-) */
58 #define PROTO_MAGIC 0x0E1E374AL
59 #define PROTO_VERSION 1
61 static int protocolCheckHeader (const ProtocolHeader *header);
65 * @defgroup protocol Protocol
66 * @brief Protocol provide methods for transport Message throught a medium.
68 * Protocol allow to send & receive @link message Message@endlink securely. It does some sanity check
69 * like checking protocol version & a magic number user for verify @link message message@endlink boundaries.
74 * Read a message from file/socket fd
75 * This methods check header, read data and
76 * create a new Message object from data.
78 * @param fd File Descriptor where data should be readen
79 * @return a newly allocated message (which must be freed
80 * using messageDel()) or NULL if error occured.
82 * @see protocolCheckHeader()
83 * @see protocolSendMessage()
87 Message *protocolReadMessage(int fd)
91 ProtocolHeader header;
96 memset(&header, 0, sizeof(header));
97 if ( (ret = read(fd, &header, sizeof(header))) == -1 ) {
101 if ( protocolCheckHeader(&header) ) {
106 msg = (Message *) malloc(header.dataLen);
112 toRead = header.dataLen;
113 while ( toRead > 0 ) {
114 if ( (ret = read(fd, buf, toRead)) == -1 ) {
126 * Write a specified message to a file/socket
127 * This methods build a header, and write message to the descriptor.
129 * @param fd File Descriptor where data should be written
130 * @param message Message to send.
131 * @return 0 if OK, -1 otherwise
133 * @see protocolReadMessage()
136 int protocolSendMessage(int fd, const Message *message)
138 ProtocolHeader header;
143 assert(message != NULL);
146 memset(&header, 0, sizeof(header));
147 header.magic = PROTO_MAGIC;
148 header.version = PROTO_VERSION;
149 header.dataLen = message->size;
150 if ( (ret = write(fd, &header, sizeof(header))) == -1 ) {
155 toWrite = message->size;
156 buf = (const char *) message;
157 while ( toWrite > 0 ) {
158 if ( (ret = write(fd, buf, message->size)) == -1 ) {
170 * Check header validity
171 * This methods check a magic number for detect data boundaries.
172 * Check a version number for compatibility between protocol version.
174 * @param header Header to check
175 * @return 0 if OK, -1 otherwise
177 * @see protocolReadMessage()
179 static int protocolCheckHeader(const ProtocolHeader *header)
181 assert(header != NULL);
183 if ( header->magic != PROTO_MAGIC ) {
188 if ( header->version < PROTO_VERSION ) {