aboutsummaryrefslogtreecommitdiffstats
path: root/src/libopm/src/libopm.c
blob: 430a29dbf5570eada5975657f465d7f17017e5d1 (plain) (blame)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
/*
 * Copyright (C) 2002-2003  Erik Fears
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to
 *
 *       The Free Software Foundation, Inc.
 *       59 Temple Place - Suite 330
 *       Boston, MA  02111-1307, USA.
 *
 *
 */

#include "setup.h"

#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <poll.h>
#ifdef HAVE_LIBCRYPTO
#include <openssl/ssl.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#include "config.h"
#include "libopm.h"
#include "memory.h"
#include "opm_error.h"
#include "opm_types.h"
#include "opm_common.h"
#include "opm_gettime.h"
#include "list.h"
#include "proxy.h"


static OPM_PROTOCOL_CONFIG_T *libopm_protocol_config_create(void);
static void libopm_protocol_config_free(OPM_PROTOCOL_CONFIG_T *);

static OPM_SCAN_T *libopm_scan_create(OPM_T *, OPM_REMOTE_T *);
static void libopm_scan_free(OPM_SCAN_T *);

static OPM_CONNECTION_T *libopm_connection_create(void);
static void libopm_connection_free(OPM_CONNECTION_T *);

static void libopm_check_establish(OPM_T *);
static void libopm_check_poll(OPM_T *);
static void libopm_check_closed(OPM_T *);
static void libopm_check_queue(OPM_T *);

static void libopm_do_connect(OPM_T *, OPM_SCAN_T *, OPM_CONNECTION_T *);
static void libopm_do_readready(OPM_T *, OPM_SCAN_T *, OPM_CONNECTION_T *);
static int libopm_do_readready_tls(OPM_T *, OPM_SCAN_T *, OPM_CONNECTION_T *);
static void libopm_do_writeready(OPM_T *, OPM_SCAN_T *, OPM_CONNECTION_T *);
static void libopm_do_hup(OPM_T *, OPM_SCAN_T *, OPM_CONNECTION_T *);
static void libopm_do_read(OPM_T *, OPM_SCAN_T *, OPM_CONNECTION_T *);
static void libopm_do_openproxy(OPM_T *, OPM_SCAN_T *, OPM_CONNECTION_T *);
static void libopm_do_callback(OPM_T *, OPM_REMOTE_T *, int, int);

static OPM_REMOTE_T *libopm_setup_remote(OPM_REMOTE_T *, OPM_CONNECTION_T *);


/* OPM_PROTOCOLS hash
 *
 *    OPM_PPROTOCOLS hashes the protocol types (int) to functions
 *    which handle the protocol (sending/receiving protocol specific
 *    data).
 *
 */
static OPM_PROTOCOL_T OPM_PROTOCOLS[] =
{
  { OPM_TYPE_HTTP,      libopm_proxy_http_write,      NULL, 0 },
  { OPM_TYPE_SOCKS4,    libopm_proxy_socks4_write,    NULL, 0 },
  { OPM_TYPE_SOCKS5,    libopm_proxy_socks5_write,    NULL, 0 },
  { OPM_TYPE_ROUTER,    libopm_proxy_router_write,    NULL, 0 },
  { OPM_TYPE_WINGATE,   libopm_proxy_wingate_write,   NULL, 0 },
  { OPM_TYPE_HTTPPOST,  libopm_proxy_httppost_write,  NULL, 0 },
  { OPM_TYPE_DREAMBOX,  libopm_proxy_dreambox_write,  NULL, 0 },
  { OPM_TYPE_HTTPS,     libopm_proxy_https_write,     libopm_do_readready_tls, 1 },
  { OPM_TYPE_HTTPSPOST, libopm_proxy_httpspost_write, libopm_do_readready_tls, 1 },
  { OPM_TYPE_SSH,       NULL,                         NULL, 0 }
};

/* opm_create
 *
 *    Initialize a new scanner and return a pointer to it.
 *
 * Parameters:
 *    None
 *
 * Return
 *    Pointer to new OPM_T (scanner)
 */
OPM_T *
opm_create(void)
{
  OPM_T *ret;

  ret = libopm_calloc(sizeof(*ret));
  ret->config = libopm_config_create();

  /* Setup callbacks */
  ret->callbacks = libopm_calloc(sizeof(OPM_CALLBACK_T) * CBLEN);

  return ret;
}

/* opm_remote_create
 *
 *    Create OPM_REMOTE_T struct, fill it with neccessary
 *    default values and return it to the client.
 *
 * Parameters:
 *    ip: IP of remote host
 *
 * Return:
 *    Address of OPM_REMOTE_T created
 *
 */
OPM_REMOTE_T *
opm_remote_create(const char *ip)
{
  OPM_REMOTE_T *ret;

  ret = libopm_calloc(sizeof(*ret));
  ret->ip = libopm_strdup(ip);

  return ret;
}

