Get the remote address on a GSocket - glib

I have a GSocket which is accepted with g_socket_listener_accept_socket(). I want to get the remote address of the connected client, and thought the following code would do it:
GSocket *socket;
GSocketAddress *socket_address;
GInetAddress *inet_address;
gchar *ip_addr;
[...]
socket_address = g_socket_get_remote_address(socket_client, &err);
inet_address = g_object_ref(g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(socket_address)));
ip_addr = g_inet_address_to_string(inet_address);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Login from %s", ip_addr);
g_free(ip_addr);
g_object_unref(socket_address);
However, I get the following error:
(process:15761): GLib-GIO-CRITICAL **: g_inet_address_to_string: assertion `G_IS_INET_ADDRESS (address)' failed
After some debugging, it turned out that the above message is generated by g_inet_address_to_string().
Am I doing something wrong?

Related

Passing parameter to RequestScan method of Network Manager service

I want to create a tuple of array of dictionary to pass argument to method RequestScan (IN a{sv} options). I write code as below.
/* Create empty dictionary */
GVariantBuilder *b;
GVariant *option;
b = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
option = g_variant_builder_end (b);
/* Start to scan */
dbusRet = g_dbus_proxy_call_sync(wIfProxy,
"RequestScan",
g_variant_new("(a{sv})", option),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
I can build the code, but when run program it output error as below.
(process:35140): GLib-CRITICAL **: 21:45:35.337: g_variant_builder_end: assertion 'ensure_valid_builder (builder)' failed
(process:35140): GLib-CRITICAL **: 21:45:35.337: g_variant_get_type: assertion 'value != NULL' failed
(process:35140): GLib-CRITICAL **: 21:45:35.337: g_variant_type_is_array: assertion 'g_variant_type_check (type)' failed
(process:35140): GLib-CRITICAL **: 21:45:35.337: g_variant_get_type_string: assertion 'value != NULL' failed
(process:35140): GLib-ERROR **: 21:45:35.337: g_variant_new: expected array GVariantBuilder but the built value has type '(null)'
Could you tell me what is the error in the implementation?

Catch the LuaSQL error to the xpcall error handler function

How can I get the error output from the LuaSQL driver as an argument to the xpcall error function as assert does?
For example, by running the following code:
local conn = assert (env:connect("demo_database",config.db.username,config.db.password,config.db.host))
I'm getting the following error:
LuaSQL: error connecting to database. MySQL: Access denied for user 'user_1'#'host1' (using password: YES)
But when I'm running the following code:
local function myerrorhandler( err )
local file = assert( io.open( "/tmp/testout.txt", "w" ) )
file:write( err.." - error\n" )
file:close()
end
local conn = xpcall (env:connect("demo_database",config.db.username,config.db.password,config.db.host), myerrorhandler)
The error I'm getting in the log file is: attempt to call a nil value - error
xpcall expects the first argument to be a function, which it will call and catch errors from. You are basically doing xpcall(func(), handler), i.e. calling func instead of just passing it to xpcall, similar to how pcall expects a function as first argument.
You basically want to wrap your code in a function:
local conn = xpcall(function()
return env:connect("demo_database", config.db.username, config.db.password, config.db.host)
end, myerrorhandler)

4-way handshake failed with freeradius on openWrt?

I used freeradius-server on openWrt to get sim IMSI. Because I did have some sim value(rand, sres, kc),I changed the source code use fake value. It can be authenticated successfully. But in the process of 4-way handshake, it failed! It just have one handshake.
I captured some package with wireshark, anyone can help me analysis the reason or have a better way to get imsi on openWrt?
eap-sim authenticate process
handshake 1 of 4
I found the reason!
At the begining, I do not have the sim value(RAND, sres, kc), so I create some fake value. The code need the correct msk value to build up PMK package, like these:
static int eap_sim_sendsuccess(EAP_HANDLER *handler)
{
unsigned char *p;
struct eap_sim_server_state *ess;
VALUE_PAIR **outvps;
VALUE_PAIR *newvp;
/* outvps is the data to the client. */
outvps= &handler->request->reply->vps;
ess = (struct eap_sim_server_state *)handler->opaque;
/* set the EAP_ID - new value */
newvp = paircreate(ATTRIBUTE_EAP_ID, PW_TYPE_INTEGER);
newvp->vp_integer = ess->sim_id++;
pairreplace(outvps, newvp);
p = ess->keys.msk; //**look here**!
add_reply(outvps, "MS-MPPE-Recv-Key", p, EAPTLS_MPPE_KEY_LEN);
p += EAPTLS_MPPE_KEY_LEN;
add_reply(outvps, "MS-MPPE-Send-Key", p, EAPTLS_MPPE_KEY_LEN);
return 1;
}
So, It built a wrong PMK package. The mobile phone recieved the 1/4 handshake and droped it.

Send email everytime php generates a fatal error

How would I write a PHP code that would send me an email everytime I get a
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 14373306 bytes) in on line <b>443</b><br />.
I'm running a script wherein a user has this URL and it will be process by my code, but the problem here is that sometimes it results to a Fatal error Allowed memory size exhausted. I want an email to be sent to me that tells me that there has this error at the same time what URL is causing that error.
So the logic is something like this.
if( error == "Fatal Error Allowed memory Size" ) {
mail("myemail#email.com", "Fatal Error - URL: http://google.com");
}
I hope the instruction is pretty clear. Your help would be greatly appreciated and rewarded!
Thanks! :-)
You can look at using register_shutdown_function(). It can be used to execute on E_ERROR(fatal error)
register_shutdown_function('shutdown');
function shutdown()
{
if(!is_null($e = error_get_last()))
{
mail("myemail#email.com", "Fatal Error - ". var_export($e, true));
}
}
I would however echo thoughts in the comments above that this is best handled using log monitoring.
Init following function inside your php file.
register_shutdown_function('mail_on_error'); //inside your php script
/** If any file having any kind of fatal error, sln team will be notified when cron will
become fail : following is the name of handler and its type
E_ERROR: 1 | E_WARNING: 2 | E_PARSE: 4 | E_NOTICE: 8
*/
function mail_on_error() {
global $objSettings;
$error = error_get_last();
//print_r($error);
if ($error['type'] == 1) {
// update file path into db
$objSettings->update_records($id=1,array('fatal_error' => json_encode($error)));
$exception_in_file_path = __FILE__;
fatal_error_structure($exception_in_file_path);
}// end if
}// end mail_on_error
fatal_error_structure should be defined on some global location. like function.inc.php. This will send an email to registered user.
function fatal_error_structure($exception_in_file_path){
$subject = "FATAL: Cron Daemon has failed";
sln_send_mail(nl2br("Please check $exception_in_file_path, This cron having FATAL error."),
$subject, 'email#address.com',$name_reciever='', 'text/plain');
}// end fatal_error_structure
vKj

SharpSSh: RunCommand in SshExec is always returning an empty string

When Using SharpSSh and the SshExec class, I can't get the RunCommand to work, it always returns an empty string. When I debug the SharpSsh library it returns -1 when it tries to read the command from a stream. It works when I use the sftp class in the same library, but that class doesn't support all the ftp commands I need.
Here is a standard example, I can't get this to produce a correct result either
SshConnectionInfo input = Util.GetInput();
SshExec exec = new SshExec(input.Host, input.User);
if(input.Pass != null) exec.Password = input.Pass;
if(input.IdentityFile != null) exec.AddIdentityFile( input.IdentityFile );
Console.Write("Connecting...");
exec.Connect();
Console.WriteLine("OK");
while(true)
{
Console.Write("Enter a command to execute ['Enter' to cancel]: ");
string command = Console.ReadLine();
if(command=="")break;
string output = exec.RunCommand(command);
Console.WriteLine(output);
}
Console.Write("Disconnecting...");
exec.Close();
Console.WriteLine("OK");
Any ideas on how I can get the RunCommand function to run some commands?
Thanks for any help :)
To get the standard output and error streams from .RunCommand,
I'll repeat the answer I posted to: SharpSSH - SSHExec, run command, and wait 5 seconds for data!
You may want to try the following overload:
SshExec exec = new SshExec("192.168.0.1", "admin", "haha");
exec.Connect();
string stdOut = null;
string stdError = null;
exec.RunCommand("interface wireless scan wlan1 duration=5", ref stdOut, ref stdError);
Console.WriteLine(stdOut);
exec.Close();
If their API does what the name implies, it should put the standard output of your command in stdOut and the standard error in stdError.
For more information about standard streams, check this out: http://en.wikipedia.org/wiki/Standard_streams

Resources