iad_services.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. *
  3. * Licensed Materials - Property of IBM and/or HCL
  4. *
  5. * IBM Informix Dynamic Server
  6. * Copyright IBM Corporation 1996, 2012
  7. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  8. *
  9. ***************************************************************************
  10. */
  11. #ifndef IAD_SERVICES_H
  12. #define IAD_SERVICES_H
  13. #include "API.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #ifndef MAX_PATH
  18. #define MAX_PATH (255)
  19. #endif
  20. /*
  21. Interface to the system-wide services file
  22. provides functions for adding and removing services, testing for the presence of services
  23. all functions return IAD_E_NOERROR on success and IAD_E_STRUCTURE_NOT_INITIALIZED if a null pointer to install structure is passed.
  24. except for iadServicesLoad, iadServicesFree and iadServicesStatus, all functions will also return IAD_E_STRUCTURE_NOT_INITIALIZED if iadServicesLoad has not been called since zeroing the structure or calling iadServicesFree.
  25. if an internal, memory related error is encountered, IAD_E_MEMORY_ALLOCATION_FAILURE is returned.
  26. //use case: add the 'tf2' service on port 27015 for tcp and udp if the port and name are both free
  27. int main() {
  28. install_t is;
  29. memset(&is,0,sizeof(install_t));
  30. if (iadServicesLoad(&is,"c:\\windows\\system32\\drivers\\etc\\hosts") != IAD_E_NOERROR) {
  31. printf("Could not load services file\n");
  32. return 1;
  33. }
  34. if (iadServicesTestPort(&is,27015,NULL) == PORT_FREE && iadServicesTestName(&is,"tf2",NULL) == NAME_FREE) {
  35. if (iadServicesAddService(&is,"tf2",27015,"tcp",0,NULL,NULL) == IAD_E_NOERROR && iadServicesAddService(&is,"tf2",27015,"udp",0,NULL,NULL)) {
  36. iadServicesDeploy(&is);
  37. }
  38. }
  39. iadServicesFree(&is);
  40. return 0;
  41. }
  42. //use case: add a generated entry for an ids instance
  43. int main() {
  44. install_t is;
  45. char *name;
  46. unsigned short port;
  47. memset(&is,0,sizeof(install_t));
  48. if (iadServicesLoad(&is,"/etc/services") != IAD_E_NOERROR) {
  49. printf("Could not load services file\n");
  50. return 1;
  51. }
  52. if (iadServicesGenerateName(&is,&name,"ids1170") == IAD_E_NOERROR && iadServicesGeneratePort(&is,&port,9088) == IAD_E_NOERROR) {
  53. if (iadServicesAddService(&is,name,port,"tcp",0,NULL,"Added by sample use case") == IAD_E_NOERROR) {
  54. if (iadServicesDeploy(&is) == IAD_E_NOERROR) {
  55. printf("You have been assigned the service '%s' on port %d\n",name,(int)port);
  56. } else {
  57. printf("An open name and port were found, but could not be written to the services file\n");
  58. }
  59. }
  60. free(name);
  61. } else {
  62. printf("An open name and port could not be found, exiting.\n");
  63. }
  64. iadServicesFree(&is);
  65. return 0;
  66. }
  67. */
  68. /*
  69. read in the services at file and get lists of used ports and names
  70. the install structure or at least the services section of it MUST have been zeroed out before this function is called
  71. file: the path to the services file
  72. returns IAD_E_INVALID_PARAMETER if the zeroed-out-requirement above is not met (double loads, neglecting to memset, etc)
  73. returns IAD_E_NULLPOINTER if file is null
  74. returns IAD_E_FILE_NONEXISTENT_ERROR if file does not exist
  75. */
  76. EXPORT int STDCALL iadServicesLoad(install_t *is, const char *file);
  77. /*
  78. returns the status of the services list
  79. Load or Deploy set the state to clean
  80. AddService, AddComment or Delete set the state to dirty
  81. Free sets the state to uninitialized
  82. returns SVCST_UNINITIALIZED if the services have been freed or have not been loaded
  83. returns SVCST_CLEAN if the services are synchronized with disk
  84. returns SVCST_DIRTY if any changes have been made
  85. */
  86. EXPORT int STDCALL iadServicesStatus(install_t *is);
  87. /*
  88. writes changes to the service list out to the file that was loaded with iadServicesLoad
  89. returns IAD_E_FAIL if no file has been loaded with iadServicesLoad
  90. returns IAD_E_INVALID_PARAMETER if file could not be written
  91. */
  92. EXPORT int STDCALL iadServicesDeploy(install_t *is);
  93. /*
  94. write out a new services file based on the services lists. this currently rewrites the entire file and does not make a backup
  95. file: the path to where the new file will be written
  96. returns IAD_E_NULLPOINTER if file is null
  97. returns IAD_E_INVALID_PARAMETER if file could not be created
  98. */
  99. EXPORT int STDCALL iadServicesDeployFile(install_t *is, const char *file);
  100. /*
  101. release all memory used to hold the services list
  102. */
  103. EXPORT void STDCALL iadServicesFree(install_t *is);
  104. /*
  105. add a new service to the services list. does not check to see if the name, port or aliases conflict with any existing entries
  106. name: the name of the service
  107. port: the port the service will use
  108. protocol: the protocol the service will use ("tcp", "udp" are common; others exist, check your platform)
  109. aliascount: the number of aliases passed in the next parameter
  110. aliases: an array of other names that the service could be known as. NULL can be passed if aliascount == 0
  111. comment: the comment that will go with the service; the '#' and end of line '\n' are added automatically, embedded '\n's are converted to spaces. NULL can be passed if no comment is desired.
  112. name and aliases must contain only numbers, letters (any case), dashes '-', and underscores '_'
  113. returns IAD_E_NULLPOINTER if name or protocol is null; also if aliascount is nonzero and aliases, or aliases[k] for 0 <= k <= aliascount is null
  114. returns IAD_E_INVALID_PARAMETER if port is zero, if the name or port is not available, or if an invalid character is found in the name or any alias
  115. */
  116. EXPORT int STDCALL iadServicesAddService(
  117. install_t *is,
  118. const char *name,
  119. unsigned short port,
  120. const char *protocol,
  121. int aliascount,
  122. const char **aliases,
  123. const char *comment);
  124. /*
  125. add a comment line to the services file
  126. text: the text of the comment. embedded line breaks may be used to create multiline comments
  127. returns IAD_E_NULLPOINTER if text is null
  128. */
  129. EXPORT int STDCALL iadServicesAddComment(install_t *is, const char *text);
  130. /*
  131. removes services that match the arguments from the services list.
  132. if name is non-null, all services with that name or that name as an alias are removed
  133. if port is not ANY_PORT, all services which use that port are removed
  134. if both are specified, services which match the port condition AND the alias condition are removed.
  135. if neither is specified, IAD_E_INVALID_PARAMETER is returned
  136. in any case, if protocol is specified, services must also match the protocol to be removed
  137. port: if not ANY_PORT, specifies the port number to delete
  138. name: if non-null, specifies the name or alias to delete
  139. protocol: if non-null, specifies which protocol to delete
  140. returns IAD_E_INVALID_PARAMETER if port and name are unspecified
  141. */
  142. EXPORT int STDCALL iadServicesRemoveService(
  143. install_t *is,
  144. unsigned short port,
  145. const char *name,
  146. const char *protocol);
  147. /*
  148. determines if any services have claimed a port/protocol combination for use
  149. also attempts to bind to the port to see if it is being used without a services entry
  150. if protocol is not specified, checks against all protocols
  151. port: the desired port to test
  152. protocol: if non-null, which protocol to test
  153. returns IAD_E_INVALID_PARAMETER if port is 0
  154. returns IAD_E_NOERROR/PORT_FREE (0) if the port is free
  155. returns PORT_USED (1) if the port is being used without a services entry (or you are a non-root user testing ports < 1024)
  156. returns PORT_CLAIMED (2) if the port is used in a services entry
  157. */
  158. EXPORT int STDCALL iadServicesTestPort(install_t *is, unsigned short port, const char *protocol);
  159. /*
  160. determines if any services have claimed a name/protocol combination for use
  161. if protocol is not specified, checks against all protocols
  162. name: the desired name to test
  163. protocol: if non-null, which protocol to test
  164. returns IAD_E_NOERROR/NAME_FREE (0) if name is free
  165. returns NAME_USED (1) if the name is in use
  166. */
  167. EXPORT int STDCALL iadServicesTestName(install_t *is, const char *name, const char *protocol);
  168. /* get the port of a given name */
  169. EXPORT unsigned short STDCALL iadServicesGetPortFromName(install_t *is, const char *name, const char *protocol);
  170. /*
  171. requests a service name that is unused for every protocol; tries preferred first, then preferred[1-9]
  172. buffer: recieves a newly allocated string containing the generated name. the caller is responsible for freeing this memory; it is invalid unless this function returned IAD_E_NOERROR
  173. preferred: a suggestion for the service name that the application would like
  174. if buffer is null, IAD_E_NULLPOINTER is returned
  175. if preferred is null or empty, IAD_E_INVALID_PARAMETER is returned
  176. if a free name could not be found, IAD_E_FAIL is returned
  177. */
  178. EXPORT int STDCALL iadServicesGenerateName(install_t *is, char **buffer, const char *preferred);
  179. /*
  180. requests a port that is unclaimed for every protocol and currently unused (not bound to or claimed by a service). if not 0, tries the preferred port first, then generates a random port if necessary
  181. port: pointer to an unsigned short to recieve a clear port number; is invalid unless function returns IAD_E_NOERROR
  182. preferred: a suggestion for the port that the application would like; may be zero to specify that no particular port is desired
  183. if port is null, IAD_E_NULLPOINTER is returned
  184. if a free port could not be found, IAD_E_FAIL is returned
  185. */
  186. EXPORT int STDCALL iadServicesGeneratePort(install_t *is, unsigned short *port, unsigned short preferred);
  187. /*
  188. enables a dirty hack to short circuit the bind test and rely on the services file only.
  189. once it's on, it's stuck on.
  190. */
  191. EXPORT int STDCALL iadServicesCheatSockets(install_t *is);
  192. #ifdef __cplusplus
  193. }
  194. #endif
  195. #endif /* IAD_SERVICES_H */