/* opm_remote_free
 *
 *    Free OPM_REMOTE_T struct and cleanup
 *
 * Parameters:
 *    remote: Struct to free
 *
 * Return:
 *    None
 */
void
opm_remote_free(OPM_REMOTE_T *remote)
{
  OPM_NODE_T *p, *next;
  OPM_PROTOCOL_CONFIG_T *ppc;

  libopm_free(remote->ip);

  LIST_FOREACH_SAFE(p, next, remote->protocols.head)
  {
    ppc = p->data;

    libopm_protocol_config_free(ppc);
    libopm_list_remove(&remote->protocols, p);
    libopm_node_free(p);
  }

  libopm_free(remote);
}

/* opm_callback
 *    Register scanner level callback
 *
 * Parameters
 *    scanner: scanner struct
 *    type:    callback type
 * Return:
 *    Error code
 */

OPM_ERR_T
opm_callback(OPM_T *scanner, int type, OPM_CALLBACK_FUNC *function, void *data)
{
  if (type < 0 || type >= CBLEN)
    return OPM_ERR_CBNOTFOUND;

  scanner->callbacks[type].func = function;
  scanner->callbacks[type].data = data;

  return OPM_SUCCESS;
}

/* opm_free
 *
 *    Free OPM_T (scanner) and cleanup
 *
 * Parameters:
 *    scanner: Address of OPM_T to cleanup
 *
 * Return:
 *    None
 */
void
opm_free(OPM_T *scanner)
{
  OPM_NODE_T *p, *next;
  OPM_PROTOCOL_CONFIG_T *ppc;
  OPM_SCAN_T *scan;

  libopm_config_free(scanner->config);

  LIST_FOREACH_SAFE(p, next, scanner->protocols.head)
  {
    ppc = p->data;

    libopm_protocol_config_free(ppc);
    libopm_list_remove(&scanner->protocols, p);
    libopm_node_free(p);
  }

  LIST_FOREACH_SAFE(p, next, scanner->scans.head)
  {
    scan = p->data;

    libopm_scan_free(scan);
    libopm_list_remove(&scanner->scans, p);
    libopm_node_free(p);
  }

  LIST_FOREACH_SAFE(p, next, scanner->queue.head)
  {
    scan = p->data;

    libopm_scan_free(scan);
    libopm_list_remove(&scanner->queue, p);
    libopm_node_free(p);
  }

  libopm_free(scanner->callbacks);
  libopm_free(scanner);
}

/* opm_config
 *
 *    Wrapper to config_set. Set configuration variables
 *    on the config struct.
 *
 * Parameters:
 *    scanner: OPM_T struct the config struct resides in
 *    key: Variable within the config struct to set
 *    value: Address of value to set variable (key) to
 *
 * Return:
 *    OPM_ERR_T containing error code
 */
OPM_ERR_T
opm_config(OPM_T *scanner, int key, const void *value)
{
  return libopm_config_set(scanner->config, key, value);
}

/* opm_addtype
 *
 *    Add a proxy type and port to the list of protocols
 *    a scanner will use.
 *
 * Parameters:
 *    scanner: pointer to scanner struct
 *    type:    type of proxy to scan (used in hashing to the functions)
 *    port:    port this specific type/protocol will scan on
 * Return:
 *    OPM_SUCCESS: Successful protocol add
 *    OPM_ERR_BADPROTOCOL: Protocol is unknown
 */
OPM_ERR_T
opm_addtype(OPM_T *scanner, int type, unsigned short int port)
{
  OPM_NODE_T *node;
  OPM_PROTOCOL_CONFIG_T *protocol_config;

  for (unsigned int i = 0; i < sizeof(OPM_PROTOCOLS) / sizeof(OPM_PROTOCOL_T); ++i)
  {
    if (type == OPM_PROTOCOLS[i].type)
    {
#ifndef HAVE_LIBCRYPTO
      if (OPM_PROTOCOLS[i].use_tls)
        return OPM_ERR_BADPROTOCOL;
#endif
      protocol_config = libopm_protocol_config_create();
      protocol_config->type = &OPM_PROTOCOLS[i];
      protocol_config->port = port;

      node = libopm_node_create(protocol_config);
      libopm_list_add(&scanner->protocols, node);

      return OPM_SUCCESS;
    }
  }

  return OPM_ERR_BADPROTOCOL;
}

/* opm_remote_addtype
 *
 *    Add a proxy type and port to the list of protocols
 *    a scanner will use.
 *
 * Parameters:
 *    remote: pointer to scanner struct
 *    type:    type of proxy to scan (used in hashing to the functions)
 *    port:    port this specific type/protocol will scan on
 * Return:
 *    OPM_SUCCESS: Successful protocol add
 *    OPM_ERR_BADPROTOCOL: Protocol is unknown
 */
