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
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/net/sshclients.h"
#include "gate/net/nameresolvers.h"
#include "gate/results.h"
#include "gate/debugging.h"
#if defined(GATE_EXTLIB_LIBSSH2)
# define GATE_NET_SSHCLIENTS_LIBSSH2_IMPL 1
# if !defined(GATE_NET_LIBSSH2_STATIC) && defined(GATE_SYS_WIN) && !defined(GATE_SYS_WINSTORE)
# define GATE_NET_LIBSSH2_STATIC 1
# endif
#else
# define GATE_NET_SSHCLIENTS_NO_IMPL 1
#endif
#if defined(GATE_NET_SSHCLIENTS_LIBSSH2_IMPL)
#if defined(_WINDLL)
# undef _WINDLL
#endif
#include <libssh2.h>
#include <libssh2_sftp.h>
#include <stdlib.h>
#include "gate/libraries.h"
#include "gate/platforms.h"
#include "gate/net/sockets.h"
#include "gate/net/sslsessions.h"
# if defined(GATE_SYS_WINCE)
# pragma comment(lib, "ws2.lib")
# endif
#define GATE_SSH2_SESSION_TIMEOUT_DEFAULT 15000
typedef struct libssh2_functions
{
int (*ssh2_init) (int flags);
void (*ssh2_exit) (void);
void (*ssh2_free) (LIBSSH2_SESSION* session, void* ptr);
char* (*ssh2_userauth_list) (LIBSSH2_SESSION* session, const char* user, unsigned int user_len);
int (*ssh2_userauth_password_ex)(LIBSSH2_SESSION* session, const char* username, unsigned int username_len, const char* password, unsigned int password_len, LIBSSH2_PASSWD_CHANGEREQ_FUNC((*passwd_change_cb)));
int (*ssh2_userauth_keyboard_interactive_ex)(LIBSSH2_SESSION* session, const char* user, unsigned int user_len, LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC((*response_callback)));
int (*ssh2_userauth_publickey_frommemory)(LIBSSH2_SESSION* session, const char* user, size_t user_len, const char* publickeydata, size_t publickeydata_len, const char* privatekeydata, size_t privatekeydata_len, const char* passphrase);
int (*ssh2_userauth_publickey_fromfile_ex)(LIBSSH2_SESSION* session, const char* user, unsigned int user_len, const char* publickey, const char* privatekey, const char* passphrase);
int (*ssh2_session_disconnect_ex)(LIBSSH2_SESSION* session, int reason, const char* description, const char* lang);
LIBSSH2_SESSION* (*ssh2_session_init_ex) (LIBSSH2_ALLOC_FUNC((*my_alloc)), LIBSSH2_FREE_FUNC((*my_free)), LIBSSH2_REALLOC_FUNC((*my_realloc)), void* abstract);
int (*ssh2_session_free) (LIBSSH2_SESSION* session);
/* int (*ssh2_session_startup) (LIBSSH2_SESSION* session, int sock); */
int (*ssh2_session_handshake) (LIBSSH2_SESSION* session, libssh2_socket_t sock);
void (*ssh2_session_set_blocking)(LIBSSH2_SESSION* session, int blocking);
int (*ssh2_session_last_errno) (LIBSSH2_SESSION* session);
int (*ssh2_session_last_error) (LIBSSH2_SESSION* session, char** errmsg, int* errmsg_len, int want_buf);
void (*ssh2_session_set_timeout) (LIBSSH2_SESSION* session, long timeout);
int (*ssh2_sftp_close_handle) (LIBSSH2_SFTP_HANDLE* hnd);
int (*ssh2_sftp_fstat_ex) (LIBSSH2_SFTP_HANDLE* hnd, LIBSSH2_SFTP_ATTRIBUTES* attrs, int setstat);
LIBSSH2_SFTP* (*ssh2_sftp_init) (LIBSSH2_SESSION* session);
unsigned long (*ssh2_sftp_last_error) (LIBSSH2_SFTP* sftp);
int (*ssh2_sftp_mkdir_ex) (LIBSSH2_SFTP* sftp, const char* path, unsigned int path_len, long mode);
LIBSSH2_SFTP_HANDLE*(*ssh2_sftp_open_ex) (LIBSSH2_SFTP* sftp, const char* filename, unsigned int filename_len, unsigned long flags, long mode, int open_type);
ssize_t (*ssh2_sftp_read) (LIBSSH2_SFTP_HANDLE* hnd, char* buffer, size_t buffer_maxlen);
int (*ssh2_sftp_readdir_ex) (LIBSSH2_SFTP_HANDLE* hnd, char* buffer, size_t buffer_maxlen, char* longentry, size_t longentry_maxlen, LIBSSH2_SFTP_ATTRIBUTES* attrs);
int (*ssh2_sftp_rename_ex) (LIBSSH2_SFTP* sftp, const char* source_filename, unsigned int source_filename_len, const char* dest_filename, unsigned int dest_filename_len, long flags);
int (*ssh2_sftp_rmdir_ex) (LIBSSH2_SFTP* sftp, const char* path, unsigned int path_len);
void (*ssh2_sftp_seek64) (LIBSSH2_SFTP_HANDLE* handle, libssh2_uint64_t offset);
int (*ssh2_sftp_stat_ex) (LIBSSH2_SFTP* sftp, const char* path, unsigned int path_len, int stat_type, LIBSSH2_SFTP_ATTRIBUTES* attrs);
int (*ssh2_sftp_shutdown) (LIBSSH2_SFTP* sftp);
int (*ssh2_sftp_statvfs) (LIBSSH2_SFTP* sftp, const char* path, size_t path_len, LIBSSH2_SFTP_STATVFS* st);
int (*ssh2_sftp_symlink_ex) (LIBSSH2_SFTP* sftp, const char* path, unsigned int path_len, char* target, unsigned int target_len, int link_type);
size_t (*ssh2_sftp_tell) (LIBSSH2_SFTP_HANDLE* handle);
libssh2_uint64_t (*ssh2_sftp_tell64) (LIBSSH2_SFTP_HANDLE* handle);
int (*ssh2_sftp_unlink_ex) (LIBSSH2_SFTP* sftp, const char* filename, unsigned int filename_len);
ssize_t (*ssh2_sftp_write) (LIBSSH2_SFTP_HANDLE* hnd, const char* buffer, size_t count);
LIBSSH2_CHANNEL* (*ssh2_channel_open_ex) (LIBSSH2_SESSION* session, const char* channel_type, unsigned int channel_type_len, unsigned int window_size, unsigned int packet_size, const char* message, unsigned int message_len);
int (*ssh2_channel_close) (LIBSSH2_CHANNEL* channel);
int (*ssh2_channel_free) (LIBSSH2_CHANNEL* channel);
ssize_t (*ssh2_channel_read_ex) (LIBSSH2_CHANNEL* channel, int stream_id, char* buf, size_t buflen);
ssize_t (*ssh2_channel_write_ex) (LIBSSH2_CHANNEL* channel, int stream_id, char const* buf, size_t buflen);
int (*ssh2_channel_process_startup) (LIBSSH2_CHANNEL* channel, const char* request, unsigned int request_len, const char* message, unsigned int message_len);
int (*ssh2_channel_eof) (LIBSSH2_CHANNEL* channel);
int (*ssh2_channel_send_eof) (LIBSSH2_CHANNEL* channel);
int (*ssh2_channel_wait_eof) (LIBSSH2_CHANNEL* channel);
int (*ssh2_channel_setenv_ex) (LIBSSH2_CHANNEL* channel, char const* varname, unsigned int varname_len, const char* value, unsigned int value_len);
int (*ssh2_channel_get_exit_signal) (LIBSSH2_CHANNEL* channel, char** exitsignal, size_t* exitsignal_len, char** errmsg, size_t* errmsg_len, char** langtag, size_t* langtag_len);
int (*ssh2_channel_get_exit_status) (LIBSSH2_CHANNEL* channel);
void (*ssh2_channel_set_blocking) (LIBSSH2_CHANNEL* channel, int blocking);
} libssh2_functions_t;
static libssh2_functions_t lib;
static volatile gate_bool_t lib_loaded;
static gate_bool_t load_libssh2()
{
gate_result_t result;
result = gate_ssl_init(true);
if (GATE_FAILED(result))
{
return false;
}
#if defined(GATE_NET_LIBSSH2_STATIC)
if (!lib_loaded)
{
lib.ssh2_init = &libssh2_init;
lib.ssh2_exit = &libssh2_exit;
lib.ssh2_free = &libssh2_free;
lib.ssh2_userauth_list = &libssh2_userauth_list;
lib.ssh2_userauth_password_ex = &libssh2_userauth_password_ex;
lib.ssh2_userauth_keyboard_interactive_ex = &libssh2_userauth_keyboard_interactive_ex;
lib.ssh2_userauth_publickey_frommemory = &libssh2_userauth_publickey_frommemory;
lib.ssh2_userauth_publickey_fromfile_ex = &libssh2_userauth_publickey_fromfile_ex;
lib.ssh2_session_set_blocking = &libssh2_session_set_blocking;
lib.ssh2_session_disconnect_ex = &libssh2_session_disconnect_ex;
lib.ssh2_session_free = &libssh2_session_free;
lib.ssh2_session_init_ex = &libssh2_session_init_ex;
/* lib.ssh2_session_startup = &libssh2_session_startup; */
lib.ssh2_session_handshake = &libssh2_session_handshake;
lib.ssh2_session_last_errno = &libssh2_session_last_errno;
lib.ssh2_session_last_error = &libssh2_session_last_error;
lib.ssh2_session_set_timeout = &libssh2_session_set_timeout;
lib.ssh2_sftp_close_handle = &libssh2_sftp_close_handle;
lib.ssh2_sftp_fstat_ex = &libssh2_sftp_fstat_ex;
lib.ssh2_sftp_init = &libssh2_sftp_init;
lib.ssh2_sftp_last_error = &libssh2_sftp_last_error;
lib.ssh2_sftp_mkdir_ex = &libssh2_sftp_mkdir_ex;
lib.ssh2_sftp_open_ex = &libssh2_sftp_open_ex;
lib.ssh2_sftp_read = &libssh2_sftp_read;
lib.ssh2_sftp_readdir_ex = &libssh2_sftp_readdir_ex;
lib.ssh2_sftp_rename_ex = &libssh2_sftp_rename_ex;
lib.ssh2_sftp_rmdir_ex = &libssh2_sftp_rmdir_ex;
lib.ssh2_sftp_seek64 = &libssh2_sftp_seek64;
lib.ssh2_sftp_shutdown = &libssh2_sftp_shutdown;
lib.ssh2_sftp_stat_ex = &libssh2_sftp_stat_ex;
lib.ssh2_sftp_statvfs = &libssh2_sftp_statvfs;
lib.ssh2_sftp_symlink_ex = &libssh2_sftp_symlink_ex;
lib.ssh2_sftp_tell = &libssh2_sftp_tell;
lib.ssh2_sftp_tell64 = &libssh2_sftp_tell64;
lib.ssh2_sftp_unlink_ex = &libssh2_sftp_unlink_ex;
lib.ssh2_sftp_write = &libssh2_sftp_write;
lib.ssh2_channel_open_ex = &libssh2_channel_open_ex;
lib.ssh2_channel_close = &libssh2_channel_close;
lib.ssh2_channel_free = &libssh2_channel_free;
lib.ssh2_channel_read_ex = &libssh2_channel_read_ex;
lib.ssh2_channel_write_ex = &libssh2_channel_write_ex;
lib.ssh2_channel_process_startup = &libssh2_channel_process_startup;
lib.ssh2_channel_eof = &libssh2_channel_eof;
lib.ssh2_channel_send_eof = &libssh2_channel_send_eof;
lib.ssh2_channel_wait_eof = &libssh2_channel_wait_eof;
lib.ssh2_channel_setenv_ex = &libssh2_channel_setenv_ex;
lib.ssh2_channel_get_exit_signal = &libssh2_channel_get_exit_signal;
lib.ssh2_channel_get_exit_status = &libssh2_channel_get_exit_status;
lib.ssh2_channel_set_blocking = &libssh2_channel_set_blocking;
lib_loaded = true;
}
#else
gate_library_t lib_ssh2 = NULL;
static gate_string_t libssh2_name =
#if defined(GATE_SYS_DARWIN)
GATE_STRING_INIT_STATIC("libssh2.dylib")
#elif defined(GATE_SYS_POSIX)
GATE_STRING_INIT_STATIC("libssh2.so")
#else
GATE_STRING_INIT_STATIC("libssh2")
#endif
;
if (!lib_loaded)
{
do
{
result = gate_library_open(&libssh2_name, &lib_ssh2, 0);
GATE_BREAK_IF_FAILED(result);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_init", &lib.ssh2_init);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_exit", &lib.ssh2_exit);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_free", &lib.ssh2_free);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_userauth_list", &lib.ssh2_userauth_list);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_userauth_password_ex", &lib.ssh2_userauth_password_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_userauth_keyboard_interactive_ex", &lib.ssh2_userauth_keyboard_interactive_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_userauth_publickey_frommemory", &lib.ssh2_userauth_publickey_frommemory);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_userauth_publickey_fromfile_ex", &lib.ssh2_userauth_publickey_fromfile_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_session_set_blocking", &lib.ssh2_session_set_blocking);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_session_disconnect_ex", &lib.ssh2_session_disconnect_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_session_free", &lib.ssh2_session_free);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_session_init_ex", &lib.ssh2_session_init_ex);
/* result |= gate_library_get_function_name(lib_ssh2, "libssh2_session_startup", &lib.ssh2_session_startup); */
result |= gate_library_get_function_name(lib_ssh2, "libssh2_session_handshake", &lib.ssh2_session_handshake);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_session_last_errno", &lib.ssh2_session_last_errno);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_session_last_error", &lib.ssh2_session_last_error);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_session_set_timeout", &lib.ssh2_session_set_timeout);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_close_handle", &lib.ssh2_sftp_close_handle);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_fstat_ex", &lib.ssh2_sftp_fstat_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_init", &lib.ssh2_sftp_init);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_last_error", &lib.ssh2_sftp_last_error);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_mkdir_ex", &lib.ssh2_sftp_mkdir_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_open_ex", &lib.ssh2_sftp_open_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_read", &lib.ssh2_sftp_read);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_readdir_ex", &lib.ssh2_sftp_readdir_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_rename_ex", &lib.ssh2_sftp_rename_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_rmdir_ex", &lib.ssh2_sftp_rmdir_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_seek64", &lib.ssh2_sftp_seek64);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_shutdown", &lib.ssh2_sftp_shutdown);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_stat_ex", &lib.ssh2_sftp_stat_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_statvfs", &lib.ssh2_sftp_statvfs);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_symlink_ex", &lib.ssh2_sftp_symlink_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_tell", &lib.ssh2_sftp_tell);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_tell64", &lib.ssh2_sftp_tell64);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_unlink_ex", &lib.ssh2_sftp_unlink_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_sftp_write", &lib.ssh2_sftp_write);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_channel_open_ex", &lib.ssh2_channel_open_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_channel_close", &lib.ssh2_channel_close);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_channel_free", &lib.ssh2_channel_free);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_channel_read_ex", &lib.ssh2_channel_read_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_channel_write_ex", &lib.ssh2_channel_write_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_channel_process_startup", &lib.ssh2_channel_process_startup);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_channel_eof", &lib.ssh2_channel_eof);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_channel_send_eof", &lib.ssh2_channel_send_eof);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_channel_wait_eof", &lib.ssh2_channel_wait_eof);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_channel_setenv_ex", &lib.ssh2_channel_setenv_ex);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_channel_get_exit_signal", &lib.ssh2_channel_get_exit_signal);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_channel_get_exit_status", &lib.ssh2_channel_get_exit_status);
result |= gate_library_get_function_name(lib_ssh2, "libssh2_channel_set_blocking", &lib.ssh2_channel_set_blocking);
GATE_BREAK_IF_FAILED(result);
/* success case: */
lib_ssh2 = NULL; /* do not unload library */
lib_loaded = true;
} while (0);
if (lib_ssh2)
{
gate_library_close(lib_ssh2);
}
}
#endif
return lib_loaded;
}
static void* gate_libssh2_alloc(size_t count, void** ptr_abstract)
{
(void)ptr_abstract;
return gate_mem_alloc(count);
}
static void gate_libssh2_free(void* ptr, void** ptr_abstract)
{
(void)ptr_abstract;
if (ptr)
{
gate_mem_dealloc(ptr);
}
}
static void* gate_libssh2_realloc(void* ptr, size_t count, void** ptr_abstract)
{
(void)ptr_abstract;
return gate_mem_realloc(ptr, count);
}
gate_result_t gate_sshclient_create(gate_sshclient_t* client, gate_string_t const* server, gate_uint16_t port)
{
gate_result_t ret;
int errcode;
gate_bool_t init_ok = false;
LIBSSH2_SESSION* session = NULL;
gate_string_t host = GATE_STRING_INIT_EMPTY;
do
{
gate_mem_clear(client, sizeof(gate_sshclient_t));
if (!load_libssh2())
{
GATE_DEBUG_TRACE("load_libssh2() failed");
ret = GATE_RESULT_NOTAVAILABLE;
break;
}
ret = gate_platform_lock();
GATE_BREAK_IF_FAILED(ret);
do
{
errcode = lib.ssh2_init(0);
if (errcode != 0)
{
GATE_DEBUG_TRACE("ssh2_init() failed");
ret = GATE_RESULT_FAILED;
break;
}
} while (0);
gate_platform_unlock();
GATE_BREAK_IF_FAILED(ret);
ret = gate_socket_parse_address(server, &host, &client->port, &client->socket_family);
GATE_BREAK_IF_FAILED(ret);
if (port)
{
client->port = port;
}
if (!gate_net_is_ipv4_address(&host) && !gate_net_is_ipv6_address(&host))
{
gate_socket_endpoint_t ep;
gate_size_t ep_count = 1;
gate_result_t result = gate_net_resolve_host(&host, GATE_SOCKET_FAMILY_INET4, &ep, &ep_count);
gate_string_t newhost = GATE_STRING_INIT_EMPTY;
if (GATE_FAILED(result) || (ep_count < 1))
{
ret = GATE_RESULT_BADADDRESS;
break;
}
result = gate_socket_print_endpoint(&ep, &newhost);
GATE_BREAK_IF_FAILED(result);
gate_string_release(&host);
gate_mem_copy(&host, &newhost, sizeof(gate_string_t));
}
gate_string_to_buffer(&host, client->server, sizeof(client->server));
init_ok = true;
session = lib.ssh2_session_init_ex(&gate_libssh2_alloc, &gate_libssh2_free, &gate_libssh2_realloc, client);
if (!session)
{
GATE_DEBUG_TRACE("ssh2_session_init_ex() failed");
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
lib.ssh2_session_set_blocking(session, 1);
lib.ssh2_session_set_timeout(session, GATE_SSH2_SESSION_TIMEOUT_DEFAULT);
/* success state reached */
client->handles[0] = session;
client->handles[1] = (void*)GATE_SOCKET_INVALID; /* no socket connected */
ret = GATE_RESULT_OK;
} while (0);
gate_string_release(&host);
return ret;
}
static gate_string_t const authlist_entry_password = GATE_STRING_INIT_STATIC("password");
static gate_string_t const authlist_entry_keyboard = GATE_STRING_INIT_STATIC("keyboard-interactive");
static void gate_sshclient_kbd_callback(
const char* name, int name_len,
const char* instruction, int instruction_len,
int num_prompts,
const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts,
LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses,
void** abstract)
{
gate_sshclient_t** pptr_client = (gate_sshclient_t**)abstract;
gate_string_t const* pass = (pptr_client && *pptr_client) ? (gate_string_t const*)((*pptr_client)->handles[2]) : NULL;
char* buffer = NULL;
(void)name;
(void)name_len;
(void)instruction;
(void)instruction_len;
(void)prompts;
if ((num_prompts == 1) && (pass != NULL))
{
gate_size_t pass_length = gate_string_length(pass);
buffer = (char*)malloc(pass_length + 3);
if (buffer)
{
gate_size_t len_copied = gate_string_to_buffer(pass, buffer, pass_length + 1);
buffer[len_copied] = 0;
responses->text = buffer;
responses->length = (unsigned int)len_copied;
}
}
} /* kbd_callback */
#define GATE_DEBUG_TRACE_SSH2_ERROR(session_ptr, origin_msg) do { \
char* ssh2_msg = NULL; \
int ssh2_msg_len = 0; \
int const ssh2_errno = lib.ssh2_session_last_error(session_ptr, &ssh2_msg, &ssh2_msg_len, 0); \
GATE_DEBUG_TRACE(origin_msg); \
GATE_DEBUG_TRACE_MSG_VALUE(ssh2_msg, ssh2_errno); \
} while(0)
static gate_result_t gate_sshclient_init_connection(gate_sshclient_t* client)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_socket_t socket_handle = GATE_SOCKET_INVALID;
gate_socket_endpoint_t ep;
gate_string_t host = GATE_STRING_INIT_EMPTY;
LIBSSH2_SESSION* session = (LIBSSH2_SESSION*)client->handles[0];
int ssh2_result;<--- The scope of the variable 'ssh2_result' can be reduced. [+]The scope of the variable 'ssh2_result' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
do
{
if (!session)
{
GATE_DEBUG_TRACE("Invalid state: no client session created");
ret = GATE_RESULT_INVALIDSTATE;
break;
}
if (client->handles[1] != (void*)GATE_SOCKET_INVALID)
{
GATE_DEBUG_TRACE("Invalid state: socket already opened, cannot connect");
ret = GATE_RESULT_INVALIDSTATE;
break;
}
ret = gate_socket_create_ex(client->socket_family, GATE_SOCKET_MSGTYPE_STREAM, GATE_SOCKET_PROTOCOL_TCP, &socket_handle);
GATE_BREAK_IF_FAILED(ret);
gate_mem_clear(&ep, sizeof(ep));
switch (client->socket_family)
{
case GATE_SOCKET_FAMILY_INET4:
{
ep.ip4.family = GATE_SOCKET_FAMILY_INET4;
ep.ip4.port = client->port;
gate_string_create_static(&host, client->server);
ret = gate_socket_parse_ip4(&host, &ep.ip4.address);
break;
}
case GATE_SOCKET_FAMILY_INET6:
{
ep.ip6.family = GATE_SOCKET_FAMILY_INET6;
ep.ip6.port = client->port;
ep.ip6.scope = 0;
gate_string_create_static(&host, client->server);
ret = gate_socket_parse_ip6(&host, &ep.ip6.address);
break;
}
default:
{
ret = GATE_RESULT_NOTSUPPORTED;
break;
}
}
GATE_BREAK_IF_FAILED(ret);
gate_socket_set(socket_handle, GATE_SOCKET_OPTION_BLOCKING, 1);
gate_socket_set(socket_handle, GATE_SOCKET_OPTION_RECEIVETIMEOUT, GATE_SSH2_SESSION_TIMEOUT_DEFAULT);
gate_socket_set(socket_handle, GATE_SOCKET_OPTION_SENDTIMEOUT, GATE_SSH2_SESSION_TIMEOUT_DEFAULT);
ret = gate_socket_connect(socket_handle, &ep);
GATE_BREAK_IF_FAILED(ret);
GATE_DEBUG_TRACE_VALUE((int)socket_handle);
lib.ssh2_session_set_blocking(session, 1);
lib.ssh2_session_set_timeout(session, GATE_SSH2_SESSION_TIMEOUT_DEFAULT);
/* ssh2_result = lib.ssh2_session_startup(session, (int)socket_handle); */
ssh2_result = lib.ssh2_session_handshake(session, (libssh2_socket_t)socket_handle);
if (ssh2_result != 0)
{
GATE_DEBUG_TRACE_SSH2_ERROR(session, "ssh2_session_handshake() failed");
ret = GATE_RESULT_FAILED;
break;
}
/* success state */
client->handles[1] = (void*)socket_handle;
socket_handle = GATE_SOCKET_INVALID;
ret = GATE_RESULT_OK;
} while (0);
if (socket_handle != GATE_SOCKET_INVALID)
{
gate_socket_close(socket_handle);
}
gate_string_release(&host);
GATE_DEBUG_TRACE_FAILED(ret, "gate_sshclient_init_connection() failed");
return ret;
}
static gate_result_t gate_sshclient_authenticate(gate_sshclient_t* client, gate_string_t const* user, gate_string_t const* pass)
{
gate_result_t ret = GATE_RESULT_FAILED;
LIBSSH2_SESSION* session = (LIBSSH2_SESSION*)client->handles[0];
char* userauthlist;<--- The scope of the variable 'userauthlist' can be reduced. [+]The scope of the variable 'userauthlist' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_string_t lst = GATE_STRING_INIT_EMPTY;
int ssh2_result;
do
{
userauthlist = lib.ssh2_userauth_list(session, user->str, (unsigned)user->length);
if (userauthlist)
{
gate_string_create_static(&lst, userauthlist);
}
if (gate_string_contains(&lst, &authlist_entry_password))
{
/* direct password usage */
ssh2_result = lib.ssh2_userauth_password_ex(session,
user->str, (unsigned)user->length,
pass->str, (unsigned)pass->length, NULL);
if (ssh2_result != 0)
{
ret = GATE_RESULT_ACCESSDENIED;
break;
}
ret = GATE_RESULT_OK;
}
else if (gate_string_contains(&lst, &authlist_entry_keyboard))
{
/* user keyboard simulation to inject password */
client->handles[2] = (void*)pass;<--- Variable 'client->handles[2]' is reassigned a value before the old one has been used.
ssh2_result = lib.ssh2_userauth_keyboard_interactive_ex(session,
user->str, (unsigned)user->length,
&gate_sshclient_kbd_callback);
client->handles[2] = NULL;<--- Variable 'client->handles[2]' is reassigned a value before the old one has been used.
if (ssh2_result != 0)
{
ret = GATE_RESULT_ACCESSDENIED;
break;
}
ret = GATE_RESULT_OK;
}
else
{
ret = GATE_RESULT_NOTSUPPORTED;
}
} while (0);
gate_string_release(&lst);
GATE_DEBUG_TRACE_FAILED(ret, "gate_sshclient_authenticate() failed");
return ret;
}
gate_result_t gate_sshclient_connect(gate_sshclient_t* client,
gate_string_t const* user, gate_string_t const* password,
gate_enumint_t flags)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_socket_t socket_handle = GATE_SOCKET_INVALID;
do
{
ret = gate_sshclient_init_connection(client);
GATE_BREAK_IF_FAILED(ret);
ret = gate_sshclient_authenticate(client, user, password);
if (GATE_FAILED(ret))
{
/* authentication failed -> release socket */
socket_handle = (gate_socket_t)client->handles[1];
client->handles[1] = (void*)GATE_SOCKET_INVALID;
gate_socket_close(socket_handle);
break;
}
/* success state */
ret = GATE_RESULT_OK;
} while (0);
GATE_DEBUG_TRACE_FAILED(ret, "gate_sshclient_connect() failed");
return ret;
}
gate_result_t gate_sshclient_connect_keys(
gate_sshclient_t* client, gate_string_t const* user,
gate_string_t const* pubkey, gate_string_t const* privkey, gate_string_t const* passphrase,
gate_enumint_t flags)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_socket_t socket_handle = GATE_SOCKET_INVALID;
ret = gate_sshclient_init_connection(client);
GATE_RETURN_IF_FAILED(ret);
do
{
LIBSSH2_SESSION* session = (LIBSSH2_SESSION*)client->handles[0];
char const* str_user = gate_string_ptr(user, 0);
gate_size_t len_user = gate_string_length(user);
char const* str_pubkey = gate_string_ptr(pubkey, 0);
gate_size_t len_pubkey = gate_string_length(pubkey);
char const* str_privkey = gate_string_ptr(privkey, 0);
gate_size_t len_privkey = gate_string_length(privkey);
char pass[1024];
char const* ptr_passphrase = NULL;
int ssh_error;
if (gate_string_to_buffer(passphrase, pass, sizeof(pass)) > 0)
{
ptr_passphrase = &pass[0];
}
ssh_error = lib.ssh2_userauth_publickey_frommemory(session,
str_user, len_user,
str_pubkey, len_pubkey,
str_privkey, len_privkey,
ptr_passphrase);
if (ssh_error != 0)
{
/* authentication failed -> release socket */
socket_handle = (gate_socket_t)client->handles[1];
client->handles[1] = (void*)GATE_SOCKET_INVALID;
gate_socket_close(socket_handle);
switch (ssh_error)
{
case LIBSSH2_ERROR_ALLOC: ret = GATE_RESULT_OUTOFMEMORY;<--- Variable 'ret' is reassigned a value before the old one has been used. 'break;' missing?
case LIBSSH2_ERROR_SOCKET_SEND: ret = GATE_RESULT_INVALIDOUTPUT;<--- Variable 'ret' is reassigned a value before the old one has been used. 'break;' missing?<--- Variable 'ret' is reassigned a value before the old one has been used. 'break;' missing?
case LIBSSH2_ERROR_SOCKET_TIMEOUT: ret = GATE_RESULT_TIMEOUT;<--- Variable 'ret' is reassigned a value before the old one has been used. 'break;' missing?<--- Variable 'ret' is reassigned a value before the old one has been used. 'break;' missing?
case LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED: ret = GATE_RESULT_INVALIDINPUT;<--- Variable 'ret' is reassigned a value before the old one has been used. 'break;' missing?<--- Variable 'ret' is reassigned a value before the old one has been used. 'break;' missing?
case LIBSSH2_ERROR_AUTHENTICATION_FAILED: ret = GATE_RESULT_ACCESSDENIED;<--- Variable 'ret' is reassigned a value before the old one has been used. 'break;' missing?<--- Variable 'ret' is reassigned a value before the old one has been used.
default: ret = GATE_RESULT_FAILED;<--- Variable 'ret' is reassigned a value before the old one has been used.
}
break;
}
/* success state */
ret = GATE_RESULT_OK;
} while (0);
GATE_DEBUG_TRACE_FAILED(ret, "gate_sshclient_connect_keys() failed");
return ret;
}
gate_result_t gate_sshclient_disconnect(gate_sshclient_t* client)
{
LIBSSH2_SESSION* session = (LIBSSH2_SESSION*)client->handles[0];
gate_socket_t socket_handle = (gate_socket_t)client->handles[1];
if (session)
{
lib.ssh2_session_disconnect_ex(session, SSH_DISCONNECT_BY_APPLICATION, "", "");
}
if (socket_handle != GATE_SOCKET_INVALID)
{
gate_socket_close(socket_handle);
client->handles[1] = (void*)GATE_SOCKET_INVALID;
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_open_sftp(gate_sshclient_t* client, gate_sshclient_sftp_t* sftp)
{
LIBSSH2_SESSION* session = client ? (LIBSSH2_SESSION*)client->handles[0] : NULL;
LIBSSH2_SFTP* ptr_sftp;
if (!session)
{
GATE_DEBUG_TRACE("missing client session");
return GATE_RESULT_NULLPOINTER;
}
ptr_sftp = lib.ssh2_sftp_init(session);
if (!ptr_sftp)
{
GATE_DEBUG_TRACE("ssh2_sftp_init() failed");
return GATE_RESULT_FAILED;
}
if (sftp)
{
*sftp = (gate_sshclient_sftp_t)ptr_sftp;
}
else
{
lib.ssh2_sftp_shutdown(ptr_sftp);
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_release(gate_sshclient_t* client)
{
LIBSSH2_SESSION* session = (LIBSSH2_SESSION*)client->handles[0];
gate_sshclient_disconnect(client);
lib.ssh2_session_free(session);
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_close_sftp(gate_sshclient_sftp_t* sftp)
{
LIBSSH2_SFTP* ptr_sftp = sftp ? *((LIBSSH2_SFTP**)sftp) : NULL;
if (ptr_sftp)
{
lib.ssh2_sftp_shutdown(ptr_sftp);
*sftp = NULL;
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_sftp_open(gate_sshclient_sftp_t* sftp, gate_string_t const* path, gate_enumint_t stream_open_flags,
gate_sshclient_sftp_handle_t* sftp_handle)
{
char file_path[GATE_MAX_FILEPATH_LENGTH];
gate_size_t file_path_length;
LIBSSH2_SFTP* ptr_sftp = sftp ? *((LIBSSH2_SFTP**)sftp) : NULL;
unsigned long flags = 0;
long mode = LIBSSH2_SFTP_S_IFREG;
int open_type = LIBSSH2_SFTP_OPENFILE;
LIBSSH2_SFTP_HANDLE* ptr_handle;
if (!ptr_sftp)
{
GATE_DEBUG_TRACE("Missing sftp session");
return GATE_RESULT_NULLPOINTER;
}
file_path_length = gate_string_to_buffer(path, file_path, sizeof(file_path));
switch (stream_open_flags)
{
case GATE_STREAM_OPEN_READ:
flags = LIBSSH2_FXF_READ;
break;
case GATE_STREAM_OPEN_WRITE:
flags = LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT | LIBSSH2_FXF_TRUNC;
mode |= LIBSSH2_SFTP_S_IRWXU | LIBSSH2_SFTP_S_IRGRP | LIBSSH2_SFTP_S_IRGRP;<--- Same expression on both sides of '|'. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
break;
case GATE_STREAM_OPEN_READWRITE:
flags = LIBSSH2_FXF_READ | LIBSSH2_FXF_WRITE;<--- Variable 'flags' is reassigned a value before the old one has been used. 'break;' missing?
case GATE_STREAM_OPEN_APPEND:
case GATE_STREAM_OPEN_APPENDWRITE:
flags = LIBSSH2_FXF_APPEND | LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT;<--- Variable 'flags' is reassigned a value before the old one has been used. 'break;' missing?<--- Variable 'flags' is reassigned a value before the old one has been used. 'break;' missing?
case GATE_STREAM_OPEN_APPENDREADWRITE:
flags = LIBSSH2_FXF_APPEND | LIBSSH2_FXF_READ | LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT;<--- Variable 'flags' is reassigned a value before the old one has been used. 'break;' missing?
default:
GATE_DEBUG_TRACE("gate_sshclient_sftp_open(): unsupported open-flags");
return GATE_RESULT_INVALIDARG;
}
ptr_handle = lib.ssh2_sftp_open_ex(ptr_sftp, file_path, (unsigned int)file_path_length, flags, mode, open_type);
if (!ptr_handle)
{
GATE_DEBUG_TRACE("ssh2_sftp_open_ex() failed");
return GATE_RESULT_FAILED;
}
if (sftp_handle)
{
*sftp_handle = (gate_sshclient_sftp_handle_t)ptr_handle;
}
else
{
lib.ssh2_sftp_close_handle(ptr_handle);
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_sftp_read(gate_sshclient_sftp_handle_t* sftp_handle, char* buffer, gate_size_t capacity, gate_size_t* returned)
{
LIBSSH2_SFTP_HANDLE* ptr_handle = sftp_handle ? *((LIBSSH2_SFTP_HANDLE**)sftp_handle) : NULL;
ssize_t result;
if (!ptr_handle)
{
GATE_DEBUG_TRACE("Missing sftp handle");
return GATE_RESULT_NULLPOINTER;
}
result = lib.ssh2_sftp_read(ptr_handle, buffer, capacity);
if (result < 0)
{
GATE_DEBUG_TRACE("ssh2_sftp_read() failed");
return GATE_RESULT_FAILED;
}
if (returned)
{
*returned = (gate_size_t)result;
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_sftp_write(gate_sshclient_sftp_handle_t* sftp_handle, char const* buffer, gate_size_t length, gate_size_t* written)
{
LIBSSH2_SFTP_HANDLE* ptr_handle = sftp_handle ? *((LIBSSH2_SFTP_HANDLE**)sftp_handle) : NULL;
ssize_t result;
if (!ptr_handle)
{
GATE_DEBUG_TRACE("Missing sftp handle");
return GATE_RESULT_NULLPOINTER;
}
result = lib.ssh2_sftp_write(ptr_handle, buffer, length);
if (result < 0)
{
GATE_DEBUG_TRACE("ssh2_sftp_write() failed");
return GATE_RESULT_FAILED;
}
if (written)
{
*written = (gate_size_t)result;
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_sftp_seek(gate_sshclient_sftp_handle_t* sftp_handle, gate_uint64_t position)
{
LIBSSH2_SFTP_HANDLE* ptr_handle = sftp_handle ? *((LIBSSH2_SFTP_HANDLE**)sftp_handle) : NULL;
if (!ptr_handle)
{
GATE_DEBUG_TRACE("Missing sftp handle");
return GATE_RESULT_NULLPOINTER;
}
lib.ssh2_sftp_seek64(ptr_handle, position);
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_sftp_tell(gate_sshclient_sftp_handle_t* sftp_handle, gate_uint64_t* position)
{
LIBSSH2_SFTP_HANDLE* ptr_handle = sftp_handle ? *((LIBSSH2_SFTP_HANDLE**)sftp_handle) : NULL;
if (!ptr_handle)
{
GATE_DEBUG_TRACE("Missing sftp handle");
return GATE_RESULT_NULLPOINTER;
}
if (position)
{
*position = lib.ssh2_sftp_tell64(ptr_handle);
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_sftp_close(gate_sshclient_sftp_handle_t* sftp_handle)
{
LIBSSH2_SFTP_HANDLE* ptr_handle = sftp_handle ? *((LIBSSH2_SFTP_HANDLE**)sftp_handle) : NULL;
if (!ptr_handle)
{
GATE_DEBUG_TRACE("Missing sftp handle");
return GATE_RESULT_NULLPOINTER;
}
lib.ssh2_sftp_close_handle(ptr_handle);
*sftp_handle = NULL;
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_sftp_opendir(gate_sshclient_sftp_t* sftp, gate_string_t const* path, gate_sshclient_sftp_handle_t* sftp_handle)
{
char file_path[GATE_MAX_FILEPATH_LENGTH];
gate_size_t file_path_length;
LIBSSH2_SFTP* ptr_sftp = sftp ? *((LIBSSH2_SFTP**)sftp) : NULL;
LIBSSH2_SFTP_HANDLE* ptr_handle;
if (!ptr_sftp)
{
GATE_DEBUG_TRACE("Missing sftp session");
return GATE_RESULT_NULLPOINTER;
}
file_path_length = gate_string_to_buffer(path, file_path, sizeof(file_path));
ptr_handle = lib.ssh2_sftp_open_ex(ptr_sftp, file_path, (unsigned int)file_path_length, 0, 0, LIBSSH2_SFTP_OPENDIR);
if (!ptr_handle)
{
GATE_DEBUG_TRACE("ssh2_sftp_open_ex() failed");
return GATE_RESULT_FAILED;
}
if (sftp_handle)
{
*sftp_handle = (gate_sshclient_sftp_handle_t)ptr_handle;
}
else
{
lib.ssh2_sftp_close_handle(ptr_handle);
}
return GATE_RESULT_OK;
}
static void convert_attributes(LIBSSH2_SFTP_ATTRIBUTES* ptr_attr, gate_file_properties_t* file_props)
{
if (ptr_attr->flags & LIBSSH2_SFTP_ATTR_SIZE)
{
file_props->size = ptr_attr->filesize;
}
if (ptr_attr->flags & LIBSSH2_SFTP_ATTR_PERMISSIONS)
{
if (LIBSSH2_SFTP_S_ISREG(ptr_attr->permissions)) file_props->attribs |= GATE_FILEENTRY_ATTRIB_FILE;
if (LIBSSH2_SFTP_S_ISDIR(ptr_attr->permissions)) file_props->attribs |= GATE_FILEENTRY_ATTRIB_DIRECTORY;
if (LIBSSH2_SFTP_S_ISCHR(ptr_attr->permissions)) file_props->attribs |= GATE_FILEENTRY_ATTRIB_DEVICE;
if (LIBSSH2_SFTP_S_ISBLK(ptr_attr->permissions)) file_props->attribs |= GATE_FILEENTRY_ATTRIB_DEVICE;
if (LIBSSH2_SFTP_S_ISFIFO(ptr_attr->permissions)) file_props->attribs |= GATE_FILEENTRY_ATTRIB_SPECIAL;
if (LIBSSH2_SFTP_S_ISSOCK(ptr_attr->permissions)) file_props->attribs |= GATE_FILEENTRY_ATTRIB_SPECIAL;
if (LIBSSH2_SFTP_S_ISLNK(ptr_attr->permissions)) file_props->attribs |= GATE_FILEENTRY_ATTRIB_FILE;
if (ptr_attr->permissions & LIBSSH2_SFTP_S_IRUSR) file_props->access |= GATE_FILEENTRY_ACCESS_OWNERREAD;
if (ptr_attr->permissions & LIBSSH2_SFTP_S_IWUSR) file_props->access |= GATE_FILEENTRY_ACCESS_OWNERWRITE;
if (ptr_attr->permissions & LIBSSH2_SFTP_S_IXUSR) file_props->access |= GATE_FILEENTRY_ACCESS_OWNEREXECUTE;
if (ptr_attr->permissions & LIBSSH2_SFTP_S_IRGRP) file_props->access |= GATE_FILEENTRY_ACCESS_GROUPREAD;
if (ptr_attr->permissions & LIBSSH2_SFTP_S_IWGRP) file_props->access |= GATE_FILEENTRY_ACCESS_GROUPWRITE;
if (ptr_attr->permissions & LIBSSH2_SFTP_S_IXGRP) file_props->access |= GATE_FILEENTRY_ACCESS_GROUPEXECUTE;
if (ptr_attr->permissions & LIBSSH2_SFTP_S_IROTH) file_props->access |= GATE_FILEENTRY_ACCESS_ALLREAD;
if (ptr_attr->permissions & LIBSSH2_SFTP_S_IWOTH) file_props->access |= GATE_FILEENTRY_ACCESS_ALLWRITE;
if (ptr_attr->permissions & LIBSSH2_SFTP_S_IXOTH) file_props->access |= GATE_FILEENTRY_ACCESS_ALLEXECUTE;
}
if (ptr_attr->flags & LIBSSH2_SFTP_ATTR_ACMODTIME)
{
gate_time_from_unix(ptr_attr->atime, &file_props->time_accessed);
gate_time_from_unix(ptr_attr->mtime, &file_props->time_modified);
gate_time_from_unix(ptr_attr->atime, &file_props->time_created);
}
}
gate_result_t gate_sshclient_sftp_readdir(gate_sshclient_sftp_handle_t* sftp_handle, gate_file_entry_t* file_entry)
{
LIBSSH2_SFTP_HANDLE* ptr_handle = sftp_handle ? *((LIBSSH2_SFTP_HANDLE**)sftp_handle) : NULL;
char buffer[GATE_MAX_FILENAME_LENGTH];
char longentry[GATE_MAX_COPYBUFFER_LENGTH];
LIBSSH2_SFTP_ATTRIBUTES attribs = GATE_INIT_EMPTY;
int result;
if (!sftp_handle)
{
GATE_DEBUG_TRACE("Missing sftp session");
return GATE_RESULT_NULLPOINTER;
}
attribs.flags = LIBSSH2_SFTP_ATTR_SIZE | LIBSSH2_SFTP_ATTR_UIDGID | LIBSSH2_SFTP_ATTR_PERMISSIONS | LIBSSH2_SFTP_ATTR_ACMODTIME;
result = lib.ssh2_sftp_readdir_ex(ptr_handle, buffer, sizeof(buffer), longentry, sizeof(longentry), &attribs);
if (result < 0)
{
return GATE_RESULT_FAILED;
}
else if (result == 0)
{
return GATE_RESULT_NODATA;
}
if (file_entry)
{
buffer[sizeof(buffer) - 1] = 0;
gate_mem_clear(file_entry, sizeof(gate_file_entry_t));
gate_str_print_text(file_entry->name, sizeof(file_entry->name), buffer, gate_str_length(buffer));
convert_attributes(&attribs, &file_entry->props);
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_sftp_listdir(gate_sshclient_sftp_t* sftp, gate_string_t const* path, gate_file_list_callback_t callback, void* userdata)
{
gate_result_t result;
gate_sshclient_sftp_handle_t handle = NULL;
gate_file_entry_t entry;
gate_bool_t continue_readdir = true;
gate_size_t len;
gate_size_t namelen;
gate_bool_t need_slash = !gate_string_ends_with_char(path, '/');
result = gate_sshclient_sftp_opendir(sftp, path, &handle);
if (GATE_FAILED(result))
{
return result;
}
while (continue_readdir)
{
result = gate_sshclient_sftp_readdir(&handle, &entry);
GATE_BREAK_IF_FAILED(result);
namelen = gate_str_length(entry.name);
if ((gate_str_compare(entry.name, namelen, ".", 1) == 0)
|| (gate_str_compare(entry.name, namelen, "..", 2) == 0))
{
/* skip pseudo-dir-entries */
}
else
{
len = gate_str_print_text(entry.path, sizeof(entry.path) - 1, path->str, path->length);
if (need_slash)
{
entry.path[len] = '/';
++len;
}
gate_str_print_text(&entry.path[len], sizeof(entry.path) - len, entry.name, namelen);
continue_readdir = callback(&entry, userdata);
}
}
gate_sshclient_sftp_close(&handle);
if (result == GATE_RESULT_NODATA)
{
result = GATE_RESULT_OK;
}
return result;
}
gate_result_t gate_sshclient_sftp_mkdir(gate_sshclient_sftp_t* sftp, gate_string_t const* path)
{
LIBSSH2_SFTP* ptr_sftp = sftp ? *((LIBSSH2_SFTP**)sftp) : NULL;
char buffer[GATE_MAX_FILEPATH_LENGTH];
gate_size_t buffer_len;
int result;
if (!ptr_sftp)
{
GATE_DEBUG_TRACE("Missing sftp session");
return GATE_RESULT_NULLPOINTER;
}
buffer_len = gate_string_to_buffer(path, buffer, sizeof(buffer));
result = lib.ssh2_sftp_mkdir_ex(ptr_sftp, buffer, (unsigned int)buffer_len, 0775);
if (result != 0)
{
GATE_DEBUG_TRACE("ssh2_sftp_mkdir_ex() failed");
return GATE_RESULT_FAILED;
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_sftp_rmdir(gate_sshclient_sftp_t* sftp, gate_string_t const* path)
{
LIBSSH2_SFTP* ptr_sftp = sftp ? *((LIBSSH2_SFTP**)sftp) : NULL;
char buffer[GATE_MAX_FILEPATH_LENGTH];
gate_size_t buffer_len;
int result;
if (!ptr_sftp)
{
GATE_DEBUG_TRACE("Missing sftp session");
return GATE_RESULT_NULLPOINTER;
}
buffer_len = gate_string_to_buffer(path, buffer, sizeof(buffer));
result = lib.ssh2_sftp_rmdir_ex(ptr_sftp, buffer, (unsigned int)buffer_len);
if (result != 0)
{
GATE_DEBUG_TRACE("ssh2_sftp_rmdir_ex() failed");
return GATE_RESULT_FAILED;
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_sftp_unlink(gate_sshclient_sftp_t* sftp, gate_string_t const* path)
{
LIBSSH2_SFTP* ptr_sftp = sftp ? *((LIBSSH2_SFTP**)sftp) : NULL;
char buffer[GATE_MAX_FILEPATH_LENGTH];
gate_size_t buffer_len;
int result;
if (!ptr_sftp)
{
GATE_DEBUG_TRACE("Missing sftp session");
return GATE_RESULT_NULLPOINTER;
}
buffer_len = gate_string_to_buffer(path, buffer, sizeof(buffer));
result = lib.ssh2_sftp_unlink_ex(ptr_sftp, buffer, (unsigned int)buffer_len);
if (result != 0)
{
GATE_DEBUG_TRACE("ssh2_sftp_unlink_ex() failed");
return GATE_RESULT_FAILED;
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_sftp_rename(gate_sshclient_sftp_t* sftp, gate_string_t const* src_path, gate_string_t const* dst_path)
{
LIBSSH2_SFTP* ptr_sftp = sftp ? *((LIBSSH2_SFTP**)sftp) : NULL;
char src_buffer[GATE_MAX_FILEPATH_LENGTH];
char dst_buffer[GATE_MAX_FILEPATH_LENGTH];
gate_size_t src_buffer_len;
gate_size_t dst_buffer_len;
int result;
if (!ptr_sftp)
{
GATE_DEBUG_TRACE("Missing sftp session");
return GATE_RESULT_NULLPOINTER;
}
src_buffer_len = gate_string_to_buffer(src_path, src_buffer, sizeof(src_buffer));
dst_buffer_len = gate_string_to_buffer(dst_path, dst_buffer, sizeof(dst_buffer));
result = lib.ssh2_sftp_rename_ex(ptr_sftp, src_buffer, (unsigned int)src_buffer_len,
dst_buffer, (unsigned int)dst_buffer_len, LIBSSH2_SFTP_RENAME_ATOMIC);
if (result != 0)
{
GATE_DEBUG_TRACE("ssh2_sftp_rename_ex() failed");
return GATE_RESULT_FAILED;
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_sftp_stat(gate_sshclient_sftp_t* sftp, gate_string_t const* path, gate_file_properties_t* file_props)
{
LIBSSH2_SFTP* ptr_sftp = sftp ? *((LIBSSH2_SFTP**)sftp) : NULL;
char buffer[GATE_MAX_FILEPATH_LENGTH];
gate_size_t buffer_len;
int result;
LIBSSH2_SFTP_ATTRIBUTES attribs = GATE_INIT_EMPTY;
if (!ptr_sftp)
{
GATE_DEBUG_TRACE("Missing sftp session");
return GATE_RESULT_NULLPOINTER;
}
buffer_len = gate_string_to_buffer(path, buffer, sizeof(buffer));
attribs.flags = LIBSSH2_SFTP_ATTR_SIZE | LIBSSH2_SFTP_ATTR_UIDGID | LIBSSH2_SFTP_ATTR_PERMISSIONS | LIBSSH2_SFTP_ATTR_ACMODTIME;
result = lib.ssh2_sftp_stat_ex(ptr_sftp, buffer, (unsigned int)buffer_len, LIBSSH2_SFTP_LSTAT, &attribs);
if (result != 0)
{
GATE_DEBUG_TRACE("ssh2_sftp_stat_ex() failed");
return GATE_RESULT_FAILED;
}
convert_attributes(&attribs, file_props);
return GATE_RESULT_OK;
}
typedef struct ssh_channel_impl_class
{
LIBSSH2_CHANNEL* channel;
gate_size_t read_buffer_used;
gate_size_t read_buffer_offset;
char read_buffer[8192];
gate_size_t err_buffer_used;
gate_size_t err_buffer_offset;
char err_buffer[8192];
} ssh_channel_impl_t;
gate_result_t gate_sshclient_channel_open(gate_sshclient_t* client, gate_sshclient_channel_t* ptr_channel)
{
ssh_channel_impl_t* impl = NULL;
LIBSSH2_SESSION* session = client ? (LIBSSH2_SESSION*)client->handles[0] : NULL;
LIBSSH2_CHANNEL* channel = NULL;
if (!session)
{
GATE_DEBUG_TRACE("missing client session");
return GATE_RESULT_NULLPOINTER;
}
channel = lib.ssh2_channel_open_ex(session, "session", 7, LIBSSH2_CHANNEL_WINDOW_DEFAULT, LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, 0);
if (!channel)
{
GATE_DEBUG_TRACE_SSH2_ERROR(session, "ssh2_channel_open_ex() failed");
return GATE_RESULT_FAILED;
}
lib.ssh2_channel_set_blocking(channel, 1);
if (ptr_channel)
{
impl = (ssh_channel_impl_t*)gate_mem_alloc(sizeof(ssh_channel_impl_t));
if (NULL == impl)
{
lib.ssh2_channel_close(channel);
return GATE_RESULT_OUTOFMEMORY;
}
impl->channel = channel;
impl->read_buffer_offset = 0;
impl->read_buffer_used = 0;
*ptr_channel = (gate_sshclient_channel_t)impl;
}
else
{
lib.ssh2_channel_close(channel);
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_channel_close(gate_sshclient_channel_t* channel)
{
ssh_channel_impl_t* impl = channel ? (ssh_channel_impl_t*)*channel : NULL;
LIBSSH2_CHANNEL* ssh_channel = impl ? impl->channel : NULL;
int result;
if (!ssh_channel)
{
return GATE_RESULT_NULLPOINTER;
}
result = lib.ssh2_channel_close(ssh_channel);
if (0 != result)
{
return GATE_RESULT_LOCKED;
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_channel_free(gate_sshclient_channel_t* channel)
{
ssh_channel_impl_t* impl = channel ? (ssh_channel_impl_t*)*channel : NULL;
LIBSSH2_CHANNEL* ssh_channel = impl ? impl->channel : NULL;
int result;
if (!ssh_channel)
{
return GATE_RESULT_NULLPOINTER;
}
result = lib.ssh2_channel_free(ssh_channel);
if (0 != result)
{
return GATE_RESULT_LOCKED;
}
gate_mem_dealloc(impl);
*channel = NULL;
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_channel_exec(gate_sshclient_channel_t* channel, gate_string_t const* command)
{
ssh_channel_impl_t* impl = channel ? (ssh_channel_impl_t*)*channel : NULL;
LIBSSH2_CHANNEL* ssh_channel = impl ? impl->channel : NULL;
gate_cstrbuffer_t sb;
int result;
char const* req_name;
unsigned int req_name_len;
char const* ptr_command;
unsigned int command_length;
if (!ssh_channel)
{
return GATE_RESULT_NULLPOINTER;
}
if (NULL == gate_cstrbuffer_create_string(&sb, command, false))
{
return GATE_RESULT_OUTOFMEMORY;
}
command_length = (unsigned int)gate_cstrbuffer_length(&sb);
if (command_length == 0)
{
ptr_command = NULL;
req_name = "shell";
req_name_len = 5;
}
else
{
ptr_command = gate_cstrbuffer_get(&sb);
req_name = "exec";
req_name_len = 4;
}
result = lib.ssh2_channel_process_startup(ssh_channel, req_name, req_name_len, ptr_command, command_length);
gate_cstrbuffer_destroy(&sb);
if (0 != result)
{
return GATE_RESULT_FAILED;
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_channel_canread(gate_sshclient_channel_t* channel, gate_enumint_t stream_type, gate_bool_t* ptr_canread)
{
ssh_channel_impl_t* impl = channel ? (ssh_channel_impl_t*)*channel : NULL;
LIBSSH2_CHANNEL* ssh_channel = impl ? impl->channel : NULL;
ssize_t bytes_returned;
int stream_id;
if (!ssh_channel || !ptr_canread)
{
return GATE_RESULT_NULLPOINTER;
}
switch (stream_type)
{
case GATE_SSHCLIENT_STREAMTYPE_DEFAULT: stream_id = 0; break;
case GATE_SSHCLIENT_STREAMTYPE_ERROR: stream_id = SSH_EXTENDED_DATA_STDERR; break;
default: stream_id = stream_type; break;
}
switch (stream_type)
{
case GATE_SSHCLIENT_STREAMTYPE_DEFAULT:
{
if (impl->read_buffer_offset < impl->read_buffer_used)
{
/* something is in the buffer */
*ptr_canread = true;
return GATE_RESULT_OK;
}
lib.ssh2_channel_set_blocking(ssh_channel, 0);
bytes_returned = lib.ssh2_channel_read_ex(ssh_channel, stream_id, &impl->read_buffer[0], sizeof(impl->read_buffer));
lib.ssh2_channel_set_blocking(ssh_channel, 1);
if (bytes_returned == LIBSSH2_ERROR_EAGAIN)
{
/* cannot read, would block */
*ptr_canread = false;
return GATE_RESULT_OK;
}
if (bytes_returned < 0)
{
/* another error, will be returned with next read attempt */
}
else
{
/* buffer is filled with data to be returned on next read attempt */
impl->read_buffer_offset = 0;
impl->read_buffer_used = (gate_size_t)bytes_returned;
}
*ptr_canread = true;
return GATE_RESULT_OK;
}
case GATE_SSHCLIENT_STREAMTYPE_ERROR:
{
if (impl->err_buffer_offset < impl->err_buffer_used)
{
/* something is in the buffer */
*ptr_canread = true;
return GATE_RESULT_OK;
}
lib.ssh2_channel_set_blocking(ssh_channel, 0);
bytes_returned = lib.ssh2_channel_read_ex(ssh_channel, stream_id, &impl->err_buffer[0], sizeof(impl->err_buffer));
lib.ssh2_channel_set_blocking(ssh_channel, 1);
if (bytes_returned == LIBSSH2_ERROR_EAGAIN)
{
/* cannot read, would block */
*ptr_canread = false;
return GATE_RESULT_OK;
}
if (bytes_returned < 0)
{
/* another error, will be returned with next read attempt */
}
else
{
/* buffer is filled with data to be returned on next read attempt */
impl->err_buffer_offset = 0;
impl->err_buffer_used = (gate_size_t)bytes_returned;
}
*ptr_canread = true;
return GATE_RESULT_OK;
}
default:
{
break;
}
}
return GATE_RESULT_NOTSUPPORTED;
}
gate_result_t gate_sshclient_channel_read(gate_sshclient_channel_t* channel, gate_enumint_t stream_type, char* buffer, gate_size_t bufferlen, gate_size_t* bufferused)
{
ssh_channel_impl_t* impl = channel ? (ssh_channel_impl_t*)*channel : NULL;
LIBSSH2_CHANNEL* ssh_channel = impl ? impl->channel : NULL;
ssize_t bytes_returned;
int stream_id = 0;
if (!ssh_channel || !buffer)
{
return GATE_RESULT_NULLPOINTER;
}
switch (stream_type)
{
case GATE_SSHCLIENT_STREAMTYPE_DEFAULT:
{
if (impl->read_buffer_offset < impl->read_buffer_used)
{
/* we have data in the read buffer */
if (bufferlen > (impl->read_buffer_used - impl->read_buffer_offset))
{
bufferlen = (impl->read_buffer_used - impl->read_buffer_offset);
}
gate_mem_copy(buffer, &impl->read_buffer[impl->read_buffer_offset], bufferlen);
impl->read_buffer_offset += bufferlen;
if (bufferused)
{
*bufferused = bufferlen;
}
return GATE_RESULT_OK;
}
stream_id = 0;
break;
}
case GATE_SSHCLIENT_STREAMTYPE_ERROR:
{
if (impl->err_buffer_offset < impl->err_buffer_used)
{
/* we have data in the error buffer */
if (bufferlen > (impl->err_buffer_used - impl->err_buffer_offset))
{
bufferlen = (impl->err_buffer_used - impl->err_buffer_offset);
}
gate_mem_copy(buffer, &impl->err_buffer[impl->err_buffer_offset], bufferlen);
impl->err_buffer_offset += bufferlen;
if (bufferused)
{
*bufferused = bufferlen;
}
return GATE_RESULT_OK;
}
stream_id = SSH_EXTENDED_DATA_STDERR;
break;
}
default:
{
stream_id = stream_type;
break;
}
}
bytes_returned = lib.ssh2_channel_read_ex(ssh_channel, stream_id, buffer, bufferlen);
if (bytes_returned < 0)
{
return GATE_RESULT_FAILED;
}
if (bufferused)
{
*bufferused = (gate_size_t)bytes_returned;
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_channel_write(gate_sshclient_channel_t* channel, gate_enumint_t stream_type, char const* buffer, gate_size_t bufferlen, gate_size_t* bufferprocessed)
{
ssh_channel_impl_t* impl = channel ? (ssh_channel_impl_t*)*channel : NULL;
LIBSSH2_CHANNEL* ssh_channel = impl ? impl->channel : NULL;
ssize_t bytes_written;
int stream_id = 0;
if (!ssh_channel || !buffer)
{
return GATE_RESULT_NULLPOINTER;
}
switch (stream_type)
{
case GATE_SSHCLIENT_STREAMTYPE_DEFAULT: stream_id = 0; break;
case GATE_SSHCLIENT_STREAMTYPE_ERROR: stream_id = SSH_EXTENDED_DATA_STDERR; break;
default: stream_id = stream_type; break;
}
bytes_written = lib.ssh2_channel_write_ex(ssh_channel, stream_id, buffer, bufferlen);
if (bytes_written < 0)
{
return GATE_RESULT_FAILED;
}
if (bufferprocessed)
{
*bufferprocessed = (gate_size_t)bytes_written;
}
return GATE_RESULT_OK;
}
gate_bool_t gate_sshclient_channel_eof(gate_sshclient_channel_t* channel)
{
ssh_channel_impl_t* impl = channel ? (ssh_channel_impl_t*)*channel : NULL;
LIBSSH2_CHANNEL* ssh_channel = impl ? impl->channel : NULL;
if (!ssh_channel)
{
return false;
}
return lib.ssh2_channel_eof(ssh_channel) == 1 ? true : false;
}
gate_result_t gate_sshclient_channel_send_eof(gate_sshclient_channel_t* channel)
{
ssh_channel_impl_t* impl = channel ? (ssh_channel_impl_t*)*channel : NULL;
LIBSSH2_CHANNEL* ssh_channel = impl ? impl->channel : NULL;
int result;
if (!ssh_channel)
{
return GATE_RESULT_NULLPOINTER;
}
result = lib.ssh2_channel_send_eof(ssh_channel);
if (result != 0)
{
return GATE_RESULT_FAILED;
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_channel_wait_eof(gate_sshclient_channel_t* channel)
{
ssh_channel_impl_t* impl = channel ? (ssh_channel_impl_t*)*channel : NULL;
LIBSSH2_CHANNEL* ssh_channel = impl ? impl->channel : NULL;
int result;
if (!ssh_channel)
{
return GATE_RESULT_NULLPOINTER;
}
result = lib.ssh2_channel_wait_eof(ssh_channel);
if (result != 0)
{
return GATE_RESULT_FAILED;
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_channel_setenv(gate_sshclient_channel_t* channel, gate_string_t const* name, gate_string_t const* value)
{
ssh_channel_impl_t* impl = channel ? (ssh_channel_impl_t*)*channel : NULL;
LIBSSH2_CHANNEL* ssh_channel = impl ? impl->channel : NULL;
int result;
if (!ssh_channel)
{
return GATE_RESULT_NULLPOINTER;
}
result = lib.ssh2_channel_setenv_ex(ssh_channel,
gate_string_ptr(name, 0), (unsigned int)gate_string_length(name),
gate_string_ptr(value, 0), (unsigned int)gate_string_length(value));
if (result != 0)
{
return GATE_RESULT_FAILED;
}
return GATE_RESULT_OK;
}
gate_result_t gate_sshclient_channel_get_exit_signal(gate_sshclient_channel_t* channel, gate_string_t* ptr_signal, gate_string_t* ptr_errmsg, gate_string_t* ptr_langtag)
{
ssh_channel_impl_t* impl = channel ? (ssh_channel_impl_t*)*channel : NULL;
LIBSSH2_CHANNEL* ssh_channel = impl ? impl->channel : NULL;
gate_result_t ret = GATE_RESULT_FAILED;
int result;<--- The scope of the variable 'result' can be reduced. [+]The scope of the variable 'result' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
char* exitsignal = NULL;
size_t exitsignal_len = 0;
char* errmsg = NULL;
size_t errmsg_len = 0;
char* langtag = NULL;
size_t langtag_len = 0;
do
{
if (!ssh_channel)
{
ret = GATE_RESULT_NULLPOINTER;
break;
}
result = lib.ssh2_channel_get_exit_signal(ssh_channel, &exitsignal, &exitsignal_len, &errmsg, &errmsg_len, &langtag, &langtag_len);
if (result != 0)
{
ret = GATE_RESULT_FAILED;
break;
}
if (ptr_signal)
{
if (NULL == gate_string_create(ptr_signal, exitsignal, exitsignal_len))
{
ret = GATE_RESULT_OUTOFMEMORY;<--- Variable 'ret' is reassigned a value before the old one has been used.
}
}
if (ptr_errmsg)
{
if (NULL == gate_string_create(ptr_errmsg, errmsg, errmsg_len))
{
if (ptr_signal) gate_string_release(ptr_signal);
ret = GATE_RESULT_OUTOFMEMORY;<--- Variable 'ret' is reassigned a value before the old one has been used.
}
}
if (ptr_langtag)
{
if (NULL == gate_string_create(ptr_langtag, langtag, langtag_len))
{
if (ptr_signal) gate_string_release(ptr_errmsg);
if (ptr_signal) gate_string_release(ptr_signal);
ret = GATE_RESULT_OUTOFMEMORY;<--- Variable 'ret' is reassigned a value before the old one has been used.
}
}
ret = GATE_RESULT_OK;<--- Variable 'ret' is reassigned a value before the old one has been used.<--- Variable 'ret' is reassigned a value before the old one has been used.<--- Variable 'ret' is reassigned a value before the old one has been used.
} while (0);
return ret;
}
gate_result_t gate_sshclient_channel_get_exit_status(gate_sshclient_channel_t* channel, int* ptr_status)
{
ssh_channel_impl_t* impl = channel ? (ssh_channel_impl_t*)*channel : NULL;
LIBSSH2_CHANNEL* ssh_channel = impl ? impl->channel : NULL;
int result;
if (!ssh_channel)
{
return GATE_RESULT_NULLPOINTER;
}
result = lib.ssh2_channel_get_exit_status(ssh_channel);
if (ptr_status)
{
*ptr_status = result;
}
return GATE_RESULT_OK;
}
#endif
#if defined(GATE_NET_SSHCLIENTS_NO_IMPL)
gate_result_t gate_sshclient_create(gate_sshclient_t* client, gate_string_t const* server, gate_uint16_t port)
{
GATE_UNUSED_ARG(client);
GATE_UNUSED_ARG(server);
GATE_UNUSED_ARG(port);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_connect(gate_sshclient_t* client,
gate_string_t const* user, gate_string_t const* password, gate_enumint_t flags)
{
GATE_UNUSED_ARG(client);
GATE_UNUSED_ARG(user);
GATE_UNUSED_ARG(password);
GATE_UNUSED_ARG(flags);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_connect_keys(gate_sshclient_t* client,
gate_string_t const* user, gate_string_t const* pubkey, gate_string_t const* privkey,
gate_string_t const* passphrase, gate_enumint_t flags)
{
GATE_UNUSED_ARG(client);
GATE_UNUSED_ARG(user);
GATE_UNUSED_ARG(pubkey);
GATE_UNUSED_ARG(privkey);
GATE_UNUSED_ARG(passphrase);
GATE_UNUSED_ARG(flags);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_disconnect(gate_sshclient_t* client)
{
GATE_UNUSED_ARG(client);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_release(gate_sshclient_t* client)
{
GATE_UNUSED_ARG(client);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_open_sftp(gate_sshclient_t* client, gate_sshclient_sftp_t* sftp)
{
GATE_UNUSED_ARG(client);
GATE_UNUSED_ARG(sftp);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_close_sftp(gate_sshclient_sftp_t* sftp)
{
GATE_UNUSED_ARG(sftp);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_sftp_open(gate_sshclient_sftp_t* sftp, gate_string_t const* path,
gate_enumint_t stream_open_flags, gate_sshclient_sftp_handle_t* sftp_handle)
{
GATE_UNUSED_ARG(sftp);
GATE_UNUSED_ARG(path);
GATE_UNUSED_ARG(stream_open_flags);
GATE_UNUSED_ARG(sftp_handle);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_sftp_read(gate_sshclient_sftp_handle_t* sftp_handle,
char* buffer, gate_size_t capacity, gate_size_t* returned)
{
GATE_UNUSED_ARG(sftp_handle);
GATE_UNUSED_ARG(buffer);
GATE_UNUSED_ARG(capacity);
GATE_UNUSED_ARG(returned);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_sftp_write(gate_sshclient_sftp_handle_t* sftp_handle,
char const* buffer, gate_size_t length, gate_size_t* written)
{
GATE_UNUSED_ARG(sftp_handle);
GATE_UNUSED_ARG(buffer);
GATE_UNUSED_ARG(length);
GATE_UNUSED_ARG(written);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_sftp_seek(gate_sshclient_sftp_handle_t* sftp_handle, gate_uint64_t position)
{
GATE_UNUSED_ARG(sftp_handle);
GATE_UNUSED_ARG(position);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_sftp_tell(gate_sshclient_sftp_handle_t* sftp_handle, gate_uint64_t* position)
{
GATE_UNUSED_ARG(sftp_handle);
GATE_UNUSED_ARG(position);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_sftp_close(gate_sshclient_sftp_handle_t* sftp_handle)
{
GATE_UNUSED_ARG(sftp_handle);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_sftp_opendir(gate_sshclient_sftp_t* sftp, gate_string_t const* path,
gate_sshclient_sftp_handle_t* sftp_handle)
{
GATE_UNUSED_ARG(sftp);
GATE_UNUSED_ARG(path);
GATE_UNUSED_ARG(sftp_handle);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_sftp_readdir(gate_sshclient_sftp_handle_t* sftp_handle, gate_file_entry_t* file_entry)
{
GATE_UNUSED_ARG(sftp_handle);
GATE_UNUSED_ARG(file_entry);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_sftp_listdir(gate_sshclient_sftp_t* sftp, gate_string_t const* path,
gate_file_list_callback_t callback, void* userdata)
{
GATE_UNUSED_ARG(sftp);
GATE_UNUSED_ARG(path);
GATE_UNUSED_ARG(callback);
GATE_UNUSED_ARG(userdata);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_sftp_mkdir(gate_sshclient_sftp_t* sftp, gate_string_t const* path)
{
GATE_UNUSED_ARG(sftp);
GATE_UNUSED_ARG(path);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_sftp_rmdir(gate_sshclient_sftp_t* sftp, gate_string_t const* path)
{
GATE_UNUSED_ARG(sftp);
GATE_UNUSED_ARG(path);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_sftp_unlink(gate_sshclient_sftp_t* sftp, gate_string_t const* path)
{
GATE_UNUSED_ARG(sftp);
GATE_UNUSED_ARG(path);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_sftp_rename(gate_sshclient_sftp_t* sftp, gate_string_t const* src_path, gate_string_t const* dst_path)
{
GATE_UNUSED_ARG(sftp);
GATE_UNUSED_ARG(src_path);
GATE_UNUSED_ARG(dst_path);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_sftp_stat(gate_sshclient_sftp_t* sftp, gate_string_t const* path, gate_file_properties_t* file_props)
{
GATE_UNUSED_ARG(sftp);
GATE_UNUSED_ARG(path);
GATE_UNUSED_ARG(file_props);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_channel_open(gate_sshclient_t* client, gate_sshclient_channel_t* ptr_channel)
{
GATE_UNUSED_ARG(client);
GATE_UNUSED_ARG(ptr_channel);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_channel_close(gate_sshclient_channel_t* channel)
{
GATE_UNUSED_ARG(channel);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_channel_free(gate_sshclient_channel_t* channel)
{
GATE_UNUSED_ARG(channel);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_channel_exec(gate_sshclient_channel_t* channel, gate_string_t const* command)
{
GATE_UNUSED_ARG(channel);
GATE_UNUSED_ARG(command);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_channel_canread(gate_sshclient_channel_t* channel, gate_enumint_t stream_type, gate_bool_t* ptr_canread)
{
GATE_UNUSED_ARG(channel);
GATE_UNUSED_ARG(stream_type);
GATE_UNUSED_ARG(ptr_canread);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_channel_read(gate_sshclient_channel_t* channel, gate_enumint_t stream_type, char* buffer, gate_size_t bufferlen, gate_size_t* bufferused)
{
GATE_UNUSED_ARG(channel);
GATE_UNUSED_ARG(stream_type);
GATE_UNUSED_ARG(buffer);
GATE_UNUSED_ARG(bufferlen);
GATE_UNUSED_ARG(bufferused);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_channel_write(gate_sshclient_channel_t* channel, gate_enumint_t stream_type, char const* buffer, gate_size_t bufferlen, gate_size_t* bufferprocessed)
{
GATE_UNUSED_ARG(channel);
GATE_UNUSED_ARG(stream_type);
GATE_UNUSED_ARG(buffer);
GATE_UNUSED_ARG(bufferlen);
GATE_UNUSED_ARG(bufferprocessed);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_bool_t gate_sshclient_channel_eof(gate_sshclient_channel_t* channel)
{
GATE_UNUSED_ARG(channel);
return false;
}
gate_result_t gate_sshclient_channel_send_eof(gate_sshclient_channel_t* channel)
{
GATE_UNUSED_ARG(channel);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_channel_wait_eof(gate_sshclient_channel_t* channel)
{
GATE_UNUSED_ARG(channel);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_channel_setenv(gate_sshclient_channel_t* channel, gate_string_t const* name, gate_string_t const* value)
{
GATE_UNUSED_ARG(channel);
GATE_UNUSED_ARG(name);
GATE_UNUSED_ARG(value);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_channel_get_exit_signal(gate_sshclient_channel_t* channel, gate_string_t* ptr_signal, gate_string_t* ptr_errmsg, gate_string_t* ptr_langtag)
{
GATE_UNUSED_ARG(channel);
GATE_UNUSED_ARG(ptr_signal);
GATE_UNUSED_ARG(ptr_errmsg);
GATE_UNUSED_ARG(ptr_langtag);
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_sshclient_channel_get_exit_status(gate_sshclient_channel_t* channel, int* ptr_status)
{
GATE_UNUSED_ARG(channel);
GATE_UNUSED_ARG(ptr_status);
return GATE_RESULT_NOTIMPLEMENTED;
}
#endif
|