File: libtlen.c

package info (click to toggle)
libtlen 20041113-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 832 kB
  • ctags: 1,514
  • sloc: ansic: 13,713; sh: 214; makefile: 156
file content (190 lines) | stat: -rw-r--r-- 4,288 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Ostatnia aktualizacja:
 *
 * - $Id: libtlen.c,v 1.38 2002/12/14 19:36:11 mati Exp $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>

#include "libtlen.h"

/*
 * tlen_init()
 *
 * Wywoywana na pocztku, w celu zainicjowania struktury sesji
 *
 * - brak parametrw
 *
 * Przed wykonaniem tej funkcji nie wolno nic robi z sesj!
 *
 */

struct tlen_session *tlen_init (void)
{
	struct tlen_session *sess;

	if (!(sess = malloc (sizeof (*sess))))
	{
		perror ("malloc");
		return NULL;
	}
	memset (sess, 0, sizeof (*sess));
	
	tlen_debug("Session initialized\n");
	
	return sess;
}

/*
 * tlen_set_hub_blocking()
 *
 * Ustawia, czy proces resolvovania huba ma by synchroniczny,
 * czy asynchroniczny
 *
 * - sess - sesja dla ktrej ustawiamy parametr
 * - blocking - 0 lub 1
 *
 * Nie ustawiamy tego rcznie, poprzez edycj struktury, a jedynie
 * t funkcj!
 *
 */

void tlen_set_hub_blocking(struct tlen_session *sess, int blocking)
{
    switch (blocking)
    {
	case 0:
	    tlen_debug("Hub blocking set to: non-blocking\n");
	    break;
	case 1:
	    tlen_debug("Hub blocking set to: blocking\n");
	    break;
    }

    sess->hub_blocking = blocking;
}

/*
 * tlen_set_auth()
 *
 * Ustawia nazw uytkownika i haso w sesji
 *
 * - sess - sesja dla ktrej ustawiamy parametr
 * - username - nazwa uytkownika
 * - password - haso
 *
 * Nie ustawiamy tego rcznie, poprzez edycj struktury, a jedynie
 * t funkcj!
 *
 */

void tlen_set_auth(struct tlen_session *sess, char *username, char *password)
{
    tlen_debug("Username: %s\nPassword: <hidden>\n",username);
    if (username) { sess->username = strdup(username); }
    if (password) { sess->password = strdup(password); }    
}

/*
 * tlen_set_proxy()
 *
 * Ustawia adres i port serwera proxy dla transmisji
 *
 * - sess - sesja dla ktrej ustawiamy parametr
 * - addr - adres serwera
 * - port - port
 *
 * NIEZAIMPLEMENTOWANE
 *
 */

void tlen_set_proxy(struct tlen_session *sess, char *addr, int port)
{
    tlen_debug("Proxy address: %s\nProxy port: %d\n",addr,port);
    if (addr) { sess->proxy_addr = strdup(addr); }
    sess->proxy_port = port;
}

/*
 * tlen_login()
 *
 * Podstawowa funkcja rozpoczynajca poczenie z serwerem
 *
 * - sess - sesja opisujca poczenie
 *
 * Funkcja wypenia rne pola w sesji, jako jej parametr podajemy
 * sesj ustawion i zainicjowan funkcjami tlen_init i tlen_set_*!
 *
 */

void tlen_login (struct tlen_session *sess)
{
	char *e = NULL;

	if (sess->username==NULL) { sess->username = ""; }
	if (sess->password==NULL) { sess->password = ""; }
	if (sess->proxy_addr==NULL) { sess->proxy_enabled = 0; } else { sess->proxy_enabled = 1; }
	if (!e&&((sess->parser=XML_ParserCreate(NULL))==NULL)) e="parser";
	sess->resolv_pid = 0;
        sess->writebuffer = sess->writebuffer_last_item = NULL;
	sess->fd = -1;
        if (!e&&(tlen_connect_hub(sess, sess->hub_blocking)==-1)) e="hub";
	if (e) {
		perror(e);
		tlen_freesession(sess);
	}
	XML_SetElementHandler(sess->parser,tlen_starttag_handler,tlen_endtag_handler);
	XML_SetUserData(sess->parser,sess);
	sess->nestlevel = 0;

}

/*
 * tlen_freesession()
 *
 * Wywoywane w celu zwolnienia pamici po sesji
 *
 * - sess - sesja, ktr zwalniamy
 *
 * Najpierw nalezy si rozczy z serwerem wysyajc status
 * TLEN_STATUS_UNAVAILABLE
 *
 */

int tlen_freesession (struct tlen_session *sess)
{
	struct tlen_event* event;
        struct tlen_writebuffer_item* wb_item;

	tlen_socket_destroy(sess);
	if (sess->parser) XML_ParserFree(sess->parser);
	if (sess->event) while ((event=tlen_getevent(sess))!=NULL) tlen_freeevent(event);
	if (sess->bufferpool) pool_free(sess->bufferpool);
	if (sess->resolv_pid) {
	    kill(sess->resolv_pid,SIGTERM);
	    waitpid(sess->resolv_pid, NULL, 0);
	}
        free(sess->sid);
        free(sess->username);
        free(sess->password);
        free(sess->description);
        wb_item = sess->writebuffer;
        while (wb_item)
        {
            struct tlen_writebuffer_item* next_item = wb_item->next;
            free(wb_item->data_mem);
            free(wb_item);
            wb_item = next_item;
        }

	free(sess);
	tlen_debug ("Session freed.\n");
	return 1;
}