distributor/utils.h

69 lines
2.6 KiB
C

/**
* Copyright (C) 2007 Juan José Gutiérrez de Quevedo <juanjo@iteisa.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
*/
#ifndef UTILS_H
#define UTILS_H
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <syslog.h>
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/select.h>
#endif /* WIN32 */
#define BUFLEN 512 /* max length of strings */
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif /* MSG_NOSIGNAL */
/* utility macros */
#define cmp(x,y) (NULL==x||NULL==y||strncasecmp(x,y,strlen(y)))
#define write_string(x,y) do { snprintf(m_buffer,sizeof(m_buffer),"w>%s",y); debug(m_buffer); send(x,y,strlen(y),MSG_NOSIGNAL); send(x,"\r\n",2,MSG_NOSIGNAL); } while(0)
#define condfree(x) do { if(NULL!=x) free(x); x=NULL; } while(0)
#ifndef WIN32
#define safestrdup(x,y) do { assert(NULL!=y); if(NULL==(x=strndup(y,strlen(y)))) { perror("Error duplicating string"); exit(-1); } } while(0)
#define closeskt(x); do { if(-1!=x) { close(x); x=-1; } } while(0)
#else
#define safestrdup(x,y) do { assert(NULL!=y); if(NULL==(x=strdup(y))) { perror("Error duplicating string"); exit(-1); } } while(0)
#define closeskt(x); do { if(-1!=x) { closesocket(x); x=-1; } } while(0)
#endif /* WIN32 */
#define _(x) x
#define MAX(x,y) (((x)>(y))?(x):(y))
#define free_and_read_string(x,y) do { condfree(x); x=read_string(y); } while(0)
#define debug(x) do { syslog(LOG_DEBUG,"%s:%d [%ld] %s",__FILE__,__LINE__,connid,x); printf("%s:%d [%ld] %s\n",__FILE__,__LINE__,connid,x); } while(0)
char *read_string(int);
int create_listening_socket(unsigned int);
int create_connected_socket(char *,unsigned int);
unsigned long resolve(char *);
unsigned char can_read(int *,int,float);
unsigned char socketeof(int);
#endif /* UTILS_H */