Get FQDN in C# running on Mono - macos

I'm using Xamarin.mac. I need to get the fully qualified domain name of the local computer. On Windows this code works:
public string GetFQDN()
{
string domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
string hostName = Dns.GetHostName();
string fqdn = "";
if (!hostName.Contains(domainName))
fqdn = hostName + "." + domainName;
else
fqdn = hostName;
return fqdn;
}
On a mac this code causes this error: System.NotSupportedException: This platform is not supported.
So, what is the equivalent in Xamarin.mac? Or just in Mono?
Just getting the computer name would be a good start.

To do this, you can pretty much do the same you'd do in C on a UNIX system, which is to retrieve the hostname with gethostname() and then use a DNS lookup to find the canonical network name for the host. Luckily, System.Net has ready-made calls for this. The following code should work on both OS X and Linux (in fact, on Linux it is more or less what hostname --fqdn does):
using System;
using System.Net;
class Program {
static void Main() {
// Step 1: Get the host name
var hostname = Dns.GetHostName();
// Step 2: Perform a DNS lookup.
// Note that the lookup is not guaranteed to succeed, especially
// if the system is misconfigured. On the other hand, if that
// happens, you probably can't connect to the host by name, anyway.
var hostinfo = Dns.GetHostEntry(hostname);
// Step 3: Retrieve the canonical name.
var fqdn = hostinfo.HostName;
Console.WriteLine("FQDN: {0}", fqdn);
}
}
Note that with a misconfigured DNS, the DNS lookup may fail, or you may get the rather useless "localhost.localdomain".
If you wish to emulate your original approach, you can use the following code to retrieve the domainname:
var domainname = new StringBuilder(256);
Mono.Unix.Native.Syscall.getdomainname(domainname,
(ulong) domainname.Capacity - 1);
You will need to add the Mono.Posix assembly to your build for this.

Related

Powershell Extracting sub string from string

