PageRenderTime 52ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/zuluCrypt-cli/utility/socket/socket.h

https://gitlab.com/m.schmidt/zuluCrypt
C Header | 320 lines | 38 code | 35 blank | 247 comment | 0 complexity | d7f6c859423fe5fb8de34bcb5ca8fa2c MD5 | raw file
Possible License(s): BSD-3-Clause-No-Nuclear-License-2014, BSD-2-Clause
  1. /*
  2. *
  3. * Copyright (c) 2012-2015
  4. * name : Francis Banyikwa
  5. * email: mhogomchungu@gmail.com
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef SOCKET
  20. #define SOCKET
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #include <sys/types.h>
  25. typedef struct SocketType_t * socket_t ;
  26. #define SocketVoid ( ( socket_t ) 0 )
  27. /*
  28. * Takes a pointer to a function to be called when memory allocation can not take place
  29. * ie if the system has run out of memory and malloc() or realloc() has failed.
  30. * This function is optional and "SocketVoid" will be returned on memory exaustion if the function
  31. * isnt set.
  32. */
  33. void SocketExitOnMemoryExaustion( void (*)( void ) ) ;
  34. /*
  35. * creates a local unix socket
  36. * SocketVoid is returned on error
  37. * socket is created with default type of SOCK_STREAM and protocol value of 0
  38. */
  39. socket_t SocketLocal( const char * address ) ;
  40. /*
  41. * creates a local unix socket
  42. * type is the second argument to socket()
  43. * protocol is the third argument to socket()
  44. * SocketVoid is returned on error
  45. */
  46. socket_t SocketLocalWithOptions( const char * address,int type,int protocol ) ;
  47. /*
  48. * create an IPv4 internet socket with an address of "address" and at port number "port"
  49. * eg socket_t socket = SocketNetByName( "google.com",80 ) ;
  50. *
  51. * address could be something like "google.com" or "192.168.1.1"
  52. *
  53. * SocketVoid is returned on error
  54. * socket is created with default type of SOCK_STREAM and protocol value of 0
  55. */
  56. socket_t SocketNet( const char * address,int port ) ;
  57. /*
  58. * create an internet socket with an address of "address" and at port number "port"
  59. * eg socket_t socket = SocketNetByName( "google.com",80 ) ;
  60. *
  61. * type is the second argument to socket()
  62. * protocol is the third argument to socket()
  63. *
  64. * SocketVoid is returned on error
  65. */
  66. socket_t SocketNetWithOptions( const char * address,int port,int type,int protocol ) ;
  67. /*
  68. * create an IPv6 internet socket with an address of "address" and at port number "port"
  69. * eg socket_t socket = SocketNet6( "google.com",80 ) ;
  70. *
  71. * address could be something like "google.com"
  72. *
  73. * SocketVoid is returned on error
  74. * socket is created with default type of SOCK_STREAM and protocol value of 0
  75. */
  76. socket_t SocketNet6( const char * address,int port ) ;
  77. /*
  78. * create an IPv6 internet socket with an address of "address" and at port number "port"
  79. * eg socket_t socket = SocketNetWithOptions6( "google.com",80,SOCK_STREAM,0 ) ;
  80. *
  81. * type is the second argument to socket()
  82. * protocol is the third argument to socket()
  83. *
  84. * SocketVoid is returned on error
  85. */
  86. socket_t SocketNetWithOptions6( const char * address,int port,int type,int protocol ) ;
  87. /*
  88. * return an address associated with the socket.
  89. *
  90. * NULL is returned if SocketVoid is passed or on error
  91. */
  92. const char * SocketAddress( socket_t ) ;
  93. /*
  94. * bind a socket. This will make a socket a server socket
  95. * 1 is returned on success
  96. * 0 is returned on error
  97. */
  98. int SocketBind( socket_t ) ;
  99. /*
  100. * return a file descriptor associated with a socket
  101. */
  102. int SocketFileDescriptor( socket_t ) ;
  103. /*
  104. * Accept a socket
  105. * SocketVoid is returned on error
  106. */
  107. socket_t SocketAccept( socket_t ) ;
  108. /*
  109. * Wait for upto time seconds for an attempt to connect.
  110. * SocketVoid is returned if time time expire or on error.
  111. * A socket_t object is returned on success.
  112. */
  113. socket_t SocketAcceptWithTimeOut( socket_t,time_t time ) ;
  114. /*
  115. * connect to a server a client socket is to connect to
  116. * 1 is returned on success
  117. * 0 is returned on error and the socket is invalidated, a new socket will have to be create to repeat
  118. * the operation
  119. */
  120. int SocketConnect( socket_t * client ) ;
  121. /*
  122. * listen for an incomming connection
  123. * 1 is returned on success
  124. * 0 is returned on error
  125. */
  126. int SocketListen( socket_t ) ;
  127. /*
  128. * get bounded amount of data from socket and allocate memory dynamically for it and return a pointer to the allocated memory through pointer "buffer"
  129. *
  130. * on success,the returned value is greater than 0 and the buffer should be free() when done with it.
  131. * on error,the returned value is 0 and no memory is allocated
  132. *
  133. * get only len number of bytes.
  134. *
  135. * The buffer is NULL terminated
  136. *
  137. * It is the responsibility of the caller to free() the buffer
  138. * This function will block until len bytes are read on the other side close the connection
  139. *
  140. * eg
  141. * char * data ;
  142. * socket_t client = SocketNetByIPAddress( "127.0.0.1",1331 ) ;
  143. * SocketConnect( client ) ;
  144. * SocketGetData_1( client,&data,32 ) ;
  145. * puts( data ) ;
  146. * free( data ) ;
  147. * SocketClose( client ) ;
  148. * SocketDelete( &clien ) ;
  149. *
  150. * returned value is the number of bytes read or -1 on error
  151. */
  152. ssize_t SocketGetData_1( socket_t,char ** buffer,size_t len ) ;
  153. /*
  154. * get an unbounded amount of data from socket,create memory for it dynamically and return a pointer to the allocated memory through pointer "buffer"
  155. *
  156. * on success,the returned value is greater than 0 and the buffer should be free() when done with it.
  157. * on error,the returned value is 0 and no memory is allocated
  158. * returned value is the number of bytes read or -1 on error
  159. * The buffer is NULL terminated
  160. *
  161. * It is the responsibility of the caller to free() the buffer
  162. */
  163. ssize_t SocketGetData( socket_t,char ** buffer ) ;
  164. /*
  165. * get data from a socket and put in a user managed buffer "buffer" of size len
  166. * The buffer will be null terminated and hence must have additional space to accomodate null character.
  167. * If you expect to read a maximum of 32 bytes from the socket for example, you must have a buffer with atleast 33 blocks in size
  168. * and you must pass 33 to the function.
  169. * returned value is the number of bytes read or -1 on error
  170. * The buffer is not NULL terminated
  171. */
  172. ssize_t SocketGetData_2( socket_t,char * buffer,size_t len ) ;
  173. /*
  174. * this function behaves like SocketGetData_2 but it takes an additional argument
  175. * of time in seconds for how long the function should wait for data and it returns
  176. * -1 if timer expired
  177. */
  178. ssize_t SocketGetData_3( socket_t,char * buffer,size_t len,int timeout ) ;
  179. /*
  180. * send data to a socket,
  181. * return number of bytes sent or -1 on error
  182. */
  183. ssize_t SocketSendData( socket_t,const char * buffer,size_t len ) ;
  184. /*
  185. * set the socket to not block the calling thread waiting for events over the socket
  186. * -1 is returned on error, other numbers on success
  187. */
  188. int SocketSetDoNotBlock( socket_t ) ;
  189. /*
  190. * set the socket to a block the calling thread waiting for events over the socket
  191. * This is the default setting.
  192. * -1 is returned on error, other numbers on success
  193. */
  194. int SocketSetBlock( socket_t ) ;
  195. /*
  196. * return 1 if a socket is a blocking socket
  197. * return 0 if a socket is a non blocking socket
  198. * return -1 on error( if socket is SocketVoid ).
  199. */
  200. int SocketIsBlocking( socket_t ) ;
  201. /*
  202. * close a socket and free up all used resources
  203. */
  204. void SocketClose( socket_t * ) ;
  205. /*
  206. * close the write channel on the socket
  207. */
  208. void SocketCloseWriteChannel( socket_t ) ;
  209. /*
  210. * close the read channel on the socket
  211. */
  212. void SocketCloseReadChannel( socket_t ) ;
  213. /*
  214. * set the maximum number of connections to accept.
  215. * This function must be called before SocketListen() to change the default from 1.
  216. */
  217. void SocketSetListenMaximum( socket_t,int ) ;
  218. /*
  219. examples:
  220. a server program that listens for new connections and print data they send until it receices "exit" message
  221. #include "socket.h"
  222. #include <string.h>
  223. #include <stdio.h>
  224. int main( void )
  225. {
  226. //socket_t server = SocketLocal( "/tmpt/socketLocal" ) ;
  227. socket_t server = SocketNetByName( "localhost",4000 ) ;
  228. socket_t client ;
  229. SocketBind( server ) ;
  230. char data[ 33 ] ;
  231. while( 1 ){
  232. SocketListen( server ) ;
  233. client = SocketAccept( server ) ;
  234. SocketGetData_2( client,data,sizeof( data ) ) ;
  235. if( strcmp( data,"exit" ) == 0 ){
  236. printf( "exit command received, exiting\n" ) ;
  237. SocketClose( &client ) ;
  238. break ;
  239. }else{
  240. printf( "message received: %s\n",data ) ;
  241. SocketClose( &client ) ;
  242. }
  243. }
  244. SocketClose( &server ) ;
  245. return 0 ;
  246. }
  247. A client program that sends a message to a server
  248. #include "socket.h"
  249. #include <string.h>
  250. #include <stdio.h>
  251. int main( int argc,char * argv[] )
  252. {
  253. if( argc != 2 )
  254. return printf( "wrong number of arguments,expecting one argument\n" ) ;
  255. const char * data = argv[ 1 ] ;
  256. //socket_t client = SocketLocal( "/tmpt/socketLocal" ) ;
  257. socket_t client = SocketNetByName( "localhost",4000 ) ;
  258. SocketConnect( &client ) ;
  259. SocketSendData( client,data,strlen( data ) ) ;
  260. SocketClose( &client ) ;
  261. return 0 ;
  262. }
  263. */
  264. #ifdef __cplusplus
  265. }
  266. #endif
  267. #endif