188 lines
5.6 KiB
C#
188 lines
5.6 KiB
C#
/***********************************************************************
|
|
*
|
|
* Copyright (C) 2005-2006 Novell, Inc. All Rights Reserved.
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; version 2.1
|
|
* of the License.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Library Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, Novell, Inc.
|
|
*
|
|
* To contact Novell about this file by physical or electronic mail,
|
|
* you may find current contact information at www.novell.com.
|
|
*
|
|
***********************************************************************/
|
|
|
|
using System;
|
|
using System.Net;
|
|
using System.IO;
|
|
using System.Net.Sockets;
|
|
using Mono.Unix;
|
|
using Mono.Unix.Native;
|
|
using System.Text;
|
|
|
|
namespace Novell.CASA.MiCasa.Communication
|
|
{
|
|
/// <summary>
|
|
/// Summary description for UnixIPCClientChannel.
|
|
/// </summary>
|
|
public class UnixIPCClientChannel : ClientChannel
|
|
{
|
|
|
|
private Socket mSocket = null;
|
|
private string socketFileName = "/var/run/.novellCASA";
|
|
private EndPoint sockEndPoint;
|
|
|
|
public UnixIPCClientChannel()
|
|
{
|
|
}
|
|
|
|
public void Open()
|
|
{
|
|
Stat socketFileStatus;
|
|
mSocket = new Socket(AddressFamily.Unix,
|
|
SocketType.Stream,
|
|
ProtocolType.IP);
|
|
|
|
if (mSocket == null)
|
|
{
|
|
throw new Exception("could not get socket");
|
|
}
|
|
|
|
Syscall.lstat(socketFileName, out socketFileStatus);
|
|
// root is the owner of the file "/var/run/.novellCASA"
|
|
if (socketFileStatus.st_uid == 0)
|
|
{
|
|
sockEndPoint = new UnixEndPoint(socketFileName);
|
|
mSocket.Connect(sockEndPoint);
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("not a valid miCASA service");
|
|
}
|
|
|
|
}
|
|
|
|
public int Read(byte[] buf)
|
|
{
|
|
buf = Read();
|
|
|
|
if (buf != null)
|
|
{
|
|
//Console.WriteLine("Bytes read = " + buf.Length);
|
|
return buf.Length;
|
|
}
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
public byte[] Read()
|
|
{
|
|
byte[] returnBuffer = null;
|
|
int bytesRecvd = 0;
|
|
|
|
try
|
|
{
|
|
/* We need to read 'msgLen' to know how many bytes to
|
|
* allocate.
|
|
*/
|
|
|
|
byte[] msgLenBytes = new byte[4];
|
|
bytesRecvd = mSocket.Receive(msgLenBytes);
|
|
if (0 == bytesRecvd)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
uint msgLen = BitConverter.ToUInt32(msgLenBytes, 0);
|
|
|
|
// micasad shouldn't return anything too large, however just in case
|
|
// don't loop forever
|
|
if (msgLen > int.MaxValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (msgLen > 6)
|
|
{
|
|
byte[] buf = null;
|
|
int bytesAvailable;
|
|
int totalBytes = 0;
|
|
int msgLencount = 0;
|
|
string bufstring = null;
|
|
|
|
// buffer for data
|
|
MemoryStream ms = new MemoryStream();
|
|
ms.Write(msgLenBytes, 0, 4);
|
|
|
|
while (totalBytes < (msgLen - 6))
|
|
{
|
|
bytesAvailable = mSocket.Available;
|
|
if (0 == bytesAvailable)
|
|
{
|
|
break;
|
|
}
|
|
buf = new byte[bytesAvailable];
|
|
bytesRecvd = mSocket.Receive(buf);
|
|
ms.Write(buf, 0, bytesRecvd);
|
|
|
|
totalBytes = totalBytes + bytesAvailable;
|
|
}
|
|
if (totalBytes == 0)
|
|
return null;
|
|
|
|
return ms.ToArray();
|
|
|
|
/*
|
|
byte[] finalbuf = ms.ToArray();
|
|
int returnBufferLen = msgIdBytes.Length + msgLenBytes.Length + totalBytes;
|
|
returnBuffer = new byte[returnBufferLen];
|
|
Array.Copy(msgIdBytes, returnBuffer, 2);
|
|
Array.Copy(msgLenBytes, 0, returnBuffer, 2, 4);
|
|
Array.Copy(finalbuf, 0, returnBuffer, 6, finalbuf.Length);
|
|
return returnBuffer;
|
|
*
|
|
*/
|
|
}
|
|
else
|
|
{
|
|
returnBuffer = new byte[4];
|
|
Array.Copy(msgLenBytes, 0, returnBuffer, 0, 4);
|
|
return returnBuffer;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public int Write(byte[] buf)
|
|
{
|
|
try
|
|
{
|
|
mSocket.Send(buf);
|
|
return buf.Length;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
mSocket.Close();
|
|
}
|
|
}
|
|
}
|