OPM_ERR_T opm_remote_addtype(OPM_REMOTE_T *remote, int type, unsigned short int port)
{
  OPM_NODE_T *node;
  OPM_PROTOCOL_CONFIG_T *protocol_config;

  for (unsigned int i = 0; i < sizeof(OPM_PROTOCOLS) / sizeof(OPM_PROTOCOL_T); ++i)
  {
    if (type == OPM_PROTOCOLS[i].type)
    {
#ifndef HAVE_LIBCRYPTO
      if (OPM_PROTOCOLS[i].use_tls)
        return OPM_ERR_BADPROTOCOL;
#endif
      protocol_config = libopm_protocol_config_create();
      protocol_config->type = &OPM_PROTOCOLS[i];
      protocol_config->port = port;

      node = libopm_node_create(protocol_config);
      libopm_list_add(&remote->protocols, node);

      return OPM_SUCCESS;
    }
  }

  return OPM_ERR_BADPROTOCOL;
}

/* libopm_protocol_config_create
 *
 *    Allocate and return address of a new OPM_PROTOCOL_CONFIG_T
 *
 * Parameters:
 *    None
 *
 * Return:
 *    Address of new OPM_PROTOCOL_CONFIG_T
 */
static OPM_PROTOCOL_CONFIG_T *
libopm_protocol_config_create(void)
{
  OPM_PROTOCOL_CONFIG_T *ret;

  ret = libopm_calloc(sizeof(*ret));

  return ret;
}

/* protocol_config_free
 *
 *    Free OPM_PROTOCOL_CONFIG_T struct
 *
 * Parameters:
 *    protocol: struct to free
 *
 * Return:
 *    None
 */
static void
libopm_protocol_config_free(OPM_PROTOCOL_CONFIG_T *protocol)
{
  libopm_free(protocol);
}

/* opm_scan
 *
 *    Scan remote host. The opm_scan function takes an OPM_REMOTE_T
 *    struct, calculates the in_addr of the remote host, and creates
 *    a scan list based on protocols defined in the scanner.
 *
 * Parameters:
 *    scanner: Scanner to scan host on
 *    remote:  OPM_REMOTE_T defining remote host
 *
 * Return:
 *    (to be written)
 */
OPM_ERR_T
opm_scan(OPM_T *scanner, OPM_REMOTE_T *remote)
{
  OPM_NODE_T *node;  /* Node we'll add scan to when we link it to scans */
  struct addrinfo hints, *res;

  if (LIST_SIZE(&scanner->protocols) == 0 &&
      LIST_SIZE(&remote->protocols) == 0)
    return OPM_ERR_NOPROTOCOLS;

  memset(&hints, 0, sizeof(hints));

  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_NUMERICHOST;

  /*
   * XXX: libopm ideally shouldn't see an IP address in string representation.
   * Could have been stuffed into the _OPM_REMOTE struct by the caller that
   * already does getaddrinfo() anyway.
   */
  if (getaddrinfo(remote->ip, NULL, &hints, &res) || res->ai_family != AF_INET)  /* XXX: only do v4 for now */
  {
    if (res)
      freeaddrinfo(res);

    return OPM_ERR_BADADDR;
  }

  OPM_SCAN_T *scan = libopm_scan_create(scanner, remote);

  memcpy(&scan->addr, res->ai_addr, res->ai_addrlen);
  scan->addr.ss_family = res->ai_family;
  scan->addr_len = res->ai_addrlen;

  freeaddrinfo(res);

  node = libopm_node_create(scan);
  libopm_list_add(&scanner->queue, node);

  return OPM_SUCCESS;
}

/* opm_end
 *
 *    End a scan prematurely.
 *
 * Parameters:
 *    scanner: Scanner to end scan on
 *    remote: Pointer to remote struct to search for and end
 *
 * Return:
 *    No return. OPM_CALLBACK_END will still be called as normal.
 */
void
opm_end(OPM_T *scanner, OPM_REMOTE_T *remote)
{
  OPM_NODE_T *node1, *node2, *next1, *next2;
  OPM_SCAN_T *scan;
  OPM_CONNECTION_T *conn;

  /* End active scans */
  opm_endscan(scanner, remote);

  /*
   * Secondly remove all traces of it in the queue. Once removed we have to call
   * OPM_CALLBACK_END
   */
  LIST_FOREACH_SAFE(node1, next1, scanner->queue.head)
  {
    scan = node1->data;

    if (scan->remote == remote)
    {
      /* Free all connections */
      LIST_FOREACH_SAFE(node2, next2, scan->connections.head)
      {
        conn = node2->data;

        libopm_list_remove(&scan->connections, node2);
        libopm_connection_free(conn);
        libopm_node_free(node2);
        continue;
      }

      /* OPM_CALLBACK_END because check_closed normally handles this */
      libopm_do_callback(scanner, scan->remote, OPM_CALLBACK_END, 0);

      /* Free up the scan */
      libopm_list_remove(&scanner->queue, node1);
      libopm_scan_free(scan);
      libopm_node_free(node1);
    }
  }
}

