3478
|
1 /*
|
|
2 * gaim-remote
|
|
3 *
|
8046
|
4 * Gaim is the legal property of its developers, whose names are too numerous
|
|
5 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
6 * source distribution.
|
3478
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
11 * (at your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program; if not, write to the Free Software
|
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 *
|
|
22 */
|
|
23
|
8046
|
24 /* Somewhat inspired by XMMS*/
|
3478
|
25
|
|
26 /* This provides code for connecting to a Gaim socket and communicating with
|
|
27 * it. It will eventually be made a library once the core and ui are split. */
|
|
28
|
3499
|
29 #include <sys/types.h>
|
3478
|
30 #include <sys/socket.h>
|
|
31 #include <sys/un.h>
|
3867
|
32 #include <unistd.h>
|
3478
|
33 #include "gaim.h"
|
|
34 #include "gaim-socket.h"
|
|
35
|
|
36 void cui_send_packet (int fd, struct gaim_cui_packet *p) {
|
|
37 int len = sizeof(p->type) + sizeof(p->subtype) + sizeof(p->length) + p->length;
|
|
38 char *pack = g_malloc(len);
|
|
39 char *a = pack;
|
|
40 memcpy (a, &(p->type), sizeof(p->type));
|
|
41 a = a + sizeof(p->type);
|
|
42 memcpy (a, &(p->subtype), sizeof(p->subtype));
|
|
43 a = a + sizeof(p->subtype);
|
|
44 memcpy (a, &(p->length), sizeof(p->length));
|
|
45 a = a + sizeof(p->length);
|
|
46 memcpy (a, p->data, p->length);
|
|
47 write(fd, pack, len);
|
|
48 g_free(pack);
|
|
49 }
|
|
50
|
|
51
|
|
52 void cui_packet_append_string(struct gaim_cui_packet *p, char *str) {
|
|
53 int len = p->length + strlen(str);
|
|
54 char *k = g_malloc(len);
|
|
55 memcpy(k, p->data, p->length);
|
|
56 memcpy(k + p->length, str, strlen(str));
|
|
57 if (p->data)
|
|
58 g_free(p->data);
|
|
59 p->data = k;
|
|
60 p->length = len;
|
|
61 }
|
|
62
|
|
63 void cui_packet_append_char(struct gaim_cui_packet *p, char c) {
|
|
64 int len = p->length + sizeof(char);
|
|
65 char *k = g_malloc(len);
|
|
66 memcpy(k, p->data, p->length);
|
|
67 k[p->length] = c;
|
|
68 if (p->data)
|
|
69 g_free(p->data);
|
|
70 p->data = k;
|
|
71 p->length = len;
|
|
72 }
|
|
73
|
|
74 void cui_packet_append_raw(struct gaim_cui_packet *p, char *str, int len) {
|
|
75 int lent = p->length + len;
|
|
76 char *k = g_malloc(lent);
|
|
77 memcpy(k, p->data, p->length);
|
|
78 memcpy(k + p->length, str, len);
|
|
79 if (p->data)
|
|
80 g_free(p->data);
|
|
81 p->data = k;
|
|
82 p->length = lent;
|
|
83 }
|
|
84
|
|
85 struct gaim_cui_packet *cui_packet_new(guchar type, guchar subtype) {
|
|
86 struct gaim_cui_packet *p = g_new0(struct gaim_cui_packet, 1);
|
|
87 p->type = type;
|
|
88 p->subtype = subtype;
|
|
89 p->length = 0;
|
|
90 p->data = NULL;
|
|
91 return p;
|
|
92 }
|
|
93
|
|
94 void cui_packet_free(struct gaim_cui_packet *p) {
|
|
95 if (p->data)
|
|
96 g_free(p->data);
|
|
97 g_free(p);
|
|
98 }
|
|
99
|
|
100 struct gaim_cui_packet *cui_read_packet(int fd) {
|
|
101 struct gaim_cui_packet *p = g_new0(struct gaim_cui_packet, 1);
|
|
102 char *data = NULL;
|
|
103
|
3867
|
104 if (!(read(fd, &p->type, sizeof(p->type)))) {
|
3478
|
105 g_free(p);
|
|
106 return NULL;
|
|
107 }
|
|
108
|
|
109
|
3867
|
110 if (!(read(fd, &p->subtype, sizeof(p->subtype)))) {
|
3478
|
111 g_free(p);
|
|
112 return NULL;
|
|
113 }
|
|
114
|
|
115
|
3867
|
116 if (!(read(fd, &p->length, sizeof(p->length)))) {
|
3478
|
117 g_free(p);
|
|
118 return NULL;
|
|
119 }
|
|
120
|
|
121 if (p->length) {
|
|
122 data = g_malloc(p->length);
|
|
123 if (!(read(fd, data, p->length))) {
|
|
124 g_free(p);
|
|
125 return NULL;
|
|
126 }
|
|
127 }
|
|
128 p->data = data;
|
3867
|
129 return p;
|
3478
|
130 }
|
|
131
|
|
132 /* copied directly from xmms_connect_to_session */
|
|
133 gint gaim_connect_to_session(gint session)
|
|
134 {
|
|
135 gint fd;
|
|
136 uid_t stored_uid, euid;
|
|
137 struct sockaddr_un saddr;
|
|
138
|
|
139 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1)
|
|
140 {
|
|
141 saddr.sun_family = AF_UNIX;
|
|
142 stored_uid = getuid();
|
|
143 euid = geteuid();
|
|
144 setuid(euid);
|
|
145 sprintf(saddr.sun_path, "%s/gaim_%s.%d", g_get_tmp_dir(), g_get_user_name(), session);
|
|
146 setreuid(stored_uid, euid);
|
|
147 if (connect(fd, (struct sockaddr *) &saddr, sizeof (saddr)) != -1)
|
|
148 return fd;
|
|
149 }
|
|
150 close(fd);
|
|
151 return -1;
|
|
152 }
|
|
153
|
|
154 gboolean gaim_session_exists(int sess)
|
|
155 {
|
|
156 struct gaim_cui_packet *pack = NULL;
|
|
157
|
|
158 int fd = gaim_connect_to_session(sess);
|
|
159 if (fd > 0) {
|
|
160 pack = cui_packet_new(CUI_TYPE_META, CUI_META_PING);
|
|
161 cui_send_packet(fd, pack);
|
|
162 cui_packet_free(pack);
|
|
163 close(fd);
|
|
164 } else {
|
|
165 return FALSE;
|
|
166 }
|
|
167 return TRUE;
|
|
168 }
|
|
169
|