~ubuntu-branches/debian/jessie/banshee-community-extensions/jessie

« back to all changes in this revision

Viewing changes to .pc/0001-Use-DBus-instead-of-NDesk.DBus.patch/src/Telepathy/Banshee.Telepathy/NDesk.DBus/UnixNativeTransport.cs

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2011-09-20 18:45:46 UTC
  • mfrom: (1.2.9 upstream) (5.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20110920184546-3ahue2qplydc4t0e
Tags: 2.2.0-1
* [4940fab] Imported Upstream version 2.2.0
  + Notable bug fixes:
    - Karaoke: Fix crash when switching to Now Playing
    - Lyrics: Fix crash when switching to Now Playing

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2006 Alp Toker <alp@atoker.com>
2
 
// This software is made available under the MIT License
3
 
// See COPYING for details
4
 
 
5
 
//We send BSD-style credentials on all platforms
6
 
//Doesn't seem to break Linux (but is redundant there)
7
 
//This may turn out to be a bad idea
8
 
#define HAVE_CMSGCRED
9
 
 
10
 
using System;
11
 
using System.IO;
12
 
using System.Text;
13
 
using System.Runtime.InteropServices;
14
 
using NDesk.Unix;
15
 
 
16
 
namespace NDesk.DBus.Transports
17
 
{
18
 
        class UnixNativeTransport : UnixTransport
19
 
        {
20
 
                //protected UnixSocket socket;
21
 
                internal UnixSocket socket;
22
 
 
23
 
                public override string AuthString ()
24
 
                {
25
 
                        long uid = UnixUid.GetEUID ();
26
 
                        return uid.ToString ();
27
 
                }
28
 
 
29
 
                public override void Open (string path, bool @abstract)
30
 
                {
31
 
                        if (String.IsNullOrEmpty (path))
32
 
                                throw new ArgumentException ("path");
33
 
 
34
 
                        if (@abstract)
35
 
                                socket = OpenAbstractUnix (path);
36
 
                        else
37
 
                                socket = OpenUnix (path);
38
 
 
39
 
                        //socket.Blocking = true;
40
 
                        SocketHandle = (long)socket.Handle;
41
 
                        //Stream = new UnixStream ((int)socket.Handle);
42
 
                        Stream = new UnixStream (socket);
43
 
                }
44
 
 
45
 
                //send peer credentials null byte
46
 
                //different platforms do this in different ways
47
 
#if HAVE_CMSGCRED
48
 
                unsafe void WriteBsdCred ()
49
 
                {
50
 
                        //null credentials byte
51
 
                        byte buf = 0;
52
 
 
53
 
                        IOVector iov = new IOVector ();
54
 
                        //iov.Base = (IntPtr)(&buf);
55
 
                        iov.Base = &buf;
56
 
                        iov.Length = 1;
57
 
 
58
 
                        msghdr msg = new msghdr ();
59
 
                        msg.msg_iov = &iov;
60
 
                        msg.msg_iovlen = 1;
61
 
 
62
 
                        cmsg cm = new cmsg ();
63
 
                        msg.msg_control = (IntPtr)(&cm);
64
 
                        msg.msg_controllen = (uint)sizeof (cmsg);
65
 
                        cm.hdr.cmsg_len = (uint)sizeof (cmsg);
66
 
                        cm.hdr.cmsg_level = 0xffff; //SOL_SOCKET
67
 
                        cm.hdr.cmsg_type = 0x03; //SCM_CREDS
68
 
 
69
 
                        int written = socket.SendMsg (&msg, 0);
70
 
                        if (written != 1)
71
 
                                throw new Exception ("Failed to write credentials");
72
 
                }
73
 
#endif
74
 
 
75
 
                public override void WriteCred ()
76
 
                {
77
 
#if HAVE_CMSGCRED
78
 
                        try {
79
 
                                WriteBsdCred ();
80
 
                                return;
81
 
                        } catch {
82
 
                                if (Protocol.Verbose)
83
 
                                        Console.Error.WriteLine ("Warning: WriteBsdCred() failed; falling back to ordinary WriteCred()");
84
 
                        }
85
 
#endif
86
 
                        //null credentials byte
87
 
                        byte buf = 0;
88
 
                        Stream.WriteByte (buf);
89
 
                }
90
 
 
91
 
                public static byte[] GetSockAddr (string path)
92
 
                {
93
 
                        byte[] p = Encoding.Default.GetBytes (path);
94
 
 
95
 
                        byte[] sa = new byte[2 + p.Length + 1];
96
 
 
97
 
                        //we use BitConverter to stay endian-safe
98
 
                        byte[] afData = BitConverter.GetBytes (UnixSocket.AF_UNIX);
99
 
                        sa[0] = afData[0];
100
 
                        sa[1] = afData[1];
101
 
 
102
 
                        for (int i = 0 ; i != p.Length ; i++)
103
 
                                sa[2 + i] = p[i];
104
 
                        sa[2 + p.Length] = 0; //null suffix for domain socket addresses, see unix(7)
105
 
 
106
 
                        return sa;
107
 
                }
108
 
 
109
 
                public static byte[] GetSockAddrAbstract (string path)
110
 
                {
111
 
                        byte[] p = Encoding.Default.GetBytes (path);
112
 
 
113
 
                        byte[] sa = new byte[2 + 1 + p.Length];
114
 
 
115
 
                        //we use BitConverter to stay endian-safe
116
 
                        byte[] afData = BitConverter.GetBytes (UnixSocket.AF_UNIX);
117
 
                        sa[0] = afData[0];
118
 
                        sa[1] = afData[1];
119
 
 
120
 
                        sa[2] = 0; //null prefix for abstract domain socket addresses, see unix(7)
121
 
                        for (int i = 0 ; i != p.Length ; i++)
122
 
                                sa[3 + i] = p[i];
123
 
 
124
 
                        return sa;
125
 
                }
126
 
 
127
 
                internal UnixSocket OpenUnix (string path)
128
 
                {
129
 
                        byte[] sa = GetSockAddr (path);
130
 
                        UnixSocket client = new UnixSocket ();
131
 
                        client.Connect (sa);
132
 
                        return client;
133
 
                }
134
 
 
135
 
                internal UnixSocket OpenAbstractUnix (string path)
136
 
                {
137
 
                        byte[] sa = GetSockAddrAbstract (path);
138
 
                        UnixSocket client = new UnixSocket ();
139
 
                        client.Connect (sa);
140
 
                        return client;
141
 
                }
142
 
        }
143
 
 
144
 
#if HAVE_CMSGCRED
145
 
        /*
146
 
        public struct msg
147
 
        {
148
 
                public IntPtr msg_next;
149
 
                public long msg_type;
150
 
                public ushort msg_ts;
151
 
                short msg_spot;
152
 
                IntPtr label;
153
 
        }
154
 
        */
155
 
 
156
 
        unsafe struct msghdr
157
 
        {
158
 
                public IntPtr msg_name; //optional address
159
 
                public uint msg_namelen; //size of address
160
 
                public IOVector *msg_iov; //scatter/gather array
161
 
                public int msg_iovlen; //# elements in msg_iov
162
 
                public IntPtr msg_control; //ancillary data, see below
163
 
                public uint msg_controllen; //ancillary data buffer len
164
 
                public int msg_flags; //flags on received message
165
 
        }
166
 
 
167
 
        struct cmsghdr
168
 
        {
169
 
                public uint cmsg_len; //data byte count, including header
170
 
                public int cmsg_level; //originating protocol
171
 
                public int cmsg_type; //protocol-specific type
172
 
        }
173
 
 
174
 
        unsafe struct cmsgcred
175
 
        {
176
 
                const int CMGROUP_MAX = 16;
177
 
 
178
 
                public int cmcred_pid; //PID of sending process
179
 
                public uint cmcred_uid; //real UID of sending process
180
 
                public uint cmcred_euid; //effective UID of sending process
181
 
                public uint cmcred_gid; //real GID of sending process
182
 
                public short cmcred_ngroups; //number or groups
183
 
                public fixed uint cmcred_groups[CMGROUP_MAX]; //groups
184
 
        }
185
 
 
186
 
        struct cmsg
187
 
        {
188
 
                public cmsghdr hdr;
189
 
                public cmsgcred cred;
190
 
        }
191
 
#endif
192
 
}