utils.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * saop: smtp authentication over pop3
  3. * Copyright (C) 2007 Juan José Gutiérrez de Quevedo <juanjo@iteisa.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. * @author Juan José Gutiérrez de Quevedo <juanjo@iteisa.com>
  19. */
  20. #ifndef UTILS_H
  21. #define UTILS_H
  22. #include <assert.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <string.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 { debug(y); debug("Writing string " #y " to socket " #x); send(x,y,strlen(y),MSG_NOSIGNAL); send(x,"\r\n",2,MSG_NOSIGNAL); } while(0)
  44. #define condfree(x) do { debug("Freeing memory block " #x); 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) { debug("Closing socket " #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) { debug("Closing socket " #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. #ifdef DEBUG
  56. #define debug(x) do { if(NULL==fsaopdebug) { fsaopdebug=fopen("c:\\saop.log","a"); } if(NULL!=fsaopdebug) { fprintf(fsaopdebug,"%s\n",x); fflush(fsaopdebug); } fprintf(stderr,"%s\n",x); fflush(stderr); } while(0)
  57. #else
  58. #define debug(x) do {} while(0)
  59. #endif /* DEBUG */
  60. char *read_string(int);
  61. int create_listening_socket(unsigned int);
  62. int create_connected_socket(char *,unsigned int);
  63. unsigned long resolve(char *);
  64. unsigned char can_read(int,float);
  65. #endif /* UTILS_H */