/* opm_endscan
 *
 *    End a scan prematurely. Only end non-queued scans. This is useful
 *    because it only checks the active scan list (saving time), where
 *    opm_end checks both the scan and the possibly large queue.
 *
 * Parameters:
 *    scanner: Scanner to end scan on
 *    remote: Pointer to remote struct to search for and end
 *
 * Return:
 *    No return. OPM_CALLBACK_END will still be called as normal.
 */
void
opm_endscan(OPM_T *scanner, OPM_REMOTE_T *remote)
{
  OPM_NODE_T *node1, *node2;
  OPM_SCAN_T *scan;
  OPM_CONNECTION_T *conn;

  /*
   * First check to see if it's in the queue, if it is set all connections closed
   * Next cycle of libopm_check_closed will take care of the garbage and handle
   * OPM_CALLBACK_END
   */
  LIST_FOREACH(node1, scanner->scans.head)
  {
    scan = node1->data;

    if (scan->remote == remote)
    {
      LIST_FOREACH(node2, scan->connections.head)
      {
        conn = node2->data;
        conn->state = OPM_STATE_CLOSED;
      }
    }
  }
}

/* opm_active

      Return number of scans in a scanner left.

   Parameters:
      scanner: Scanner to return active scans on

   Return:
      Number of active scans, both queued and active.
*/
size_t
opm_active(OPM_T *scanner)
{
  return LIST_SIZE(&scanner->queue) + LIST_SIZE(&scanner->scans);
}

/* scan_create
 *
 *    Create new OPM_SCAN_T struct
 *
 * Parameters:
 *    scanner: Scanner the scan is being created for. This
 *             is needed to get information on currently set
 *             protocols/config.
 *
 *    remote: Remote host this scan will be scanning
 *
 * Return
 *    Address of new struct
 */
static OPM_SCAN_T *
libopm_scan_create(OPM_T *scanner, OPM_REMOTE_T *remote)
{
  OPM_SCAN_T *ret;
  OPM_CONNECTION_T *conn;
  OPM_NODE_T *node, *p;
#ifdef HAVE_LIBCRYPTO
  static int tls_init = 0;
  static SSL_CTX *ctx_client;

  if (!tls_init)
  {
    tls_init = 1;
    SSLeay_add_ssl_algorithms();

    ctx_client = SSL_CTX_new(SSLv23_client_method());
    if (!ctx_client)
      exit(EXIT_FAILURE);
  }
#endif

  ret = libopm_calloc(sizeof(*ret));
  ret->remote = remote;

  /* Setup list of connections, one for each protocol */
  LIST_FOREACH(p, scanner->protocols.head)
  {
    conn = libopm_connection_create();

    conn->protocol = ((OPM_PROTOCOL_CONFIG_T *)p->data)->type;
    conn->port     = ((OPM_PROTOCOL_CONFIG_T *)p->data)->port;

#ifdef HAVE_LIBCRYPTO
    if (conn->protocol->use_tls)
      /* SSL_new does only fail if OOM in which case HOPM exits anyway */
      conn->tls_handle = SSL_new(ctx_client);
#endif

    node = libopm_node_create(conn);
    libopm_list_add(&ret->connections, node);
  }

  /*
   * Do the same for any specific protocols the remote struct might be configured with
   */
  LIST_FOREACH(p, remote->protocols.head)
  {
    conn = libopm_connection_create();

    conn->protocol = ((OPM_PROTOCOL_CONFIG_T *)p->data)->type;
    conn->port     = ((OPM_PROTOCOL_CONFIG_T *)p->data)->port;

#ifdef HAVE_LIBCRYPTO
    if (conn->protocol->use_tls)
      /* SSL_new does only fail if OOM in which case HOPM exits anyway */
      conn->tls_handle = SSL_new(ctx_client);
#endif

    node = libopm_node_create(conn);
    libopm_list_add(&ret->connections, node);
  }

  return ret;
}

/* scan_free
 *
 *    Free and cleanup OPM_SCAN_T struct
 *
 * Parametsr:
 *    scan: Scan struct to free
 *
 * Return:
 *    None
 */
static void
libopm_scan_free(OPM_SCAN_T *scan)
{
  OPM_NODE_T *p, *next;
  OPM_CONNECTION_T *conn;

  LIST_FOREACH_SAFE(p, next, scan->connections.head)
  {
    conn = p->data;

    libopm_connection_free(conn);
    libopm_list_remove(&scan->connections, p);
    libopm_node_free(p);
  }

  libopm_free(scan);
}

/* connection_create
 *
 *    Allocate new OPM_CONNECTION_T
 *
 * Parameters:
 *    None
 *
 * Return:
 *    Address of new OPM_CONNECTION_T
 */
static OPM_CONNECTION_T *
libopm_connection_create(void)
{
  OPM_CONNECTION_T *ret;

  ret = libopm_calloc(sizeof(*ret));
  ret->state = OPM_STATE_UNESTABLISHED;

  return ret;
}

