Changeset 5392

Show
Ignore:
Timestamp:
03/14/08 17:09:28 (6 months ago)
Author:
morris
Message:

Sync with new socket API

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • wired/trunk/wired/server.c

    r5345 r5392  
    6262static wi_array_t                                       *wd_transfer_sockets; 
    6363 
    64 static wi_socket_context_t                     *wd_control_socket_context
    65 static wi_socket_context_t                     *wd_transfer_socket_context
     64static wi_socket_tls_t                         *wd_control_socket_tls
     65static wi_socket_tls_t                         *wd_transfer_socket_tls
    6666 
    6767wi_string_t                                                     *wd_banner; 
     
    174174void wd_server_apply_settings(void) { 
    175175        wi_data_t               *data; 
     176        wi_rsa_t                *private_key; 
     177        wi_x509_t               *certificate; 
    176178 
    177179        /* reload banner */ 
     
    200202        /* set SSL cipher list */ 
    201203        if(wd_settings.controlcipher) { 
    202                 if(!wi_socket_context_set_ssl_ciphers(wd_control_socket_context, wd_settings.controlcipher)) { 
     204                if(!wi_socket_tls_set_ciphers(wd_control_socket_tls, wd_settings.controlcipher)) { 
    203205                        wi_log_err(WI_STR("Could not set SSL cipher list \"%@\": %m"), 
    204206                                wd_settings.controlcipher); 
     
    207209 
    208210        if(wd_settings.transfercipher) { 
    209                 if(!wi_socket_context_set_ssl_ciphers(wd_transfer_socket_context, wd_settings.transfercipher)) { 
     211                if(!wi_socket_tls_set_ciphers(wd_transfer_socket_tls, wd_settings.transfercipher)) { 
    210212                        wi_log_err(WI_STR("Could not set SSL cipher list \"%@\": %m"), 
    211213                                wd_settings.transfercipher); 
     
    215217        /* load SSL certificate */ 
    216218        if(wd_settings.certificate) { 
    217                 if(!wi_socket_context_set_ssl_certificate(wd_control_socket_context, wd_settings.certificate) || 
    218                    !wi_socket_context_set_ssl_certificate(wd_transfer_socket_context, wd_settings.certificate)) { 
     219                certificate = wi_x509_init_with_pem_file(wi_x509_alloc(), wd_settings.certificate); 
     220                 
     221                if(certificate) { 
     222                        if(!wi_socket_tls_set_certificate(wd_control_socket_tls, certificate) || 
     223                           !wi_socket_tls_set_certificate(wd_transfer_socket_tls, certificate)) { 
     224                                wi_log_err(WI_STR("Could not set certificate: %m")); 
     225                        } 
     226                         
     227                        wi_release(certificate); 
     228                } else { 
    219229                        wi_log_err(WI_STR("Could not load certificate %@: %m"), 
     230                                wd_settings.certificate); 
     231                } 
     232 
     233                private_key = wi_rsa_init_with_pem_file(wi_rsa_alloc(), wd_settings.certificate); 
     234                 
     235                if(private_key) { 
     236                        if(!wi_socket_tls_set_private_key(wd_control_socket_tls, private_key) || 
     237                           !wi_socket_tls_set_private_key(wd_transfer_socket_tls, private_key)) { 
     238                                wi_log_err(WI_STR("Could not set private key: %m")); 
     239                        } 
     240                         
     241                        wi_release(private_key); 
     242                } else { 
     243                        wi_log_err(WI_STR("Could not load private key %@: %m"), 
    220244                                wd_settings.certificate); 
    221245                } 
     
    264288        unsigned char   dh1024_g[] = { 0x02 }; 
    265289 
    266         wd_control_socket_context      = wi_socket_context_init(wi_socket_context_alloc()); 
    267         wd_transfer_socket_context     = wi_socket_context_init(wi_socket_context_alloc()); 
    268          
    269         if(!wi_socket_context_set_ssl_type(wd_control_socket_context, WI_SOCKET_SSL_SERVER) || 
    270            !wi_socket_context_set_ssl_type(wd_transfer_socket_context, WI_SOCKET_SSL_SERVER)) 
    271                 wi_log_err(WI_STR("Could not set SSL context: %m")); 
    272          
    273         if(!wi_socket_context_set_ssl_dh(wd_control_socket_context, dh1024_p, sizeof(dh1024_p), dh1024_g, sizeof(dh1024_g)) || 
    274            !wi_socket_context_set_ssl_dh(wd_transfer_socket_context, dh1024_p, sizeof(dh1024_p), dh1024_g, sizeof(dh1024_g))) 
     290        wd_control_socket_tls  = wi_socket_tls_init(wi_socket_tls_alloc()); 
     291        wd_transfer_socket_tls = wi_socket_tls_init(wi_socket_tls_alloc()); 
     292         
     293        if(!wi_socket_tls_set_type(wd_control_socket_tls, WI_SOCKET_TLS_SERVER) || 
     294           !wi_socket_tls_set_type(wd_transfer_socket_tls, WI_SOCKET_TLS_SERVER)) 
     295                wi_log_err(WI_STR("Could not set SSL tls: %m")); 
     296         
     297        if(!wi_socket_tls_set_dh(wd_control_socket_tls, dh1024_p, sizeof(dh1024_p), dh1024_g, sizeof(dh1024_g)) || 
     298           !wi_socket_tls_set_dh(wd_transfer_socket_tls, dh1024_p, sizeof(dh1024_p), dh1024_g, sizeof(dh1024_g))) 
    275299                wi_log_err(WI_STR("Could not set anonymous DH key: %m")); 
    276300} 
     
    328352 
    329353                /* accept new user */ 
    330                 socket = wi_socket_accept_multiple(wd_control_sockets, wd_control_socket_context, 30.0, &address); 
     354                socket = wi_socket_accept_multiple(wd_control_sockets, 30.0, &address); 
    331355 
    332356                if(!address) { 
     
    339363                 
    340364                if(!socket) { 
     365                        wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); 
     366                         
     367                        continue; 
     368                } 
     369                 
     370                if(!wi_socket_accept_tls(socket, wd_control_socket_tls, 30.0)) { 
    341371                        wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); 
    342372                         
     
    374404 
    375405                /* accept new connection */ 
    376                 socket = wi_socket_accept_multiple(wd_transfer_sockets, wd_transfer_socket_context, 30.0, &address); 
     406                socket = wi_socket_accept_multiple(wd_transfer_sockets, 30.0, &address); 
    377407                 
    378408                if(!address) { 
     
    385415                 
    386416                if(!socket) { 
     417                        wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); 
     418                         
     419                        continue; 
     420                } 
     421 
     422                if(!wi_socket_accept_tls(socket, wd_transfer_socket_tls, 30.0)) { 
    387423                        wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); 
    388424                         
     
    437473         
    438474        wd_user_lock_socket(user); 
    439         wi_socket_write(wd_user_socket(user), 0.0, WI_STR("%u %@%c"), n, string, WD_MESSAGE_SEPARATOR); 
     475        wi_socket_write_format(wd_user_socket(user), 0.0, WI_STR("%u %@%c"), n, string, WD_MESSAGE_SEPARATOR); 
    440476        wd_user_unlock_socket(user); 
    441477         
     
    471507        va_end(ap); 
    472508         
    473         wi_socket_write(socket, 0.0, WI_STR("%u %@%c"), n, string, WD_MESSAGE_SEPARATOR); 
     509        wi_socket_write_format(socket, 0.0, WI_STR("%u %@%c"), n, string, WD_MESSAGE_SEPARATOR); 
    474510         
    475511        wi_release(string);      
     
    498534                if(wd_user_state(user) == WD_USER_LOGGED_IN) { 
    499535                        wd_user_lock_socket(user); 
    500                         wi_socket_write(wd_user_socket(user), 0.0, WI_STR("%u %@%c"), n, string, WD_MESSAGE_SEPARATOR); 
     536                        wi_socket_write_format(wd_user_socket(user), 0.0, WI_STR("%u %@%c"), n, string, WD_MESSAGE_SEPARATOR); 
    501537                        wd_user_unlock_socket(user); 
    502538                } 
  • wired/trunk/wired/trackers.c

    r5147 r5392  
    5050        wi_boolean_t                                            active; 
    5151 
    52         wi_socket_context_t                                     *context; 
     52        wi_rsa_t                                                        *public_key; 
     53        wi_socket_tls_t                                         *tls; 
    5354        wi_socket_t                                                     *socket; 
    5455        wi_address_t                                            *address; 
     
    142143                } 
    143144 
    144                 tracker->context = wi_socket_context_init(wi_socket_context_alloc()); 
    145                  
    146                 if(!wi_socket_context_set_ssl_type(tracker->context, WI_SOCKET_SSL_CLIENT)) { 
     145                tracker->tls = wi_socket_tls_init(wi_socket_tls_alloc()); 
     146                 
     147                if(!wi_socket_tls_set_type(tracker->tls, WI_SOCKET_TLS_CLIENT)) { 
    147148                        wi_log_warn(WI_STR("Could not set SSL context: %m")); 
    148149                         
    149150                        continue; 
     151                } 
     152 
     153                if(wd_settings.controlcipher) { 
     154                        if(!wi_socket_tls_set_ciphers(tracker->tls, wd_settings.controlcipher)) { 
     155                                wi_log_err(WI_STR("Could not set SSL cipher list \"%@\": %m"), 
     156                                        wd_settings.controlcipher); 
     157                                 
     158                                continue; 
     159                        } 
    150160                } 
    151161 
     
    214224        wd_tracker_t            *tracker = instance; 
    215225 
    216         wi_release(tracker->context); 
     226        wi_release(tracker->tls); 
    217227 
    218228        wi_release(tracker->addresses); 
     
    324334        wi_array_t                      *arguments; 
    325335        wi_string_t                     *ip, *string; 
    326         void                            *key; 
    327336        uint32_t                        message; 
    328337        wi_boolean_t            fatal = false; 
     
    350359                } 
    351360 
    352                 if(!wi_socket_connect(tracker->socket, tracker->context, 30.0)) { 
     361                if(!wi_socket_connect(tracker->socket, 30.0)) { 
     362                        wi_log_err(WI_STR("Could not connect to tracker %@: %m"), 
     363                                tracker->host); 
     364                         
     365                        continue; 
     366                } 
     367                 
     368                if(!wi_socket_connect_tls(tracker->socket, tracker->tls, 30.0)) { 
    353369                        wi_log_err(WI_STR("Could not connect to tracker %@: %m"), 
    354370                                tracker->host); 
     
    398414                tracker->key = wi_retain(WI_ARRAY(arguments, 0)); 
    399415                 
    400                 key = wi_socket_ssl_pubkey(tracker->socket); 
    401  
    402                 if(!key) { 
     416                tracker->public_key = wi_retain(wi_socket_ssl_public_key(tracker->socket)); 
     417 
     418                if(!tracker->public_key) { 
    403419                        wi_log_err(WI_STR("Could not get public key from the tracker %@: %m"), 
    404420                                tracker->host); 
     
    407423                } 
    408424                 
    409                 wi_socket_context_set_ssl_pubkey(tracker->context, key); 
    410                  
    411425                wi_log_info(WI_STR("Registered with the tracker %@"), 
    412426                        tracker->host); 
     
    425439static void wd_tracker_update(wd_tracker_t *tracker) { 
    426440        wi_socket_t                     *socket; 
     441        wi_data_t                       *data; 
     442        wi_string_t                     *string; 
    427443        wi_integer_t            bytes; 
    428444        uint32_t                        guest, download; 
     
    440456        } 
    441457         
    442         bytes = wi_socket_sendto(socket, tracker->context, WI_STR("UPDATE %#@%c%u%c%u%c%u%c%u%c%llu%c"), 
    443                                                          tracker->key,                  WD_FIELD_SEPARATOR, 
    444                                                          wd_current_users,              WD_FIELD_SEPARATOR, 
    445                                                          guest,                                 WD_FIELD_SEPARATOR, 
    446                                                          download,                              WD_FIELD_SEPARATOR, 
    447                                                          wd_files_unique_count, WD_FIELD_SEPARATOR, 
    448                                                          wd_files_unique_size,  WD_MESSAGE_SEPARATOR); 
     458         
     459        string = wi_string_with_format(WI_STR("UPDATE %#@%c%u%c%u%c%u%c%u%c%llu%c"), 
     460                                                                   tracker->key,                        WD_FIELD_SEPARATOR, 
     461                                                                   wd_current_users,            WD_FIELD_SEPARATOR, 
     462                                                                   guest,                                       WD_FIELD_SEPARATOR, 
     463                                                                   download,                            WD_FIELD_SEPARATOR, 
     464                                                                   wd_files_unique_count,       WD_FIELD_SEPARATOR, 
     465                                                                   wd_files_unique_size,        WD_MESSAGE_SEPARATOR); 
     466         
     467        data = wi_rsa_encrypt(tracker->public_key, wi_string_data(string)); 
     468         
     469        bytes = wi_socket_sendto_data(socket, data); 
    449470         
    450471        if(bytes < 0) { 
     
    493514        va_end(ap); 
    494515         
    495         bytes = wi_socket_write(tracker->socket, 30.0, WI_STR("%@%c"), string, WD_MESSAGE_SEPARATOR); 
     516        bytes = wi_socket_write_format(tracker->socket, 30.0, WI_STR("%@%c"), string, WD_MESSAGE_SEPARATOR); 
    496517 
    497518        if(bytes <= 0) {