summaryrefslogtreecommitdiff
path: root/addons/smtp/nxd_smtp_client.c
blob: dbadbac78662c9f527587d4d450543b5467714d4 (plain)
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
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/

/**************************************************************************/
/**************************************************************************/
/**                                                                       */ 
/**                                                                       */  
/** NetX SMTP Client Component                                            */
/**                                                                       */
/**   Simple Mail Transfer Protocol (SMTP)                                */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


#define NX_SMTP_SOURCE_CODE


/* Force error checking to be disabled in this module.  */

#ifndef NX_DISABLE_ERROR_CHECKING
#define NX_DISABLE_ERROR_CHECKING
#endif



#include "ctype.h"
#include "nx_api.h"
#include "nx_ip.h"
#include "nx_ipv4.h"
#include "nxd_smtp_client.h"
#ifdef FEATURE_NX_IPV6
#include "nx_ipv6.h"
#endif
#include "nx_tcp.h"

/* Necessary for threadx thread state macros. */
extern  TX_THREAD   *_tx_thread_current_ptr; 
extern  TX_THREAD    _tx_timer_thread; 
extern  volatile ULONG _tx_thread_system_state;

#define         NX_SMTP_BUFFER_SIZE           512
static CHAR     _nx_smtp_buffer[NX_SMTP_BUFFER_SIZE]; 


/* Define internal SMTP Client functions.  */

