PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/utility.h

http://github.com/gunnarbeutner/shroudbnc
C Header | 178 lines | 96 code | 32 blank | 50 comment | 3 complexity | b90431f42104683f095eca047fa4c173 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*******************************************************************************
  2. * shroudBNC - an object-oriented framework for IRC *
  3. * Copyright (C) 2005-2014 Gunnar Beutner *
  4. * *
  5. * This program is free software; you can redistribute it and/or *
  6. * modify it under the terms of the GNU General Public License *
  7. * as published by the Free Software Foundation; either version 2 *
  8. * of the License, or (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the Free Software *
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. *******************************************************************************/
  19. #ifndef UTILITY_H
  20. #define UTILITY_H
  21. /**
  22. * command_t
  23. *
  24. * A command.
  25. */
  26. typedef struct command_s {
  27. char *Category; /**< the command's category */
  28. char *Description; /**< a short description of the command */
  29. char *HelpText; /**< the command's help text */
  30. } command_t;
  31. /** A list of commands. */
  32. typedef class CHashtable<command_t *, false> *commandlist_t;
  33. SBNCAPI const char * ArgParseServerLine(const char *Data);
  34. SBNCAPI const char *ArgTokenize(const char *Data);
  35. SBNCAPI const char **ArgToArray(const char *Args);
  36. SBNCAPI void ArgRejoinArray(const char **ArgV, int Index);
  37. SBNCAPI const char **ArgDupArray(const char **ArgV);
  38. SBNCAPI void ArgFree(const char *Args);
  39. SBNCAPI void ArgFreeArray(const char **Array);
  40. SBNCAPI const char *ArgGet(const char *Args, int Arg);
  41. SBNCAPI int ArgCount(const char *Args);
  42. /**
  43. * tokendata_t
  44. *
  45. * Used for storing tokenized strings.
  46. */
  47. typedef struct tokendata_s {
  48. unsigned int Count; /**< number of tokens */
  49. size_t Pointers[32]; /**< relative pointers to individual tokens */
  50. char String[512]; /**< the tokenized string */
  51. } tokendata_t;
  52. /* Version 2 of some tokenization functions
  53. * these functions have some limitations:
  54. * -only up to 32 tokens per string are supported
  55. * -strings cannot be longer than 512 chars
  56. */
  57. tokendata_t ArgTokenize2(const char *String);
  58. const char **ArgToArray2(const tokendata_t& Tokens);
  59. const char *ArgGet2(const tokendata_t& Tokens, unsigned int Arg);
  60. unsigned int ArgCount2(const tokendata_t& Tokens);
  61. SOCKET SocketAndConnect(const char *Host, unsigned int Port, const char *BindIp = NULL);
  62. SOCKET SocketAndConnectResolved(const sockaddr *Host, const sockaddr *BindIp, int *error);
  63. SOCKET CreateListener(unsigned int Port, const char *BindIp = NULL, int Family = AF_INET);
  64. char *NickFromHostmask(const char *Hostmask);
  65. const char *UtilMd5(const char *String, const char *Salt, bool BrokenAlgo = false);
  66. const char *GenerateSalt(void);
  67. const char *SaltFromHash(const char *Hash);
  68. void DestroyString(char *String);
  69. SBNCAPI void FlushCommands(commandlist_t *Commands);
  70. SBNCAPI void AddCommand(commandlist_t *Commands, const char *Name, const char *Category, const char *Description, const char *HelpText);
  71. SBNCAPI void DeleteCommand(commandlist_t *Commands, const char *Name);
  72. SBNCAPI int CmpCommandT(const void *pA, const void *pB);
  73. #define BNCVERSION SBNC_VERSION
  74. #define INTERFACEVERSION 25
  75. extern const char *g_ErrorFile;
  76. extern unsigned int g_ErrorLine;
  77. void StrTrim(char *String, char Character);
  78. char *strmcpy(char *Destination, const char *Source, size_t Size);
  79. char *strmcat(char *Destination, const char *Source, size_t Size);
  80. #ifdef HAVE_IPV6
  81. # define SOCKADDR_LEN(Family) ((Family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6))
  82. # define INADDR_LEN(Family) ((Family == AF_INET) ? sizeof(in_addr) : sizeof(in6_addr))
  83. #else /* HAVE_IPV6 */
  84. # define SOCKADDR_LEN(Family) (sizeof(sockaddr_in))
  85. # define INADDR_LEN(Family) (sizeof(in_addr))
  86. #endif /* HAVE_IPV6 */
  87. SBNCAPI const char *IpToString(sockaddr *Address);
  88. SBNCAPI bool StringToIp(const char *IP, int Family, sockaddr *SockAddr, socklen_t Length);
  89. SBNCAPI int CompareAddress(const sockaddr *pA, const sockaddr *pB);
  90. SBNCAPI const sockaddr *HostEntToSockAddr(hostent *HostEnt);
  91. int SetPermissions(const char *Filename, int Modes);
  92. void FreeString(char *String);
  93. void SSL_CTX_set_passwd_cb(SSL_CTX *Context);
  94. #if defined(_DEBUG) && defined(_WIN32)
  95. LONG WINAPI GuardPageHandler(EXCEPTION_POINTERS *Exception);
  96. #endif /* defined(_DEBUG) && defined(_WIN32) */
  97. #ifdef HAVE_POLL
  98. # include <sys/poll.h>
  99. #else /* HAVE_POLL */
  100. struct pollfd {
  101. int fd;
  102. short events;
  103. short revents;
  104. };
  105. #define POLLIN 001
  106. #define POLLPRI 002
  107. #define POLLOUT 004
  108. #define POLLNORM POLLIN
  109. #define POLLERR 010
  110. #define POLLHUP 020
  111. #define POLLNVAL 040
  112. int poll(struct pollfd *fds, unsigned long nfds, int timo);
  113. #endif /* HAVE_POLL */
  114. int sn_getline(char *buf, size_t size);
  115. int sn_getline_passwd(char *buf, size_t size);
  116. SBNCAPI bool RcFailedInternal(int ReturnCode, const char *File, int Line);
  117. SBNCAPI bool AllocFailedInternal(const void *Ptr, const char *File, int Line);
  118. /**
  119. * RcFailed
  120. *
  121. * Checks whether the specified return code signifies
  122. * a failed function call (rc < 0).
  123. *
  124. * @param RC the return code
  125. */
  126. #define RcFailed(RC) RcFailedInternal(RC, __FILE__, __LINE__)
  127. /**
  128. * AllocFailed
  129. *
  130. * Checks whether the result of an allocation function
  131. * is NULL.
  132. *
  133. * @param Variable the variable holding the result
  134. */
  135. #define AllocFailed(Variable) AllocFailedInternal(Variable, __FILE__, __LINE__)
  136. #ifndef _WIN32
  137. lt_dlhandle sbncLoadLibrary(const char *Filename);
  138. #endif
  139. #ifdef __cplusplus
  140. extern "C" {
  141. #endif /* __cplusplus */
  142. SBNCAPI void gfree(void *ptr);
  143. #ifdef __cplusplus
  144. }
  145. #endif /* __cplusplus */
  146. #endif /* UTILITY_H */