1 | #include "server.h" |
---|
2 | |
---|
3 | int main(int argc, char* argv[]) |
---|
4 | { |
---|
5 | /* handle to socket */ |
---|
6 | int hSocket = (int)NULL, |
---|
7 | hServerSocket = (int)NULL; |
---|
8 | |
---|
9 | int nAddressSize = sizeof(struct sockaddr_in); |
---|
10 | int nHostPort = (int)NULL; |
---|
11 | int err; |
---|
12 | |
---|
13 | /* holds info about a machine */ |
---|
14 | struct hostent * pHostInfo = (struct hostent *)NULL; |
---|
15 | |
---|
16 | /* Internet socket address stuct */ |
---|
17 | struct sockaddr_in Address; |
---|
18 | |
---|
19 | struct client * clients = (struct client *)NULL; |
---|
20 | pthread_t threads; |
---|
21 | //pthread_t clients; |
---|
22 | |
---|
23 | SSL_CTX * ctx; |
---|
24 | SSL * myssl; |
---|
25 | |
---|
26 | OpenSSL_add_all_algorithms(); |
---|
27 | SSL_library_init(); |
---|
28 | SSL_load_error_strings(); |
---|
29 | |
---|
30 | |
---|
31 | if ( !(ctx = SSL_CTX_new(TLSv1_server_method())) ) |
---|
32 | { |
---|
33 | printf("Error creating the context.\n"); |
---|
34 | exit(0); |
---|
35 | } |
---|
36 | |
---|
37 | if ( SSL_CTX_set_cipher_list(ctx, CIPHER_LIST) < 1 ) |
---|
38 | { |
---|
39 | printf("Error setting the cipher list.\n"); |
---|
40 | exit(0); |
---|
41 | } |
---|
42 | |
---|
43 | /*Indicate the certificate file to be used*/ |
---|
44 | if ( SSL_CTX_use_certificate_file(ctx, CERT_FILE, SSL_FILETYPE_PEM) < 1 ) |
---|
45 | { |
---|
46 | printf("Error setting the certificate file.\n"); |
---|
47 | exit(0); |
---|
48 | } |
---|
49 | |
---|
50 | /*Load the password for the Private Key*/ |
---|
51 | SSL_CTX_set_default_passwd_cb_userdata(ctx, KEY_PASSWD); |
---|
52 | |
---|
53 | /*Indicate the key file to be used*/ |
---|
54 | if ( SSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, SSL_FILETYPE_PEM) < 1 ) |
---|
55 | { |
---|
56 | printf("Error setting the key file.\n"); |
---|
57 | exit(0); |
---|
58 | } |
---|
59 | |
---|
60 | /*Make sure the key and certificate file match*/ |
---|
61 | if ( !SSL_CTX_check_private_key(ctx) ) |
---|
62 | { |
---|
63 | printf("Private key does not match the certificate public key\n"); |
---|
64 | exit(0); |
---|
65 | } |
---|
66 | |
---|
67 | /* Set the list of trusted CAs based on the file and/or directory provided*/ |
---|
68 | if ( SSL_CTX_load_verify_locations(ctx, CA_FILE, CA_DIR) < 1 ) |
---|
69 | { |
---|
70 | printf("Error setting verify location\n"); |
---|
71 | exit(0); |
---|
72 | } |
---|
73 | |
---|
74 | /* Set for server verification*/ |
---|
75 | SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); |
---|
76 | |
---|
77 | if ( argc != 2 || (nHostPort = strtol(argv[1], 0, 10)) == 0 ) |
---|
78 | { |
---|
79 | printf("\n<PORT>\n\n"); |
---|
80 | printf("\nCould not make a socket\n"); |
---|
81 | return 0; |
---|
82 | } |
---|
83 | |
---|
84 | printf("\nStarting server"); |
---|
85 | printf("\nMaking socket"); |
---|
86 | |
---|
87 | /* make a socket */ |
---|
88 | hServerSocket = socket(AF_INET, SOCK_STREAM, 0); |
---|
89 | if ( hServerSocket == SOCKET_ERROR ) |
---|
90 | { |
---|
91 | printf("\nCould not make a socket\n"); |
---|
92 | return 0; |
---|
93 | } |
---|
94 | |
---|
95 | /* fill address struct */ |
---|
96 | Address.sin_addr.s_addr = INADDR_ANY; |
---|
97 | Address.sin_port = htons(nHostPort); |
---|
98 | Address.sin_family = AF_INET; |
---|
99 | |
---|
100 | printf("\nBinding to port %d", nHostPort); |
---|
101 | |
---|
102 | /* bind to a port */ |
---|
103 | if ( bind(hServerSocket, (struct sockaddr*)&Address, sizeof(Address)) == SOCKET_ERROR ) |
---|
104 | { |
---|
105 | printf("\nCould not connect to host\n"); |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|
109 | /* get port number */ |
---|
110 | getsockname( hServerSocket, (struct sockaddr *) &Address,(socklen_t *)&nAddressSize); |
---|
111 | printf("opened socket as fd (%d) on port (%d) for stream i/o\n", hServerSocket, ntohs(Address.sin_port)); |
---|
112 | |
---|
113 | printf("Server\n\ |
---|
114 | sin_family = %d\n\ |
---|
115 | sin_addr.s_addr = %d\n\ |
---|
116 | sin_port = %d\n" |
---|
117 | , Address.sin_family |
---|
118 | , Address.sin_addr.s_addr |
---|
119 | , ntohs(Address.sin_port) |
---|
120 | ); |
---|
121 | printf("\nMaking a listen queue of %d elements", QUEUE_SIZE); |
---|
122 | |
---|
123 | /* establish listen queue */ |
---|
124 | if ( listen(hServerSocket, QUEUE_SIZE) == SOCKET_ERROR ) |
---|
125 | { |
---|
126 | printf("\nCould not listen\n"); |
---|
127 | return 0; |
---|
128 | } |
---|
129 | |
---|
130 | for ( ; ; ) |
---|
131 | { |
---|
132 | fflush(stdin); |
---|
133 | fflush(stdout); |
---|
134 | ///printf("\nWaiting for a connection\n"); |
---|
135 | puts("\nWaiting for a connection\n"); |
---|
136 | |
---|
137 | /* get the connected socket */ |
---|
138 | hSocket = accept(hServerSocket, (struct sockaddr*)&Address, (socklen_t *)&nAddressSize); |
---|
139 | clients = (struct client *) malloc(sizeof(struct client)); |
---|
140 | clients->hSocket = hSocket; |
---|
141 | |
---|
142 | /*Create new ssl object*/ |
---|
143 | if( !(clients->myssl = SSL_new(ctx)) ) |
---|
144 | { |
---|
145 | printf("Error creating SSL structure.\n"); |
---|
146 | exit(0); |
---|
147 | } |
---|
148 | |
---|
149 | /*Bind the socket to the SSL structure*/ |
---|
150 | SSL_set_fd(clients->myssl, hSocket); |
---|
151 | |
---|
152 | puts("_____TESTE______"); |
---|
153 | /*Connect to the server, SSL layer.*/ |
---|
154 | err = SSL_accept(clients->myssl); |
---|
155 | puts("_____TESTE______"); |
---|
156 | |
---|
157 | /*Check for error in connect.*/ |
---|
158 | if ( err < 1 ) |
---|
159 | { |
---|
160 | printf("SSL error #%d in accept,program terminated\n", err); |
---|
161 | err = SSL_get_error(clients->myssl, 0); |
---|
162 | printf("SSL error #%d in accept,program terminated\n", err); |
---|
163 | |
---|
164 | switch ( err ) |
---|
165 | { |
---|
166 | case SSL_ERROR_NONE : puts("SSL_ERROR_NONE");break; |
---|
167 | case SSL_ERROR_SSL : puts("SSL_ERROR_SSL");break; |
---|
168 | case SSL_ERROR_WANT_READ : puts("SSL_ERROR_WANT_READ"); |
---|
169 | } |
---|
170 | |
---|
171 | if ( err == 5 ) |
---|
172 | printf("sockerrno is: %d\n", err); |
---|
173 | |
---|
174 | |
---|
175 | close(hSocket); |
---|
176 | SSL_CTX_free(ctx); |
---|
177 | exit(0); |
---|
178 | } |
---|
179 | |
---|
180 | //clients->myssl = myssl; |
---|
181 | pthread_create(&threads, NULL, handler, (void *)clients); |
---|
182 | clients = (struct client *)NULL; |
---|
183 | } |
---|
184 | pthread_exit(NULL); |
---|
185 | puts("FIM LAssO"); |
---|
186 | } |
---|
187 | |
---|
188 | void * handler(void * pClient) |
---|
189 | { |
---|
190 | char pBuffer[BUFFER_SIZE]; |
---|
191 | struct client * client = (struct client *)pClient; |
---|
192 | int i = 0, err; |
---|
193 | |
---|
194 | printf("\nGot a connection"); |
---|
195 | strcpy(pBuffer,MESSAGE); |
---|
196 | printf("\nSending \"%s\" to client",pBuffer); |
---|
197 | |
---|
198 | /* number returned by read() and write() is the number of bytes |
---|
199 | ** read or written, with -1 being that an error occured |
---|
200 | ** write what we received back to the server */ |
---|
201 | //write(client->hSocket, pBuffer, strlen(pBuffer)+1); |
---|
202 | err=SSL_write(client->myssl, pBuffer, strlen(pBuffer)+1); |
---|
203 | printf("-------%s\n\n", pBuffer); |
---|
204 | |
---|
205 | |
---|
206 | /* read from socket into buffer */ |
---|
207 | //read(client->hSocket, pBuffer, BUFFER_SIZE); |
---|
208 | err = SSL_read(client->myssl, pBuffer, BUFFER_SIZE); |
---|
209 | |
---|
210 | printf("-------%s\n\n", pBuffer); |
---|
211 | |
---|
212 | if ( strcmp(pBuffer, MESSAGE) == 0 ) |
---|
213 | printf("\nThe messages match"); |
---|
214 | else |
---|
215 | printf("\nSomething was changed in the message"); |
---|
216 | |
---|
217 | while (i < 10) |
---|
218 | { |
---|
219 | printf("%d\n", i); |
---|
220 | usleep(1000000); |
---|
221 | i++; |
---|
222 | } |
---|
223 | |
---|
224 | printf("\nClosing the socket"); |
---|
225 | |
---|
226 | /*pBuffer[0] = '\0'; |
---|
227 | printf("\n\nread = %s\n\nsd = %d\n\n\n", pBuffer, SSL_get_shutdown(client->myssl)); |
---|
228 | SSL_shutdown(client->myssl); |
---|
229 | pBuffer[0] = '\0'; |
---|
230 | printf("\n\nread = %s\n\nsd = %d\n\n\n", pBuffer, SSL_get_shutdown(client->myssl)); |
---|
231 | SSL_shutdown(client->myssl); |
---|
232 | pBuffer[0] = '\0'; |
---|
233 | printf("\n\nread = %s\n\nsd = %d\n\n\n", pBuffer, SSL_get_shutdown(client->myssl)); |
---|
234 | SSL_free(client->myssl); |
---|
235 | */ |
---|
236 | /* close socket */ |
---|
237 | if ( close(client->hSocket) == SOCKET_ERROR ) |
---|
238 | { |
---|
239 | printf("\nCould not close socket\n"); |
---|
240 | return 0; |
---|
241 | } |
---|
242 | |
---|
243 | fflush(stdin); |
---|
244 | fflush(stdout); |
---|
245 | free(client); |
---|
246 | pthread_exit(NULL); |
---|
247 | } |
---|