1 /*
2  * Ostatnia aktualizacja:
3  *
4  * - $Id: libtlen.c,v 1.38 2002/12/14 19:36:11 mati Exp $
5  *
6  */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <sys/select.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 
16 #include "libtlen.h"
17 
18 /*
19  * tlen_init()
20  *
21  * Wywo�ywana na pocz�tku, w celu zainicjowania struktury sesji
22  *
23  * - brak parametr�w
24  *
25  * Przed wykonaniem tej funkcji nie wolno nic robi� z sesj�!
26  *
27  */
28 
tlen_init(void)29 struct tlen_session *tlen_init (void)
30 {
31 	struct tlen_session *sess;
32 
33 	if (!(sess = malloc (sizeof (*sess))))
34 	{
35 		perror ("malloc");
36 		return NULL;
37 	}
38 	memset (sess, 0, sizeof (*sess));
39 
40 	tlen_debug("Session initialized\n");
41 
42 	return sess;
43 }
44 
45 /*
46  * tlen_set_hub_blocking()
47  *
48  * Ustawia, czy proces resolvovania huba ma by� synchroniczny,
49  * czy asynchroniczny
50  *
51  * - sess - sesja dla kt�rej ustawiamy parametr
52  * - blocking - 0 lub 1
53  *
54  * Nie ustawiamy tego r�cznie, poprzez edycj� struktury, a jedynie
55  * t� funkcj�!
56  *
57  */
58 
tlen_set_hub_blocking(struct tlen_session * sess,int blocking)59 void tlen_set_hub_blocking(struct tlen_session *sess, int blocking)
60 {
61     switch (blocking)
62     {
63 	case 0:
64 	    tlen_debug("Hub blocking set to: non-blocking\n");
65 	    break;
66 	case 1:
67 	    tlen_debug("Hub blocking set to: blocking\n");
68 	    break;
69     }
70 
71     sess->hub_blocking = blocking;
72 }
73 
74 /*
75  * tlen_set_auth()
76  *
77  * Ustawia nazw� u�ytkownika i has�o w sesji
78  *
79  * - sess - sesja dla kt�rej ustawiamy parametr
80  * - username - nazwa u�ytkownika
81  * - password - has�o
82  *
83  * Nie ustawiamy tego r�cznie, poprzez edycj� struktury, a jedynie
84  * t� funkcj�!
85  *
86  */
87 
tlen_set_auth(struct tlen_session * sess,char * username,char * password)88 void tlen_set_auth(struct tlen_session *sess, char *username, char *password)
89 {
90     tlen_debug("Username: %s\nPassword: <hidden>\n",username);
91     if (username) { sess->username = strdup(username); }
92     if (password) { sess->password = strdup(password); }
93 }
94 
95 /*
96  * tlen_set_proxy()
97  *
98  * Ustawia adres i port serwera proxy dla transmisji
99  *
100  * - sess - sesja dla kt�rej ustawiamy parametr
101  * - addr - adres serwera
102  * - port - port
103  *
104  * NIEZAIMPLEMENTOWANE
105  *
106  */
107 
tlen_set_proxy(struct tlen_session * sess,char * addr,int port)108 void tlen_set_proxy(struct tlen_session *sess, char *addr, int port)
109 {
110     tlen_debug("Proxy address: %s\nProxy port: %d\n",addr,port);
111     if (addr) { sess->proxy_addr = strdup(addr); }
112     sess->proxy_port = port;
113 }
114 
115 /*
116  * tlen_login()
117  *
118  * Podstawowa funkcja rozpoczynaj�ca po��czenie z serwerem
119  *
120  * - sess - sesja opisuj�ca po��czenie
121  *
122  * Funkcja wype�nia r�ne pola w sesji, jako jej parametr podajemy
123  * sesj� ustawion� i zainicjowan� funkcjami tlen_init i tlen_set_*!
124  *
125  */
126 
tlen_login(struct tlen_session * sess)127 void tlen_login (struct tlen_session *sess)
128 {
129 	char *e = NULL;
130 
131 	if (sess->username==NULL) { sess->username = ""; }
132 	if (sess->password==NULL) { sess->password = ""; }
133 	if (sess->proxy_addr==NULL) { sess->proxy_enabled = 0; } else { sess->proxy_enabled = 1; }
134 	if (!e&&((sess->parser=XML_ParserCreate(NULL))==NULL)) e="parser";
135 	sess->resolv_pid = 0;
136         sess->writebuffer = sess->writebuffer_last_item = NULL;
137 	sess->fd = -1;
138         if (!e&&(tlen_connect_hub(sess, sess->hub_blocking)==-1)) e="hub";
139 	if (e) {
140 		perror(e);
141 		tlen_freesession(sess);
142 	}
143 	XML_SetElementHandler(sess->parser,tlen_starttag_handler,tlen_endtag_handler);
144 	XML_SetUserData(sess->parser,sess);
145 	sess->nestlevel = 0;
146 
147 }
148 
149 /*
150  * tlen_freesession()
151  *
152  * Wywo�ywane w celu zwolnienia pami�ci po sesji
153  *
154  * - sess - sesja, kt�r� zwalniamy
155  *
156  * Najpierw nalezy si� roz��czy� z serwerem wysy�aj�c status
157  * TLEN_STATUS_UNAVAILABLE
158  *
159  */
160 
tlen_freesession(struct tlen_session * sess)161 int tlen_freesession (struct tlen_session *sess)
162 {
163 	struct tlen_event* event;
164         struct tlen_writebuffer_item* wb_item;
165 
166 	tlen_socket_destroy(sess);
167 	if (sess->parser) XML_ParserFree(sess->parser);
168 	if (sess->event) while ((event=tlen_getevent(sess))!=NULL) tlen_freeevent(event);
169 	if (sess->bufferpool) pool_free(sess->bufferpool);
170 	if (sess->resolv_pid) {
171 	    kill(sess->resolv_pid,SIGTERM);
172 	    waitpid(sess->resolv_pid, NULL, 0);
173 	}
174         free(sess->sid);
175         free(sess->username);
176         free(sess->password);
177         free(sess->description);
178         wb_item = sess->writebuffer;
179         while (wb_item)
180         {
181             struct tlen_writebuffer_item* next_item = wb_item->next;
182             free(wb_item->data_mem);
183             free(wb_item);
184             wb_item = next_item;
185         }
186 
187 	free(sess);
188 	tlen_debug ("Session freed.\n");
189 	return 1;
190 }
191