Mercurial > pidgin
annotate plugins/gaim-remote/remote-socket.c @ 5873:a18e88c4dace
[gaim-migrate @ 6305]
Even MORE of a cleanup! Ooohh yeahhh.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Sat, 14 Jun 2003 23:25:41 +0000 |
parents | 059d95c67cda |
children | 5239a3b4ab33 |
rev | line source |
---|---|
5859 | 1 /* |
2 * gaim-remote | |
3 * | |
4 * Copyright (C) 2002, Sean Egan <bj91704@binghamton.edu> | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 * | |
20 */ | |
21 | |
22 /* Somewhat inspired by XMMS: | |
23 * Copyright (C) 1998-2002 Peter Alm, Mikael Alm, Olle Hallnas, | |
24 * Thomas Nilsson and 4Front Technologies | |
25 * Copyright (C) 1999-2002 Haavard Kvaalen | |
26 */ | |
27 | |
28 /* This provides code for connecting to a Gaim socket and communicating with | |
29 * it. It will eventually be made a library once the core and ui are split. */ | |
30 | |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5859
diff
changeset
|
31 #include "internal.h" |
5859 | 32 #include <sys/un.h> |
5872
059d95c67cda
[gaim-migrate @ 6304]
Christian Hammond <chipx86@chipx86.com>
parents:
5859
diff
changeset
|
33 #include <gaim-remote/remote.h> |
5859 | 34 |
35 void | |
36 gaim_remote_session_send_packet(int fd, GaimRemotePacket *p) | |
37 { | |
38 int len = sizeof(p->type) + sizeof(p->subtype) + | |
39 sizeof(p->length) + p->length; | |
40 char *pack = g_malloc(len); | |
41 char *a = pack; | |
42 | |
43 memcpy (a, &(p->type), sizeof(p->type)); | |
44 a = a + sizeof(p->type); | |
45 memcpy (a, &(p->subtype), sizeof(p->subtype)); | |
46 a = a + sizeof(p->subtype); | |
47 memcpy (a, &(p->length), sizeof(p->length)); | |
48 a = a + sizeof(p->length); | |
49 memcpy (a, p->data, p->length); | |
50 write(fd, pack, len); | |
51 g_free(pack); | |
52 } | |
53 | |
54 void | |
55 gaim_remote_packet_append_string(GaimRemotePacket *p, char *str) | |
56 { | |
57 int len = p->length + strlen(str); | |
58 char *k = g_malloc(len); | |
59 | |
60 memcpy(k, p->data, p->length); | |
61 memcpy(k + p->length, str, strlen(str)); | |
62 | |
63 if (p->data) | |
64 g_free(p->data); | |
65 | |
66 p->data = k; | |
67 p->length = len; | |
68 } | |
69 | |
70 void | |
71 gaim_remote_packet_append_char(GaimRemotePacket *p, char c) | |
72 { | |
73 int len = p->length + sizeof(char); | |
74 char *k = g_malloc(len); | |
75 | |
76 memcpy(k, p->data, p->length); | |
77 k[p->length] = c; | |
78 | |
79 if (p->data) | |
80 g_free(p->data); | |
81 | |
82 p->data = k; | |
83 p->length = len; | |
84 } | |
85 | |
86 void | |
87 gaim_remote_packet_append_raw(GaimRemotePacket *p, char *str, int len) | |
88 { | |
89 int lent = p->length + len; | |
90 char *k = g_malloc(lent); | |
91 | |
92 memcpy(k, p->data, p->length); | |
93 memcpy(k + p->length, str, len); | |
94 | |
95 if (p->data) | |
96 g_free(p->data); | |
97 | |
98 p->data = k; | |
99 p->length = lent; | |
100 } | |
101 | |
102 GaimRemotePacket * | |
103 gaim_remote_packet_new(guchar type, guchar subtype) | |
104 { | |
105 GaimRemotePacket *p = g_new0(GaimRemotePacket, 1); | |
106 p->type = type; | |
107 p->subtype = subtype; | |
108 p->length = 0; | |
109 p->data = NULL; | |
110 return p; | |
111 } | |
112 | |
113 void | |
114 gaim_remote_packet_free(GaimRemotePacket *p) | |
115 { | |
116 if (p->data) | |
117 g_free(p->data); | |
118 g_free(p); | |
119 } | |
120 | |
121 GaimRemotePacket * | |
122 gaim_remote_session_read_packet(int fd) | |
123 { | |
124 GaimRemotePacket *p = g_new0(GaimRemotePacket, 1); | |
125 char *data = NULL; | |
126 | |
127 if (!(read(fd, &p->type, sizeof(p->type)))) { | |
128 g_free(p); | |
129 return NULL; | |
130 } | |
131 | |
132 if (!(read(fd, &p->subtype, sizeof(p->subtype)))) { | |
133 g_free(p); | |
134 return NULL; | |
135 } | |
136 | |
137 if (!(read(fd, &p->length, sizeof(p->length)))) { | |
138 g_free(p); | |
139 return NULL; | |
140 } | |
141 | |
142 if (p->length) { | |
143 data = g_malloc(p->length); | |
144 | |
145 if (!(read(fd, data, p->length))) { | |
146 g_free(p); | |
147 return NULL; | |
148 } | |
149 } | |
150 | |
151 p->data = data; | |
152 | |
153 return p; | |
154 } | |
155 | |
156 /* copied directly from xmms_connect_to_session */ | |
157 int | |
158 gaim_remote_session_connect(int session) | |
159 { | |
160 gint fd; | |
161 uid_t stored_uid, euid; | |
162 struct sockaddr_un saddr; | |
163 | |
164 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1) | |
165 { | |
166 saddr.sun_family = AF_UNIX; | |
167 stored_uid = getuid(); | |
168 euid = geteuid(); | |
169 setuid(euid); | |
170 sprintf(saddr.sun_path, "%s/gaim_%s.%d", | |
171 g_get_tmp_dir(), g_get_user_name(), session); | |
172 setreuid(stored_uid, euid); | |
173 | |
174 if (connect(fd, (struct sockaddr *) &saddr, sizeof (saddr)) != -1) | |
175 return fd; | |
176 } | |
177 | |
178 close(fd); | |
179 | |
180 return -1; | |
181 } | |
182 | |
183 gboolean | |
184 gaim_remote_session_exists(int sess) | |
185 { | |
186 GaimRemotePacket *pack = NULL; | |
187 int fd = gaim_remote_session_connect(sess); | |
188 | |
189 if (fd > 0) { | |
190 pack = gaim_remote_packet_new(CUI_TYPE_META, CUI_META_PING); | |
191 gaim_remote_session_send_packet(fd, pack); | |
192 gaim_remote_packet_free(pack); | |
193 close(fd); | |
194 | |
195 return TRUE; | |
196 } | |
197 | |
198 return FALSE; | |
199 } | |
200 |