utils.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Copyright (C) 2007 Juan José Gutiérrez de Quevedo <juanjo@iteisa.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
  18. */
  19. #ifndef UTILS_H
  20. #define UTILS_H
  21. #include <assert.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <string.h>
  26. #include <syslog.h>
  27. #ifdef WIN32
  28. #include <winsock2.h>
  29. #include <ws2tcpip.h>
  30. #else
  31. #include <sys/socket.h>
  32. #include <netdb.h>
  33. #include <arpa/inet.h>
  34. #include <netinet/in.h>
  35. #include <sys/select.h>
  36. #endif /* WIN32 */
  37. #define BUFLEN 512 /* max length of strings */
  38. #ifndef MSG_NOSIGNAL
  39. #define MSG_NOSIGNAL 0
  40. #endif /* MSG_NOSIGNAL */
  41. /* utility macros */
  42. #define cmp(x,y) (NULL==x||NULL==y||strncasecmp(x,y,strlen(y)))
  43. #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)
  44. #define condfree(x) do { if(NULL!=x) free(x); x=NULL; } while(0)
  45. #ifndef WIN32
  46. #define safestrdup(x,y) do { assert(NULL!=y); if(NULL==(x=strndup(y,strlen(y)))) { perror("Error duplicating string"); exit(-1); } } while(0)
  47. #define closeskt(x); do { if(-1!=x) { close(x); x=-1; } } while(0)
  48. #else
  49. #define safestrdup(x,y) do { assert(NULL!=y); if(NULL==(x=strdup(y))) { perror("Error duplicating string"); exit(-1); } } while(0)
  50. #define closeskt(x); do { if(-1!=x) { closesocket(x); x=-1; } } while(0)
  51. #endif /* WIN32 */
  52. #define _(x) x
  53. #define MAX(x,y) (((x)>(y))?(x):(y))
  54. #define free_and_read_string(x,y) do { condfree(x); x=read_string(y); } while(0)
  55. #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)
  56. char *read_string(int);
  57. int create_listening_socket(unsigned int);
  58. int create_connected_socket(char *,unsigned int);
  59. unsigned long resolve(char *);
  60. unsigned char can_read(int *,int,float);
  61. unsigned char socketeof(int);
  62. #endif /* UTILS_H */