i have a String that either be network address or could be host name
$example1 = '\\192.168.3.3\s$\blabla\blabla\bla.txt'
$example2 = '\\srv\s$\blabla\bla.txt'
Im trying to test connection to the servers so i need only the \*****\ part.
$example1 = 192.168.3.3., $example2 = srv
im trying to use the -Match operation but im getting errors, can anyone assist?
There is a quick solution:
$example1.split("\")[2]
Works for both IP address or host names.

Embarcadero Rad Studio Firemonkey Indy POP3 IPv6 socket error 11004

I have the following problem. I use Embarcadero Rad Studio 10.1 Berlin Firemonkey. I want to connect to a POP3 server within an IPv6 network where IPv4 is not enabled and I get the socket error # 11004 error. My code is:
int port = 110;
pop3->Host = ...
pop3->Username = ...
pop3->Password = ..
pop3->AuthType = patUserPass;
pop3->IOHandler = iOHandlerStack;
iOHandlerStack->IPVersion = TIdIPVersion::Id_IPv6;
pop3->UseTLS = utNoTLSSupport;
pop3->Port = port;
iOHandlerStack->Port = port;
try
{
pop3->ConnectTimeout = 5000;
pop3->Connect();
int msgc = pop3->CheckMessages();
pop3->Disconnect();
ShowMessage("OK: " + IntToStr(msgc));
}
catch (Exception &exc)
{
ShowMessage(exc.Message);
}
If I use the application within an IPv4 network then after connecting the IPVersion property is set to Id_IPv4 automatically and there is no problem.
Interesting thing is that the following code works within IPv6 network:
IdTCPClient->IPVersion = TIdIPVersion::Id_IPv6;
IdTCPClient->Host = ...
IdTCPClient->Port = 80;
IdTCPClient->Connect();
IdTCPClient->Disconnect();
ShowMessage("OK");
Any suggestion?
You need to set the pop3->IPVersion property (which is inherited from TIdTCPClient) instead of the iOHandlerStack->IPVersion property directly.
Connect() overrides the IOHandler's IPVersion property value with the higher layer TIdTCPClient::IPVersion property value.
So, if you are leaving pop3->IPVersion to its default value of Id_IPv4, that would explain why you can't connect using IPv6. And why your TIdTCPClient example works.
Also, you don't need to set the iOHandlerStack->Port property manually, Connect() handles that as well.

I am unable to use a Type Provider on a localhost for an Android Emulator

I am unable to use a Type Provider on a localhost for an Android Emulator.
This doesn't work for me when using F#'s Type Provider because the TypeProvider establishes a connection at design-time.
The Type Provider is happy when I use localhost
[<Literal>]
let JsonPath = "http://localhost:48213/api/cars"
type Repository = JsonProvider<JsonPath>
However, I receive a connection refused when attempting to use the localhost path. Details about this error can be found here.
As a result, I believe the resolution is to use an alternative IP address:
10.0.2.2
The Type Provider is NOT happy when I use 10.0.2.2
Replacing "localhost" with "10.0.2.2" on the IP address results in a compilation error stemming from the TypeProvider not establishing a connection to the updated IP address.
The following code will not compile when I attempt to use "10.0.2.2" as the alternative IP (AKA: workaround) which is the required IP for the emulator to work:
[<Literal>] (* "10.0.2.2" is the workaround IP for local host *)
let JsonPath = "http://10.0.2.2:48213/api/cars"
type Repository = JsonProvider<JsonPath>
type Car = { Make:string ; Model:string }
let getCars() =
Repository.Load JsonPath
|> Array.toSeq
|> Seq.map(fun x -> { Make=x.Make ; Model=x.Model })
In conclusion, how can I access a file via a WebAPI service run locally, and still keep the Type Provider happy when executing the Type Provider on an Emulator?
The static parameter you provide when you create the JsonProvider is purely for design time, in order for it to generate you the types. When you call Repositor.Load this will be the uri you use for runtime. They should be treated separately, like this:
[<Literal>]
let DevelopmentJsonPath = "http://localhost:48213/api/cars"
type Repository = JsonProvider<DevelopmentJsonPath>
type Car = { Make:string ; Model:string }
let runtimeUri = "http://production.mycompany.com/api/cars"
let getCars() =
Repository.Load runtimeUri
|> Array.toSeq
|> Seq.map(fun x -> { Make=x.Make ; Model=x.Model })

InitiateShutdown fails with RPC_S_SERVER_UNAVAILABLE error for a remote computer

I'm trying to implement rebooting of a remote computer with InitiateShutdown API using the following code, but it fails with RPC_S_SERVER_UNAVAILABLE or 1722 error code:
//Process is running as administrator
//Select a remote machine to reboot:
//INFO: Tried it with and w/o two opening slashes.
LPCTSTR pServerName = L"192.168.42.105";
//Or use 127.0.0.1 if you don't have access to another machine on your network.
//This will attempt to reboot your local machine.
//In that case make sure to call shutdown /a /m \\127.0.0.1 to cancel it.
if(AdjustPrivilege(NULL, L"SeShutdownPrivilege", TRUE) &&
AdjustPrivilege(pServerName, L"SeRemoteShutdownPrivilege", TRUE))
{
int nErrorCode = ::InitiateShutdown(pServerName, NULL, 30,
SHUTDOWN_INSTALL_UPDATES | SHUTDOWN_RESTART, 0);
//Receive nErrorCode == 1722, or RPC_S_SERVER_UNAVAILABLE
}
BOOL AdjustPrivilege(LPCTSTR pStrMachine, LPCTSTR pPrivilegeName, BOOL bEnable)
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
BOOL bRes = FALSE;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return FALSE;
if(LookupPrivilegeValue(pStrMachine, pPrivilegeName, &tkp.Privileges[0].Luid))
{
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : SE_PRIVILEGE_REMOVED;
bRes = AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
int nOSError = GetLastError();
if(bRes)
{
if(nOSError != ERROR_SUCCESS)
bRes = FALSE;
}
}
CloseHandle(hToken);
return bRes;
}
So to prepare for this code to run I do the following on this computer, which is Windows 7 Pro (as I would do for the Microsoft's shutdown tool):
Run the following "as administrator" to allow SMB access to the logged in user D1 on the 192.168.42.105 computer (per this answer):
NET USE \\192.168.42.105\IPC$ 1234 /USER:D1
Run the process with my code above "as administrator".
And then do the following on remote computer, or 192.168.42.105, that has Windows 7 Pro (per answer here with most upvotes):
Control Panel, Network and Sharing Center, Change Advanced Sharing settings
"Private" enable "Turn on File and Printer sharing"
Set the following key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System
LocalAccountTokenFilterPolicy=dword:1
RUN secpol.msc, then go to Local Security Policy, Security Settings, Local Policies, User Rights Assignment. Add "Everyone" to "Force shutdown from a remote system". (Just remember to remove it after you're done testing!)
Note that the following shutdown command seems to work just fine to reboot the remote computer:
shutdown /r /m \\192.168.42.105 /t 30
What am I missing with my code?
EDIT:
OK. I will admit that I was merely interested in why InitiateShutdown doesn't seem to "want" to work with a remote server connection, while InitiateSystemShutdownEx or InitiateSystemShutdown had no issues at all. (Unfortunately the latter two did not have the dwShutdownFlags parameter, which I needed to pass the SHUTDOWN_INSTALL_UPDATES flag to, which caused my persistence...)
At this point I had no other way of finding out than dusting out a copy of WinDbg... I'm still trying to dig into it, but so far this is what I found...
(A) It turns out that InitiateSystemShutdownEx internally uses a totally different RPC call. W/o too many details, it initiates RPC binding with RpcStringBindingComposeW using the following parameters:
ObjUuid = NULL
ProtSeq = ncacn_np
NetworkAddr = \\192.168.42.105
EndPoint = \\PIPE\\InitShutdown
Options = NULL
or the following binding string:
ncacn_np:\\\\192.168.42.105[\\PIPE\\InitShutdown]
(B) While InitiateShutdown on the other hand uses the following binding parameters:
ObjUuid = 765294ba-60bc-48b8-92e9-89fd77769d91
ProtSeq = ncacn_ip_tcp
NetworkAddr = 192.168.42.105
EndPoint = NULL
Options = NULL
which it later translates into the following binding string:
ncacn_np:\\\\192.168.42.105[\\PIPE\\lsarpc]
that it uses to obtain the RPC handle that it passes to WsdrInitiateShutdown (that seems to have its own specification):
So as you see, the InitiateShutdown call is technically treated as Unknown RPC service (for the UUID {765294ba-60bc-48b8-92e9-89fd77769d91}), which later causes a whole bunch of credential checks between the server and the client:
which, honestly, I'm not sure I want to step into with a low-level debugger :)
At this stage I will say that I am not very well versed on "Local Security Authority" interface (or the \PIPE\lsarpc named pipe configuration.) So if anyone knows what configuration is missing on the server side to allow this RPC call to go through, I would appreciate if you could post your take on it?

Firebird connection to remote server using FbConnectionStringBuilder

We're having trouble connecting to a remote Firebird server using the .NET provider FbConnectionStringBuilder class. We can connect to a local database file in either embedded or server mode however when it comes to establishing a connection to a remote server we can't establish the connection.
We assign properties of the FbConnectionStringBuilder class with the following code (server mode). I have omitted the code which assigns properties for embedded mode.
var cs = new FbConnectionStringBuilder
{
Database = databaseSessionInfo.PathAbsoluteToDatabase,
Charset = "UTF8",
Dialect = 3,
};
cs.DataSource = databaseSessionInfo.Hostname;
cs.Port = databaseSessionInfo.Port;
cs.ServerType = (FbServerType)databaseSessionInfo.Mode;
cs.Pooling = true;
cs.ConnectionLifeTime = 30;
if (databaseSessionInfo.UseCustomUserAccount)
{
cs.UserID = databaseSessionInfo.Username;
cs.Password = databaseSessionInfo.Password;
}
else
{
cs.UserID = Constants.DB_DefaultUsername;
cs.Password = Constants.DB_DefaultPassword;
}
Pretty straightforward. Our software contains a connection configuration screen whereby a user can supply different connection properties. These properties get assigned to the FbConnectionStringBuilder class using the code above.
The connection builder class outputs a connection string in the following format:
initial catalog="P:\Source\database.fdb";character set=UTF8;dialect=3;data source=localhost;port number=3050;server type=Default;pooling=True;connection lifetime=30;user id=USER;password=example
However literature on Firebird connection strings as indicated on this page (Firebird Connection Strings) talk of a different structure. I can only assume the FbConnectionStringBuilder class builds a Firebird connection string satisfying the requirements of Firebird.
Does the FbConnectionStringBuilder class append the hostname to the connection string correctly?
The Firebird server is running on the server. I assume there is no need to install it on the client?
What libraries need to be installed with the client to support a remote server connection?
Are we doing this right?
Any advice is appreciated.
Answering your questions:
1) Yes it will.
2) Correct, the client library will connect over the network.
3) If you use the ADO library, just FirebirdSql.Data.FirebirdClient.dll
4) Maybe, I dont know if this will help but this is how I connect.
FbConnectionStringBuilder ret = new FbConnectionStringBuilder();
ret.DataSource = Host;
ret.Port = Port;
ret.Database = Database;
ret.UserID = User;
ret.Password = Password;
ret.Charset = CharacterSet;
FbConnection ret = new FbConnection(connectionString);
Interestingly, what is the absolute path you are providing to the StringBuilder ? Is it the server absolute path, or some kind of network mapped drive?
Also, I assume you've reviewed firewall settings and allowing port 3050 inbound.

Resources