1 /// Provides connection parameters for memgraph. 2 module memgraph.params; 3 4 import std.string, std.conv; 5 6 import memgraph.mgclient; 7 8 /// An object containing connection parameters for `Client.connect(Params)`. 9 struct Params { 10 /// DNS resolvable name of host to connect to. Either one of host or 11 /// address parameters must be specified (defaults to `localhost`). 12 string host = "localhost"; 13 /// Port number to connect to at the server host (defaults to 7687). 14 ushort port = 7687; 15 /// Numeric IP address of host to connect to. This should be in the 16 /// standard IPv4 address format. You can also use IPv6 if your machine 17 /// supports it. Either one of host or address parameters must be 18 /// specified. 19 string address; 20 /// Username, if authentication is required. 21 string username; 22 /// Password to be used if the server demands password authentication. 23 string password; 24 /// This option determines whether a secure connection will be negotiated 25 /// with the server. There are 2 possible values: 26 /// - `MG_SSLMODE_DISABLE` 27 /// Only try a non-SSL connection (default). 28 /// - `MG_SSLMODE_REQUIRE` 29 /// Only try an SSL connection. 30 mg_sslmode sslMode = mg_sslmode.MG_SSLMODE_DISABLE; 31 /// This parameter specifies the file name of the client SSL certificate. 32 /// It is ignored in case an SSL connection is not made. 33 string sslCert; 34 /// This parameter specifies the location of the secret key used for the 35 /// client certificate. This parameter is ignored in case an SSL connection 36 /// is not made. 37 string sslKey; 38 /// Useragent used when connecting to memgraph, defaults to 39 /// Alternate name and version of the client to send to server. Default is 40 /// "memgraph-d/major.minor.patch". 41 string userAgent; 42 /// A pointer to a function of prototype `mg_trust_callback_type`: 43 /// int trust_callback(const char *hostname, const char *ip_address, 44 /// const char *key_type, const char *fingerprint, 45 /// void *trust_data); 46 /// 47 /// After performing the SSL handshake, `mg_connect` will call this 48 /// function providing the hostname, IP address, public key type and 49 /// fingerprint and user provided data. If the function returns a non-zero 50 /// value, SSL connection will be immediately terminated. This can be used 51 /// to implement TOFU (trust on first use) mechanism. 52 /// It might happen that hostname can not be determined, in that case the 53 /// trust callback will be called with hostname="undefined". 54 mg_trust_callback_type sslTrustCallback; 55 /// Additional data that will be provided to the sslTrustCallback function. 56 void *sslTrustData; 57 58 /// Destructor, destroys the internal session parameters. 59 ~this() { 60 if (ptr_) 61 mg_session_params_destroy(ptr_); 62 } 63 64 package: 65 const (mg_session_params *) ptr() { 66 if (!ptr_) 67 ptr_ = mg_session_params_make(); 68 if (host.length) 69 mg_session_params_set_host(ptr_, toStringz(host)); 70 if (address.length) 71 mg_session_params_set_address(ptr_, toStringz(address)); 72 if (port) 73 mg_session_params_set_port(ptr_, port); 74 if (username.length) 75 mg_session_params_set_username(ptr_, toStringz(username)); 76 if (password.length) 77 mg_session_params_set_password(ptr_, toStringz(password)); 78 79 if (!userAgent.length) 80 userAgent = to!string("memgraph-d/" ~ fromStringz(mg_client_version())); 81 mg_session_params_set_user_agent(ptr_, toStringz(userAgent)); 82 83 mg_session_params_set_sslmode(ptr_, sslMode); 84 if (sslCert.length) 85 mg_session_params_set_sslcert(ptr_, toStringz(sslCert)); 86 if (sslKey.length) 87 mg_session_params_set_sslkey(ptr_, toStringz(sslKey)); 88 89 if (sslTrustCallback) 90 mg_session_params_set_trust_callback(ptr_, sslTrustCallback); 91 if (sslTrustData) 92 mg_session_params_set_trust_data(ptr_, sslTrustData); 93 94 return ptr_; 95 } 96 97 private: 98 /// Pointer to private `mg_session_params` instance that 99 /// contains all parameters for this `Params` structure. 100 mg_session_params *ptr_; 101 } 102 103 unittest { 104 Params p; 105 106 assert(p.host == "localhost"); 107 assert(p.port == 7687); 108 assert(p.sslMode == mg_sslmode.MG_SSLMODE_DISABLE); 109 110 p.address = "127.0.0.1"; 111 p.username = "sini"; 112 p.password = "whatever"; 113 114 p.sslCert = "someCertFile"; 115 p.sslKey = "someKeyFile"; 116 117 ubyte[] trustData = cast(ubyte[])"trustData"; 118 p.sslTrustData = cast(void*)trustData; 119 p.sslTrustCallback = (hostname, ip_address, key_type, fingerprint, trust_data) { return 0; }; 120 121 assert(p.ptr() != null); 122 }