Sat, 14 Sep 2002 02:34:38 +0000
[gaim-migrate @ 3562]
Happy BSD!
| 3478 | 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 | ||
| 3499 | 31 | #include <sys/types.h> |
| 3478 | 32 | #include <sys/socket.h> |
| 33 | #include <sys/un.h> | |
| 34 | #include "gaim.h" | |
| 35 | #include "gaim-socket.h" | |
| 36 | ||
| 37 | void cui_send_packet (int fd, struct gaim_cui_packet *p) { | |
| 38 | int len = sizeof(p->type) + sizeof(p->subtype) + sizeof(p->length) + p->length; | |
| 39 | char *pack = g_malloc(len); | |
| 40 | char *a = pack; | |
| 41 | memcpy (a, &(p->type), sizeof(p->type)); | |
| 42 | a = a + sizeof(p->type); | |
| 43 | memcpy (a, &(p->subtype), sizeof(p->subtype)); | |
| 44 | a = a + sizeof(p->subtype); | |
| 45 | memcpy (a, &(p->length), sizeof(p->length)); | |
| 46 | a = a + sizeof(p->length); | |
| 47 | memcpy (a, p->data, p->length); | |
| 48 | write(fd, pack, len); | |
| 49 | g_free(pack); | |
| 50 | } | |
| 51 | ||
| 52 | ||
| 53 | void cui_packet_append_string(struct gaim_cui_packet *p, char *str) { | |
| 54 | int len = p->length + strlen(str); | |
| 55 | char *k = g_malloc(len); | |
| 56 | memcpy(k, p->data, p->length); | |
| 57 | memcpy(k + p->length, str, strlen(str)); | |
| 58 | if (p->data) | |
| 59 | g_free(p->data); | |
| 60 | p->data = k; | |
| 61 | p->length = len; | |
| 62 | } | |
| 63 | ||
| 64 | void cui_packet_append_char(struct gaim_cui_packet *p, char c) { | |
| 65 | int len = p->length + sizeof(char); | |
| 66 | char *k = g_malloc(len); | |
| 67 | memcpy(k, p->data, p->length); | |
| 68 | k[p->length] = c; | |
| 69 | if (p->data) | |
| 70 | g_free(p->data); | |
| 71 | p->data = k; | |
| 72 | p->length = len; | |
| 73 | } | |
| 74 | ||
| 75 | void cui_packet_append_raw(struct gaim_cui_packet *p, char *str, int len) { | |
| 76 | int lent = p->length + len; | |
| 77 | char *k = g_malloc(lent); | |
| 78 | memcpy(k, p->data, p->length); | |
| 79 | memcpy(k + p->length, str, len); | |
| 80 | if (p->data) | |
| 81 | g_free(p->data); | |
| 82 | p->data = k; | |
| 83 | p->length = lent; | |
| 84 | } | |
| 85 | ||
| 86 | struct gaim_cui_packet *cui_packet_new(guchar type, guchar subtype) { | |
| 87 | struct gaim_cui_packet *p = g_new0(struct gaim_cui_packet, 1); | |
| 88 | p->type = type; | |
| 89 | p->subtype = subtype; | |
| 90 | p->length = 0; | |
| 91 | p->data = NULL; | |
| 92 | return p; | |
| 93 | } | |
| 94 | ||
| 95 | void cui_packet_free(struct gaim_cui_packet *p) { | |
| 96 | if (p->data) | |
| 97 | g_free(p->data); | |
| 98 | g_free(p); | |
| 99 | } | |
| 100 | ||
| 101 | struct gaim_cui_packet *cui_read_packet(int fd) { | |
| 102 | struct gaim_cui_packet *p = g_new0(struct gaim_cui_packet, 1); | |
| 103 | char *data = NULL; | |
| 104 | ||
| 105 | if (!(read(fd, p->type, sizeof(p->type)))) { | |
| 106 | g_free(p); | |
| 107 | return NULL; | |
| 108 | } | |
| 109 | ||
| 110 | ||
| 111 | if (!(read(fd, p->subtype, sizeof(p->subtype)))) { | |
| 112 | g_free(p); | |
| 113 | return NULL; | |
| 114 | } | |
| 115 | ||
| 116 | ||
| 117 | if (!(read(fd, p->length, sizeof(p->length)))) { | |
| 118 | g_free(p); | |
| 119 | return NULL; | |
| 120 | } | |
| 121 | ||
| 122 | if (p->length) { | |
| 123 | data = g_malloc(p->length); | |
| 124 | if (!(read(fd, data, p->length))) { | |
| 125 | g_free(p); | |
| 126 | return NULL; | |
| 127 | } | |
| 128 | } | |
| 129 | p->data = data; | |
| 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 |