/* connection_free
 *
 *    Free OPM_CONNECTION_T struct
 *
 * Parameters:
 *    conn: Address of struct to free
 *
 * Return:
 *    None
 */
static void
libopm_connection_free(OPM_CONNECTION_T *conn)
{
  libopm_free(conn);
}

/* opm_cycle
 *
 *   Perform tasks (called by client's loop)
 *
 * Parameters:
 *   None
 *  Return:
 *    None
 */
void
opm_cycle(OPM_T *scanner)
{
  libopm_check_queue(scanner);      /* Move scans from the queue to the live scan list */
  libopm_check_establish(scanner);  /* Make new connections if possible                */
  libopm_check_poll(scanner);       /* Poll connections for IO  and proxy test         */
  libopm_check_closed(scanner);     /* Check for closed or timed out connections       */
}

/* check_queue
 *
 * Move scans from the queue to the live scan list as long as there is
 * room.
 *
 * Parameters:
 *    scanner: Scanner to check queue on
 *
 * Return:
 *    None
 */
static void
libopm_check_queue(OPM_T *scanner)
{
  OPM_NODE_T *node;
  OPM_SCAN_T *scan;
  unsigned int protocols, projected, fd_limit;

  if (LIST_SIZE(&scanner->queue) == 0)
    return;

  fd_limit = *(int *)libopm_config(scanner->config, OPM_CONFIG_FD_LIMIT);
  projected = scanner->fd_use;

  /*
   * We want to keep the live scan list as small as possible, so only move
   * queued scans to the live list if they will not push above fd_limit
   */
  while (LIST_SIZE(&scanner->queue) > 0)
  {
    /* Grab the top scan */
    scan = scanner->queue.head->data;
    protocols = LIST_SIZE(&scan->connections);

    /* Check if it will fit in the live scan list */
    if ((protocols + projected) > fd_limit)
      break;

    /*
     * Scans on the top of the queue were added first, swap the head off the
     * top of the queue and add it to the tail of the live scan list
     */
    node = libopm_list_remove(&scanner->queue, scanner->queue.head);
    libopm_list_add(&scanner->scans, node);
    projected += protocols;
  }
}

/* check_establish
 *
 * Make new connections if there are free file descriptors and connections
 * to be made.
 *
 * Parameters:
 *   scanner: Scanner to check for establish on
 * Return:
 *   None
 */
static void
libopm_check_establish(OPM_T *scanner)
{
  OPM_NODE_T *node1, *node2;
  OPM_SCAN_T *scan;
  OPM_CONNECTION_T *conn;
  unsigned int fd_limit;

  if (LIST_SIZE(&scanner->scans) == 0)
    return;

  fd_limit = *(int *)libopm_config(scanner->config, OPM_CONFIG_FD_LIMIT);

  if (scanner->fd_use >= fd_limit)
    return;

  LIST_FOREACH(node1, scanner->scans.head)
  {
    scan = node1->data;

    LIST_FOREACH(node2, scan->connections.head)
    {
      /* Only scan if we have free file descriptors */
      if (scanner->fd_use >= fd_limit)
        return;

      conn = node2->data;

      if (conn->state == OPM_STATE_UNESTABLISHED)
        libopm_do_connect(scanner, scan, conn);
    }
  }
}

/* check_closed
 *
 * Check for connections which have timed out or are
 * closed. Connections timed out still need to be closed.
 *
 * Remove the connection from the list of connections, free
 * the connection struct and free the list node. Then if this is
 * the last connection of the scan, consider the scan completed and
 * free the scan aswell (and callback that the scan ended).
 *
 * Parameters:
 *   scanner: Scanner to check on
 * Return:
 *   None
 */
