/src/request.h

https://github.com/yaddns/yaddns · C Header · 162 lines · 79 code · 24 blank · 59 comment · 1 complexity · bd59d1734a6c10922b8d238b9014b8a0 MD5 · raw file

  1. /*
  2. * Yaddns - Yet Another ddns client
  3. * Copyright (C) 2008 Anthony Viallard <anthony.viallard@patatrac.info>
  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, either version 3 of the License, or
  8. * (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, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef _YADDNS_REQUEST_H_
  19. #define _YADDNS_REQUEST_H_
  20. #include <sys/time.h>
  21. #include <netinet/in.h>
  22. #include "list.h"
  23. #include "util.h"
  24. #define REQUEST_DATA_MAX_SIZE 512
  25. #define REQUEST_PENDING_ACTION_TIMEOUT 30
  26. #define REQ_OPT_BIND_ADDR 0x01 << 0
  27. #define REQ_ERR_UNKNOWN 0
  28. #define REQ_ERR_SYSTEM 1
  29. #define REQ_ERR_CONNECT_FAILED 2
  30. #define REQ_ERR_CONNECT_TIMEOUT 3
  31. #define REQ_ERR_RESPONSE_TIMEOUT 4
  32. #define REQ_ERR_SENDING_TIMEOUT 5
  33. static inline const char *strreqerr(unsigned int req_err)
  34. {
  35. const char *req_err_str[] = {
  36. "Unknown error",
  37. "System error",
  38. "Connection has failed",
  39. "Connection timeout",
  40. "Receive timeout",
  41. "Send timeout",
  42. };
  43. if(req_err >= ARRAY_SIZE(req_err_str))
  44. {
  45. req_err = 0;
  46. }
  47. return req_err_str[req_err];
  48. }
  49. /*
  50. * This module is for helping send an request and receive
  51. * response.
  52. *
  53. * A request has 8 flow states availables:
  54. * - FSError => An error occur (view errcode for more info)
  55. * - FSCreated => The request is created
  56. * - FSConnecting => The request has a connecting
  57. * socket to request_host
  58. * - FSConnected => The request has a connection
  59. * - FSSending => The request is sent
  60. * - FSWaitingResponse => Waiting request response
  61. * - FSResponseReceived => Request response was received
  62. * - FSFinished => Request is terminated
  63. *
  64. * A request with FSError is along of errcode variable which detail
  65. * the error (view REQ_ERR_* macro def).
  66. */
  67. struct request;
  68. struct request_ctl {
  69. void (*hook_func)(struct request *request, void *hook_data);
  70. /* unsigned long hook_mask; */
  71. void *hook_data; /* data given in arg to hook_func when is called */
  72. };
  73. struct request_host {
  74. char addr[128];
  75. unsigned short int port;
  76. };
  77. struct request_buff {
  78. char data[REQUEST_DATA_MAX_SIZE];
  79. size_t data_size; /* total count of chars in buf */
  80. size_t data_ack; /* count of chars read or write yet */
  81. };
  82. struct request_opt {
  83. unsigned long mask;
  84. struct in_addr bind_addr;
  85. };
  86. struct request {
  87. int s;
  88. struct request_host host;
  89. struct request_ctl ctl;
  90. struct request_buff buff;
  91. struct request_opt opt;
  92. enum {
  93. FSError = -1,
  94. FSCreated,
  95. FSConnecting,
  96. FSConnected,
  97. FSSending,
  98. FSWaitingResponse,
  99. FSResponseReceived,
  100. FSFinished,
  101. } state;
  102. unsigned int errcode;
  103. struct timeval last_pending_action;
  104. struct list_head list;
  105. };
  106. /*
  107. * init request list
  108. */
  109. void request_ctl_init(void);
  110. /*
  111. * cleanup request list
  112. */
  113. void request_ctl_cleanup(void);
  114. /*
  115. * Send a request
  116. */
  117. int request_send(struct request_host *host,
  118. struct request_ctl *ctl,
  119. struct request_buff *buff,
  120. struct request_opt *opt);
  121. /*
  122. * request_ctl_selectfds
  123. *
  124. * - fill fd_set with fd to watch
  125. * - set max_fd
  126. */
  127. void request_ctl_selectfds(fd_set *readset, fd_set *writeset, int *max_fd);
  128. /*
  129. * request_processfds
  130. *
  131. * - following fd_set, manage request flow
  132. */
  133. void request_ctl_processfds(fd_set *readset, fd_set *writeset);
  134. /*
  135. * Remove all current requests
  136. */
  137. int request_ctl_remove_by_hook_data(const void *hook_data);
  138. #endif