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*/
|
10320
|
25
|
3478
|
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-socket.h"
|
|
34
|
|
35 void cui_send_packet (int fd, struct gaim_cui_packet *p) {
|
|
36 int len = sizeof(p->type) + sizeof(p->subtype) + sizeof(p->length) + p->length;
|
|
37 char *pack = g_malloc(len);
|
|
38 char *a = pack;
|
|
39 memcpy (a, &(p->type), sizeof(p->type));
|
|
40 a = a + sizeof(p->type);
|
|
41 memcpy (a, &(p->subtype), sizeof(p->subtype));
|
|
42 a = a + sizeof(p->subtype);
|
|
43 memcpy (a, &(p->length), sizeof(p->length));
|
|
44 a = a + sizeof(p->length);
|
|
45 memcpy (a, p->data, p->length);
|
|
46 write(fd, pack, len);
|
|
47 g_free(pack);
|
|
48 }
|
|
49
|
|
50
|
|
51 void cui_packet_append_string(struct gaim_cui_packet *p, char *str) {
|
|
52 int len = p->length + strlen(str);
|
|
53 char *k = g_malloc(len);
|
|
54 memcpy(k, p->data, p->length);
|
|
55 memcpy(k + p->length, str, strlen(str));
|
|
56 if (p->data)
|
|
57 g_free(p->data);
|
|
58 p->data = k;
|
|
59 p->length = len;
|
|
60 }
|
|
61
|
|
62 void cui_packet_append_char(struct gaim_cui_packet *p, char c) {
|
|
63 int len = p->length + sizeof(char);
|
|
64 char *k = g_malloc(len);
|
|
65 memcpy(k, p->data, p->length);
|
|
66 k[p->length] = c;
|
|
67 if (p->data)
|
|
68 g_free(p->data);
|
|
69 p->data = k;
|
|
70 p->length = len;
|
|
71 }
|
|
72
|
|
73 void cui_packet_append_raw(struct gaim_cui_packet *p, char *str, int len) {
|
|
74 int lent = p->length + len;
|
|
75 char *k = g_malloc(lent);
|
|
76 memcpy(k, p->data, p->length);
|
|
77 memcpy(k + p->length, str, len);
|
|
78 if (p->data)
|
|
79 g_free(p->data);
|
|
80 p->data = k;
|
|
81 p->length = lent;
|
|
82 }
|
|
83
|
|
84 struct gaim_cui_packet *cui_packet_new(guchar type, guchar subtype) {
|
|
85 struct gaim_cui_packet *p = g_new0(struct gaim_cui_packet, 1);
|
|
86 p->type = type;
|
|
87 p->subtype = subtype;
|
|
88 p->length = 0;
|
|
89 p->data = NULL;
|
|
90 return p;
|
|
91 }
|
|
92
|
|
93 void cui_packet_free(struct gaim_cui_packet *p) {
|
|
94 if (p->data)
|
|
95 g_free(p->data);
|
|
96 g_free(p);
|
|
97 }
|
|
98
|
|
99 struct gaim_cui_packet *cui_read_packet(int fd) {
|
|
100 struct gaim_cui_packet *p = g_new0(struct gaim_cui_packet, 1);
|
|
101 char *data = NULL;
|
|
102
|
3867
|
103 if (!(read(fd, &p->type, sizeof(p->type)))) {
|
3478
|
104 g_free(p);
|
|
105 return NULL;
|
|
106 }
|
|
107
|
|
108
|
3867
|
109 if (!(read(fd, &p->subtype, sizeof(p->subtype)))) {
|
3478
|
110 g_free(p);
|
|
111 return NULL;
|
|
112 }
|
|
113
|
|
114
|
3867
|
115 if (!(read(fd, &p->length, sizeof(p->length)))) {
|
3478
|
116 g_free(p);
|
|
117 return NULL;
|
|
118 }
|
|
119
|
|
120 if (p->length) {
|
|
121 data = g_malloc(p->length);
|
|
122 if (!(read(fd, data, p->length))) {
|
|
123 g_free(p);
|
|
124 return NULL;
|
|
125 }
|
|
126 }
|
|
127 p->data = data;
|
3867
|
128 return p;
|
3478
|
129 }
|
|
130
|
|
131 /* copied directly from xmms_connect_to_session */
|
|
132 gint gaim_connect_to_session(gint session)
|
|
133 {
|
|
134 gint fd;
|
|
135 uid_t stored_uid, euid;
|
|
136 struct sockaddr_un saddr;
|
|
137
|
|
138 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1)
|
|
139 {
|
|
140 saddr.sun_family = AF_UNIX;
|
|
141 stored_uid = getuid();
|
|
142 euid = geteuid();
|
|
143 setuid(euid);
|
|
144 sprintf(saddr.sun_path, "%s/gaim_%s.%d", g_get_tmp_dir(), g_get_user_name(), session);
|
|
145 setreuid(stored_uid, euid);
|
|
146 if (connect(fd, (struct sockaddr *) &saddr, sizeof (saddr)) != -1)
|
|
147 return fd;
|
|
148 }
|
|
149 close(fd);
|
|
150 return -1;
|
|
151 }
|
|
152
|
|
153 gboolean gaim_session_exists(int sess)
|
|
154 {
|
|
155 struct gaim_cui_packet *pack = NULL;
|
|
156
|
|
157 int fd = gaim_connect_to_session(sess);
|
|
158 if (fd > 0) {
|
|
159 pack = cui_packet_new(CUI_TYPE_META, CUI_META_PING);
|
|
160 cui_send_packet(fd, pack);
|
|
161 cui_packet_free(pack);
|
|
162 close(fd);
|
|
163 } else {
|
|
164 return FALSE;
|
|
165 }
|
|
166 return TRUE;
|
|
167 }
|
|
168
|