Mercurial > pidgin
annotate src/protocols/yahoo/conn.c @ 2475:ba7ee4c1908c
[gaim-migrate @ 2488]
BMiller's fixes so gg compiles on solaris
committer: Tailor Script <tailor@pidgin.im>
| author | Eric Warmenhoven <eric@warmenhoven.org> |
|---|---|
| date | Wed, 10 Oct 2001 20:03:17 +0000 |
| parents | f89313102962 |
| children |
| rev | line source |
|---|---|
| 2086 | 1 /* |
| 2 * libyay | |
| 3 * | |
| 4 * Copyright (C) 2001 Eric Warmenhoven <warmenhoven@yahoo.com> | |
| 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 | |
|
2145
91223be78b70
[gaim-migrate @ 2155]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
22 #include <string.h> |
| 2086 | 23 #include "internal.h" |
| 24 | |
| 25 void (*yahoo_socket_notify)(struct yahoo_session *, int, int, gboolean) = NULL; | |
| 26 void (*yahoo_print)(struct yahoo_session *, int, const char *) = NULL; | |
| 27 int (*yahoo_connector)(struct yahoo_session *, const char *, int, gpointer) = NULL; | |
| 28 | |
| 29 struct yahoo_session *yahoo_new() | |
| 30 { | |
| 31 struct yahoo_session *sess; | |
| 32 | |
| 33 if (!(sess = g_new0(struct yahoo_session, 1))) | |
| 34 return NULL; | |
| 35 | |
| 36 return sess; | |
| 37 } | |
| 38 | |
| 39 void yahoo_set_proxy(struct yahoo_session *session, int proxy_type, char *proxy_host, int proxy_port) | |
| 40 { | |
| 41 if (!session || !proxy_type || !proxy_host) | |
| 42 return; | |
| 43 | |
| 44 session->proxy_type = proxy_type; | |
| 45 session->proxy_host = g_strdup(proxy_host); | |
| 46 session->proxy_port = proxy_port; | |
| 47 } | |
| 48 | |
| 49 static int yahoo_connect_host(struct yahoo_session *sess, const char *host, int port, int *statusret) | |
| 50 { | |
| 51 struct sockaddr_in sa; | |
| 52 struct hostent *hp; | |
| 53 int fd; | |
| 54 | |
| 55 if (!(hp = gethostbyname(host))) { | |
| 56 YAHOO_PRINT(sess, YAHOO_LOG_WARNING, "Resolve error"); | |
| 57 if (statusret) | |
| 58 *statusret = (h_errno | YAHOO_CONN_STATUS_RESOLVERR); | |
| 59 return -1; | |
| 60 } | |
| 61 | |
| 62 memset(&sa, 0, sizeof(struct sockaddr_in)); | |
| 63 sa.sin_port = htons(port); | |
| 64 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length); | |
| 65 sa.sin_family = hp->h_addrtype; | |
| 66 | |
| 67 fd = socket(hp->h_addrtype, SOCK_STREAM, 0); | |
| 68 | |
| 69 fcntl(fd, F_SETFL, O_NONBLOCK); | |
| 70 | |
| 71 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) { | |
| 72 if ((errno == EINPROGRESS) || (errno == EINTR)) { | |
| 73 YAHOO_PRINT(sess, YAHOO_LOG_NOTICE, "Connect would block"); | |
| 74 if (statusret) | |
| 75 *statusret |= YAHOO_CONN_STATUS_INPROGRESS; | |
| 76 return fd; | |
| 77 } | |
| 78 close(fd); | |
| 79 fd = -1; | |
| 80 } | |
| 81 | |
| 82 return fd; | |
| 83 } | |
| 84 | |
| 85 int yahoo_connected(struct yahoo_session *session, gpointer data, int fd) | |
| 86 { | |
| 87 struct yahoo_conn *conn = data; | |
| 88 | |
| 89 if (fd < 0) { | |
| 90 YAHOO_PRINT(session, YAHOO_LOG_WARNING, "connect failed"); | |
| 91 session->connlist = g_list_remove(session->connlist, conn); | |
| 92 g_free(conn); | |
| 93 return 0; | |
| 94 } | |
| 95 | |
| 96 YAHOO_PRINT(session, YAHOO_LOG_NOTICE, "connect succeeded"); | |
| 97 conn->socket = fd; | |
| 98 conn->connected = TRUE; | |
| 99 if (conn->type == YAHOO_CONN_TYPE_AUTH) { | |
| 100 if (session->callbacks[YAHOO_HANDLE_AUTHCONNECT].function) | |
|
2145
91223be78b70
[gaim-migrate @ 2155]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
101 if (!(*session->callbacks[YAHOO_HANDLE_AUTHCONNECT].function)(session)) |
|
91223be78b70
[gaim-migrate @ 2155]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
102 return 0; |
| 2086 | 103 } else if (conn->type == YAHOO_CONN_TYPE_MAIN) { |
| 104 if (session->callbacks[YAHOO_HANDLE_MAINCONNECT].function) | |
|
2145
91223be78b70
[gaim-migrate @ 2155]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
105 if (!(*session->callbacks[YAHOO_HANDLE_MAINCONNECT].function)(session)) |
|
91223be78b70
[gaim-migrate @ 2155]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
106 return 0; |
| 2086 | 107 } else if (conn->type == YAHOO_CONN_TYPE_DUMB) { |
| 108 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, "sending to buddy list host"); | |
| 109 yahoo_write(session, conn, conn->txqueue, strlen(conn->txqueue)); | |
| 110 g_free(conn->txqueue); | |
| 111 conn->txqueue = NULL; | |
| 112 } | |
| 113 | |
| 114 if (yahoo_socket_notify) | |
| 115 (*yahoo_socket_notify)(session, conn->socket, YAHOO_SOCKET_READ, TRUE); | |
| 116 else | |
| 117 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "yahoo_socket_notify not set up"); | |
| 118 | |
| 119 return 1; | |
| 120 } | |
| 121 | |
| 122 struct yahoo_conn *yahoo_new_conn(struct yahoo_session *session, int type, const char *host, int port) | |
| 123 { | |
| 124 struct yahoo_conn *conn; | |
| 125 int status; | |
| 126 | |
| 127 if (!session) | |
| 128 return NULL; | |
| 129 | |
| 130 conn = g_new0(struct yahoo_conn, 1); | |
| 131 conn->type = type; | |
| 132 | |
| 133 if (yahoo_connector) { | |
| 134 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, "Connecting using user-specified connect routine"); | |
| 135 if ((*yahoo_connector)(session, host, port, conn) < 0) { | |
| 136 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "connect failed"); | |
| 137 g_free(conn); | |
| 138 return NULL; | |
| 139 } | |
| 140 session->connlist = g_list_append(session->connlist, conn); | |
| 141 return conn; | |
| 142 } | |
| 143 | |
| 144 if (session->proxy_type) { | |
| 145 YAHOO_PRINT(session, YAHOO_LOG_DEBUG, "connecting to proxy"); | |
| 146 conn->socket = yahoo_connect_host(session, session->proxy_host, | |
| 147 session->proxy_port, &status); | |
| 148 if (type == YAHOO_CONN_TYPE_MAIN) | |
| 149 conn->type = YAHOO_CONN_TYPE_PROXY; | |
| 150 } else | |
| 151 conn->socket = yahoo_connect_host(session, host, port, &status); | |
| 152 | |
| 153 if (conn->socket < 0) { | |
| 154 g_free(conn); | |
| 155 return NULL; | |
| 156 } | |
| 157 | |
| 158 if (yahoo_socket_notify) | |
| 159 (*yahoo_socket_notify)(session, conn->socket, YAHOO_SOCKET_WRITE, TRUE); | |
| 160 else | |
| 161 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "yahoo_socket_notify not set up"); | |
| 162 | |
| 163 session->connlist = g_list_append(session->connlist, conn); | |
| 164 | |
| 165 return conn; | |
| 166 } | |
| 167 | |
| 168 struct yahoo_conn *yahoo_getconn_type(struct yahoo_session *sess, int type) | |
| 169 { | |
| 170 GList *c; | |
| 171 struct yahoo_conn *conn; | |
| 172 | |
| 173 if (!sess) | |
| 174 return NULL; | |
| 175 | |
| 176 c = sess->connlist; | |
| 177 while (c) { | |
| 178 conn = c->data; | |
| 179 if (conn->type == type) | |
| 180 return conn; | |
| 181 c = g_list_next(c); | |
| 182 } | |
| 183 | |
| 184 return NULL; | |
| 185 } | |
| 186 | |
| 187 struct yahoo_conn *yahoo_find_conn(struct yahoo_session *sess, int socket) | |
| 188 { | |
| 189 GList *c; | |
| 190 struct yahoo_conn *conn; | |
| 191 | |
| 192 c = sess->connlist; | |
| 193 while (c) { | |
| 194 conn = c->data; | |
| 195 if (conn->socket == socket) | |
| 196 return conn; | |
| 197 c = g_list_next(c); | |
| 198 } | |
| 199 | |
| 200 return NULL; | |
| 201 } | |
| 202 | |
| 203 int yahoo_connect(struct yahoo_session *session, const char *host, int port) | |
| 204 { | |
| 205 if (!session) | |
| 206 return 0; | |
| 207 | |
| 208 if (session->auth_host) | |
| 209 g_free(session->auth_host); | |
| 210 if (host && *host) | |
| 211 session->auth_host = g_strdup(host); | |
| 212 else | |
| 213 session->auth_host = g_strdup(YAHOO_AUTH_HOST); | |
| 214 | |
| 215 if (port) | |
| 216 session->auth_port = port; | |
| 217 else | |
| 218 session->auth_port = YAHOO_AUTH_PORT; | |
| 219 | |
| 220 if (!yahoo_new_conn(session, YAHOO_CONN_TYPE_AUTH, session->auth_host, session->auth_port)) | |
| 221 return 0; | |
| 222 | |
| 223 return 1; | |
| 224 } | |
| 225 | |
| 226 int yahoo_major_connect(struct yahoo_session *session, const char *host, int port) | |
| 227 { | |
| 228 if (!session) | |
| 229 return 0; | |
| 230 | |
| 231 if (session->pager_host) | |
| 232 g_free(session->pager_host); | |
| 233 if (host && *host) | |
| 234 session->pager_host = g_strdup(host); | |
| 235 else | |
| 236 session->pager_host = g_strdup(YAHOO_PAGER_HOST); | |
| 237 | |
| 238 if (port) | |
| 239 session->pager_port = port; | |
| 240 else | |
| 241 session->pager_port = YAHOO_AUTH_PORT; | |
| 242 | |
| 243 if (!yahoo_new_conn(session, YAHOO_CONN_TYPE_MAIN, session->pager_host, session->pager_port)) | |
| 244 return 0; | |
| 245 | |
| 246 return 1; | |
| 247 } | |
| 248 | |
| 249 void yahoo_close(struct yahoo_session *session, struct yahoo_conn *conn) | |
| 250 { | |
| 251 if (!session || !conn) | |
| 252 return; | |
| 253 | |
| 254 if (!g_list_find(session->connlist, conn)) | |
| 255 return; | |
| 256 | |
| 257 if (yahoo_socket_notify) { | |
| 258 if (conn->connected) | |
| 259 (*yahoo_socket_notify)(session, conn->socket, YAHOO_SOCKET_READ, FALSE); | |
| 260 else | |
| 261 (*yahoo_socket_notify)(session, conn->socket, YAHOO_SOCKET_WRITE, FALSE); | |
| 262 } | |
| 263 close(conn->socket); | |
| 264 | |
| 265 session->connlist = g_list_remove(session->connlist, conn); | |
| 266 if (conn->txqueue) | |
| 267 g_free(conn->txqueue); | |
| 268 g_free(conn); | |
| 269 } | |
| 270 | |
| 271 int yahoo_disconnect(struct yahoo_session *session) | |
| 272 { | |
| 273 if (!session) | |
| 274 return 0; | |
|
2451
f89313102962
[gaim-migrate @ 2464]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2145
diff
changeset
|
275 yahoo_logoff(session); |
| 2086 | 276 if (session->name) |
| 277 g_free(session->name); | |
| 278 session->name = NULL; | |
| 279 while (session->connlist) | |
| 280 yahoo_close(session, session->connlist->data); | |
| 281 if (session->cookie) | |
| 282 g_free(session->cookie); | |
| 283 session->cookie = NULL; | |
| 284 if (session->login_cookie) | |
| 285 g_free(session->login_cookie); | |
| 286 session->login_cookie = NULL; | |
| 287 while (session->ignored) { | |
| 288 g_free(session->ignored->data); | |
| 289 session->ignored = g_list_remove(session->ignored, session->ignored->data); | |
| 290 } | |
| 291 if (session->identities) | |
| 292 g_strfreev(session->identities); | |
| 293 session->identities = NULL; | |
| 294 if (session->login) | |
| 295 g_free(session->login); | |
| 296 session->login = NULL; | |
| 297 while (session->groups) { | |
| 298 struct yahoo_group *grp = session->groups->data; | |
| 299 g_strfreev(grp->buddies); | |
| 300 g_free(grp->name); | |
| 301 g_free(grp); | |
| 302 session->groups = g_list_remove(session->groups, grp); | |
| 303 } | |
| 304 if (session->auth_host) | |
| 305 g_free(session->auth_host); | |
| 306 session->auth_host = NULL; | |
| 307 if (session->pager_host) | |
| 308 g_free(session->pager_host); | |
| 309 session->pager_host = NULL; | |
| 310 return 0; | |
| 311 } | |
| 312 | |
| 313 int yahoo_delete(struct yahoo_session *session) | |
| 314 { | |
| 315 if (!session) | |
| 316 return 0; | |
| 317 if (session->proxy_host) | |
| 318 g_free(session->proxy_host); | |
| 319 g_free(session); | |
| 320 return 0; | |
| 321 } |
