1546
|
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
|
|
22 #include "internal.h"
|
|
23
|
|
24 void (*yahoo_socket_notify)(struct yahoo_session *, int, int, gboolean) = NULL;
|
|
25 void (*yahoo_print)(struct yahoo_session *, int, const char *) = NULL;
|
|
26
|
|
27 struct yahoo_session *yahoo_new()
|
|
28 {
|
|
29 struct yahoo_session *sess;
|
|
30
|
|
31 if (!(sess = g_new0(struct yahoo_session, 1)))
|
|
32 return NULL;
|
|
33
|
|
34 return sess;
|
|
35 }
|
|
36
|
|
37 static int yahoo_connect_host(struct yahoo_session *sess, const char *host, int port, int *statusret)
|
|
38 {
|
|
39 struct sockaddr_in sa;
|
|
40 struct hostent *hp;
|
|
41 int fd;
|
|
42
|
|
43 if (!(hp = gethostbyname(host))) {
|
|
44 YAHOO_PRINT(sess, YAHOO_LOG_WARNING, "Resolve error");
|
|
45 if (statusret)
|
|
46 *statusret = (h_errno | YAHOO_CONN_STATUS_RESOLVERR);
|
|
47 return -1;
|
|
48 }
|
|
49
|
|
50 memset(&sa, 0, sizeof(struct sockaddr_in));
|
|
51 sa.sin_port = htons(port);
|
|
52 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
|
|
53 sa.sin_family = hp->h_addrtype;
|
|
54
|
|
55 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
|
|
56
|
|
57 fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
58
|
|
59 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
|
|
60 if ((errno == EINPROGRESS) || (errno == EINTR)) {
|
|
61 YAHOO_PRINT(sess, YAHOO_LOG_NOTICE, "Connect would block");
|
|
62 if (statusret)
|
|
63 *statusret |= YAHOO_CONN_STATUS_INPROGRESS;
|
|
64 return fd;
|
|
65 }
|
|
66 close(fd);
|
|
67 fd = -1;
|
|
68 }
|
|
69
|
|
70 return fd;
|
|
71 }
|
|
72
|
|
73 struct yahoo_conn *yahoo_new_conn(struct yahoo_session *session, int type, const char *host, int port)
|
|
74 {
|
|
75 struct yahoo_conn *conn;
|
|
76 int status;
|
|
77
|
|
78 if (!session)
|
|
79 return NULL;
|
|
80
|
|
81 conn = g_new0(struct yahoo_conn, 1);
|
|
82 conn->type = type;
|
|
83
|
|
84 if (host) {
|
|
85 conn->socket = yahoo_connect_host(session, host, port, &status);
|
|
86 } else {
|
|
87 switch (type) {
|
|
88 case YAHOO_CONN_TYPE_AUTH:
|
|
89 conn->socket = yahoo_connect_host(session, YAHOO_AUTH_HOST,
|
|
90 YAHOO_AUTH_PORT, &status);
|
|
91 break;
|
|
92 case YAHOO_CONN_TYPE_MAIN:
|
|
93 conn->socket = yahoo_connect_host(session, YAHOO_PAGER_HOST,
|
|
94 YAHOO_PAGER_PORT, &status);
|
|
95 break;
|
|
96 case YAHOO_CONN_TYPE_DUMB:
|
|
97 conn->socket = yahoo_connect_host(session, YAHOO_DATA_HOST,
|
|
98 YAHOO_DATA_PORT, &status);
|
|
99 break;
|
|
100 }
|
|
101 }
|
|
102
|
|
103 if (conn->socket < 0) {
|
|
104 g_free(conn);
|
|
105 return NULL;
|
|
106 }
|
|
107
|
|
108 if (yahoo_socket_notify)
|
|
109 (*yahoo_socket_notify)(session, conn->socket, YAHOO_SOCKET_WRITE, TRUE);
|
|
110 else
|
|
111 YAHOO_PRINT(session, YAHOO_LOG_CRITICAL, "yahoo_socket_notify not set up");
|
|
112
|
|
113 session->connlist = g_list_append(session->connlist, conn);
|
|
114
|
|
115 return conn;
|
|
116 }
|
|
117
|
|
118 struct yahoo_conn *yahoo_getconn_type(struct yahoo_session *sess, int type)
|
|
119 {
|
|
120 GList *c;
|
|
121 struct yahoo_conn *conn;
|
|
122
|
|
123 if (!sess)
|
|
124 return NULL;
|
|
125
|
|
126 c = sess->connlist;
|
|
127 while (c) {
|
|
128 conn = c->data;
|
|
129 if (conn->type == type)
|
|
130 return conn;
|
|
131 c = g_list_next(c);
|
|
132 }
|
|
133
|
|
134 return NULL;
|
|
135 }
|
|
136
|
|
137 struct yahoo_conn *yahoo_find_conn(struct yahoo_session *sess, int socket)
|
|
138 {
|
|
139 GList *c;
|
|
140 struct yahoo_conn *conn;
|
|
141
|
|
142 c = sess->connlist;
|
|
143 while (c) {
|
|
144 conn = c->data;
|
|
145 if (conn->socket == socket)
|
|
146 return conn;
|
|
147 c = g_list_next(c);
|
|
148 }
|
|
149
|
|
150 return NULL;
|
|
151 }
|
|
152
|
|
153 int yahoo_connect(struct yahoo_session *session, const char *host, int port)
|
|
154 {
|
|
155 if (!session)
|
|
156 return 0;
|
|
157
|
|
158 if (!yahoo_new_conn(session, YAHOO_CONN_TYPE_AUTH, host, port))
|
|
159 return 0;
|
|
160
|
|
161 return 1;
|
|
162 }
|
|
163
|
|
164 int yahoo_major_connect(struct yahoo_session *session, const char *host, int port)
|
|
165 {
|
|
166 if (!session)
|
|
167 return 0;
|
|
168
|
|
169 if (!yahoo_new_conn(session, YAHOO_CONN_TYPE_MAIN, host, port))
|
|
170 return 0;
|
|
171
|
|
172 return 1;
|
|
173 }
|
|
174
|
|
175 void yahoo_close(struct yahoo_session *session, struct yahoo_conn *conn)
|
|
176 {
|
|
177 if (!session || !conn)
|
|
178 return;
|
|
179
|
|
180 if (!g_list_find(session->connlist, conn))
|
|
181 return;
|
|
182
|
|
183 if (yahoo_socket_notify)
|
|
184 (*yahoo_socket_notify)(session, conn->socket, YAHOO_SOCKET_READ, FALSE);
|
|
185 close(conn->socket);
|
|
186
|
|
187 session->connlist = g_list_remove(session->connlist, conn);
|
|
188 if (conn->txqueue)
|
|
189 g_free(conn->txqueue);
|
|
190 g_free(conn);
|
|
191 }
|
|
192
|
|
193 int yahoo_disconnect(struct yahoo_session *session)
|
|
194 {
|
|
195 if (!session)
|
|
196 return 0;
|
|
197 if (session->name)
|
|
198 g_free(session->name);
|
|
199 yahoo_logoff(session);
|
|
200 session->name = NULL;
|
|
201 while (session->connlist)
|
|
202 yahoo_close(session, session->connlist->data);
|
|
203 if (session->cookie)
|
|
204 g_free(session->cookie);
|
|
205 session->cookie = NULL;
|
|
206 if (session->login_cookie)
|
|
207 g_free(session->login_cookie);
|
|
208 session->login_cookie = NULL;
|
|
209 while (session->ignored) {
|
|
210 g_free(session->ignored->data);
|
|
211 session->ignored = g_list_remove(session->ignored, session->ignored->data);
|
|
212 }
|
|
213 if (session->identities)
|
|
214 g_strfreev(session->identities);
|
|
215 session->identities = NULL;
|
|
216 if (session->login)
|
|
217 g_free(session->login);
|
|
218 session->login = NULL;
|
|
219 while (session->groups) {
|
|
220 struct yahoo_group *grp = session->groups->data;
|
|
221 g_strfreev(grp->buddies);
|
|
222 g_free(grp->name);
|
|
223 g_free(grp);
|
|
224 session->groups = g_list_remove(session->groups, grp);
|
|
225 }
|
|
226 return 0;
|
|
227 }
|
|
228
|
|
229 int yahoo_delete(struct yahoo_session *session)
|
|
230 {
|
|
231 if (!session)
|
|
232 return 0;
|
|
233 g_free(session);
|
|
234 return 0;
|
|
235 }
|