PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/inline-more/otherlibs/win32unix/sockopt.c

http://github.com/OCamlPro/OCamlPro-OCaml-Branch
C | 229 lines | 189 code | 26 blank | 14 comment | 14 complexity | 76c699c78b9c4b9dc9f0a771f2a7d2d7 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0
  1. /***********************************************************************/
  2. /* */
  3. /* Objective Caml */
  4. /* */
  5. /* Xavier Leroy and Pascal Cuoq, projet Cristal, INRIA Rocquencourt */
  6. /* */
  7. /* Copyright 1996 Institut National de Recherche en Informatique et */
  8. /* en Automatique. All rights reserved. This file is distributed */
  9. /* under the terms of the GNU Library General Public License, with */
  10. /* the special exception on linking described in file ../../LICENSE. */
  11. /* */
  12. /***********************************************************************/
  13. /* $Id$ */
  14. #include <errno.h>
  15. #include <mlvalues.h>
  16. #include <memory.h>
  17. #include <alloc.h>
  18. #include <fail.h>
  19. #include "unixsupport.h"
  20. #include "socketaddr.h"
  21. #ifndef IPPROTO_IPV6
  22. #define IPPROTO_IPV6 (-1)
  23. #endif
  24. #ifndef IPV6_V6ONLY
  25. #define IPV6_V6ONLY (-1)
  26. #endif
  27. enum option_type {
  28. TYPE_BOOL = 0,
  29. TYPE_INT = 1,
  30. TYPE_LINGER = 2,
  31. TYPE_TIMEVAL = 3,
  32. TYPE_UNIX_ERROR = 4
  33. };
  34. struct socket_option {
  35. int level;
  36. int option;
  37. };
  38. /* Table of options, indexed by type */
  39. static struct socket_option sockopt_bool[] = {
  40. { SOL_SOCKET, SO_DEBUG },
  41. { SOL_SOCKET, SO_BROADCAST },
  42. { SOL_SOCKET, SO_REUSEADDR },
  43. { SOL_SOCKET, SO_KEEPALIVE },
  44. { SOL_SOCKET, SO_DONTROUTE },
  45. { SOL_SOCKET, SO_OOBINLINE },
  46. { SOL_SOCKET, SO_ACCEPTCONN },
  47. { IPPROTO_TCP, TCP_NODELAY },
  48. { IPPROTO_IPV6, IPV6_V6ONLY}
  49. };
  50. static struct socket_option sockopt_int[] = {
  51. { SOL_SOCKET, SO_SNDBUF },
  52. { SOL_SOCKET, SO_RCVBUF },
  53. { SOL_SOCKET, SO_ERROR },
  54. { SOL_SOCKET, SO_TYPE },
  55. { SOL_SOCKET, SO_RCVLOWAT },
  56. { SOL_SOCKET, SO_SNDLOWAT } };
  57. static struct socket_option sockopt_linger[] = {
  58. { SOL_SOCKET, SO_LINGER }
  59. };
  60. static struct socket_option sockopt_timeval[] = {
  61. { SOL_SOCKET, SO_RCVTIMEO },
  62. { SOL_SOCKET, SO_SNDTIMEO }
  63. };
  64. static struct socket_option sockopt_unix_error[] = {
  65. { SOL_SOCKET, SO_ERROR }
  66. };
  67. static struct socket_option * sockopt_table[] = {
  68. sockopt_bool,
  69. sockopt_int,
  70. sockopt_linger,
  71. sockopt_timeval,
  72. sockopt_unix_error
  73. };
  74. static char * getsockopt_fun_name[] = {
  75. "getsockopt",
  76. "getsockopt_int",
  77. "getsockopt_optint",
  78. "getsockopt_float",
  79. "getsockopt_error"
  80. };
  81. static char * setsockopt_fun_name[] = {
  82. "setsockopt",
  83. "setsockopt_int",
  84. "setsockopt_optint",
  85. "setsockopt_float",
  86. "setsockopt_error"
  87. };
  88. union option_value {
  89. int i;
  90. struct linger lg;
  91. struct timeval tv;
  92. };
  93. CAMLexport value
  94. unix_getsockopt_aux(char * name,
  95. enum option_type ty, int level, int option,
  96. value socket)
  97. {
  98. union option_value optval;
  99. socklen_param_type optsize;
  100. switch (ty) {
  101. case TYPE_BOOL:
  102. case TYPE_INT:
  103. case TYPE_UNIX_ERROR:
  104. optsize = sizeof(optval.i); break;
  105. case TYPE_LINGER:
  106. optsize = sizeof(optval.lg); break;
  107. case TYPE_TIMEVAL:
  108. optsize = sizeof(optval.tv); break;
  109. default:
  110. unix_error(EINVAL, name, Nothing);
  111. }
  112. if (getsockopt(Socket_val(socket), level, option,
  113. (void *) &optval, &optsize) == -1)
  114. uerror(name, Nothing);
  115. switch (ty) {
  116. case TYPE_BOOL:
  117. case TYPE_INT:
  118. return Val_int(optval.i);
  119. case TYPE_LINGER:
  120. if (optval.lg.l_onoff == 0) {
  121. return Val_int(0); /* None */
  122. } else {
  123. value res = alloc_small(1, 0); /* Some */
  124. Field(res, 0) = Val_int(optval.lg.l_linger);
  125. return res;
  126. }
  127. case TYPE_TIMEVAL:
  128. return copy_double((double) optval.tv.tv_sec
  129. + (double) optval.tv.tv_usec / 1e6);
  130. case TYPE_UNIX_ERROR:
  131. if (optval.i == 0) {
  132. return Val_int(0); /* None */
  133. } else {
  134. value err, res;
  135. err = unix_error_of_code(optval.i);
  136. Begin_root(err);
  137. res = alloc_small(1, 0); /* Some */
  138. Field(res, 0) = err;
  139. End_roots();
  140. return res;
  141. }
  142. default:
  143. unix_error(EINVAL, name, Nothing);
  144. return Val_unit; /* Avoid warning */
  145. }
  146. }
  147. CAMLexport value
  148. unix_setsockopt_aux(char * name,
  149. enum option_type ty, int level, int option,
  150. value socket, value val)
  151. {
  152. union option_value optval;
  153. socklen_param_type optsize;
  154. double f;
  155. switch (ty) {
  156. case TYPE_BOOL:
  157. case TYPE_INT:
  158. optsize = sizeof(optval.i);
  159. optval.i = Int_val(val);
  160. break;
  161. case TYPE_LINGER:
  162. optsize = sizeof(optval.lg);
  163. optval.lg.l_onoff = Is_block (val);
  164. if (optval.lg.l_onoff)
  165. optval.lg.l_linger = Int_val (Field (val, 0));
  166. break;
  167. case TYPE_TIMEVAL:
  168. f = Double_val(val);
  169. optsize = sizeof(optval.tv);
  170. optval.tv.tv_sec = (int) f;
  171. optval.tv.tv_usec = (int) (1e6 * (f - optval.tv.tv_sec));
  172. break;
  173. case TYPE_UNIX_ERROR:
  174. default:
  175. unix_error(EINVAL, name, Nothing);
  176. }
  177. if (setsockopt(Socket_val(socket), level, option,
  178. (void *) &optval, optsize) == -1)
  179. uerror(name, Nothing);
  180. return Val_unit;
  181. }
  182. CAMLprim value unix_getsockopt(value vty, value vsocket, value voption)
  183. {
  184. enum option_type ty = Int_val(vty);
  185. struct socket_option * opt = &(sockopt_table[ty][Int_val(voption)]);
  186. return unix_getsockopt_aux(getsockopt_fun_name[ty],
  187. ty,
  188. opt->level,
  189. opt->option,
  190. vsocket);
  191. }
  192. CAMLprim value unix_setsockopt(value vty, value vsocket, value voption,
  193. value val)
  194. {
  195. enum option_type ty = Int_val(vty);
  196. struct socket_option * opt = &(sockopt_table[ty][Int_val(voption)]);
  197. return unix_setsockopt_aux(setsockopt_fun_name[ty],
  198. ty,
  199. opt->level,
  200. opt->option,
  201. vsocket,
  202. val);
  203. }