static void
libopm_check_closed(OPM_T *scanner)
{
  time_t present;
  int timeout;
  OPM_NODE_T *node1, *node2, *next1, *next2;
  OPM_SCAN_T *scan;
  OPM_CONNECTION_T *conn;

  if (LIST_SIZE(&scanner->scans) == 0)
    return;

  present = opm_gettime();
  timeout = *(int *)libopm_config(scanner->config, OPM_CONFIG_TIMEOUT);

  LIST_FOREACH_SAFE(node1, next1, scanner->scans.head)
  {
    scan = node1->data;

    LIST_FOREACH_SAFE(node2, next2, scan->connections.head)
    {
      conn = node2->data;

      if (conn->state == OPM_STATE_CLOSED)
      {
#ifdef HAVE_LIBCRYPTO
        if (conn->protocol->use_tls)
        {
          SSL_set_shutdown(conn->tls_handle, SSL_RECEIVED_SHUTDOWN);
          if (!SSL_shutdown(conn->tls_handle))
            SSL_shutdown(conn->tls_handle);
          SSL_free(conn->tls_handle);
        }
#endif
        if (conn->fd > -1)
          close(conn->fd);

        scanner->fd_use--;

        libopm_list_remove(&scan->connections, node2);
        libopm_connection_free(conn);
        libopm_node_free(node2);
        continue;
      }

      if (((present - conn->creation) >= timeout) && conn->state != OPM_STATE_UNESTABLISHED)
      {
#ifdef HAVE_LIBCRYPTO
        if (conn->protocol->use_tls)
        {
          SSL_set_shutdown(conn->tls_handle, SSL_RECEIVED_SHUTDOWN);
          if (!SSL_shutdown(conn->tls_handle))
            SSL_shutdown(conn->tls_handle);
          SSL_free(conn->tls_handle);
        }
#endif
        if (conn->fd > -1)
          close(conn->fd);

        scanner->fd_use--;

        libopm_do_callback(scanner, libopm_setup_remote(scan->remote, conn), OPM_CALLBACK_TIMEOUT, 0);
        libopm_list_remove(&scan->connections, node2);
        libopm_connection_free(conn);
        libopm_node_free(node2);
        continue;
      }
    }

    /*
     * No more connections left in this scan, let the client know the scan has
     * ended, then remove the scan from the scanner, and free it up.
     */
    if (LIST_SIZE(&scan->connections) == 0)
    {
      libopm_do_callback(scanner, scan->remote, OPM_CALLBACK_END, 0);
      libopm_list_remove(&scanner->scans, node1);
      libopm_scan_free(scan);
      libopm_node_free(node1);
    }
  }
}

/* do_connect
 *
 * Call socket() and connect() to start a scan.
 *
 * Parametsr:
 *    scan: Scan struct containing the connection
 *    conn: Connection to establish
 * Return:
 *    None
 */
static void
libopm_do_connect(OPM_T * scanner, OPM_SCAN_T *scan, OPM_CONNECTION_T *conn)
{
  assert(scan->addr.ss_family == AF_INET);

  if (scan->addr.ss_family == AF_INET6)
    ((struct sockaddr_in6 *)&scan->addr)->sin6_port = htons(conn->port);
  else
    ((struct sockaddr_in *)&scan->addr)->sin_port = htons(conn->port);

  scanner->fd_use++;  /* Increase file descriptor use */

  conn->fd = socket(scan->addr.ss_family, SOCK_STREAM, 0);
  if (conn->fd == -1)
  {
    libopm_do_callback(scanner, libopm_setup_remote(scan->remote, conn), OPM_CALLBACK_ERROR, OPM_ERR_NOFD);
    conn->state = OPM_STATE_CLOSED;
    return;
  }

  struct sockaddr_storage *bind_ip = libopm_config(scanner->config, OPM_CONFIG_BIND_IP);
  if (bind_ip)
  {
    size_t addr_len;

    if (bind_ip->ss_family == AF_INET6)
    {
      struct sockaddr_in6 *in = (struct sockaddr_in6 *)bind_ip;
      in->sin6_port = htons(0);
      addr_len = sizeof(*in);
    }
    else
    {
      struct sockaddr_in *in = (struct sockaddr_in *)bind_ip;
      in->sin_port = htons(0);
      addr_len = sizeof(*in);
    }

    if (bind(conn->fd, (const struct sockaddr *)bind_ip, addr_len) == -1)
    {
      libopm_do_callback(scanner, libopm_setup_remote(scan->remote, conn), OPM_CALLBACK_ERROR, OPM_ERR_BIND);
      conn->state = OPM_STATE_CLOSED;
      return;
    }
  }

  /* Set socket non blocking */
  fcntl(conn->fd, F_SETFL, O_NONBLOCK);

  connect(conn->fd, (const struct sockaddr *)&scan->addr, scan->addr_len);

#ifdef HAVE_LIBCRYPTO
  if (conn->protocol->use_tls)
    SSL_set_fd(conn->tls_handle, conn->fd);
#endif

  conn->state = OPM_STATE_ESTABLISHED;
  conn->creation = opm_gettime();  /* Stamp creation time, for timeout */
}

/* check_poll
 *
 * Check sockets for ready read/write
 *
 * Parameters:
 *    scanner: Scanner to isolate check on
 * Return:
 *    None
 */
