Mercurial > pidgin
annotate plugins/gaim-remote/remote-socket.c @ 7380:259210cf5dfa
[gaim-migrate @ 7975]
if we're gonna put this back, we may as well do it right
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Thu, 30 Oct 2003 15:00:26 +0000 |
parents | 5239a3b4ab33 |
children | b4b9dabdd7c7 |
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> |
6063 | 34 #include <sys/types.h> |
35 #include <unistd.h> | |
5859 | 36 |
37 void | |
38 gaim_remote_session_send_packet(int fd, GaimRemotePacket *p) | |
39 { | |
40 int len = sizeof(p->type) + sizeof(p->subtype) + | |
41 sizeof(p->length) + p->length; | |
42 char *pack = g_malloc(len); | |
43 char *a = pack; | |
44 | |
45 memcpy (a, &(p->type), sizeof(p->type)); | |
46 a = a + sizeof(p->type); | |
47 memcpy (a, &(p->subtype), sizeof(p->subtype)); | |
48 a = a + sizeof(p->subtype); | |
49 memcpy (a, &(p->length), sizeof(p->length)); | |
50 a = a + sizeof(p->length); | |
51 memcpy (a, p->data, p->length); | |
52 write(fd, pack, len); | |
53 g_free(pack); | |
54 } | |
55 | |
56 void | |
57 gaim_remote_packet_append_string(GaimRemotePacket *p, char *str) | |
58 { | |
59 int len = p->length + strlen(str); | |
60 char *k = g_malloc(len); | |
61 | |
62 memcpy(k, p->data, p->length); | |
63 memcpy(k + p->length, str, strlen(str)); | |
64 | |
65 if (p->data) | |
66 g_free(p->data); | |
67 | |
68 p->data = k; | |
69 p->length = len; | |
70 } | |
71 | |
72 void | |
73 gaim_remote_packet_append_char(GaimRemotePacket *p, char c) | |
74 { | |
75 int len = p->length + sizeof(char); | |
76 char *k = g_malloc(len); | |
77 | |
78 memcpy(k, p->data, p->length); | |
79 k[p->length] = c; | |
80 | |
81 if (p->data) | |
82 g_free(p->data); | |
83 | |
84 p->data = k; | |
85 p->length = len; | |
86 } | |
87 | |
88 void | |
89 gaim_remote_packet_append_raw(GaimRemotePacket *p, char *str, int len) | |
90 { | |
91 int lent = p->length + len; | |
92 char *k = g_malloc(lent); | |
93 | |
94 memcpy(k, p->data, p->length); | |
95 memcpy(k + p->length, str, len); | |
96 | |
97 if (p->data) | |
98 g_free(p->data); | |
99 | |
100 p->data = k; | |
101 p->length = lent; | |
102 } | |
103 | |
104 GaimRemotePacket * | |
105 gaim_remote_packet_new(guchar type, guchar subtype) | |
106 { | |
107 GaimRemotePacket *p = g_new0(GaimRemotePacket, 1); | |
108 p->type = type; | |
109 p->subtype = subtype; | |
110 p->length = 0; | |
111 p->data = NULL; | |
112 return p; | |
113 } | |
114 | |
115 void | |
116 gaim_remote_packet_free(GaimRemotePacket *p) | |
117 { | |
118 if (p->data) | |
119 g_free(p->data); | |
120 g_free(p); | |
121 } | |
122 | |
123 GaimRemotePacket * | |
124 gaim_remote_session_read_packet(int fd) | |
125 { | |
126 GaimRemotePacket *p = g_new0(GaimRemotePacket, 1); | |
127 char *data = NULL; | |
128 | |
129 if (!(read(fd, &p->type, sizeof(p->type)))) { | |
130 g_free(p); | |
131 return NULL; | |
132 } | |
133 | |
134 if (!(read(fd, &p->subtype, sizeof(p->subtype)))) { | |
135 g_free(p); | |
136 return NULL; | |
137 } | |
138 | |
139 if (!(read(fd, &p->length, sizeof(p->length)))) { | |
140 g_free(p); | |
141 return NULL; | |
142 } | |
143 | |
144 if (p->length) { | |
145 data = g_malloc(p->length); | |
146 | |
147 if (!(read(fd, data, p->length))) { | |
148 g_free(p); | |
149 return NULL; | |
150 } | |
151 } | |
152 | |
153 p->data = data; | |
154 | |
155 return p; | |
156 } | |
157 | |
158 /* copied directly from xmms_connect_to_session */ | |
159 int | |
160 gaim_remote_session_connect(int session) | |
161 { | |
162 gint fd; | |
163 uid_t stored_uid, euid; | |
164 struct sockaddr_un saddr; | |
165 | |
166 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1) | |
167 { | |
168 saddr.sun_family = AF_UNIX; | |
169 stored_uid = getuid(); | |
170 euid = geteuid(); | |
171 setuid(euid); | |
172 sprintf(saddr.sun_path, "%s/gaim_%s.%d", | |
173 g_get_tmp_dir(), g_get_user_name(), session); | |
174 setreuid(stored_uid, euid); | |
175 | |
176 if (connect(fd, (struct sockaddr *) &saddr, sizeof (saddr)) != -1) | |
177 return fd; | |
178 } | |
179 | |
180 close(fd); | |
181 | |
182 return -1; | |
183 } | |
184 | |
185 gboolean | |
186 gaim_remote_session_exists(int sess) | |
187 { | |
188 GaimRemotePacket *pack = NULL; | |
189 int fd = gaim_remote_session_connect(sess); | |
190 | |
191 if (fd > 0) { | |
192 pack = gaim_remote_packet_new(CUI_TYPE_META, CUI_META_PING); | |
193 gaim_remote_session_send_packet(fd, pack); | |
194 gaim_remote_packet_free(pack); | |
195 close(fd); | |
196 | |
197 return TRUE; | |
198 } | |
199 | |
200 return FALSE; | |
201 } | |
202 |