Source file session.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
type open_netsnmp_session

type t =
  { session : open_netsnmp_session
  ; session_id : int
  }
[@@deriving fields ~getters]

module Snmp_version = struct
  type t =
    | Version_1
    | Version_2c
    | Version_3
end

module Snmp_sec_auth_proto = struct
  type t =
    | Ignore
    | UsmHMACMD5AuthProtocol
end

module Snmp_security_level = struct
  type t =
    | NoAuthNoPriv
    | AuthNoPriv
end

(* Internal type used only for C interop *)
module Snmp_session_info = struct
  type t =
    { version : Snmp_version.t
    ; retries : int
    ; timeout : int
    ; peername : string
    ; localname : string
    ; local_port : int
    ; community : string
    ; securityName : string
    ; securityLevel : Snmp_security_level.t
    ; securityAuthProto : Snmp_sec_auth_proto.t
    ; securityAuthPassword : string
    }
end

external netsnmp_init : unit -> unit = "caml_netsnmp_init"

external snmp_sess_open_c
  :  Snmp_session_info.t
  -> open_netsnmp_session
  = "caml_snmp_sess_open"

external snmp_sess_close_c : open_netsnmp_session -> unit = "caml_snmp_sess_close"

external snmp_sess_synch_response_c
  :  open_netsnmp_session
  -> Pdu.t
  -> (Oid.t * ASN1_value.t) list
  = "caml_snmp_sess_synch_response"

let next_session_id =
  let next_session_id = ref Int.min_int in
  fun () ->
    if !next_session_id = Int.max_int
    then raise (Failure "Ran out of session ids")
    else (
      let ret = !next_session_id in
      incr next_session_id;
      ret)
;;

let snmp_sess_open
  ~version
  ~retries
  ~timeout
  ~peername
  ~localname
  ~local_port
  ~community
  ~securityName
  ~securityLevel
  ~securityAuthProto
  ~securityAuthPassword
  ()
  =
  let session_info =
    { Snmp_session_info.version
    ; retries
    ; timeout
    ; peername
    ; localname
    ; local_port
    ; community
    ; securityName
    ; securityLevel
    ; securityAuthProto
    ; securityAuthPassword
    }
  in
  let session = snmp_sess_open_c session_info in
  { session; session_id = next_session_id () }
;;

let snmp_sess_close t = snmp_sess_close_c t.session
let snmp_sess_synch_response t pdu = snmp_sess_synch_response_c t.session pdu |> List.rev