static void
libopm_check_poll(OPM_T *scanner)
{
  OPM_NODE_T *node1, *node2;
  OPM_SCAN_T *scan;
  OPM_CONNECTION_T *conn;
  unsigned int size = 0;
  static unsigned int ufds_size;
  static struct pollfd *ufds = NULL;

  /* Grow pollfd array (ufds) as needed */
  if (ufds_size < (*(unsigned int *)libopm_config(scanner->config, OPM_CONFIG_FD_LIMIT)))
  {
    libopm_free(ufds);

    ufds = libopm_calloc((sizeof *ufds) * (*(unsigned int *)libopm_config(scanner->config, OPM_CONFIG_FD_LIMIT)));
    ufds_size = (*(unsigned int *)libopm_config(scanner->config, OPM_CONFIG_FD_LIMIT));
  }

  if (LIST_SIZE(&scanner->scans) == 0)
    return;

  LIST_FOREACH(node1, scanner->scans.head)
  {
    scan = node1->data;

    LIST_FOREACH(node2, scan->connections.head)
    {
      if (size >= ufds_size)
        break;

      conn = node2->data;

      if (conn->state < OPM_STATE_ESTABLISHED ||
          conn->state == OPM_STATE_CLOSED)
        continue;

      ufds[size].events = 0;
      ufds[size].revents = 0;
      ufds[size].fd = conn->fd;

      /* Check for HUNG UP. */
      ufds[size].events |= POLLHUP;
      /* Check for INVALID FD */
      ufds[size].events |= POLLNVAL;

      switch (conn->state)
      {
        case OPM_STATE_ESTABLISHED:
          ufds[size].events |= POLLOUT;
          break;
        case OPM_STATE_NEGSENT:
          ufds[size].events |= POLLIN;
          break;
      }

      size++;
    }
  }

  switch (poll(ufds, size, 0))
  {
    case -1:
      /* error in select/poll */
      return;
    case 0:
      /* Nothing to do */
      return;

    /* Pass pointer to connection to handler. */
  }

  LIST_FOREACH(node1, scanner->scans.head)
  {
    scan = node1->data;

    LIST_FOREACH(node2, scan->connections.head)
    {
      conn = node2->data;

      for (unsigned int i = 0; i < size; ++i)
      {
        if ((ufds[i].fd == conn->fd) && (conn->state != OPM_STATE_CLOSED))
        {
          if (ufds[i].revents & POLLIN)
            libopm_do_readready(scanner, scan, conn);
          if (ufds[i].revents & POLLOUT)
            libopm_do_writeready(scanner, scan, conn);
          if (ufds[i].revents & POLLHUP)
            libopm_do_hup(scanner, scan, conn);
        }
      }
    }
  }
}

static int
libopm_do_readready_tls(OPM_T *scanner, OPM_SCAN_T *scan, OPM_CONNECTION_T *conn)
{
#ifdef HAVE_LIBCRYPTO
  int max_read, length;
  char readbuf[LIBOPM_TLS_RECORD_SIZE];

  if (!SSL_is_init_finished(conn->tls_handle))
    return 0;

  if ((length = SSL_read(conn->tls_handle, readbuf, sizeof(readbuf))) <= 0)
  {
    switch (SSL_get_error(conn->tls_handle, length))
    {
      /* TBD: possibly could recover here from some errors */ 
      default:
        libopm_do_hup(scanner, scan, conn);
        return 0;
    }
  }

  max_read = *(int *)libopm_config(scanner->config, OPM_CONFIG_MAX_READ);

  for (const char *p = readbuf, *end = readbuf + length; p < end; ++p)
  {
    conn->bytes_read++;

    if (conn->bytes_read >= max_read)
    {
      libopm_do_callback(scanner, libopm_setup_remote(scan->remote, conn), OPM_CALLBACK_ERROR, OPM_ERR_MAX_READ);
      conn->state = OPM_STATE_CLOSED;
      return 0;
    }

    if (*p == '\0' || *p == '\r')
      continue;

    if (*p == '\n')
    {
      conn->readbuf[conn->readlen] = '\0';
      conn->readlen = 0;

      libopm_do_read(scanner, scan, conn);

      if (conn->state == OPM_STATE_CLOSED)
        return 0;

      continue;
    }

    if (conn->readlen < READBUFLEN)
      conn->readbuf[++(conn->readlen) - 1] = *p;  /* -1 to pad for null term */
  }
#endif
  return 0;
}

/* do_readready
 *
 *    Remote connection is read ready, read the data into a buffer and check it against
 *    the target_string if neccessary
 *
 *    Parameters:
 *       scanner: Scanner doing the scan
 *       scan: Specific scan
 *       conn: Specific connection in the scan
 *
 *    Return:
 *       None
 */
static void
libopm_do_readready(OPM_T *scanner, OPM_SCAN_T *scan, OPM_CONNECTION_T *conn)
{
  int max_read;
  char c;

  /*
   * If protocol has a specific read function, call that instead of
   * reading data from here.
   */
  if (conn->protocol->read_function)
  {
    conn->protocol->read_function(scanner, scan, conn);
    return;
  }

  max_read = *(int *)libopm_config(scanner->config, OPM_CONFIG_MAX_READ);

  while (1)
  {
    switch (read(conn->fd, &c, 1))
    {
      case 0:
        libopm_do_hup(scanner, scan, conn);
        return;

      case -1:
        if (errno != EAGAIN)
          libopm_do_hup(scanner, scan, conn);
        return;

      default:
        conn->bytes_read++;

        if (conn->bytes_read >= max_read)
        {
          libopm_do_callback(scanner, libopm_setup_remote(scan->remote, conn), OPM_CALLBACK_ERROR, OPM_ERR_MAX_READ);
          conn->state = OPM_STATE_CLOSED;
          return;
        }

        if (c == '\0' || c == '\r')
          continue;

        if (c == '\n')
        {
          conn->readbuf[conn->readlen] = '\0';
          conn->readlen = 0;

          libopm_do_read(scanner, scan, conn);

          if (conn->state == OPM_STATE_CLOSED)
            return;

          continue;
        }

        if (conn->readlen < READBUFLEN)
          conn->readbuf[++(conn->readlen) - 1] = c;  /* -1 to pad for null term */
    }
  }
}