static VOID _nx_smtp_find_crlf(UCHAR *buffer, UINT length, UCHAR **CRLF, UINT reverse);
static UINT _nx_smtp_cmd_idle(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_rsp_idle(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_cmd_greeting(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_rsp_greeting(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_cmd_ehlo(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_rsp_ehlo(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_cmd_helo(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_rsp_helo(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_cmd_mail(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_rsp_mail(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_cmd_rcpt(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_rsp_rcpt(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_cmd_data(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_rsp_data(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_cmd_message(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_rsp_message(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_cmd_rset(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_rsp_rset(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_cmd_quit(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_rsp_quit(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_cmd_noop(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_rsp_noop(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_cmd_auth(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_rsp_auth(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_cmd_auth_challenge(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_rsp_auth_challenge(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_rsp_hello_command(NX_SMTP_CLIENT* client_ptr);
static UINT _nx_smtp_utility_read_server_code(NX_SMTP_CLIENT *client_ptr, ULONG timeout, UINT  receive_all_lines);
static UINT _nx_smtp_utility_send_to_server(NX_SMTP_CLIENT *client_ptr, CHAR *buffer_ptr, UINT buffer_length, ULONG timeout);
static UINT _nx_smtp_utility_authentication_challenge(NX_SMTP_CLIENT *client_ptr, UCHAR *buffer_ptr, UINT length);  
static UINT _nx_smtp_client_process(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_utility_send_header_to_server(NX_SMTP_CLIENT *client_ptr, ULONG timeout) ;
static VOID _nx_smtp_utility_parse_server_services(NX_SMTP_CLIENT *client_ptr);
static UINT _nx_smtp_parse_250_response(UCHAR *buffer_ptr, UINT buffer_length, UINT *is_last_code);
static UINT _nx_smtp_parse_response(NX_SMTP_CLIENT *client_ptr, UCHAR *buffer, UINT arguement_index, 
                                    UINT buffer_length, UCHAR *arguement, UINT arguement_length, 
                                    UINT include_crlf);

static NX_SMTP_CLIENT_STATES protocol_states[] =
{
    {_nx_smtp_cmd_idle               , _nx_smtp_rsp_idle},
    {_nx_smtp_cmd_greeting           , _nx_smtp_rsp_greeting},
    {_nx_smtp_cmd_ehlo               , _nx_smtp_rsp_ehlo},
    {_nx_smtp_cmd_helo               , _nx_smtp_rsp_helo},
    {_nx_smtp_cmd_mail               , _nx_smtp_rsp_mail},
    {_nx_smtp_cmd_rcpt               , _nx_smtp_rsp_rcpt},
    {_nx_smtp_cmd_data               , _nx_smtp_rsp_data},
    {_nx_smtp_cmd_message            , _nx_smtp_rsp_message},
    {_nx_smtp_cmd_rset               , _nx_smtp_rsp_rset},
    {_nx_smtp_cmd_quit               , _nx_smtp_rsp_quit},
    {_nx_smtp_cmd_noop               , _nx_smtp_rsp_noop},
    {_nx_smtp_cmd_auth               , _nx_smtp_rsp_auth},
    {_nx_smtp_cmd_auth_challenge     , _nx_smtp_rsp_auth_challenge},     
};




/**************************************************************************/ 
/*                                                                        */ 
/*  FUNCTION                                               RELEASE        */ 
/*                                                                        */ 
/*    _nxde_smtp_client_create                            PORTABLE C      */ 
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */ 
/*                                                                        */ 
/*    This function performs error checking on the client create service. */ 
/*                                                                        */   
/*    Note: The string lengths of username,password,from_address and      */
/*    client_domain are limited by internal buffer size.                  */
/*                                                                        */ 
/*  INPUT                                                                 */ 
/*                                                                        */ 
/*                                                                        */ 
/*    client_ptr                        Pointer to SMTP Client instance   */ 
/*    ip_ptr                            Pointer to IP instance            */
/*    packet_pool_ptr                   Pointer to client packet pool     */
/*    username                          Pointer to username               */ 
/*    password                          Pointer to password               */ 
/*    from_address                      Pointer to Client email address   */
/*    client_domain                     Pointer to client domain          */ 
/*    authentication_type               SMTP authentication type          */ 
/*    server_address                    Pointer to server address         */ 
/*    server_port                       SMTP server TCP port              */
/*                                                                        */ 
/*  OUTPUT                                                                */ 
/*                                                                        */ 
/*    NX_PTR_ERROR                       Invalid pointer parameter        */ 
/*    NX_SMTP_INVALID_PARAM              Invalid non pointer input        */
/*    status                             Actual completion status         */ 
/*                                                                        */ 
/*  CALLS                                                                 */ 
/*                                                                        */ 
/*    _nxd_smtp_client_create           Actual SMTP client create service */ 
/*                                                                        */ 
/*  CALLED BY                                                             */ 
/*                                                                        */ 
/*    Application Code                                                    */ 
/*                                                                        */ 
/**************************************************************************/
UINT  _nxde_smtp_client_create(NX_SMTP_CLIENT *client_ptr, NX_IP *ip_ptr, NX_PACKET_POOL *client_packet_pool_ptr, 
                               CHAR *username, CHAR *password, CHAR *from_address,
                               CHAR *client_domain, UINT authentication_type, 
                               NXD_ADDRESS *server_address, UINT port)
{

UINT status;


    if ((ip_ptr == NX_NULL) || (client_ptr == NX_NULL) || (client_packet_pool_ptr == NX_NULL) || 
        (from_address == NX_NULL) || (username == NX_NULL) || (password == NX_NULL) ||
        (client_domain == NX_NULL) || (server_address == NX_NULL))
    {

        /* Return error status.  */
       return(NX_PTR_ERROR);
    }

        /* Check for invalid non pointer input. */
    if (ip_ptr -> nx_ip_id != NX_IP_ID)
    {

        return NX_SMTP_INVALID_PARAM;
    }

    
    /* Make sure the IP version is set correctly. */
    if((server_address -> nxd_ip_version != NX_IP_VERSION_V4) &&
       (server_address -> nxd_ip_version != NX_IP_VERSION_V6))
    {
        return(NX_IP_ADDRESS_ERROR);
    }

    /* Call the actual client create service.  */
    status =  _nxd_smtp_client_create(client_ptr, ip_ptr, client_packet_pool_ptr, username, password,
                                      from_address, client_domain, authentication_type, 
                                      server_address, port);

    /* Return completion status.  */
    return(status);
}


/**************************************************************************/ 
/*                                                                        */ 
/*  FUNCTION                                               RELEASE        */ 
/*                                                                        */ 
/*    _nxd_smtp_client_create                             PORTABLE C      */ 
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */ 
/*                                                                        */ 
/*    This function creates a SMTP client instance for sending mail to an */
/*    SMTP Server over IPv4 or IPv6 networks. It also creates the TCP     */
/*    socket for making connections with the SMTP server, sets the        */
/*    server IP address and port, and sets the Client username and SMTP   */
/*    address (from address) used in all SMTP mail transmissions.         */ 
/*                                                                        */   
/*    Note: The string lengths of username,password,from_address and      */
/*    client_domain are limited by internal buffer size.                  */
/*                                                                        */ 
/*   INPUT                                                                */ 
/*                                                                        */ 
/*    client_ptr                        Pointer to SMTP Client instance   */ 
/*    ip_ptr                            Pointer to IP instance            */
/*    packet_pool_ptr                   Pointer to client packet pool     */
/*    username                          Pointer to username               */ 
/*    password                          Pointer to password               */ 
/*    from_address                      Pointer to Client email address   */
/*    client_domain                     Pointer to client domain          */ 
/*    authentication_type               SMTP authentication type          */ 
/*    server_address                    Pointer to server address         */ 
/*    server_port                       SMTP server TCP port              */
/*                                                                        */ 
/*  OUTPUT                                                                */ 
/*                                                                        */ 
/*    NX_SUCCESS                        Successful completion status      */ 
/*    status                            Actual completion status          */
/*                                                                        */ 
/*  CALLED BY                                                             */ 
/*                                                                        */ 
/*    Application Code                                                    */ 
/*                                                                        */ 
/*  CALLS                                                                 */ 
/*                                                                        */ 
/*    memset                            Clear area of memory              */ 
/*    memcpy                            Copy data to area of memory       */
/*    nx_tcp_socket_create              Create a NetX TCP socket          */ 
/*                                                                        */ 
/**************************************************************************/
UINT  _nxd_smtp_client_create(NX_SMTP_CLIENT *client_ptr, NX_IP *ip_ptr, NX_PACKET_POOL *client_packet_pool_ptr, 
                              CHAR *username, CHAR *password, CHAR *from_address,
                              CHAR *client_domain, UINT authentication_type, 
                              NXD_ADDRESS *server_address, UINT port)
{                            

UINT status = NX_SUCCESS;
UINT str_length;
NX_SMTP_CLIENT_MAIL    *mail_ptr;


    /* Initialize client as not ready for SMTP yet. */
    client_ptr -> nx_smtp_client_init_status = NX_FALSE;

    mail_ptr = &client_ptr -> nx_smtp_client_mail;

    /* Clear the Client and session memory.  */
    memset(client_ptr, 0, sizeof(NX_SMTP_CLIENT));

    /* Configure client with input parameters.  */
    status = _nx_utility_string_length_check(username, &str_length, NX_SMTP_CLIENT_MAX_USERNAME);
    if (status)
    {
        return(status);
    }

    memcpy(client_ptr -> nx_smtp_username, username, str_length); /* Use case of memcpy is verified. */

    status = _nx_utility_string_length_check(password, &str_length, NX_SMTP_CLIENT_MAX_PASSWORD);
    if (status)
    {
        return(status);
    }

    memcpy(client_ptr -> nx_smtp_password, password, str_length); /* Use case of memcpy is verified. */

    status = _nx_utility_string_length_check(client_domain, &str_length, NX_SMTP_CLIENT_MAX_USERNAME);
    if (status)
    {
        return(status);
    }

    memcpy(client_ptr -> nx_smtp_client_domain, client_domain, str_length); /* Use case of memcpy is verified. */

    /* Set the mail server IP address and port number.  */

#ifdef FEATURE_NX_IPV6
    client_ptr -> nx_smtp_server_address.nxd_ip_version = server_address -> nxd_ip_version;
    client_ptr -> nx_smtp_server_address.nxd_ip_address.v6[0] = server_address -> nxd_ip_address.v6[0] ;
    client_ptr -> nx_smtp_server_address.nxd_ip_address.v6[1] = server_address -> nxd_ip_address.v6[1] ;
    client_ptr -> nx_smtp_server_address.nxd_ip_address.v6[2] = server_address -> nxd_ip_address.v6[2] ;
    client_ptr -> nx_smtp_server_address.nxd_ip_address.v6[3] = server_address -> nxd_ip_address.v6[3] ;
#else

    client_ptr -> nx_smtp_server_address.nxd_ip_version = server_address -> nxd_ip_version;
    client_ptr -> nx_smtp_server_address.nxd_ip_address.v4 = server_address -> nxd_ip_address.v4;
#endif
    client_ptr -> nx_smtp_server_port = (port & 0xFFFF);

    /* Set up the "from" address. */
    mail_ptr -> nx_smtp_client_mail_from_address = from_address;

    /* Check if authentication type is specified. */
    if ((authentication_type != NX_SMTP_CLIENT_AUTH_PLAIN) &&
        (authentication_type != NX_SMTP_CLIENT_AUTH_LOGIN) &&
        (authentication_type != NX_SMTP_CLIENT_AUTH_NONE))
    {

        /* No. Set the default authentication type. */
        authentication_type = NX_SMTP_CLIENT_AUTH_PLAIN;
    }

    client_ptr -> nx_smtp_client_authentication_type = authentication_type;

    /* Configure client IP options.  */
    client_ptr -> nx_smtp_client_ip_ptr   =  ip_ptr;
    client_ptr -> nx_smtp_client_packet_pool_ptr =  client_packet_pool_ptr;

    /* Set the Client ID to indicate the SMTP client thread is ready.  */
    client_ptr -> nx_smtp_client_id = NX_SMTP_CLIENT_ID;

    /* Create a tcp socket to send/receive SMTP data.  */
    status =  nx_tcp_socket_create(client_ptr -> nx_smtp_client_ip_ptr,
                                   &client_ptr -> nx_smtp_client_socket, "SMTP Client socket",
                                   NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, 
                                   NX_SMTP_CLIENT_TCP_WINDOW_SIZE,
                                   NX_NULL, NX_NULL);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        /* Return error status.  */
        return(status);
    }

    /* Initialize client as ready for conducting an SMTP session. */
    client_ptr -> nx_smtp_client_init_status = NX_TRUE;

    return(status);
}


/**************************************************************************/ 
/*                                                                        */ 
/*  FUNCTION                                               RELEASE        */ 
/*                                                                        */ 
/*    _nxe_smtp_client_delete                             PORTABLE C      */ 
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */ 
/*                                                                        */ 
/*    This function checks for errors on the client delete service.       */ 
/*                                                                        */ 
/*   INPUT                                                                */ 
/*                                                                        */ 
/*    client_ptr                         Pointer to client struct         */ 
/*                                                                        */ 
/*  OUTPUT                                                                */ 
/*                                                                        */ 
/*    NX_PTR_ERROR                       Invalid pointer parameter        */ 
/*    status                             Actual completion status         */ 
/*                                                                        */ 
/*  CALLS                                                                 */ 
/*                                                                        */ 
/*    _nx_smtp_client_delete            Actual SMTP client delete service */ 
/*                                                                        */ 
/*  CALLED BY                                                             */ 
/*                                                                        */ 
/*    Application Code                                                    */ 
/*                                                                        */ 
/**************************************************************************/
UINT  _nxe_smtp_client_delete(NX_SMTP_CLIENT *client_ptr)
{

UINT status;


    /* Check for the validity of input parameter.  */
    if ((client_ptr == NX_NULL) || (client_ptr -> nx_smtp_client_id != NX_SMTP_CLIENT_ID))
    {

        /* Return error status.  */
        return(NX_PTR_ERROR);
    }

    /* Call the actual client create service.  */
    status =  _nx_smtp_client_delete(client_ptr);

    /* Return completion status.  */
    return(status);
}


/**************************************************************************/ 
/*                                                                        */ 
/*  FUNCTION                                               RELEASE        */ 
/*                                                                        */ 
/*    _nx_smtp_client_delete                              PORTABLE C      */ 
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */ 
/*                                                                        */ 
/*    This function deletes a previously created SMTP client and releases */
/*    all client resources (sockets, packet pools).                       */
/*                                                                        */ 
/*   INPUT                                                                */ 
/*                                                                        */ 
/*    client_ptr                        Pointer to SMTP Client instance   */ 
/*                                                                        */ 
/*  OUTPUT                                                                */ 
/*                                                                        */ 
/*    NX_SUCCESS                        Successful completion status      */ 
/*    status                            Actual completion status          */ 
/*                                                                        */ 
/*  CALLS                                                                 */ 
/*                                                                        */ 
/*    _nx_smtp_session_delete           Delete the SMTP Client session    */
/*                                                                        */ 
/*  CALLED BY                                                             */ 
/*                                                                        */ 
/*    Application Code                                                    */ 
/*                                                                        */ 
/**************************************************************************/
UINT  _nx_smtp_client_delete(NX_SMTP_CLIENT *client_ptr)
{

UINT status;


    if ((client_ptr  -> nx_smtp_client_socket).nx_tcp_socket_state == NX_TCP_ESTABLISHED)
    {

        nx_tcp_socket_disconnect(&client_ptr -> nx_smtp_client_socket, NX_SMTP_CLIENT_DISCONNECT_TIMEOUT);

        nx_tcp_client_socket_unbind(&client_ptr -> nx_smtp_client_socket);
    }

    status =  nx_tcp_socket_delete(&(client_ptr -> nx_smtp_client_socket));

    /* Check status.  */
    if (status)
    {        
        return status;
    }

    /* Clear client memory. */
    memset(client_ptr, 0, sizeof(NX_SMTP_CLIENT));

    /* Return success status.  */
    return(NX_SUCCESS);
}



/**************************************************************************/ 
/*                                                                        */ 
/*  FUNCTION                                               RELEASE        */ 
/*                                                                        */ 
/*    _nxe_smtp_mail_send                                 PORTABLE C      */ 
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */ 
/*                                                                        */ 
/*    This function checks for errors on the mail send service.           */ 
/*                                                                        */   
/*    Note: The string lengths of recipient_address and subject are       */
/*    limited by internal buffer size.                                    */
/*                                                                        */ 
/*   INPUT                                                                */ 
/*                                                                        */ 
/*    client_ptr                      Pointer to Client instance          */ 
/*    recipient_address               Pointer to recipient (To) address   */
/*    priority                        Mail send priority level            */ 
/*    subject                         Pointer to mail subject text        */
/*    mail_body                       Pointer to mail message text        */
/*    mail_body_length                Size of mail message text           */
/*                                                                        */ 
/*  OUTPUT                                                                */ 
/*                                                                        */ 
/*    NX_PTR_ERROR                    Invalid pointer parameter           */ 
/*    status                          Actual completion status            */ 
/*                                                                        */ 
/*  CALLS                                                                 */ 
/*                                                                        */ 
/*    _nx_smtp_mail_send              Actual SMTP mail send service       */ 
/*                                                                        */ 
/*  CALLED BY                                                             */ 
/*                                                                        */ 
/*    Application Code                                                    */ 
/*                                                                        */ 
/**************************************************************************/
UINT  _nxe_smtp_mail_send(NX_SMTP_CLIENT *client_ptr, CHAR *recipient_address, UINT priority, 
                          CHAR *subject, CHAR *mail_body, UINT mail_body_length)
{

UINT  status;


    /* Check for invalid pointer input. */
    if((client_ptr == NX_NULL) || (recipient_address == NX_NULL) || 
       (mail_body == NX_NULL) || (subject == NX_NULL))

    {
        return NX_PTR_ERROR;
    }

    if (mail_body_length == 0) 
    {
        return NX_SMTP_INVALID_PARAM;
    }

    /* Check if this function is called from the appropriate thread.  */
    NX_THREADS_ONLY_CALLER_CHECKING

    status = _nx_smtp_mail_send(client_ptr, recipient_address,  priority, 
                                subject, mail_body, mail_body_length);

    return status;
}

/**************************************************************************/ 
/*                                                                        */ 
/*  FUNCTION                                               RELEASE        */ 
/*                                                                        */ 
/*    _nx_smtp_mail_send                                  PORTABLE C      */ 
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */ 
/*                                                                        */ 
/*    This function creates and sends an SMTP mail item with the input    */
/*    parameters for a previously created SMTP Client. It supports IPv4   */
/*    and IPv6 connections.                                               */
/*                                                                        */ 
/*    Before calling this service, the SMTP application must initialize   */
/*    the SMTP Client (nx_smtp_client_init). Thereafter it need not       */
/*    reinitialize the SMTP Client to send subsequent mail items.         */
/*                                                                        */
/*    This service assumes the syntax of the from_address is correct.     */
/*                                                                        */   
/*    Note: The string lengths of recipient_address and subject are       */
/*    limited by internal buffer size.                                    */
/*                                                                        */
/*   INPUT                                                                */ 
/*                                                                        */
/*    client_ptr                      Pointer to Client instance          */ 
/*    recipient_address               Pointer to recipient (To) address   */
/*    priority                        Mail send priority level            */ 
/*    subject                         Pointer to mail subject text        */
/*    mail_body                       Pointer to mail message text        */
/*    mail_body_length                Size of mail message text           */
/*                                                                        */
/*  OUTPUT                                                                */ 
/*                                                                        */ 
/*    NX_SUCCESS                      Success completion status           */ 
/*    status                          Actual completion status            */ 
/*                                                                        */ 
/*  CALLS                                                                 */ 
/*                                                                        */ 
/*    memset                          Clear data in memory                */ 
/*    _nx_smtp_client_process         Conducts SMTP session for           */
/*                                      transmitting mail message         */
/*                                                                        */ 
/*  CALLED BY                                                             */ 
/*                                                                        */ 
/*    Application Code                                                    */ 
/*                                                                        */ 
/**************************************************************************/
UINT  _nx_smtp_mail_send(NX_SMTP_CLIENT *client_ptr, 
                         CHAR *recipient_address, 
                         UINT priority, CHAR *subject, 
                         CHAR *mail_body, UINT mail_body_length)
{

UINT                        status;
NX_SMTP_CLIENT_MAIL         *mail_ptr;


    client_ptr -> nx_smtp_client_mute = NX_FALSE;

    /* Verify Client is properly set up and ready to conduct an SMTP session. */
    if (client_ptr -> nx_smtp_client_init_status != NX_TRUE)
    {
        return NX_SMTP_CLIENT_NOT_INTIALIZED;
    }

    /* Initialize the Client to idle. */
    client_ptr -> nx_smtp_client_cmd_state = NX_SMTP_CLIENT_STATE_IDLE;
    client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_IDLE;


    /* Set local variables for convenience. */
    mail_ptr = &client_ptr -> nx_smtp_client_mail;

    /* Did we get a valid priority type?  */
    if ((priority != NX_SMTP_MAIL_PRIORITY_LOW)    &&
        (priority != NX_SMTP_MAIL_PRIORITY_NORMAL) &&
        (priority != NX_SMTP_MAIL_PRIORITY_HIGH))
    {

        /* No, default priority to normal.  */
        mail_ptr -> nx_smtp_client_mail_priority =  NX_SMTP_MAIL_PRIORITY_NORMAL;
    }
    else
    {

        /* Yes, apply it to the session priority.  */
        mail_ptr -> nx_smtp_client_mail_priority = priority;
    }
    mail_ptr -> nx_smtp_client_mail_subject = subject;
    mail_ptr -> nx_smtp_client_mail_body = mail_body;
    mail_ptr -> nx_smtp_client_mail_body_length = mail_body_length;
    mail_ptr -> nx_smtp_client_mail_recipient_address = recipient_address;

    /* Mark mail item as unsent */
    client_ptr -> nx_smtp_client_mail_status = NX_SUCCESS;

    /* Set up the recipient. */

    /* Set up a local pointer to the session for convenience. */

    /* Set session members to initial values.  */
    client_ptr -> nx_smtp_client_authentication_state = NX_SMTP_NOT_AUTHENTICATED;
    client_ptr -> nx_smtp_client_reply_code_status = 0;

    /* Start this session by setting the state  to the first step in SMTP Protocol, greeting.  */
    client_ptr -> nx_smtp_client_cmd_state =  NX_SMTP_CLIENT_STATE_GREETING;
    client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_GREETING;

    status = _nx_smtp_client_process(client_ptr);

    /* Return success status.  */
    return(status);

}

/**************************************************************************/ 
/*                                                                        */ 
/*  FUNCTION                                               RELEASE        */ 
/*                                                                        */ 
/*    _nx_smtp_client_process                             PORTABLE C      */ 
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */ 
/*                                                                        */ 
/*    This function executes the SMTP client state machine to transmit a  */
/*    previously created mail item to an SMTP server. For more details,   */
/*    See nx_smtp_mail_send.                                              */
/*                                                                        */ 
/*   INPUT                                                                */ 
/*                                                                        */ 
/*    client_ptr                      Pointer to SMTP Client instance     */ 
/*                                                                        */ 
/*  OUTPUT                                                                */ 
/*                                                                        */ 
/*    NX_SUCCESS                      Success completion status           */ 
/*    status                          Actual completion status            */ 
/*                                                                        */ 
/*  CALLS                                                                 */ 
/*    nx_tcp_client_socket_bind       Bind NetX TCP socket to local port  */
/*    nx_tcp_client_socket_unbind     Release port from NetX TCP socket   */
/*    nx_tcp_client_socket_connect    Connect to a TCP server socket      */
/*    nx_tcp_client_socket_disconnect Disconnect from TCP server socket   */
/*                                                                        */ 
/*  CALLED BY                                                             */ 
/*                                                                        */ 
/*    Application Code                                                    */ 
/*                                                                        */ 
/**************************************************************************/
static UINT _nx_smtp_client_process(NX_SMTP_CLIENT *client_ptr)
{

UINT                    status;
UINT                    close_connection = NX_FALSE;


    /* Initialize completion status to successful outcome. */
    status = NX_SUCCESS;

    /* Run the SMTP protocol state machine till session terminates.  */
    while (status == NX_SUCCESS)  
    {

        /* Check if we are starting a mail transaction. */
        if (client_ptr -> nx_smtp_client_cmd_state == NX_SMTP_CLIENT_STATE_GREETING)
        {

            /* We are so we need to set up a connection. Bind the socket to client port.  */
            status =  nx_tcp_client_socket_bind(&client_ptr -> nx_smtp_client_socket, NX_ANY_PORT, 100);

            /* Check for error.  */
            if (status != NX_SUCCESS)
            {
        
                /* Reset the Client to idle. The caller must resend the email. */
                client_ptr -> nx_smtp_client_cmd_state = NX_SMTP_CLIENT_STATE_IDLE;
                client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_IDLE;

                return status;
            }

            /* Connect to the SMTP server using our tcp socket.  */
            status =  nxd_tcp_client_socket_connect(&client_ptr -> nx_smtp_client_socket, 
                                                    &client_ptr -> nx_smtp_server_address,
                                                    client_ptr -> nx_smtp_server_port, 
                                                    NX_SMTP_CLIENT_CONNECTION_TIMEOUT);
           
            /* Check for error.  */
            if (status != NX_SUCCESS)
            {   

                nx_tcp_client_socket_unbind(&client_ptr -> nx_smtp_client_socket);

                /* Reset the Client to idle. The caller must resend the email.*/
                client_ptr -> nx_smtp_client_cmd_state = NX_SMTP_CLIENT_STATE_IDLE;
                client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_IDLE;
                return status; 
            }

            /* Successful connection set up. Now let the Client task process the GREETING command. */
        }

        /* Is the next command waiting the outcome of the previous reply handler?  */
        if ((client_ptr -> nx_smtp_client_cmd_state == NX_SMTP_CLIENT_STATE_AWAITING_REPLY) &&
            /* Do not advance the state if the client is in mute. */
            (!client_ptr -> nx_smtp_client_mute))
        {

            /* Yes, update the session state to the next command to send.  */
            client_ptr -> nx_smtp_client_cmd_state =  client_ptr -> nx_smtp_client_rsp_state;
        }

        if (!client_ptr -> nx_smtp_client_mute)
        {

            /* Execute the next command in the SMTP state machine.  */
            status =  (*protocol_states[client_ptr -> nx_smtp_client_cmd_state].cmd)(client_ptr);
    
            /* Check for internal error state in the SMTP state machine.  */
            if (status != NX_SUCCESS)
            {
    
                /* This is likely an internal error and we need to break off the connection, reset the Client state
                   to an idle state.  */
    
                /* Set the status to close the connection down. */
                close_connection = NX_TRUE;
            }
        }

        /* Reset server reply to null. */
        client_ptr -> nx_smtp_client_reply_code_status = 0;

        /* Wait for a response unless we are closing the connection, or have
           encountered an internal error (packet allocation error for example). */
        if (close_connection == NX_FALSE)
        {    

            /* Awaiting the server reply to the command just sent.  */
            status = (*protocol_states[client_ptr -> nx_smtp_client_rsp_state].rsp)(client_ptr);
    
            /* Check for internal error state in the SMTP state machine.  */
            if (status != NX_SUCCESS)
            {
    
                /* This is an internal error or invalid server reply of some kind. Just shut down the
                   connection, notify the host application and set the Client to idle. */
    
                /* Set the status to close the connection down. */
                close_connection = NX_TRUE;
            }
    
            /* Are we at the end of the Client state (received a reply to our QUIT message to the server)? */
            else if (client_ptr -> nx_smtp_client_rsp_state == NX_SMTP_CLIENT_STATE_COMPLETED_NORMALLY) 
            {
    
                /* Yes, time to close it down and return to an idle state. */
                close_connection = NX_TRUE;
            }
            /* else keep the connection open... */
        }

        /* Check again if we need to break down the connection. */
        if (close_connection == NX_TRUE)
        {

            UINT timeout = 0;

            /* We do. Depending on the reason for closing the connection, set the timeout. If we experienced
               an internal error, e.g. packet allocation error, we probably won't be able to send
               a fin or rst packet anyway, so set the timeout to zero and close down the connection.

               RFC 2821 Section 3.9: if client must abort processing due to internal conditions or socket reset,
               it should handle as error code 451 from the server which is to abort processing immediately.
             */   
            if (status != NX_SUCCESS)
            {
                timeout = NX_NO_WAIT;
            }
            else
            {
                timeout =  NX_SMTP_CLIENT_DISCONNECT_TIMEOUT;
            }

            /* Disconnect client socket from server.  */
            nx_tcp_socket_disconnect(&client_ptr -> nx_smtp_client_socket, timeout);
    
            /* Unbind the port from client socket.  */
            nx_tcp_client_socket_unbind(&client_ptr -> nx_smtp_client_socket);

            /* Indicate the Client is idle. */
            client_ptr -> nx_smtp_client_cmd_state = NX_SMTP_CLIENT_STATE_IDLE;
            client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_IDLE;

            /* Did the session terminate normally (e.g. no internal errors)? */
            if (status == NX_SUCCESS)
            {

                /* Yes, so set the mail transmit status as the completion status. */
                status = client_ptr -> nx_smtp_client_mail_status;
            }

            return status;  
        } 

        /* Clear the abort status. */
        close_connection = NX_FALSE;
    } 

    return status;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_cmd_greeting                               PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This is the start of the SMTP Client process for sending an mail    */
/*    item. It sets the client state to send a greeting command to the    */
/*    server.                                                             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_cmd_greeting(NX_SMTP_CLIENT *client_ptr)
{


    /* Set session state to wait on the outcome of the greeting response handler.  */
    client_ptr -> nx_smtp_client_cmd_state =  NX_SMTP_CLIENT_STATE_AWAITING_REPLY;

    /* Return successful session status.  */
    return(NX_SUCCESS);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_cmd_idle                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    Execute the idle state of the SMTP Client (do nothing while waiting */
/*    for the next send mail request.                                     */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_cmd_idle(NX_SMTP_CLIENT *client_ptr)
{

    client_ptr -> nx_smtp_client_cmd_state =  NX_SMTP_CLIENT_STATE_IDLE;

    /* Return successful session status.  */
    return(NX_SUCCESS);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_rsp_idle                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    Execute the idle state of the SMTP Client (waiting for the next     */
/*    send mail request).  The state machine has a 'response' state for   */
/*    every command state, so this is the response handler for the idle   */
/*    state.                                                              */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_rsp_idle(NX_SMTP_CLIENT *client_ptr)
{

    client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_IDLE;  

    /* Return successful session status.  */
    return(NX_SUCCESS);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_rsp_greeting                               PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This handles the server's response to the client greeting and set   */
/*    the next session state and next command to send to the server.      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                   Session to send mail to SMTP Server   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                    Successful completion status          */
/*    status                        Actual completion status              */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_utility_read_server_code                                   */
/*                                  Extracts the server reply code and    */
/*                                  stores reply text to session buffer   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process        Runs the SMTP state machine          */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_rsp_greeting(NX_SMTP_CLIENT *client_ptr)
{

UINT     status;


    /* Get the server greeting message.  */
    status = _nx_smtp_utility_read_server_code(client_ptr, NX_SMTP_GREETING_TIMEOUT, NX_TRUE);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        /* Return error status.  */
        return (status);
    }

    /* Process based on the reply protocol code from server.  */
    if (client_ptr -> nx_smtp_client_reply_code_status == NX_SMTP_CODE_GREETING_OK)       
    {

        /* Yes, set session state to next state, EHLO.  */
        client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_EHLO;
    }
    else
    {
      
        /* All other codes.  */

        /* Server did not accept the greeting, exit session gracefully.  */
        client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_QUIT;
        
        /* Indicate mail cannot be sent. */
        client_ptr -> nx_smtp_client_mail_status = NX_SMTP_GREET_REPLY_ERROR;

        
    }

    if (client_ptr -> nx_smtp_server_packet)
    {
        nx_packet_release(client_ptr -> nx_smtp_server_packet);
    }
    /* Return successful session state.  */
    return(NX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_cmd_helo                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    Create the HELO command text and send to the SMTP server.           */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    status                                Actual completion status.     */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_utility_send_to_server       Sends data to server          */
/*    memcpy                                Copy data to area of memory   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_cmd_helo(NX_SMTP_CLIENT *client_ptr)
{

UINT     status;
UINT     index;
UINT     domain_length;


    memset(&_nx_smtp_buffer[0], 0, NX_SMTP_BUFFER_SIZE);

    if (_nx_utility_string_length_check(client_ptr -> nx_smtp_client_domain, &domain_length, NX_SMTP_CLIENT_MAX_USERNAME))
    {
        return(NX_SIZE_ERROR);
    }

    if (NX_SMTP_BUFFER_SIZE < (sizeof(NX_SMTP_COMMAND_HELO) + domain_length + sizeof(NX_SMTP_LINE_TERMINATOR) - 1))
    {

        /* Buffer size too small. */
        return(NX_SMTP_INTERNAL_ERROR);
    }

    /* Format the HELO command.  */
    memcpy(&_nx_smtp_buffer[0], NX_SMTP_COMMAND_HELO, sizeof(NX_SMTP_COMMAND_HELO) - 1); /* Use case of memcpy is verified. */
    index = sizeof(NX_SMTP_COMMAND_HELO) - 1;

    _nx_smtp_buffer[index++] = ' ';

    memcpy(&_nx_smtp_buffer[index],  client_ptr -> nx_smtp_client_domain, domain_length); /* Use case of memcpy is verified. */
    index += domain_length;
    memcpy(&_nx_smtp_buffer[index],  NX_SMTP_LINE_TERMINATOR, sizeof(NX_SMTP_LINE_TERMINATOR) - 1); /* Use case of memcpy is verified. */
    index += sizeof(NX_SMTP_LINE_TERMINATOR) - 1;

    /* Send the HELO command.  */
    status =  _nx_smtp_utility_send_to_server(client_ptr, _nx_smtp_buffer, index, NX_SMTP_CLIENT_SEND_TIMEOUT);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        return status;
    }

    /* Set session state to wait next response from server.  */
    client_ptr -> nx_smtp_client_cmd_state =  NX_SMTP_CLIENT_STATE_AWAITING_REPLY;

    /* Return normal session status.  */
    return NX_SUCCESS;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_rsp_helo                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function handles the server reply to the HELO command and set  */
/*    the next session state and next command to send to the server.  This*/
/*    is the GREETING state where we do not initiate the greeting.        */
/*    Instead we wait for the server's 'greeting' message.                */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                   Session to send mail to SMTP Server   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                    Successful completion status          */
/*    status                        Actual completion status              */ 
/*                                                                        */
/*  CALLS                                                                 */
/*    _nx_smtp_parse_response       Parse specified argument from buffer  */
/*    _nx_smtp_rsp_hello_command    Handle HELO/EHLO reply from Server    */
/*    nx_packet_release             Release NetX receive packet           */
/*    memset                        Clear specified area of memory        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process       Runs the SMTP state machine           */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_rsp_helo(NX_SMTP_CLIENT *client_ptr)
{

UINT        status;

    /* Get the server response to the EHLO command.  */
    status = _nx_smtp_utility_read_server_code(client_ptr, NX_SMTP_ENVELOPE_TIMEOUT, NX_TRUE);

    /* Check for error.  */
    if (status != NX_SUCCESS) 
    { 

        /* This is either no packet received or an invalid server response.  */ 

        /* Return error status.  */ 
        return status; 
    } 

    /* Update session with specific services offered, if any, by server.  */
    _nx_smtp_utility_parse_server_services(client_ptr);

    /* We are done with the packet now. */
    nx_packet_release(client_ptr -> nx_smtp_server_packet);

        /* if server rejected hello, set the session state to terminate */
    if (client_ptr -> nx_smtp_client_rsp_state == NX_SMTP_CLIENT_STATE_QUIT)
    {

        /* Return successful session status to execute QUIT command.  */
        return NX_SUCCESS;
    }

    /* Handle the server reply code first.  */
    _nx_smtp_rsp_hello_command(client_ptr);

    /* if server rejected hello, set the session state to terminate */
    if (client_ptr -> nx_smtp_client_rsp_state == NX_SMTP_CLIENT_STATE_QUIT)
    {

        /* Return successful session status to execute QUIT command.  */
        return NX_SUCCESS;
    }

    /* Server accepted the HELO command. Continue the SMTP session.  */

    /* Return successful completion status.  */
    return NX_SUCCESS;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_cmd_ehlo                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function creates the EHLO command and sends it to the server.  */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    status                                Actual completion status      */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_utility_send_to_server       Send data to server           */
/*    memcpy                                Copy data to area of memory   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_cmd_ehlo(NX_SMTP_CLIENT *client_ptr)
{

UINT     status;
UINT     index;
UINT     domain_length;

    
    memset(&_nx_smtp_buffer[0], 0, NX_SMTP_BUFFER_SIZE);

    if (_nx_utility_string_length_check(client_ptr -> nx_smtp_client_domain, &domain_length, NX_SMTP_CLIENT_MAX_USERNAME))
    {
        return(NX_SIZE_ERROR);
    }

    if (NX_SMTP_BUFFER_SIZE < (sizeof(NX_SMTP_COMMAND_EHLO) + domain_length + sizeof(NX_SMTP_LINE_TERMINATOR) - 1))
    {

        /* Buffer size too small. */
        return(NX_SMTP_INTERNAL_ERROR);
    }
    
    /* Format the EHLO command.  */
    memcpy(&_nx_smtp_buffer[0],  NX_SMTP_COMMAND_EHLO, sizeof(NX_SMTP_COMMAND_EHLO) - 1); /* Use case of memcpy is verified. */
    index = sizeof(NX_SMTP_COMMAND_EHLO) - 1;

    _nx_smtp_buffer[index++] = ' ';
    memcpy(&_nx_smtp_buffer[index],  client_ptr -> nx_smtp_client_domain, domain_length); /* Use case of memcpy is verified. */
    index += domain_length;
    memcpy(&_nx_smtp_buffer[index],  NX_SMTP_LINE_TERMINATOR, sizeof(NX_SMTP_LINE_TERMINATOR) - 1); /* Use case of memcpy is verified. */
    index += sizeof(NX_SMTP_LINE_TERMINATOR) - 1;
    
    /* Send the EHLO command.  */
    status = _nx_smtp_utility_send_to_server(client_ptr, _nx_smtp_buffer, index, NX_SMTP_CLIENT_SEND_TIMEOUT);
    
    /* Check for error.  */
    if (status != NX_SUCCESS)
    {
    
        return status;
    }

    /* Set session state to wait on the outcome of the EHLO response handler.  */
    client_ptr -> nx_smtp_client_cmd_state =  NX_SMTP_CLIENT_STATE_AWAITING_REPLY;
    
    /* Return normal session status.  */
    return NX_SUCCESS;
}



/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_rsp_ehlo                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This handles the server reply to the EHLO command and determines the*/
/*    next session state and next command to send to the server.          */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    status                                Actual completion status      */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*   _nx_smtp_utility_parse_server_services  Parser for extracting server */
/*                                                services and reply code */
/*   _nx_smtp_utility_read_server_code       Parse the server replies     */
/*                                                code and text           */
/*   _nx_smtp_rsp_hello_command              Server HELO/EHLO reply       */
/*                                                handler                 */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_rsp_ehlo(NX_SMTP_CLIENT *client_ptr)
{

UINT            status;


    /* Get the server response to the EHLO command.  */
    status = _nx_smtp_utility_read_server_code(client_ptr, NX_SMTP_ENVELOPE_TIMEOUT, NX_TRUE);

    /* Check for error.  */
    if (status != NX_SUCCESS) 
    { 

        /* This is either no packet received or an invalid server response.  */ 

        /* Return error status.  */ 
        return status; 
    } 
 
    /* Update session with specific services offered, if any, by server.  */
    _nx_smtp_utility_parse_server_services(client_ptr);

    /* We are done with the packet now. */
    nx_packet_release(client_ptr -> nx_smtp_server_packet);

    if (client_ptr -> nx_smtp_client_mute)
    {
        /* We expect another response packet from the server...
           keep the same state, don't send anything. */
        return NX_SUCCESS;
    }

    /* Handle the server reply code first.  */
    _nx_smtp_rsp_hello_command(client_ptr);

    /* if server rejected hello, set the session state to terminate */
    if (client_ptr -> nx_smtp_client_rsp_state == NX_SMTP_CLIENT_STATE_QUIT)
    {

        /* Return successful session status to execute QUIT command.  */
        return NX_SUCCESS;
    }

    /* Determine if we go to authenticatio next (otherwise we go to the MAIL state).   */
    if (client_ptr -> nx_smtp_client_authentication_type != NX_SMTP_CLIENT_AUTH_NONE)
    {

        /* Yes, set session to the AUTH state before going on to MAIL.  */
        client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_AUTH;                        
    }

    /* Return successful completion status.  */
    return NX_SUCCESS;
}


/**************************************************************************/ 
/*                                                                        */ 
/*  FUNCTION                                               RELEASE        */ 
/*                                                                        */ 
/*    _nx_smtp_rsp_hello_command                          PORTABLE C      */ 
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */ 
/*                                                                        */ 
/*    This function handles the server response to HELO and EHLO commands */
/*    common to both command states.  If server accepts the EHLO/HELO     */
/*    command, proceed with the session                                   */
/*                                                                        */ 
/*   INPUT                                                                */ 
/*                                                                        */ 
/*    session_ptr                   Session to send mail to SMTP Server   */
/*                                                                        */ 
/*  OUTPUT                                                                */ 
/*                                                                        */ 
/*    NX_SUCCESS                    Successful completion status          */
/*                                                                        */ 
/*  CALLS                                                                 */ 
/*                                                                        */ 
/*    None                                                                */ 
/*                                                                        */ 
/*  CALLED BY                                                             */ 
/*                                  Session server EHLO reply handler     */ 
/*    _nx_smtp_rsp_ehlo             Session server HELO reply handler     */ 
/*    _nx_smtp_rsp_helo                                                   */ 
/*                                                                        */ 
/**************************************************************************/
UINT  _nx_smtp_rsp_hello_command(NX_SMTP_CLIENT* client_ptr)
{

UINT     first_digit_server_reply;

    first_digit_server_reply = client_ptr -> nx_smtp_client_reply_code_status/ 100;

    /* Process the server reply starting with the first digit.  */
    if (first_digit_server_reply == 2)
    {


        /* Did server accept the EHLO/HELO command?  */
        if (client_ptr -> nx_smtp_client_reply_code_status == NX_SMTP_CODE_OK_TO_CONTINUE)
        {

            /* Set session to the next state (MAIL).  */
            client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_MAIL;

            /* Return successful session status.  */
            return NX_SUCCESS;
        }
    }

    /* If we are here, an error occurred. Indicate mail cannot be sent. */
    client_ptr -> nx_smtp_client_mail_status = NX_SMTP_HELLO_REPLY_ERROR;

    /* Set session state to QUIT.  */
    client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_QUIT; 

    /* Return successful session status.  */
    return NX_SUCCESS;
}



/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_cmd_auth                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function creates the text of the SMTP AUTH command text and    */
/*    sends it to the SMTP server. If the session is already              */ 
/*    authenticated, this function proceeds to the MAIL state (next stage */
/*    of the SMTP protocol) instead.                                      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                   Session to send mail to SMTP Server   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    status                                Actual completion status      */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_cmd_mail                     Send MAIL command to server   */
/*    _nx_smtp_utility_send_to_server       Send data to server           */
/*    memset                                Clear area of memory          */
/*    memcpy                                Copy data to area of memory   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP session         */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_cmd_auth(NX_SMTP_CLIENT *client_ptr)
{

UINT            status = NX_SUCCESS;
UINT            index;

    memset(&_nx_smtp_buffer[0], 0, NX_SMTP_BUFFER_SIZE);

    /* Is session already successfully authenticated?  */
    if (client_ptr -> nx_smtp_client_authentication_state == NX_SMTP_AUTHENTICATION_SUCCEEDED)
    {

        /* Yes, server will reject any authentication command; skip to MAIL state.  */
        client_ptr -> nx_smtp_client_cmd_state = NX_SMTP_CLIENT_STATE_MAIL;
        client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_MAIL;

        /* Call the session MAIL service.  */
        _nx_smtp_cmd_mail(client_ptr);

        /* Return successful session status.  */
        return NX_SUCCESS;
    }

    /* Mark the session authentication as in progress.  */
    client_ptr -> nx_smtp_client_authentication_state = NX_SMTP_AUTHENTICATION_IN_PROGRESS;

    /* Set the authentication prompt depending on the Client authentication type. */
    if (client_ptr -> nx_smtp_client_authentication_type == NX_SMTP_CLIENT_AUTH_LOGIN)
    {

        memcpy(&_nx_smtp_buffer[0],  NX_SMTP_CLIENT_AUTH_LOGIN_TEXT, sizeof(NX_SMTP_CLIENT_AUTH_LOGIN_TEXT) - 1); /* Use case of memcpy is verified. */
        index = sizeof(NX_SMTP_CLIENT_AUTH_LOGIN_TEXT) - 1;
    }
    else if (client_ptr -> nx_smtp_client_authentication_type == NX_SMTP_CLIENT_AUTH_CRAM_MD5)
    {

        memcpy(&_nx_smtp_buffer[0],  NX_SMTP_CLIENT_AUTH_CRAM_MD5_TEXT, sizeof(NX_SMTP_CLIENT_AUTH_CRAM_MD5_TEXT) - 1); /* Use case of memcpy is verified. */
        index = sizeof(NX_SMTP_CLIENT_AUTH_CRAM_MD5_TEXT) - 1;
    }
    else 
    {

        memcpy(&_nx_smtp_buffer[0],  NX_SMTP_CLIENT_AUTH_PLAIN_TEXT, sizeof(NX_SMTP_CLIENT_AUTH_PLAIN_TEXT) - 1); /* Use case of memcpy is verified. */
        index = sizeof(NX_SMTP_CLIENT_AUTH_PLAIN_TEXT) - 1;
    }

    memcpy(&_nx_smtp_buffer[index],  NX_SMTP_LINE_TERMINATOR, sizeof(NX_SMTP_LINE_TERMINATOR) - 1); /* Use case of memcpy is verified. */
    index += sizeof(NX_SMTP_LINE_TERMINATOR) - 1;

    /* Send the AUTH command to the server.  */
    status =  _nx_smtp_utility_send_to_server(client_ptr, _nx_smtp_buffer, index, NX_SMTP_CLIENT_SEND_TIMEOUT);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        return status;
    }

    /* Set session state to wait on the outcome of the EHLO response handler.  */
    client_ptr -> nx_smtp_client_cmd_state =  NX_SMTP_CLIENT_STATE_AWAITING_REPLY;
    
    /* Return normal session status.  */
    return NX_SUCCESS;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_rsp_auth                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function handles the server reply to the AUTH command and set  */
/*    determine the next session state and next command to send to the    */
/*    server.                                                             */
/*                                                                        */
/*    During the authentication process, it attempts to find a match for  */
/*    the server authentication type and decodes each server              */
/*    authentication challenge (e.g. USERNAME, PASSWORD). It then sets the*/
/*    session authentication state depending on server acceptance of its  */
/*    replies to its challenges.                                          */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                           Successful completion status   */
/*    status                               Actual completion status       */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_utility_read_server_code     Parser for server reply       */
/*    _nx_smtp_utility_authentication_challenge                           */
/*                                          Respond to server             */
/*                                                authentication challenge*/
/*    _nx_smtp_rsp_auth                     When called by itself,respond */
/*                                               to server authentication */
/*                                               challenge                */
/*    _nx_smtp_utility_send_to_server       Send data to server           */
/*    nx_packet_release                     Release NetX receive packet   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_rsp_auth(NX_SMTP_CLIENT *client_ptr)
{

UINT        status;
UINT        server_reply_first_digit; 
UCHAR       *carriage_return_linefeed_ptr = NX_NULL;
UINT        auth_length;


    /* Get the server response to the AUTH command.  */
    status   =  _nx_smtp_utility_read_server_code(client_ptr, NX_SMTP_ENVELOPE_TIMEOUT, NX_TRUE);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        /* This is an server response or no packet received.  */ 

        /* Return error status.  */
        return(status);
    }

    /* Use the first digit to group possible server replies.  */
    server_reply_first_digit = client_ptr -> nx_smtp_client_reply_code_status / 100;

    /* Process the reply code from server.  */
    if (server_reply_first_digit == 2)       
    {

        if (client_ptr -> nx_smtp_client_reply_code_status == NX_SMTP_CODE_AUTHENTICATION_SUCCESSFUL)
        {

            /* Advance to the MAIL state.  */
            client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_MAIL;

            /* Mark the session as authenticated successfully.  */
            client_ptr -> nx_smtp_client_authentication_state = NX_SMTP_AUTHENTICATION_SUCCEEDED;
        }
        else
        {             

            /* The server returns a 2xx code. Determine if it is an error code. */
            if ((client_ptr -> nx_smtp_client_reply_code_status == NX_SMTP_CODE_CANNOT_VERIFY_RECIPIENT) ||
                (client_ptr -> nx_smtp_client_reply_code_status == NX_SMTP_CODE_ACKNOWLEDGE_QUIT)) 
            {

                /* It is. Abort. */
                client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_QUIT;

                /* Indicate mail cannot be sent. */
                client_ptr -> nx_smtp_client_mail_status = NX_SMTP_AUTH_REPLY_ERROR;
            }
            else
            {
                /* It is not the code expected. Discard and keep the SMTP Client in the same state. */
            }

            /* We are done with the packet now. */
            nx_packet_release(client_ptr -> nx_smtp_server_packet);

            return NX_SUCCESS;
        }
     }
     else if (server_reply_first_digit == 3)
     {

        /* Check if the server accepted our authentication type. */
        if (client_ptr -> nx_smtp_client_reply_code_status != NX_SMTP_CODE_AUTHENTICATION_TYPE_ACCEPTED)
        {

            /* It did not.  Or we may be out of synch with the SMTP Server, if we get a
               354 message for example. Abort the mail session. */
            client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_QUIT;

            /* Indicate mail cannot be sent. */
            client_ptr -> nx_smtp_client_mail_status = NX_SMTP_AUTH_REPLY_ERROR;

            /* We are done with the packet now. */
            nx_packet_release(client_ptr -> nx_smtp_server_packet);

            return NX_SUCCESS;
        }
        /* Authentication in progress.  */

     }
     else
     {

        /* Set session to the QUIT state.  */
        client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_QUIT;

        /* Indicate mail cannot be sent. */
        client_ptr -> nx_smtp_client_mail_status = NX_SMTP_AUTH_REPLY_ERROR;

        /* We are done with the packet now. */
        nx_packet_release(client_ptr -> nx_smtp_server_packet);

        return NX_SUCCESS;
    }

    /* Is authentication in progress?  */    
    if (client_ptr -> nx_smtp_client_authentication_state != NX_SMTP_AUTHENTICATION_SUCCEEDED) 
    {

        /* Set allowed authentication string length.  */
        auth_length = client_ptr -> nx_smtp_server_packet -> nx_packet_length;

        /* Find the end (e.g. CRLF) of the server reply.  */
        _nx_smtp_find_crlf(client_ptr -> nx_smtp_server_packet -> nx_packet_prepend_ptr, 
                           auth_length, &carriage_return_linefeed_ptr, NX_FALSE);                         

        /* Check for invalid server reply (e.g. missing CRLF).  */
        if (carriage_return_linefeed_ptr== NX_NULL)
        {

            client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_QUIT;

            /* Indicate mail cannot be sent. */
            client_ptr -> nx_smtp_client_mail_status = NX_SMTP_SERVER_ERROR_REPLY;

            /* We are done with the packet now. */
            nx_packet_release(client_ptr -> nx_smtp_server_packet);

            /* Return completion status.  */
            return NX_SUCCESS;
        }

        /* Yes, process (decode) the server's challenge.  */ 
        status = _nx_smtp_utility_authentication_challenge(client_ptr, client_ptr -> nx_smtp_server_packet -> nx_packet_prepend_ptr, 
                                                           (UINT)(carriage_return_linefeed_ptr - client_ptr -> nx_smtp_server_packet -> nx_packet_prepend_ptr));

        /* Did the session successfully handle server challenge?  */
        if (status == NX_SUCCESS)
        {

            /* Yes, set session to respond to the next server challenge.  */
            client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_AUTH_CHALLENGE;
        }
        /* Did the session have problems parsing the server challenge?  */
        else 
        {

            client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_QUIT;

            /* Indicate mail cannot be sent. */
            client_ptr -> nx_smtp_client_mail_status = NX_SMTP_AUTH_REPLY_ERROR;

            nx_packet_release(client_ptr -> nx_smtp_server_packet);

            /* Return completion status.  */
            return NX_SUCCESS;
        }
    }

    /* We are done with the packet now. */
    nx_packet_release(client_ptr -> nx_smtp_server_packet);
    
    /* Return successful status.  */
    return NX_SUCCESS;
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_cmd_auth_challenge                         PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function creates a reply to the server authentication challenge*/ 
/*    It determines which challenge the server sent (e.g Username,        */
/*    Password) and encodes the session response using base64 encryption. */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_utility_send_to_server       Send data to server           */
/*    _nx_utility_base64_encode             Base64 encode specified text  */
/*    memcpy                                Copy data to area of memory   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP session         */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_cmd_auth_challenge(NX_SMTP_CLIENT *client_ptr)
{

UINT     status;
UINT     index;
UCHAR    auth_reply_encoded[NX_SMTP_CLIENT_AUTH_CHALLENGE_ENCODED_SIZE + 1];
UINT     auth_reply_encoded_size;
UINT     length;
UINT     password_length;

    /* Initialize the encoded reply to NULL.  */
    memset(auth_reply_encoded, 0, sizeof(auth_reply_encoded));

    /* Verify string length.  */
    if (_nx_utility_string_length_check(client_ptr -> nx_smtp_password, &password_length, NX_SMTP_CLIENT_MAX_PASSWORD))
    {
        return(NX_SIZE_ERROR);
    }

    /* Did the server send us the Username prompt?  */
    if (client_ptr -> nx_smtp_client_authentication_reply == NX_SMTP_CLIENT_REPLY_TO_USERNAME_PROMPT)
    {
        if (_nx_utility_string_length_check(client_ptr -> nx_smtp_username, &length, NX_SMTP_CLIENT_MAX_USERNAME))
        {
            return(NX_SIZE_ERROR);
        }


        if (client_ptr -> nx_smtp_client_authentication_type == NX_SMTP_CLIENT_AUTH_PLAIN)
        {

            UCHAR plain_auth_buffer[NX_SMTP_CLIENT_AUTH_CHALLENGE_SIZE];  

            if (NX_SMTP_CLIENT_AUTH_CHALLENGE_SIZE < (length + 1 + length + 1 + password_length))
            {

                /* Buffer size too small. */
                return(NX_SMTP_INTERNAL_ERROR);
            }

            memset(&plain_auth_buffer[0], 0, NX_SMTP_CLIENT_AUTH_CHALLENGE_SIZE);

            /* Process the client name as per AUTH PLAIN syntax: authorization-id\0authentication-id\0password. */
            memcpy(&plain_auth_buffer[0], client_ptr -> nx_smtp_username,  length); /* Use case of memcpy is verified. */
            length++;
            memcpy(&plain_auth_buffer[length], client_ptr -> nx_smtp_username,  length);  /* Use case of memcpy is verified. */
            length += length;
            memcpy(&plain_auth_buffer[length], client_ptr -> nx_smtp_password, password_length); /* Use case of memcpy is verified. */
            length += password_length;
    
            /* Now encode the combined client username/password.  */
            _nx_utility_base64_encode(&plain_auth_buffer[0], length, auth_reply_encoded, sizeof(auth_reply_encoded), &auth_reply_encoded_size);

        }
        else
        {

            /* Just encode the client username.  */
            _nx_utility_base64_encode((UCHAR *)client_ptr -> nx_smtp_username, length, auth_reply_encoded, sizeof(auth_reply_encoded), &auth_reply_encoded_size);
        }

    }
    /* Or did the server send us the Password prompt?  */
    else if (client_ptr -> nx_smtp_client_authentication_reply == NX_SMTP_CLIENT_REPLY_TO_PASSWORD_PROMPT)
    {

        /* Encode the client password.  */
        _nx_utility_base64_encode((UCHAR *)client_ptr -> nx_smtp_password, password_length, auth_reply_encoded, sizeof(auth_reply_encoded), &auth_reply_encoded_size);
    }

    else 
    {

        /* Unknown prompt: Send the '*' to terminate the authentication process.  */
        memcpy(auth_reply_encoded, NX_SMTP_CANCEL_AUTHENTICATION,  sizeof(NX_SMTP_CANCEL_AUTHENTICATION)); /* Use case of memcpy is verified. */
        auth_reply_encoded_size = sizeof(NX_SMTP_CANCEL_AUTHENTICATION) - 1;
    }

    if (sizeof(_nx_smtp_buffer) < (auth_reply_encoded_size + sizeof(NX_SMTP_LINE_TERMINATOR) - 1))
    {

        /* Buffer size too small. */
        return(NX_SMTP_INTERNAL_ERROR);
    }

    /* Format the encoded response.  */
    memcpy(&_nx_smtp_buffer[0],auth_reply_encoded, auth_reply_encoded_size); /* Use case of memcpy is verified. */
    index = auth_reply_encoded_size;
    memcpy(&_nx_smtp_buffer[index],  NX_SMTP_LINE_TERMINATOR, sizeof(NX_SMTP_LINE_TERMINATOR) - 1); /* Use case of memcpy is verified. */
    index += sizeof(NX_SMTP_LINE_TERMINATOR) - 1;

    /* Send the response back to the server.  */
    status =  _nx_smtp_utility_send_to_server(client_ptr, _nx_smtp_buffer, index, NX_SMTP_CLIENT_SEND_TIMEOUT);

    /* Check for successful.  */
    if (status == NX_SUCCESS)
    {

        /* Set session state to wait on the outcome of the AUTH challenge handler.  */
        client_ptr -> nx_smtp_client_cmd_state =  NX_SMTP_CLIENT_STATE_AWAITING_REPLY;
    }

    /* Return completion status.  */
    return status;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_rsp_auth_challenge                         PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This receives the server's response to the AUTH challenge sent by   */       
/*    the session previously and actually just calls the same session     */
/*    AUTH server reply handler again.                                    */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                      SMTP Session for sending mail      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                           Actual completion status           */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*   _nx_smtp_rsp_auth                 AUTH server reply handler          */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process          Runs the SMTP state machine        */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_rsp_auth_challenge(NX_SMTP_CLIENT *client_ptr)
{

UINT status;


    /* Run the session AUTH server reply handler directly.  */
    status = _nx_smtp_rsp_auth(client_ptr);

    /* Return completion status.  */
    return status;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_cmd_mail                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function creates the MAIL command and sets the Client state to */
/*    send it to the SMTP server.                                         */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    status                                Actual completion status      */ 
/*    memcpy                                Copy data to area of memory   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_utility_send_to_server       Send data to server           */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_cmd_mail(NX_SMTP_CLIENT *client_ptr)
{

UINT index;   
UINT status;
UINT mail_from_length;
NX_SMTP_CLIENT_MAIL *mail_ptr = &client_ptr -> nx_smtp_client_mail;


    memset(_nx_smtp_buffer, 0, NX_SMTP_BUFFER_SIZE);

    if (_nx_utility_string_length_check(mail_ptr -> nx_smtp_client_mail_from_address, &mail_from_length, NX_SMTP_BUFFER_SIZE))
    {
        return(NX_SIZE_ERROR);
    }

    /* Check if authentication status still left as 'in progress'.  */
    if (client_ptr -> nx_smtp_client_authentication_state == NX_SMTP_AUTHENTICATION_IN_PROGRESS)
    {

        /* For some reason, authentication did not complete. Reset status here.  */
        client_ptr -> nx_smtp_client_authentication_state = NX_SMTP_NOT_AUTHENTICATED;    
    }

    /* Validate message size. */
    if((sizeof(NX_SMTP_COMMAND_MAIL) + mail_from_length +
        sizeof(NX_SMTP_LINE_TERMINATOR) + 2) > NX_SMTP_BUFFER_SIZE)
    {
        /* Message size exceeds buffer size. */
        return(NX_SMTP_INTERNAL_ERROR);
    }

    /* Create the command text.  */
    memcpy(&_nx_smtp_buffer[0], NX_SMTP_COMMAND_MAIL, sizeof(NX_SMTP_COMMAND_MAIL) - 1); /* Use case of memcpy is verified. */
    index = sizeof(NX_SMTP_COMMAND_MAIL) - 1;

    _nx_smtp_buffer[index++] = ':';
    _nx_smtp_buffer[index++] = '<';
    memcpy(&_nx_smtp_buffer[index], mail_ptr -> nx_smtp_client_mail_from_address, mail_from_length); /* Use case of memcpy is verified. */
    index += mail_from_length; 
    _nx_smtp_buffer[index++] = '>';

    _nx_smtp_buffer[index++] = ' ';
    memcpy(&_nx_smtp_buffer[index],  NX_SMTP_LINE_TERMINATOR, sizeof(NX_SMTP_LINE_TERMINATOR) - 1); /* Use case of memcpy is verified. */
    index += sizeof(NX_SMTP_LINE_TERMINATOR) - 1;

    /* Send the session command we constructed above.  */
    status =  _nx_smtp_utility_send_to_server(client_ptr, _nx_smtp_buffer, index, NX_SMTP_CLIENT_SEND_TIMEOUT);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        /* Return error status.*/
        return status;
    }

    /* Set session state to wait on the outcome of the MAIL response handler.  */
    client_ptr -> nx_smtp_client_cmd_state =  NX_SMTP_CLIENT_STATE_AWAITING_REPLY;

    /* Return successful session status.  */
    return(NX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_rsp_mail                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This receives the server's response to the MAIL command; it         */
/*    extracts the server reply code and reply text and store both to the */
/*    Client session. It will also set the next SMTP command to send to   */
/*    the server.                                                         */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    status                                Actual completion status      */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_utility_read_server_code     Parse the server reply        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_rsp_mail(NX_SMTP_CLIENT *client_ptr)
{

UINT  status;

    /* Get the server response to the MAIL command.  */
    status   =  _nx_smtp_utility_read_server_code(client_ptr, NX_SMTP_ENVELOPE_TIMEOUT, NX_TRUE);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        /* This is an invalid line or corrupted transmission from the server.  */ 

        /* Return error status.  */
        return(status);
    }

    /* Did server accept the MAIL command?  */
    if (client_ptr -> nx_smtp_client_reply_code_status == NX_SMTP_CODE_OK_TO_CONTINUE)
    {

        /* Yes, set the session to the RCPT state.  */
        client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_RCPT;
    }
    else
    {

        /* Set session state to QUIT.  */
        client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_QUIT;
    
        /* Indicate mail cannot be sent. */
        client_ptr -> nx_smtp_client_mail_status = NX_SMTP_MAIL_REPLY_ERROR;
    }

    /* We are done with the packet now. */
    nx_packet_release(client_ptr -> nx_smtp_server_packet);

    /* Return successful session status.  */
    return(NX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_cmd_rcpt                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function creates the RCPT command and sets the Client state to */
/*    send it to the SMTP server.                                         */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    status                                Actual completion status      */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_utility_send_to_server       Send data to server           */
/*    memcpy                                Copy data to area of memory   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_cmd_rcpt(NX_SMTP_CLIENT *client_ptr)
{

UINT                        status;
UINT                        index;
UINT                        recipient_length;
NX_SMTP_CLIENT_MAIL         *mail_ptr;


    mail_ptr = &(client_ptr -> nx_smtp_client_mail);

    if (_nx_utility_string_length_check(mail_ptr -> nx_smtp_client_mail_recipient_address, &recipient_length, NX_SMTP_BUFFER_SIZE))
    {
        return(NX_SIZE_ERROR);
    }

    memset(_nx_smtp_buffer, 0, NX_SMTP_BUFFER_SIZE);

    /* Validate message size. */
    if((sizeof(NX_SMTP_COMMAND_RCPT) + recipient_length +
        sizeof(NX_SMTP_LINE_TERMINATOR) + 2) > NX_SMTP_BUFFER_SIZE)
    {
        /* Message size exceeds buffer size. */
        return(NX_SMTP_INTERNAL_ERROR);
    }

    /* Create the command text.  */
    memcpy(&_nx_smtp_buffer[0], NX_SMTP_COMMAND_RCPT, sizeof(NX_SMTP_COMMAND_RCPT) - 1); /* Use case of memcpy is verified. */
    index = sizeof(NX_SMTP_COMMAND_RCPT) - 1; 

    _nx_smtp_buffer[index++] = ':';
    _nx_smtp_buffer[index++] = '<';
    memcpy(&_nx_smtp_buffer[index],  mail_ptr -> nx_smtp_client_mail_recipient_address, /* Use case of memcpy is verified. */
           recipient_length);
    index += recipient_length;
    _nx_smtp_buffer[index++] = '>';
    _nx_smtp_buffer[index++] = ' ';
    memcpy(&_nx_smtp_buffer[index],  NX_SMTP_LINE_TERMINATOR, sizeof(NX_SMTP_LINE_TERMINATOR) - 1); /* Use case of memcpy is verified. */
    index += sizeof(NX_SMTP_LINE_TERMINATOR) - 1;

    /* Send the RCPT command.  */
    status =  _nx_smtp_utility_send_to_server(client_ptr, _nx_smtp_buffer, index, NX_SMTP_CLIENT_SEND_TIMEOUT);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        return status;
    }

    /* Set session state to wait next response from server.  */
    client_ptr -> nx_smtp_client_cmd_state =  NX_SMTP_CLIENT_STATE_AWAITING_REPLY;

    /* Return normal session status.  */
    return NX_SUCCESS;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_rsp_rcpt                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This service handles the server's response to the RCPT command and  */
/*    set the next session state and next command to send to the server.  */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                        SMTP Session for sending mail    */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                         Successful completion status     */
/*    status                             Actual completion status         */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*   _nx_smtp_utility_read_server_code   Parse the server reply           */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process            Runs the SMTP state machine      */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_rsp_rcpt(NX_SMTP_CLIENT *client_ptr)
{

UINT  status;


    /* Get server response to RCPT command.  */
    status = _nx_smtp_utility_read_server_code(client_ptr, NX_SMTP_ENVELOPE_TIMEOUT, NX_TRUE);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        /* Return error status.  */
        return(status);
    }

    /* Did server accept RCPT command?  */
    if (client_ptr -> nx_smtp_client_reply_code_status != NX_SMTP_CODE_OK_TO_CONTINUE)
    {   
        /* NO, Set session state to QUIT.  */
        client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_QUIT;

        /* Indicate mail cannot be sent. */
        client_ptr -> nx_smtp_client_mail_status = NX_SMTP_RCPT_REPLY_ERROR;
    }  
    else
    {
    
        /* Ok for server to send the mail message */
        client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_DATA;  
    }

    /* We are done with the packet now. */
    nx_packet_release(client_ptr -> nx_smtp_server_packet);

    /* Return successful session status.  */
    return NX_SUCCESS;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_cmd_data                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function sets the DATA command text, verifies the mail to send */
/*    has a valid recipient and sends it to the  SMTP server.             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    status                                Actual completion status      */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_utility_send_to_server       Send SMTP commend to server   */
/*    memcpy                                Copy data to area of memory   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_cmd_data(NX_SMTP_CLIENT *client_ptr)
{

UINT     status;
UINT     index;


    memset(_nx_smtp_buffer, 0, NX_SMTP_BUFFER_SIZE);

    /* Format the DATA command.  */
    memcpy(&_nx_smtp_buffer[0], NX_SMTP_COMMAND_DATA, sizeof(NX_SMTP_COMMAND_DATA) - 1); /* Use case of memcpy is verified. */
    index = sizeof(NX_SMTP_COMMAND_DATA) - 1;
    memcpy(&_nx_smtp_buffer[index],  NX_SMTP_LINE_TERMINATOR, sizeof(NX_SMTP_LINE_TERMINATOR) - 1); /* Use case of memcpy is verified. */
    index += sizeof(NX_SMTP_LINE_TERMINATOR) - 1;

    /* Send the DATA command.  */
    status =  _nx_smtp_utility_send_to_server(client_ptr, _nx_smtp_buffer, index, NX_SMTP_CLIENT_SEND_TIMEOUT);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        return status;
    }

    /* Set session state to wait next response from server.  */
    client_ptr -> nx_smtp_client_cmd_state =  NX_SMTP_CLIENT_STATE_AWAITING_REPLY;

    /* Return normal session status.  */
    return NX_SUCCESS;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_rsp_data                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function handles the server reply to the DATA command and set  */
/*    the next SMTP Client command to send to the server.                 */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    status                                Actual completion status      */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*   _nx_smtp_utility_read_server_code      Parse the server reply        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_rsp_data(NX_SMTP_CLIENT *client_ptr)
{

UINT            status;


    /* Get the response to DATA command.  */
    status = _nx_smtp_utility_read_server_code(client_ptr, NX_SMTP_ENVELOPE_TIMEOUT, NX_TRUE);

    /* Check for errors.  */
    if (status != NX_SUCCESS)
    {

        /* Return error status.  */
        return (status);
    }

    if (client_ptr -> nx_smtp_client_reply_code_status == NX_SMTP_CODE_SEND_MAIL_INPUT)
    {

        /* Yes, set session state to next step.  */
        client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_MESSAGE;  
    }
    else
    {

        /* No, any other 3xy code in inappropriate. Quit the session.  */
        client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_QUIT;

        /* Indicate mail cannot be sent. */
        client_ptr -> nx_smtp_client_mail_status = NX_SMTP_DATA_REPLY_ERROR;
    }

    /* We are done with the packet now. */
    nx_packet_release(client_ptr -> nx_smtp_server_packet);

    /* Return successful session status.  */
    return NX_SUCCESS;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_cmd_message                                PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function handles the mail message text handling for message    */
/*    text.  It attempts to fit as much of each message into each packet  */
/*    as possible.                                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           Session for sending mail      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    NX_NOT_ENABLED                        IPv6 not enabled              */
/*    status                                Actual completion status      */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_utility_send_to_server       Send data to server           */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_cmd_message(NX_SMTP_CLIENT *client_ptr)
{

UINT                        status;
ULONG                       data_left_to_transmit;
ULONG                       packet_payload;
CHAR                        *data_to_send;
NX_PACKET_POOL              *pool_ptr;
NX_SMTP_CLIENT_MAIL         *mail_ptr;


    /* Set local variable for convenience.  */
    mail_ptr = &client_ptr -> nx_smtp_client_mail;

    /* Set session state to wait next response from server.  */
    client_ptr -> nx_smtp_client_cmd_state =  NX_SMTP_CLIENT_STATE_AWAITING_REPLY;

    pool_ptr  = client_ptr -> nx_smtp_client_packet_pool_ptr; 

    /* Compute packet payload after IP and TCP headers are subtracted.  */
    if (client_ptr -> nx_smtp_server_address.nxd_ip_version == NX_IP_VERSION_V4)
    {

#ifndef NX_DISABLE_IPV4
        packet_payload = pool_ptr -> nx_packet_pool_payload_size - sizeof(NX_TCP_HEADER) - sizeof(NX_IPV4_HEADER);
#else
        return NX_NOT_ENABLED;
#endif /* NX_DISABLE_IPV4 */
    }
    else
    {
#ifdef FEATURE_NX_IPV6
        packet_payload = pool_ptr -> nx_packet_pool_payload_size - sizeof(NX_TCP_HEADER) - sizeof(NX_IPV6_HEADER);
#else
        return NX_NOT_ENABLED;
#endif
    }


    /* Send the Mail header */
    status = _nx_smtp_utility_send_header_to_server(client_ptr, NX_SMTP_CLIENT_SEND_TIMEOUT);
    if(status)
        return(status);

    /* Set up local variable of size of mail message. */
    data_left_to_transmit = mail_ptr -> nx_smtp_client_mail_body_length;

    /* Set a local pointer to (rest of) mail message to send. */
    data_to_send = mail_ptr -> nx_smtp_client_mail_body;

    /* Send the next chunk of mail text buffer till nothing is left.  */
    while(data_left_to_transmit)
    {

        /* Check if remaining mail text will fit in a packet.*/
        if (packet_payload >= data_left_to_transmit)
        {

            /* Yes, send it off! This will load the specified data into an packet from the SMTP client packet pool.  */
            status =  _nx_smtp_utility_send_to_server(client_ptr, data_to_send, data_left_to_transmit, 
                                                      NX_SMTP_CLIENT_SEND_TIMEOUT);

            /* Check for error.  */
            if (status != NX_SUCCESS)
            {

                /* Return error status.  */
                return status;
            }

            break;
        }

        /* No, break up mail message into multiple packets.  */
        else
        {
    
            /* Fill up the packet with as much data as it will hold and send it off! */
            status =  _nx_smtp_utility_send_to_server(client_ptr, data_to_send, packet_payload, 
                                                      NX_SMTP_CLIENT_SEND_TIMEOUT);

            /* Check for error.  */
            if (status != NX_SUCCESS)
            {

                /* Return error status.  */
                return status;
            }

            /* Update pointer to remainder of message to send, update how much is left to send. */
            data_left_to_transmit -= packet_payload;  
            data_to_send += packet_payload;
        }
    }

    /* Send end of message (EOM) to server or it will handle subsequent commands as message text.  */
    status =  _nx_smtp_utility_send_to_server(client_ptr, NX_SMTP_EOM, sizeof(NX_SMTP_EOM) - 1, NX_SMTP_CLIENT_SEND_TIMEOUT);

    /* Return normal session status.  */
    return status;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_rsp_message                                PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This receives the server's response to sending it mail message data.*/
/*    It extracts the server reply code and reply text and store both to  */
/*    the Client session. It then sets the next session state, sets up    */
/*    the next command to send to the server.                             */
/*                                                                        */
/*    If the server accepts the mail message, this function determines if */
/*    if there is more session mail to transact, and if so sets the next  */
/*    Client mail.                                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           Session for sending mail      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    status                                Actual completion status      */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_utility_read_server_code     Parse the server reply code   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_rsp_message(NX_SMTP_CLIENT *client_ptr)
{

UINT     status;


    /* Get the server reply to receiving session mail message data.  */
    status = _nx_smtp_utility_read_server_code(client_ptr, NX_SMTP_MESSAGE_TIMEOUT, NX_TRUE);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        /* Return the error status.  */
        return(status);
    }

    /* Process server reply code by first digit.  */
    if (client_ptr -> nx_smtp_client_reply_code_status == NX_SMTP_CODE_OK_TO_CONTINUE)
    {

        /* Message accepted by server. Mark the Client mail as sent.  */
        client_ptr -> nx_smtp_client_mail_status = NX_SUCCESS;
    }
    else
    {

        /* Indicate mail cannot be sent. */
        client_ptr -> nx_smtp_client_mail_status = NX_SMTP_MESSAGE_REPLY_ERROR;
    }

    /* We're done with this session */
    client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_QUIT;

    /* We are done with the packet now. */
    nx_packet_release(client_ptr -> nx_smtp_server_packet);

    /* Return successful session status.  */
    return(NX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_cmd_quit                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function creates the QUIT command text and and sends the Quit  */
/*    command to the  SMTP server.                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    status                                Actual completion status      */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_utility_send_to_server       Send data to server           */
/*    memcpy                                Copy data to area of memory   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_cmd_quit(NX_SMTP_CLIENT *client_ptr)
{

UINT     status;
UINT     index;


    memset(_nx_smtp_buffer, 0, NX_SMTP_BUFFER_SIZE);

    /* Format the QUIT command.  */
    memcpy(&_nx_smtp_buffer[0], NX_SMTP_COMMAND_QUIT, sizeof(NX_SMTP_COMMAND_QUIT) - 1); /* Use case of memcpy is verified. */
    index = sizeof(NX_SMTP_COMMAND_QUIT) - 1;
    memcpy(&_nx_smtp_buffer[index],  NX_SMTP_LINE_TERMINATOR, sizeof(NX_SMTP_LINE_TERMINATOR)); /* Use case of memcpy is verified. */
    index += sizeof(NX_SMTP_LINE_TERMINATOR) - 1;

    /* Send the QUIT command.  */
    status =  _nx_smtp_utility_send_to_server(client_ptr, _nx_smtp_buffer, index, NX_SMTP_CLIENT_SEND_TIMEOUT);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        /* Return error status.  */
        return status;
    }

    /* Set session state to wait on the outcome of the EHLO response handler.  */
    client_ptr -> nx_smtp_client_cmd_state =  NX_SMTP_CLIENT_STATE_AWAITING_REPLY;
    
    /* Return normal session status.  */
    return NX_SUCCESS;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_rsp_quit                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function handles the server reply to the SMTP QUIT command and */
/*    sets the session state to the session has terminated normally.      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    status                                Actual completion status      */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*   _nx_smtp_utility_read_server_code      Parse the server reply        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_rsp_quit(NX_SMTP_CLIENT *client_ptr)
{

UINT    status;


    /* Get server response to QUIT command.  */
    status = _nx_smtp_utility_read_server_code(client_ptr, NX_SMTP_ENVELOPE_TIMEOUT, NX_TRUE);

    /* Check for error status.  */
    if (status != NX_SUCCESS)
    {

        /* Return the error status.  */
        return(status);
    }

    /* Set the session state to normal termination. Normal is when the SMTP Client can
       receive SMTP server replies and regardless if mail can be sent, can at least
       terminate the session with the Quit command and normal TCP socket disconnect.  */
    client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_COMPLETED_NORMALLY; 

    /* We are done with the packet now. */
    nx_packet_release(client_ptr -> nx_smtp_server_packet);

    /* Return successful session status.  */
    return NX_SUCCESS;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_cmd_rset                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This functions creates the RSET command for the Client task to send */
/*    it to the SMTP server.                                              */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_utility_send_to_server       Send data to server           */
/*    memcpy                                Copy data to area of memory   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_cmd_rset(NX_SMTP_CLIENT *client_ptr)
{

ULONG    timeout;
UINT     status;
UINT     index;


    memset(_nx_smtp_buffer, 0, NX_SMTP_BUFFER_SIZE);

    /* Get TCP send timeout.  */
    timeout =  NX_SMTP_ENVELOPE_TIMEOUT;

    /* Format the RSET command.  */
    memcpy(&_nx_smtp_buffer[0], NX_SMTP_COMMAND_RSET, sizeof(NX_SMTP_COMMAND_RSET) - 1); /* Use case of memcpy is verified. */
    index = sizeof(NX_SMTP_COMMAND_RSET) - 1;
    memcpy(&_nx_smtp_buffer[index],  NX_SMTP_LINE_TERMINATOR, sizeof(NX_SMTP_LINE_TERMINATOR) - 1); /* Use case of memcpy is verified. */
    index += sizeof( NX_SMTP_LINE_TERMINATOR) - 1;

    /* Send the RSET command.  */
    status =  _nx_smtp_utility_send_to_server(client_ptr, _nx_smtp_buffer, index, timeout);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        return status;
    }

    /* Set session state to wait on the outcome of the EHLO response handler.  */
    client_ptr -> nx_smtp_client_cmd_state =  NX_SMTP_CLIENT_STATE_AWAITING_REPLY;
    
    /* Return normal session status.  */
    return NX_SUCCESS;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_rsp_rset                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function handles the RSET server reply.                        */
/*                                                                        */
/*    Resetting an SMTP session means clearing the Client mail transaction*/
/*    including recipients and mail text.                                 */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    status                                Actual completion status      */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*   _nx_smtp_utility_read_server_code      Parse the server reply        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_rsp_rset(NX_SMTP_CLIENT *client_ptr)
{

UINT  status;


    /* Get server response to RSET command.  */
    status =  _nx_smtp_utility_read_server_code(client_ptr, NX_SMTP_ENVELOPE_TIMEOUT, NX_TRUE);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        /* Return error status.  */
        return(status);
    }

    /* Did the session receive the 250 OK from the server?  */  
    if (client_ptr -> nx_smtp_client_reply_code_status != NX_SMTP_CODE_OK_TO_CONTINUE)
    {

        /* Yes, set the session state to QUIT.  */
        client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_QUIT;            
        /* Indicate mail cannot be sent. */
        client_ptr -> nx_smtp_client_mail_status = NX_SMTP_SERVER_ERROR_CODE_RECEIVED;
    }
    else
    {

        /* Yes, session accepted the RSET command with the 250 code.
           Reset session state back to MAIL.  */
        client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_MAIL;

    }

    /* Return successful session status.  */
    return NX_SUCCESS;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_cmd_noop                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function creates the SMTP NOOP command text and sends it to the*/
/*    SMTP server.                                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                           SMTP Session for sending mail */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_utility_send_to_server       Send data to server           */
/*    memcpy                                Copy data to area of memory   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process               Runs the SMTP state machine   */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_cmd_noop(NX_SMTP_CLIENT *client_ptr)
{

UINT     status;
UINT     index;


    memset(_nx_smtp_buffer, 0, NX_SMTP_BUFFER_SIZE);

    /* Format the NOOP command.  */
    memcpy(&_nx_smtp_buffer[0], NX_SMTP_COMMAND_NOOP, sizeof(NX_SMTP_COMMAND_NOOP) - 1); /* Use case of memcpy is verified. */
    index = sizeof(NX_SMTP_COMMAND_NOOP) - 1;
    memcpy(&_nx_smtp_buffer[index],  NX_SMTP_LINE_TERMINATOR, sizeof(NX_SMTP_LINE_TERMINATOR) - 1); /* Use case of memcpy is verified. */
    index += sizeof(NX_SMTP_LINE_TERMINATOR) - 1;

    /* Send the NOOP command.  */
    status =  _nx_smtp_utility_send_to_server(client_ptr, _nx_smtp_buffer, index, NX_SMTP_CLIENT_SEND_TIMEOUT);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        return status;
    }

    /* Set session state to wait on the outcome of the EHLO response handler.  */
    client_ptr -> nx_smtp_client_cmd_state =  NX_SMTP_CLIENT_STATE_AWAITING_REPLY;
    
    /* Return normal session status.  */
    return NX_SUCCESS;
}



/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_rsp_noop                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function handles the NOOP server reply. It will not            */
/*    automatically advance the session state like most other SMTP client */
/*    commands. However, an indication of server problems will set the    */ 
/*    session state to QUIT.                                              */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                       TCP connection to send mail       */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                        Successful completion status      */
/*    status                            Actual completion status          */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*   _nx_smtp_utility_read_server_code  Parse the server reply            */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_client_process           Runs the SMTP state machine       */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_rsp_noop(NX_SMTP_CLIENT *client_ptr)
{

UINT              status;
UINT              first_digit_server_reply;


    /* Get the response for NOOP command.  */
    status =  _nx_smtp_utility_read_server_code(client_ptr, NX_SMTP_ENVELOPE_TIMEOUT, NX_TRUE);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        /* Return the error status.  */
        return(status);
    }

    /* Get category of server reply from the first digit.  */
    first_digit_server_reply = client_ptr -> nx_smtp_client_reply_code_status / 100;

    /* Is the server experiencing problems forcing the session to abort?  */
    if (first_digit_server_reply == 4)
    {

        /* Yes, set the session state to quit.  */
        client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_QUIT;

        /* Indicate mail cannot be sent. */
        client_ptr -> nx_smtp_client_mail_status = NX_SMTP_SERVER_ERROR_CODE_RECEIVED;
    }

    /* Return successful session status.  */
    return NX_SUCCESS;
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_utility_read_server_code                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function parses the 3 digit SMTP server code and stores the    */
/*    text of the server reply if there is any to the session buffer.     */
/*    Its use is intended primarily for session server reply handlers.    */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                   Session to send mail to SMTP Server   */
/*    timeout                       Timeout on receiving server reply     */
/*    receive_all_lines             Indicates if SMTP Client should expect*/
/*                                    one packet or multiple packets e.g. */
/*                                    a multi packet server reply.        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                        Successful completion status      */
/*    status                            Actual completion status          */ 
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*   _nx_smtp_parse_response            Parse an argument from text       */
/*   nx_tcp_socket_receive              Receive data over TCP socket      */
/*   nx_packet_release                  Release packet to packet pool     */
/*   memset                             Clear specified area of memory    */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Session server reply handlers     Handle server replies to SMTP     */
/*                                            commands in the session     */
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_utility_read_server_code(NX_SMTP_CLIENT *client_ptr, ULONG timeout, UINT  receive_all_lines)     
{

UINT         status; 
NX_PACKET    *packet_ptr;
UCHAR        server_code[NX_SMTP_SERVER_REPLY_CODE_SIZE + 1];
UCHAR        *work_ptr;
UINT         buffer_length;

    NX_PARAMETER_NOT_USED(receive_all_lines);

    /* Initialize argument to NULL.  */
    memset(server_code, 0, NX_SMTP_SERVER_REPLY_CODE_SIZE + 1);

    /* Process all packets received as part of server reply. A server response may
       have several 'lines' in it e.g. 250-EHLO\r\n250-AUTH LOGIN\r\n250 HELP,
      and this may be spread over multiple packets. */

    /* Wait to receive the next packet.  */
    status = nx_tcp_socket_receive(&(client_ptr -> nx_smtp_client_socket), &packet_ptr, timeout);

    /* Check for errors.  */
    if (status != NX_SUCCESS)
    {

        /* Set session state to QUIT.  */
        client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_QUIT;

        /* Return error status.  */
        return status;
    }

#ifndef NX_DISABLE_PACKET_CHAIN
    if (packet_ptr -> nx_packet_next)
    {
        
        /* Chained packet is not supported. */
        nx_packet_release(packet_ptr);

        /* Set session state to QUIT.  */
        client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_QUIT;

        /* Indicate mail cannot be sent. */
        client_ptr -> nx_smtp_client_mail_status = NX_SMTP_SERVER_ERROR_REPLY; 

        /* While there was an error with the packet, the SMTP Client is ok. So just return
           completion status and we quit the session. */
        return NX_SUCCESS;
    }
#endif /* NX_DISABLE_PACKET_CHAIN */

    /* Save the location of the server response buffer. */
    client_ptr -> nx_smtp_server_packet = packet_ptr;

    work_ptr = client_ptr -> nx_smtp_server_packet -> nx_packet_prepend_ptr;
    buffer_length = client_ptr -> nx_smtp_server_packet -> nx_packet_length;  

    /* Yes, parse the server code.  Note that with SMTP protocol, the server reply
       may be multiple lines/packets. To determine if we have the end of the reply
       we look for the command code followed by a space (not a hyphen). */
    status = _nx_smtp_parse_response(client_ptr, work_ptr, 1, buffer_length, &server_code[0], 
                                     NX_SMTP_SERVER_REPLY_CODE_SIZE, NX_FALSE);

    /* Was a server reply code found in the first line?  */
    if (status != NX_SUCCESS) 
    {

        /* No, it wasn't. */
        nx_packet_release(packet_ptr);
        client_ptr -> nx_smtp_server_packet = NX_NULL;

        /* Set session state to QUIT.  */
        client_ptr -> nx_smtp_client_rsp_state =  NX_SMTP_CLIENT_STATE_QUIT;

        /* Indicate mail cannot be sent. */
        client_ptr -> nx_smtp_client_mail_status = NX_SMTP_SERVER_ERROR_REPLY; 

        /* While there was an error with the packet, the SMTP Client is ok. So just return
           completion status and we quit the session. */
        return NX_SUCCESS;
    }
    
    /* Convert server code from text to unsigned int and store to session.  */
    client_ptr -> nx_smtp_client_reply_code_status =  strtoul((const char *)server_code, NULL, 10); 

    /* Return completion status.  */
    return (NX_SUCCESS);
}

/**************************************************************************/ 
/*                                                                        */ 
/*  FUNCTION                                               RELEASE        */ 
/*                                                                        */ 
/*    _nx_smtp_utility_send_to_server                     PORTABLE C      */ 
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */ 
/*                                                                        */ 
/*    This function compiles the message/command to send the SMTP server. */
/*    It returns a non success status if packet allocation fails or an    */
/*    error results from creating the SMTP command packet.                */
/*                                                                        */ 
/*   INPUT                                                                */ 
/*                                                                        */ 
/*    session_ptr                   Session to send mail to SMTP Server   */
/*    buffer_ptr                    Pointer to buffer for to send         */ 
/*    buffer_length                 Size of buffer                        */ 
/*    timeout                       Timeout for sending data out          */
/*                                                                        */ 
/*  OUTPUT                                                                */ 
/*                                                                        */ 
/*    NX_SUCCESS                      Successful completion               */ 
/*    status                          Actual completion status            */ 
/*                                                                        */ 
/*  CALLS                                                                 */ 
/*                                                                        */ 
/*   nx_packet_allocate              Allocate packet from specified pool  */ 
/*   nx_packet_data_append           Appends buffer to packet data        */
/*   nx_packet_release               Releases send packet back to pool    */ 
/*   nx_tcp_socket_send              Sends packet out over TCP socket     */
/*                                                                        */ 
/*  CALLED BY                                                             */ 
/*                                                                        */ 
/*   _nx_smtp_session_run            Runs the SMTP state machine          */
/*   Session server reply handlers   Handle server reply so SMTP command  */ 
/*   Session SMTP command services   Send SMTP commands to server         */ 
/*                                                                        */ 
/**************************************************************************/
UINT  _nx_smtp_utility_send_to_server(NX_SMTP_CLIENT *client_ptr, CHAR *buffer_ptr, UINT buffer_length, ULONG timeout) 
{

UINT            status;
NX_PACKET       *packet_ptr;
UINT            packet_type;


    if (client_ptr -> nx_smtp_server_address.nxd_ip_version == NX_IP_VERSION_V6)
        packet_type = NX_IPv6_TCP_PACKET;
    else
        packet_type = NX_IPv4_TCP_PACKET;

    /* Allocate a packet.  */
    status =  nx_packet_allocate(client_ptr -> nx_smtp_client_packet_pool_ptr,
                                 &packet_ptr, packet_type, NX_SMTP_CLIENT_PACKET_TIMEOUT);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        /* Abort the session. If we cannot allocate a packet we cannot send a QUIT message. */   
        client_ptr -> nx_smtp_client_cmd_state = NX_SMTP_CLIENT_STATE_IDLE;
        client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_IDLE;

        client_ptr -> nx_smtp_client_mail_status = NX_SMTP_PACKET_ALLOCATE_ERROR;

        /* Return error status.  */
        return(status);
    } 

    /* Add message to packet data.  */
    status = nx_packet_data_append(packet_ptr, buffer_ptr, buffer_length, 
                                   client_ptr -> nx_smtp_client_packet_pool_ptr, 
                                   NX_SMTP_CLIENT_PACKET_TIMEOUT);
    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        /* Release the packet.  */
        nx_packet_release(packet_ptr);

        /* Abort further processing. This error is most likely a packet allocate. */
        client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_IDLE;
        client_ptr -> nx_smtp_client_cmd_state = NX_SMTP_CLIENT_STATE_IDLE;

        client_ptr -> nx_smtp_client_mail_status = NX_SMTP_INTERNAL_ERROR;

        /* Return error status.  */
        return(status);
    }

    /* Send the packet out.  */
    status =  nx_tcp_socket_send(&client_ptr -> nx_smtp_client_socket, packet_ptr, timeout);

    /* Check for error.  */
    if (status != NX_SUCCESS)
    {

        /* Release the packet.  */
        nx_packet_release(packet_ptr);

        /* Abort further processing. If we are unable to send, we won't be able to send a quit message either. */
        client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_IDLE;
        client_ptr -> nx_smtp_client_cmd_state = NX_SMTP_CLIENT_STATE_IDLE;

        /* Return error status.  */
        return(status);
    }

    /* Return successful session status.  */
    return(NX_SUCCESS);
}



/**************************************************************************/ 
/*                                                                        */ 
/*  FUNCTION                                               RELEASE        */ 
/*                                                                        */ 
/*    _nx_smtp_utility_send_to_server                     PORTABLE C      */ 
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */ 
/*                                                                        */ 
/*    This function compiles the message/command to send the SMTP server. */
/*    It returns a non success status if packet allocation fails or an    */
/*    error results from creating the SMTP command packet.                */
/*                                                                        */ 
/*   INPUT                                                                */ 
/*                                                                        */ 
/*    session_ptr                   Session to send mail to SMTP Server   */
/*    buffer_ptr                    Pointer to buffer for to send         */ 
/*    buffer_length                 Size of buffer                        */ 
/*    timeout                       Timeout for sending data out          */
/*                                                                        */ 
/*  OUTPUT                                                                */ 
/*                                                                        */ 
/*    NX_SUCCESS                      Successful completion               */ 
/*    status                          Actual completion status            */ 
/*                                                                        */ 
/*  CALLS                                                                 */ 
/*                                                                        */ 
/*   nx_packet_allocate              Allocate packet from specified pool  */ 
/*   nx_packet_data_append           Appends buffer to packet data        */
/*   nx_packet_release               Releases send packet back to pool    */ 
/*   nx_tcp_socket_send              Sends packet out over TCP socket     */
/*                                                                        */ 
/*  CALLED BY                                                             */ 
/*                                                                        */ 
/*   _nx_smtp_session_run            Runs the SMTP state machine          */
/*   Session server reply handlers   Handle server reply so SMTP command  */ 
/*   Session SMTP command services   Send SMTP commands to server         */ 
/*                                                                        */ 
/**************************************************************************/
UINT  _nx_smtp_utility_send_header_to_server(NX_SMTP_CLIENT *client_ptr, ULONG timeout) 
{

UINT                 status;
NX_PACKET           *packet_ptr = NX_NULL;
UINT                 packet_type;
NX_SMTP_CLIENT_MAIL *mail_ptr;
NX_PACKET_POOL      *pool_ptr;
UINT                 recipient_length;
UINT                 mail_from_length;
UINT                 subject_length;


    mail_ptr = &client_ptr -> nx_smtp_client_mail;

    if (client_ptr -> nx_smtp_server_address.nxd_ip_version == NX_IP_VERSION_V6)
        packet_type = NX_IPv6_TCP_PACKET;
    else
        packet_type = NX_IPv4_TCP_PACKET;

    /* Verify string length.  */
    if (_nx_utility_string_length_check(mail_ptr -> nx_smtp_client_mail_recipient_address, &recipient_length, NX_SMTP_BUFFER_SIZE))
    {
        return(NX_SIZE_ERROR);
    }

    if (_nx_utility_string_length_check(mail_ptr -> nx_smtp_client_mail_from_address, &mail_from_length, NX_SMTP_BUFFER_SIZE))
    {
        return(NX_SIZE_ERROR);
    }

    if (_nx_utility_string_length_check(mail_ptr -> nx_smtp_client_mail_subject, &subject_length, NX_SMTP_BUFFER_SIZE))
    {
        return(NX_SIZE_ERROR);
    }


    pool_ptr = client_ptr -> nx_smtp_client_packet_pool_ptr;

    /* Allocate a packet.  */
    status =  nx_packet_allocate(pool_ptr, &packet_ptr, packet_type, NX_SMTP_CLIENT_PACKET_TIMEOUT);

    /* Check for error.  */
    if (status == NX_SUCCESS)
    {
        if (/* Add "To:  <recipient_address> CRLF" */
           (nx_packet_data_append(packet_ptr, NX_SMTP_TO_STRING, sizeof(NX_SMTP_TO_STRING) - 1,
                                  pool_ptr, NX_SMTP_CLIENT_PACKET_TIMEOUT) == NX_SUCCESS) &&
           (nx_packet_data_append(packet_ptr, mail_ptr -> nx_smtp_client_mail_recipient_address,
                                  recipient_length,
                                  pool_ptr, NX_SMTP_CLIENT_PACKET_TIMEOUT) == NX_SUCCESS) &&
           (nx_packet_data_append(packet_ptr, NX_SMTP_LINE_TERMINATOR, sizeof(NX_SMTP_LINE_TERMINATOR) - 1,
                                  pool_ptr, NX_SMTP_CLIENT_PACKET_TIMEOUT) == NX_SUCCESS) &&
           /* Add "From:  <from_address> CRLF" */
           (nx_packet_data_append(packet_ptr, NX_SMTP_FROM_STRING, sizeof(NX_SMTP_FROM_STRING) - 1,
                                  pool_ptr, NX_SMTP_CLIENT_PACKET_TIMEOUT) == NX_SUCCESS) &&
           (nx_packet_data_append(packet_ptr, mail_ptr -> nx_smtp_client_mail_from_address,
                                  mail_from_length,
                                  pool_ptr, NX_SMTP_CLIENT_PACKET_TIMEOUT) == NX_SUCCESS) &&
           (nx_packet_data_append(packet_ptr, NX_SMTP_LINE_TERMINATOR, sizeof(NX_SMTP_LINE_TERMINATOR) - 1,
                                  pool_ptr, NX_SMTP_CLIENT_PACKET_TIMEOUT) == NX_SUCCESS) &&
           /* Add "Subject:  Subject_line CRLF" */
           (nx_packet_data_append(packet_ptr, NX_SMTP_SUBJECT_STRING, sizeof(NX_SMTP_SUBJECT_STRING) - 1,
                                  pool_ptr, NX_SMTP_CLIENT_PACKET_TIMEOUT) == NX_SUCCESS) &&
           (nx_packet_data_append(packet_ptr, mail_ptr -> nx_smtp_client_mail_subject,
                                  subject_length,
                                  pool_ptr, NX_SMTP_CLIENT_PACKET_TIMEOUT) == NX_SUCCESS) &&
           (nx_packet_data_append(packet_ptr, NX_SMTP_LINE_TERMINATOR, sizeof(NX_SMTP_LINE_TERMINATOR) - 1,
                                  pool_ptr, NX_SMTP_CLIENT_PACKET_TIMEOUT) == NX_SUCCESS) && 
           /* Add the rest of the mail header components. */
           (nx_packet_data_append(packet_ptr, NX_SMTP_MAIL_HEADER_COMPONENTS, sizeof(NX_SMTP_MAIL_HEADER_COMPONENTS) - 1,
                                  pool_ptr, NX_SMTP_CLIENT_PACKET_TIMEOUT) == NX_SUCCESS))
        {
            /* Send the packet out.  */
            status =  nx_tcp_socket_send(&client_ptr -> nx_smtp_client_socket, packet_ptr, timeout);

            if(status)
            {
                /* Send failed. */
                status = NX_SMTP_INTERNAL_ERROR;
            }
        } 
        else
        {
            /* One of the Mail header components failed to be appended. */
            status = NX_SMTP_INTERNAL_ERROR;
        }
    }

    if(status)
    {

        /* Abort the session. If we cannot allocate a packet we cannot send a QUIT message. */   
        client_ptr -> nx_smtp_client_cmd_state = NX_SMTP_CLIENT_STATE_IDLE;
        client_ptr -> nx_smtp_client_rsp_state = NX_SMTP_CLIENT_STATE_IDLE;

        client_ptr -> nx_smtp_client_mail_status = NX_SMTP_INTERNAL_ERROR;

        if(packet_ptr)
        {
            /* Release the packet.  */
            nx_packet_release(packet_ptr);            
        }
    } 


    /* Return successful session status.  */
    return(NX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_utility_authentication_challenge           PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function handles parsing and decoding the server challenge,    */
/*    and setting up the client response to the challenge.  It does not   */
/*    alter the session state or determine the next session command.      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                   Session to send mail to SMTP Server   */
/*    server_challenge              Buffer containing server challenge    */ 
/*    length                        Size of server challenge buffer       */ 
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                     Successful completion status         */
/*    NX_SMTP_PARAM_ERROR            Server parsing (parameter) error     */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_smtp_parse_response       Parses argument from buffer text      */
/*    _nx_utility_base64_decode     Decodes base64 text                   */
/*    memset                        Clears specified area of memory       */
/*    memcmp                        Compares data between areas of memory */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_rsp_auth             AUTH server reply handler             */ 
/*                                                                        */
/**************************************************************************/
UINT  _nx_smtp_utility_authentication_challenge(NX_SMTP_CLIENT *client_ptr, UCHAR *server_challenge, UINT length)
{

UCHAR  encrypted_server_prompt[NX_SMTP_SERVER_CHALLENGE_MAX_STRING + 1];  
UCHAR  decoded_server_prompt[NX_SMTP_SERVER_CHALLENGE_MAX_STRING + 1];  
UINT   encrypted_server_prompt_size;
UINT   decoded_server_prompt_size;

    if (client_ptr -> nx_smtp_client_authentication_type == NX_SMTP_CLIENT_AUTH_PLAIN)
    {
        /* Set the session to reply to a username challenge.  */
        client_ptr -> nx_smtp_client_authentication_reply = NX_SMTP_CLIENT_REPLY_TO_USERNAME_PROMPT;

        /* Return successful session status.  */
        return NX_SUCCESS;
    }

    /* Clear buffers for storing encrypted and decoded server challenges.  */
    memset(encrypted_server_prompt, 0, NX_SMTP_SERVER_CHALLENGE_MAX_STRING + 1);
    memset(decoded_server_prompt, 0, NX_SMTP_SERVER_CHALLENGE_MAX_STRING + 1);

    /* Parse the 2nd argument for the server challenge we need to decode.  */
    _nx_smtp_parse_response(client_ptr, &server_challenge[0], 2, length, &encrypted_server_prompt[0], 
                            NX_SMTP_SERVER_CHALLENGE_MAX_STRING, NX_FALSE);

    /* Was a valid argument parsed?  */
    if (*encrypted_server_prompt == NX_NULL)
    {

        /* No , unable to parse server prompt.  */

        /* Return error status.  */
        return NX_SMTP_INVALID_SERVER_REPLY;
    }

    /* Calculate the name length.  */
    if (_nx_utility_string_length_check((CHAR *)encrypted_server_prompt, &encrypted_server_prompt_size, NX_SMTP_SERVER_CHALLENGE_MAX_STRING))
    {
        return NX_SMTP_INVALID_SERVER_REPLY;
    }

    /* Decode the parsed server challenge.  */
    _nx_utility_base64_decode(encrypted_server_prompt, encrypted_server_prompt_size, decoded_server_prompt, sizeof(decoded_server_prompt), &decoded_server_prompt_size);

    /* Is this a username prompt?  */
    if ((decoded_server_prompt_size == 9) &&
        ((decoded_server_prompt[0] == 'U') || (decoded_server_prompt[0] == 'u')) &&
        ((decoded_server_prompt[1] == 'S') || (decoded_server_prompt[1] == 's')) &&
        ((decoded_server_prompt[2] == 'E') || (decoded_server_prompt[2] == 'e')) &&
        ((decoded_server_prompt[3] == 'R') || (decoded_server_prompt[3] == 'r')) &&
        ((decoded_server_prompt[4] == 'N') || (decoded_server_prompt[4] == 'n')) &&
        ((decoded_server_prompt[5] == 'A') || (decoded_server_prompt[5] == 'a')) &&
        ((decoded_server_prompt[6] == 'M') || (decoded_server_prompt[6] == 'm')) &&
        ((decoded_server_prompt[7] == 'E') || (decoded_server_prompt[7] == 'e')) &&
        (decoded_server_prompt[8] == ':'))
    {

        /* Yes, set the session to reply to a username challenge.  */
        client_ptr -> nx_smtp_client_authentication_reply = NX_SMTP_CLIENT_REPLY_TO_USERNAME_PROMPT;
    }
    /* Is this a password prompt?  */
    else if ((decoded_server_prompt_size == 9) &&
             ((decoded_server_prompt[0] == 'P') || (decoded_server_prompt[0] == 'p')) &&
             ((decoded_server_prompt[1] == 'A') || (decoded_server_prompt[1] == 'a')) &&
             ((decoded_server_prompt[2] == 'S') || (decoded_server_prompt[2] == 's')) &&
             ((decoded_server_prompt[3] == 'S') || (decoded_server_prompt[3] == 's')) &&
             ((decoded_server_prompt[4] == 'W') || (decoded_server_prompt[4] == 'w')) &&
             ((decoded_server_prompt[5] == 'O') || (decoded_server_prompt[5] == 'o')) &&
             ((decoded_server_prompt[6] == 'R') || (decoded_server_prompt[6] == 'r')) &&
             ((decoded_server_prompt[7] == 'D') || (decoded_server_prompt[7] == 'd')) &&
             (decoded_server_prompt[8] == ':'))
    {

        /* Yes, set the session to reply to a password challenge.  */
        client_ptr -> nx_smtp_client_authentication_reply = NX_SMTP_CLIENT_REPLY_TO_PASSWORD_PROMPT;
    }
    else
    {

        /* Indicate invalid authentication from server.  */
        return NX_SMTP_CLIENT_REPLY_TO_UNKNOWN_PROMPT;
    }

    /* Return successful session status.  */
    return NX_SUCCESS;
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_smtp_utility_parse_server_services              PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for the AUTH option in the server response. If */
/*    authentication is offered, it attempts to find a match between      */
/*    authentication options offered by the server and the client         */
/*    authentication type. If no match between client and server is found,*/
/*    or no authentication is listed in the server response, it sets the  */
/*    client authentication type to PLAIN.                                */       
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    session_ptr                   Session to send mail to SMTP Server   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    strstr                        Search for a string within a string   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_smtp_rsp_ehlo             EHLO server reply handler             */
/*                                                                        */
/**************************************************************************/
VOID  _nx_smtp_utility_parse_server_services(NX_SMTP_CLIENT *client_ptr)
{

UINT   plain_option = NX_FALSE;
UINT   login_option = NX_FALSE;
UCHAR  *work_ptr; 
UCHAR  *temp_ptr;
ULONG  length, new_length;
UINT   i;
UINT   found = NX_FALSE;
  

    /* Does the server list an authentication type, and  does the client want authentication? */

    /* Set local variable for convenience. */
    length = client_ptr -> nx_smtp_server_packet -> nx_packet_length;

    /* Find the location of the AUTH command. */
    work_ptr = client_ptr -> nx_smtp_server_packet -> nx_packet_prepend_ptr;

    /* Check length. */
    if (length <= 4)
    {
        return;
    }

    for (i = 0; i < length - 4; i++)
    {
        if (
             ((*work_ptr == 'A') || (*work_ptr == 'a')) &&
             ((*(work_ptr + 1) == 'U') || (*(work_ptr + 1) == 'u')) &&
             ((*(work_ptr + 2) == 'T') || (*(work_ptr + 2) == 't')) &&
             ((*(work_ptr + 3) == 'H') || (*(work_ptr + 3) == 'h')) 
           )
        {

            found = NX_TRUE;
            work_ptr += 4;
            break;
        }

        work_ptr++;
    }

    /* Check if this packet has the AUTH keyword. */
    if (!found )
    {

        /* It does not. So leave the Client authentication type as is. */
        return;
    }

    /* Check if the client prefers no authentication. */
    if (found && (client_ptr -> nx_smtp_client_authentication_type == NX_SMTP_CLIENT_AUTH_NONE))
    {

        /* There is an AUTH keyword but the client prefers not to authenticate. */
        return;
    }

    /* Save the location where the search stopped. */
    temp_ptr = work_ptr;

    found = NX_FALSE;
    new_length = length - (ULONG)(temp_ptr - client_ptr -> nx_smtp_server_packet -> nx_packet_prepend_ptr);

    /* Check length. */
    if (new_length < 5)
    {
        return;
    }
    else
    {
        new_length -= 5;
    }

    /* Search for supported authentication types. */
    for (i = 0; i < new_length; i++)
    {
        if (
             ((*work_ptr == 'L') || (*work_ptr == 'l')) &&
             ((*(work_ptr + 1) == 'O') || (*(work_ptr + 1) == 'o')) &&
             ((*(work_ptr + 2) == 'G') || (*(work_ptr + 2) == 'g')) &&
             ((*(work_ptr + 3) == 'I') || (*(work_ptr + 3) == 'i')) &&
             ((*(work_ptr + 4) == 'N') || (*(work_ptr + 4) == 'n')) 
           )
        {
            found = NX_TRUE;
            break;
        }

        work_ptr++;
    }

    /* Is there a LOGIN option offered? */
    if (found)
    {
        /* Yes, set the login option flag. */
        login_option = NX_TRUE;
    }

    found = NX_FALSE;

    /* Restore the location for a new search. */
    work_ptr = temp_ptr;

    for (i = 0; i < new_length; i++)
    {
        if (
             ((*work_ptr == 'P') || (*work_ptr == 'p')) &&
             ((*(work_ptr + 1) == 'L') || (*(work_ptr + 1) == 'l')) &&
             ((*(work_ptr + 2) == 'A') || (*(work_ptr + 2) == 'a')) &&
             ((*(work_ptr + 3) == 'I') || (*(work_ptr + 3) == 'i')) &&
             ((*(work_ptr + 4) == 'N') || (*(work_ptr + 4) == 'n')) 
           )
        {
            found = NX_TRUE;
            break;
        }

        work_ptr++;
    }

    /* Is there a PLAIN option offered?  */
    if (found)
    {
        /* Yes, set the plain option flag. */
        plain_option = NX_TRUE;
    }

    /* Compare Server list of authentication types to client preferred authentication type. */

    /* Handle the case of the client prefers LOGIN authentication. */
    if (client_ptr -> nx_smtp_client_authentication_type == NX_SMTP_CLIENT_AUTH_LOGIN)
    {

        /* Yes; Check if server supports offers LOGIN authentication. */
        if (login_option)
        {

            return;
        }
        else
        {
            /* Switch client to plain authentication. */
            client_ptr -> nx_smtp_client_authentication_type = NX_SMTP_CLIENT_AUTH_PLAIN;

            return;
        }
    }

    /* Check if server listed PLAIN authentication. */
    if (plain_option && (client_ptr -> nx_smtp_client_authentication_type == NX_SMTP_CLIENT_AUTH_PLAIN))
    {
        /* Yes, and there's a match, we're done here.  */
        return;
    }

    /* If we are here, the server offers LOGIN authentication but the Client preference is something else. */
    if (login_option)
    {

        /* Switch client to LOGIN authentication. */
        client_ptr -> nx_smtp_client_authentication_type = NX_SMTP_CLIENT_AUTH_LOGIN;

        return;
    }

    /* Handle the case of no matches between server/client.  Assume the server requires authentication
       and set Client type to plain. */
    client_ptr -> nx_smtp_client_authentication_type = NX_SMTP_CLIENT_AUTH_PLAIN;

    /* Return.  */
    return;

}

/**************************************************************************/ 
/*                                                                        */ 
/*  FUNCTION                                               RELEASE        */ 
/*                                                                        */ 
/*    _nx_smtp_parse_response                             PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*  This function parses the argument specified by the argument index     */
/*  from the supplied buffer. The first argument is at index 1.           */
/*  If the specified argument can't be found the argument pointer at NULL.*/
/*                                                                        */
/*  Carriage return/line feeds can be treated as word word separator if   */
/*  specified by the crlf_are_word_breaks parameter. For OK TO CONTINUE   */
/*  messages (250), this function searches the whole message for the last */
/*  occurance of the server code e.g. code followed by a space.           */
/*                                                                        */
/*  Carriage return/line feeds are removed if found on the end of the     */
/*  parsed argument.                                                      */
/*                                                                        */
/*  INPUT                                                                 */ 
/*                                                                        */ 
/*    client_ptr                        Pointer to client instance        */
/*    buffer                            Pointer to buffer to parse        */
/*    argument_index                    Identify which argument to parse  */
/*    buffer_length                     Size of buffer to parse           */
/*    argument                          Argument to parse from buffer     */ 
/*    argument_length                   Size of argument buffer           */
/*    crlf_are_word_breaks              Treat \r\n's as word breaks       */
/*                                                                        */ 
/*  OUTPUT                                                                */ 
/*                                                                        */ 
/*     NX_SUCCESS                       Successfully parsed reply code or */ 
/*                                        authentication challenge        */
/*     NX_SMTP_INVALID_PARAM            Invalid input                     */
/*                                                                        */ 
/*  CALLS                                                                 */ 
/*                                                                        */ 
/*    toupper                           Convert chars to upper case       */
/*                                                                        */ 
/*  CALLED BY                                                             */ 
/*                                                                        */ 
/*    Application Code                                                    */ 
/*                                                                        */ 
/**************************************************************************/
UINT  _nx_smtp_parse_response(NX_SMTP_CLIENT *client_ptr, UCHAR *buffer, UINT argument_index, 
                              UINT buffer_length, UCHAR *argument, 
                              UINT argument_length, UINT crlf_are_word_breaks)
{


UINT i = 0;
UINT j = 0;
UINT argument_char_count = 0;
UCHAR *work_ptr;
UINT is_last_code;


    /* Check for invalid input parameters.  */
    if ((buffer == NX_NULL) || (buffer_length == 0) || (argument_index == 0))
    {

        return NX_SMTP_INVALID_PARAM;
    }

    work_ptr = argument;

    /* Is this the first word?
       The first word is the reply code, not an SMTP command parameter */
    if (argument_index == 1)
    {

        /* Yes, search each character up to the end of the buffer for
           the first separator.  */
        while (i < buffer_length)
        {

            /* Have we reached a word break?  */
            if (
                 (argument[0] != 0) &&
                 ((*buffer == ' ') || (*buffer == '-'))
               )
            {
                /* Yes, this marks the end of the first argument (word).  */
                break;
            }

            /* Are we starting a number?  */
            else if ((*buffer >= 0x30) && (*buffer <= 0x39))
            {

                if (work_ptr >= argument + argument_length)
                {
                    return(NX_SMTP_INTERNAL_ERROR);
                }

                /* Yes, copy to buffer. */
                *work_ptr = *buffer;  
                work_ptr++;
            }
            else
            {
                /* Not a number. make sure the argument buffer is reset */
                memset(&argument[0], 0, argument_length);
                work_ptr = argument;
            }

            /* Get the next character in the buffer.  */
            buffer++;
            i++;
        }
    }
    else
    {
        /*  No, we're not parsing the first argument. This is a special case for parsing
            the server authentication challenge, not just the reply code.  */

        /*  Mark the start of the argument at the separator after
            the end of the previous argument.  */
        while ((j < argument_index) && (i < buffer_length))
        {

            /* Keep track of the number of separators in the buffer */
            /* OD0A marks the end of a line usually in SMTP */

            /* Did we hit a line terminator?  */
            if ((*buffer == 0x0D) && (*(buffer + 1) == 0x0A))
            {

                /* Yes, Update the count of separators.  */
                j++;

                /* Are line terminators as word breaks?  */
                if (crlf_are_word_breaks  == NX_FALSE)
                {

                    /* No, this is the end of the search buffer.
                      Argument not parsed from buffer, return error.  */
                    return NX_SMTP_INVALID_SERVER_REPLY;                    
                }

                /* Get the next character after '\n' byte in the buffer.  */
                i++;
                buffer++;
            }           
            /* Did we hit a space or a dash in the first argument?  */
            else if ((*buffer == ' ') || ((*buffer == '-') && (j == 0)))
            {

                /* Yes; these are counted as separators (note that dashes found in arguments
                   after the first argument are NOT considered separators.  */
                j++; 
            }
            else
            {

                /* No, is this the argument to parse?  */
                if (j == (argument_index - 1))
                {
                    /* Yes; did we go past the argument size limit before the next word break?  */
                    if (argument_char_count >= argument_length)
                    {

                        /* Yes, unable to finish parsing this buffer. Return error.  */
                        return NX_SMTP_INVALID_SERVER_REPLY;
                    }

                    /* No, copy the next character into the argument.  */
                    argument_char_count++;

                    /* Convert to uppercase if the caller requests.  */
                    *argument++ = *buffer;
                }
            }

            /* Get the next character in the buffer.  */
            buffer++;
            i++;
        }
    }

    /* Determine if this is the last 250 in the message (e.g. followed by a space, not hyphen) */
    work_ptr = buffer - 3;

    if ((*work_ptr == '2') && (*(work_ptr + 1) == '5') && (*(work_ptr + 2) == '0') && (*(work_ptr + 3) == '-'))
    {

        UINT status;

        /* Parse the rest of the buffer to see if this packet contains the last 250 code. */
        status = _nx_smtp_parse_250_response(buffer, buffer_length - i, &is_last_code);
        if (status)
        {
            /* Error with parsing response. */
            return status;
        }

        /* Did we parse the whole 250 response? */
        if (is_last_code != NX_TRUE)
        {
            /* NO, so we are waiting for another 250 packet. */
            client_ptr -> nx_smtp_client_mute = NX_TRUE;
        }
        else
        {

            /* YES, so we are NOT waiting for a packet with the last 250 code. */
            client_ptr -> nx_smtp_client_mute = NX_FALSE;
        }
    }
    else if ((*work_ptr == '2') && (*(work_ptr + 1) == '5') && (*(work_ptr + 2) == '0') && (*(work_ptr + 3) == ' '))
    {
        client_ptr -> nx_smtp_client_mute = NX_FALSE;
    }

    return NX_SUCCESS;
}

/**************************************************************************/ 
/*                                                                        */ 
/*  FUNCTION                                               RELEASE        */ 
/*                                                                        */ 
/*    _nx_smtp_parse_250_response                         PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*  This function is called to search the rest of the message for the last*/
/*  instance of the server code 250. The first 250 has been found but     */
/*  followed by a hyphen, indicating more 250's in the message.           */
/*                                                                        */
/*  INPUT                                                                 */ 
/*                                                                        */ 
/*    buffer_ptr                        Pointer to buffer to parse        */
/*    buffer_length                     Size of buffer to parse           */
/*    is_last_code                      Indicate if last 250 found        */
/*                                                                        */ 
/*  OUTPUT                                                                */ 
/*                                                                        */ 
/*     NX_SUCCESS                       Successfully parsed last code     */ 
/*                                                                        */ 
/*                                                                        */ 
/*  CALLED BY                                                             */ 
/*                                                                        */ 
/*    _nx_smtp_parse_response          Parse (any) code in server message */ 
/*                                                                        */ 
/**************************************************************************/
UINT _nx_smtp_parse_250_response(UCHAR *buffer_ptr, UINT buffer_length, UINT *is_last_code)
{

    *is_last_code = NX_FALSE;

    /* Are there more 250 codes in this buffer? Advance the buffer
       pointer and search for a 250 followed by a space. */       

    while (buffer_length > 3)
    {
        /* Find next 250[sp] in the buffer */
        if ((*buffer_ptr == '2') && 
            (*(buffer_ptr + 1) == '5') && 
            (*(buffer_ptr + 2) == '0') && 
            (*(buffer_ptr + 3) == ' '))
        {
            *is_last_code = NX_TRUE;
            break;
        }
        else
        {
            buffer_ptr++;
            buffer_length--;
        }
    }

    return NX_SUCCESS;
}

/**************************************************************************/ 
/*                                                                        */ 
/*  FUNCTION                                               RELEASE        */ 
/*                                                                        */ 
/*    _nx_smtp_find_crlf                                  PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*  This function returns a pointer to the first carriage return/line     */
/*  feed (CRLF) sequence found in the buffer.  If no CRLF is found within */
/*  length specified, the CRLF pointer is left at NULL. If specified the  */
/*  search can start at the end of the buffer and work backwards to the   */
/*  first CRLF found.                                                     */
/*                                                                        */ 
/*  INPUT                                                                 */ 
/*                                                                        */ 
/*    buffer                          Pointer to buffer to search         */
/*    length                          Size of buffer to search            */
/*    CRLF                            Pointer to CRLF found in buffer     */ 
/*    in_reverse                      Search buffer in reverse direction  */
/*                                                                        */ 
/*  OUTPUT                                                                */ 
/*                                                                        */ 
/*    None                                                                */ 
/*                                                                        */ 
/*  CALLS                                                                 */ 
/*                                                                        */ 
/*    None                                                                */
/*                                                                        */ 
/*  CALLED BY                                                             */ 
/*                                                                        */ 
/*    Application Code                                                    */ 
/*                                                                        */ 
/**************************************************************************/
VOID  _nx_smtp_find_crlf(UCHAR *buffer, UINT length, UCHAR **CRLF, UINT in_reverse)
{

UINT i = 0;

    *CRLF = NULL;

    if ((buffer == NX_NULL) || (length == 0))
    {
        return;
    }


    /* Search for CRLF from end to start of buffer (reverse) */
    if (in_reverse == NX_TRUE)
    {

        /* Move pointer out to the end of the buffer.  */
        buffer += length - 1;

        while (i < length - 1)
        {
            if (*(buffer - 1) == 0x0D && *buffer == 0x0A)
            {
                /* Set the location of CRLF sequence.  */
                *CRLF = buffer -  1; 
                break;
            }

            /* Move the pointer back one.  */
            buffer--;

            /* Keep track how many bytes we've gone.  */
            i++;
        }
    }
    /* Search for CRLF starting at the beginning of the buffer */
    else
    {
        while( i < length - 1)
        {
            if (*buffer == 0x0D && *(buffer + 1) == 0x0A)
            {
                /* Set the location of CRLF sequence.  */
                *CRLF = buffer;  

                break;
            }
            i++;
            buffer++;
        }
    }

    return ;
}