/* * File Name: common.h * Author: Jade Cheng * Date: March 29, 2009 * Course: ICS 451 * Assignment: Project 2 */ #ifndef COMMON_H_ #define COMMON_H_ /* Standard C */ #include <assert.h> #include <ctype.h> #include <errno.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> /* Sockets */ #include <arpa/inet.h> #include <netdb.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/time.h> #include <sys/types.h> #include <unistd.h> /** To allow timeval without struct. */ typedef struct timeval timeval; #ifdef DEBUG /** * In the DEBUG configuration, the program displays additional error messages * to stderr, but this does not happen in the RELEASE configuration. Most * functions in the program return either EXIT_SUCCESS or EXIT_FAILURE, and * this macro makes it easy to trace errors in these functions. */ #define RETURN_FAILURE() \ return return_failure(__FILE__, __LINE__, __func__, errno) #else /** This macro returns EXIT_FAILURE. */ #define RETURN_FAILURE() return EXIT_FAILURE #endif /** This macro returns EXIT_FAILURE if the argument is false (zero). */ #define RETURN_IF_FALSE(E) if (0 == (E)) RETURN_FAILURE() /** This macro returns EXIT_FAILURE if the argument equals EXIT_FAILURE. */ #define RETURN_IF_FAILED(E) if ((E) != EXIT_SUCCESS) RETURN_FAILURE() /** The maximum number of command line arguments. */ #define MAX_ARGS 21 /** The maximum length of the name of some content (a file). */ #define MAX_NAME_LENGTH 20 /** * The maximum number of bytes for a name of some content. This includes * the null-terminator. */ #define NAME_BUFFER_SIZE (MAX_NAME_LENGTH + 1) /** The maximum length of a content file. */ #define MAX_CONTENT_LENGTH 1024 /** Data corresponding to a peer. */ typedef struct peer_t { /** The port number. */ u_short port; /** The IP address in host order. */ u_long addr; } peer_t; /** * Prints a binary buffer to a file. * * @param f The file pointer. * @param input The input buffer. * @param len The number of bytes to print. */ extern void fprint_buffer(FILE * f, const void * input, size_t len); /** * Prints information about a peer to a file. * * @param f The file pointer. * @param peer The peer. */ extern void fprint_peer(FILE * f, const peer_t * peer); /** * Compares two peers. * * @param peer1 The first peer. * @param peer2 The second peer. * * @return A value compatible with strcmp. */ extern int peer_cmp(const peer_t * peer1, const peer_t * peer2); #ifdef DEBUG /** * Returns EXIT_FAILURE after displaying some error message to stderr. * * @param file The name of the file. * @param line The line number that failed. * @param function The name of the function that failed. * @param info The value of errno when things failed. * * @return EXIT_FAILURE. */ extern int return_failure( const char * file, const size_t line, const char * function, const int info); #endif /** * Verifies a name is a valid file name. * * @param name The name. * * @return EXIT_SUCCESS or EXIT_FAILURE. */ extern int verify_name(const char * name); #endif /* COMMON_H_ */