/* do_read
 *
 *    A line of data has been read from the socket, check it against
 *    target string.
 *
 *
 *
 *    Parameters:
 *       scanner: Scanner doing the scan
 *       scan: Specific scan
 *       conn: Specific connection in the scan
 *
 *    Return:
 *       None
 */
static void
libopm_do_read(OPM_T *scanner, OPM_SCAN_T *scan, OPM_CONNECTION_T *conn)
{
  OPM_LIST_T *list;
  OPM_NODE_T *node;

  /* Check readbuf against target strings */
  list = (OPM_LIST_T *)libopm_config(scanner->config, OPM_CONFIG_TARGET_STRING);

  LIST_FOREACH(node, list->head)
  {
    const char *target_string = node->data;

    if (strstr(conn->readbuf, target_string))
    {
      libopm_do_openproxy(scanner, scan, conn);
      break;
    }
  }
}

/* do_openproxy
 *
 *    An open proxy was found on connection conn. Cleanup the connection and
 *    call the appropriate callback to let the client know the proxy was found.
 *
 *    Parameters:
 *       scanner: Scanner doing the scan
 *       scan: Specific scan
 *       conn: Specific connection in the scan
 *
 *    Return:
 *       None
 */
static void
libopm_do_openproxy(OPM_T *scanner, OPM_SCAN_T *scan, OPM_CONNECTION_T *conn)
{
  /* Mark the connection for close */
  conn->state = OPM_STATE_CLOSED;

  /* Call client's open proxy callback */
  libopm_do_callback(scanner, libopm_setup_remote(scan->remote, conn), OPM_CALLBACK_OPENPROXY, 0);
}

/*  do_writeready
 *
 *    Remote connection is write ready, call the specific protocol
 *    function for writing to this socket.
 *
 *    Parameters:
 *       scanner: Scanner doing the scan
 *       scan: Specific scan
 *       conn: Specific connection in the scan
 *
 *    Return:
 *       None
 */
static void
libopm_do_writeready(OPM_T *scanner, OPM_SCAN_T *scan, OPM_CONNECTION_T *conn)
{
  OPM_PROTOCOL_T *protocol;

#ifdef HAVE_LIBCRYPTO
  if (conn->protocol->use_tls)
  {
    if (!SSL_is_init_finished(conn->tls_handle))
    {
      SSL_connect(conn->tls_handle);
      return;
    }
  }
#endif

  protocol = conn->protocol;

  /* Call write function for specific protocol */
  if (protocol->write_function)
    protocol->write_function(scanner, scan, conn);

  /* Flag as NEGSENT so we don't have to send data again*/
  conn->state = OPM_STATE_NEGSENT;
}

/* do_hup
 *
 *    Connection ended prematurely
 *
 * Parameters:
 *       scanner: Scanner doing the scan
 *       scan: Specific scan
 *       conn: Specific connection in the scan
 *       error: OPM_ERR_T containing the error type
 * Return:
 *       None
 */
static void
libopm_do_hup(OPM_T *scanner, OPM_SCAN_T *scan, OPM_CONNECTION_T *conn)
{
  /* Mark the connection for close */
  conn->state = OPM_STATE_CLOSED;

  libopm_do_callback(scanner, libopm_setup_remote(scan->remote, conn), OPM_CALLBACK_NEGFAIL, 0);
}

/* do_callback
 *
 *    Call callback
 *
 * Parameters:
 *    scanner: scanner remote is on
 *    remote:  remote callback is for
 *    type:    callback type
 *    var:     optional var passed back (error codes, etc )
 * Return:
 *    None
 */
static void
libopm_do_callback(OPM_T *scanner, OPM_REMOTE_T *remote, int type, int var)
{
  /* Callback is out of range */
  if (type < 0 || type >= CBLEN)
    return;

  if (scanner->callbacks[type].func)
    (scanner->callbacks[type].func)(scanner, remote, var, scanner->callbacks[type].data);
}

/* setup_remote
 *
 * Setup an OPM_REMOTE_T with information from an OPM_CONNECTION_T
 * for callback
 *
 * Parameters:
 *    remote, conn
 *
 * Return:
 *    remote
 */
static OPM_REMOTE_T *
libopm_setup_remote(OPM_REMOTE_T *remote, OPM_CONNECTION_T *conn)
{
  remote->port = conn->port;
  remote->bytes_read = conn->bytes_read;
  remote->protocol = conn->protocol->type;

  return remote;
}