source: trunk/instant_messenger/socket/BKP_20071105/BKP_20071026/BKP_20071019/BKP_20071018/BKP_20071009/BKP_20071008/teste.c @ 151

Revision 151, 3.9 KB checked in by niltonneto, 16 years ago (diff)

Commit da nova versão do módulo, usando agente em C.
Vide Página do módulo do Trac:
http://www.expressolivre.org/dev/wiki/messenger

A versão anterior encontra-se na subpasta bkp (32/64).

Line 
1#include "unp.h"
2#include "xmpp.h"
3#include <malloc.h>
4
5//#define HOST "im.pr.gov.br"
6//#define IP "200.189.113.148"
7//#define HOST "localhost"
8//#define IP "10.15.20.42"
9#define HOST "im.celepar.parana"
10#define IP "10.15.20.219"
11#define PORT "5222"
12
13
14int tls = 0;
15
16void Read(void * fd, void *buf, int count)
17{
18        bzero(buf, count);
19        if ( tls == 0 )
20                read(*(int *) fd, buf, (ssize_t)count);
21        else
22                SSL_read((SSL *) fd, buf, count);
23}
24
25void Write(void * fd, void *buf, int count)
26{
27        if ( tls == 0 )
28                write(*(int *) fd, buf, (ssize_t)count);
29        else
30                SSL_write((SSL *) fd, buf, count);
31}
32
33int main(void)
34{
35        struct sockaddr_in socketaddr;
36        int socketfd,
37                nHostPort;
38        int err,
39                i,
40                unit;
41        extern int tls;
42        char buff[4096];
43        char * xml = (char)NULL;
44
45        /*SSL PART*/
46        //SSL_METHOD *meth;
47        SSL_CTX  *ctx;
48        SSL  *myssl;
49        /*SSL PART*/
50
51        xml = (char *) malloc(sizeof(__CONNECT__));
52
53        if ( (nHostPort = strtol(PORT, 0, 10)) == 0 )
54        {
55                printf("\n<port>\n\n");
56                printf("\ncould not make a socket\n");
57                return 0;
58        }
59
60        socketfd = socket(AF_INET, SOCK_STREAM, 0);
61
62        socketaddr.sin_family = AF_INET;
63        //socketaddr.sin_addr.s_addr = INADDR_ANY;//inet_addr(IP);
64        socketaddr.sin_addr.s_addr = inet_addr(IP);
65        socketaddr.sin_port = htons(nHostPort);
66
67        //OpenSSL_add_all_algorithms();
68        SSL_library_init();
69        //SSL_load_error_strings();
70
71        //meth=TLSv1_client_method();
72
73        //ctx=SSL_CTX_new(TLSv1_client_method());
74        if ( !(ctx = SSL_CTX_new(TLSv1_client_method())) )
75        {
76                printf("Error creating the context.\n");
77                exit(0);
78        }
79
80        err = connect(socketfd, (struct sockaddr*)&socketaddr, sizeof(socketaddr));
81        //fcntl(socketfd, F_SETFL, fcntl(socketfd, F_GETFL, 0) | O_NDELAY | O_NONBLOCK);
82
83        if ( err < 0 )
84        {
85                printf("Socket returned error #%d,program terminated\n", err);
86                exit(0);
87        }
88
89        sprintf(xml, __CONNECT__, HOST);
90        Write(&socketfd, xml, strlen(xml));
91        //usleep(500);
92        int stop = 0;
93        do
94        {
95                Read (&socketfd, buff, sizeof(buff));
96                if ( strstr(buff, __FEATURES_BEGIN__) )
97                        stop = 1;
98                else
99                        usleep(100);
100        }
101        while ( !stop );
102
103puts(buff);
104        stop = 0;
105        int starttls = 0;
106
107        if ( strstr(buff, __STARTTLS_SERVER__) )
108                starttls = 1;
109
110/*      if ( !strstr(buff, __FEATURES_END__) )
111        {
112                do
113                {
114                        Read (&socketfd, buff, sizeof(buff));
115                        if ( strstr(buff, __STARTTLS_SERVER__) )
116                                starttls = 1;
117                        if ( strstr(buff, __FEATURES_END__) )
118                                stop = 1;
119                        else
120                                usleep(100);
121                }
122                while ( !stop );
123        }*/
124
125        if ( starttls )
126        {
127                sprintf(xml, __STARTTLS_CLIENT__);
128                Write(&socketfd, xml, strlen(xml));
129
130                stop = 0;
131                do
132                {
133                        Read (&socketfd, buff, sizeof(buff));
134                        if ( strstr(buff, __PROCEED__) )
135                                stop = 1;
136                        else
137                                usleep(100);
138                }
139                while ( !stop );
140                printf("%s\n\n", buff);
141                bzero(buff, 4096);
142
143                SSL_CTX_set_verify(ctx,SSL_VERIFY_NONE,NULL);
144                /*Create new ssl object*/
145                myssl=SSL_new(ctx);
146
147                if(!myssl){
148                        printf("Error creating SSL structure.\n");
149                        exit(0);
150                }
151
152                /*Bind the socket to the SSL structure*/
153                SSL_set_fd(myssl,socketfd);
154
155                /*Connect to the server, SSL layer.*/
156                err=SSL_connect(myssl);
157                printf("SSL error #%d in accept,program terminated\n",err);
158                /*Check for error in connect.*/
159                if (err<1) {
160
161                        err=SSL_get_error(myssl,err);
162                        printf("SSL error #%d in accept,program terminated\n",err);
163                        printf("%d -- %d\n\n", SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE);
164
165                        if(err==5){printf("sockerrno is:\n");}
166
167                        close(socketfd);
168                        SSL_CTX_free(ctx);
169                        exit(0);
170                }
171
172                tls = 1;
173
174                /*Print out connection details*/
175                printf("SSL connection on socket %x,Version: %s, Cipher: %s\n\n\n",
176                                socketfd,
177                                SSL_get_version(myssl),
178                                SSL_get_cipher(myssl));
179
180                sprintf(xml, __CONNECT__, HOST);
181                SSL_write(myssl, xml, strlen(xml));
182                usleep(10000);
183                SSL_read(myssl, buff, sizeof(buff));
184                printf("%s\n\n", buff);
185                bzero(buff, 4096);
186
187                usleep(10000);
188                SSL_read(myssl, buff, sizeof(buff));
189                printf("%s\n\n", buff);
190                bzero(buff, 4096);
191
192                SSL_clear(myssl);
193                SSL_shutdown(myssl);
194                SSL_free(myssl);
195                SSL_CTX_free(ctx);
196        }
197        fflush(stdin);
198    fflush(stdout);
199        exit(0);
200}
Note: See TracBrowser for help on using the repository browser.