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