/** * Copyright (C) 2007 Juan José Gutiérrez de Quevedo * * 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 */ #ifndef UTILS_H #define UTILS_H #include #include #include #include #include #include #ifdef WIN32 #include #include #else #include #include #include #include #include #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 */