socks5.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978
  1. /* socks5.c -- generated by Trunnel v1.5.2.
  2. * https://gitweb.torproject.org/trunnel.git
  3. * You probably shouldn't edit this file.
  4. */
  5. #include <stdlib.h>
  6. #include "trunnel-impl.h"
  7. #include "socks5.h"
  8. #define TRUNNEL_SET_ERROR_CODE(obj) \
  9. do { \
  10. (obj)->trunnel_error_code_ = 1; \
  11. } while (0)
  12. #if defined(__COVERITY__) || defined(__clang_analyzer__)
  13. /* If we're running a static analysis tool, we don't want it to complain
  14. * that some of our remaining-bytes checks are dead-code. */
  15. int socks_deadcode_dummy__ = 0;
  16. #define OR_DEADCODE_DUMMY || socks_deadcode_dummy__
  17. #else
  18. #define OR_DEADCODE_DUMMY
  19. #endif
  20. #define CHECK_REMAINING(nbytes, label) \
  21. do { \
  22. if (remaining < (nbytes) OR_DEADCODE_DUMMY) { \
  23. goto label; \
  24. } \
  25. } while (0)
  26. domainname_t *
  27. domainname_new(void)
  28. {
  29. domainname_t *val = trunnel_calloc(1, sizeof(domainname_t));
  30. if (NULL == val)
  31. return NULL;
  32. return val;
  33. }
  34. /** Release all storage held inside 'obj', but do not free 'obj'.
  35. */
  36. static void
  37. domainname_clear(domainname_t *obj)
  38. {
  39. (void) obj;
  40. TRUNNEL_DYNARRAY_WIPE(&obj->name);
  41. TRUNNEL_DYNARRAY_CLEAR(&obj->name);
  42. }
  43. void
  44. domainname_free(domainname_t *obj)
  45. {
  46. if (obj == NULL)
  47. return;
  48. domainname_clear(obj);
  49. trunnel_memwipe(obj, sizeof(domainname_t));
  50. trunnel_free_(obj);
  51. }
  52. uint8_t
  53. domainname_get_len(const domainname_t *inp)
  54. {
  55. return inp->len;
  56. }
  57. int
  58. domainname_set_len(domainname_t *inp, uint8_t val)
  59. {
  60. inp->len = val;
  61. return 0;
  62. }
  63. size_t
  64. domainname_getlen_name(const domainname_t *inp)
  65. {
  66. return TRUNNEL_DYNARRAY_LEN(&inp->name);
  67. }
  68. char
  69. domainname_get_name(domainname_t *inp, size_t idx)
  70. {
  71. return TRUNNEL_DYNARRAY_GET(&inp->name, idx);
  72. }
  73. char
  74. domainname_getconst_name(const domainname_t *inp, size_t idx)
  75. {
  76. return domainname_get_name((domainname_t*)inp, idx);
  77. }
  78. int
  79. domainname_set_name(domainname_t *inp, size_t idx, char elt)
  80. {
  81. TRUNNEL_DYNARRAY_SET(&inp->name, idx, elt);
  82. return 0;
  83. }
  84. int
  85. domainname_add_name(domainname_t *inp, char elt)
  86. {
  87. #if SIZE_MAX >= UINT8_MAX
  88. if (inp->name.n_ == UINT8_MAX)
  89. goto trunnel_alloc_failed;
  90. #endif
  91. TRUNNEL_DYNARRAY_ADD(char, &inp->name, elt, {});
  92. return 0;
  93. trunnel_alloc_failed:
  94. TRUNNEL_SET_ERROR_CODE(inp);
  95. return -1;
  96. }
  97. char *
  98. domainname_getarray_name(domainname_t *inp)
  99. {
  100. return inp->name.elts_;
  101. }
  102. const char *
  103. domainname_getconstarray_name(const domainname_t *inp)
  104. {
  105. return (const char *)domainname_getarray_name((domainname_t*)inp);
  106. }
  107. int
  108. domainname_setlen_name(domainname_t *inp, size_t newlen)
  109. {
  110. #if UINT8_MAX < SIZE_MAX
  111. if (newlen > UINT8_MAX)
  112. goto trunnel_alloc_failed;
  113. #endif
  114. return trunnel_string_setlen(&inp->name, newlen,
  115. &inp->trunnel_error_code_);
  116. trunnel_alloc_failed:
  117. TRUNNEL_SET_ERROR_CODE(inp);
  118. return -1;
  119. }
  120. const char *
  121. domainname_getstr_name(domainname_t *inp)
  122. {
  123. return trunnel_string_getstr(&inp->name);
  124. }
  125. int
  126. domainname_setstr0_name(domainname_t *inp, const char *val, size_t len)
  127. {
  128. #if UINT8_MAX < SIZE_MAX
  129. if (len > UINT8_MAX) {
  130. TRUNNEL_SET_ERROR_CODE(inp);
  131. return -1;
  132. }
  133. #endif
  134. return trunnel_string_setstr0(&inp->name, val, len, &inp->trunnel_error_code_);
  135. }
  136. int
  137. domainname_setstr_name(domainname_t *inp, const char *val)
  138. {
  139. return domainname_setstr0_name(inp, val, strlen(val));
  140. }
  141. const char *
  142. domainname_check(const domainname_t *obj)
  143. {
  144. if (obj == NULL)
  145. return "Object was NULL";
  146. if (obj->trunnel_error_code_)
  147. return "A set function failed on this object";
  148. if (TRUNNEL_DYNARRAY_LEN(&obj->name) != obj->len)
  149. return "Length mismatch for name";
  150. return NULL;
  151. }
  152. ssize_t
  153. domainname_encoded_len(const domainname_t *obj)
  154. {
  155. ssize_t result = 0;
  156. if (NULL != domainname_check(obj))
  157. return -1;
  158. /* Length of u8 len */
  159. result += 1;
  160. /* Length of char name[len] */
  161. result += TRUNNEL_DYNARRAY_LEN(&obj->name);
  162. return result;
  163. }
  164. int
  165. domainname_clear_errors(domainname_t *obj)
  166. {
  167. int r = obj->trunnel_error_code_;
  168. obj->trunnel_error_code_ = 0;
  169. return r;
  170. }
  171. ssize_t
  172. domainname_encode(uint8_t *output, const size_t avail, const domainname_t *obj)
  173. {
  174. ssize_t result = 0;
  175. size_t written = 0;
  176. uint8_t *ptr = output;
  177. const char *msg;
  178. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  179. const ssize_t encoded_len = domainname_encoded_len(obj);
  180. #endif
  181. if (NULL != (msg = domainname_check(obj)))
  182. goto check_failed;
  183. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  184. trunnel_assert(encoded_len >= 0);
  185. #endif
  186. /* Encode u8 len */
  187. trunnel_assert(written <= avail);
  188. if (avail - written < 1)
  189. goto truncated;
  190. trunnel_set_uint8(ptr, (obj->len));
  191. written += 1; ptr += 1;
  192. /* Encode char name[len] */
  193. {
  194. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->name);
  195. trunnel_assert(obj->len == elt_len);
  196. trunnel_assert(written <= avail);
  197. if (avail - written < elt_len)
  198. goto truncated;
  199. if (elt_len)
  200. memcpy(ptr, obj->name.elts_, elt_len);
  201. written += elt_len; ptr += elt_len;
  202. }
  203. trunnel_assert(ptr == output + written);
  204. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  205. {
  206. trunnel_assert(encoded_len >= 0);
  207. trunnel_assert((size_t)encoded_len == written);
  208. }
  209. #endif
  210. return written;
  211. truncated:
  212. result = -2;
  213. goto fail;
  214. check_failed:
  215. (void)msg;
  216. result = -1;
  217. goto fail;
  218. fail:
  219. trunnel_assert(result < 0);
  220. return result;
  221. }
  222. /** As domainname_parse(), but do not allocate the output object.
  223. */
  224. static ssize_t
  225. domainname_parse_into(domainname_t *obj, const uint8_t *input, const size_t len_in)
  226. {
  227. const uint8_t *ptr = input;
  228. size_t remaining = len_in;
  229. ssize_t result = 0;
  230. (void)result;
  231. /* Parse u8 len */
  232. CHECK_REMAINING(1, truncated);
  233. obj->len = (trunnel_get_uint8(ptr));
  234. remaining -= 1; ptr += 1;
  235. /* Parse char name[len] */
  236. CHECK_REMAINING(obj->len, truncated);
  237. if (domainname_setstr0_name(obj, (const char*)ptr, obj->len))
  238. goto fail;
  239. ptr += obj->len; remaining -= obj->len;
  240. trunnel_assert(ptr + remaining == input + len_in);
  241. return len_in - remaining;
  242. truncated:
  243. return -2;
  244. fail:
  245. result = -1;
  246. return result;
  247. }
  248. ssize_t
  249. domainname_parse(domainname_t **output, const uint8_t *input, const size_t len_in)
  250. {
  251. ssize_t result;
  252. *output = domainname_new();
  253. if (NULL == *output)
  254. return -1;
  255. result = domainname_parse_into(*output, input, len_in);
  256. if (result < 0) {
  257. domainname_free(*output);
  258. *output = NULL;
  259. }
  260. return result;
  261. }
  262. socks4_client_request_t *
  263. socks4_client_request_new(void)
  264. {
  265. socks4_client_request_t *val = trunnel_calloc(1, sizeof(socks4_client_request_t));
  266. if (NULL == val)
  267. return NULL;
  268. val->version = 4;
  269. val->command = CMD_BIND;
  270. return val;
  271. }
  272. /** Release all storage held inside 'obj', but do not free 'obj'.
  273. */
  274. static void
  275. socks4_client_request_clear(socks4_client_request_t *obj)
  276. {
  277. (void) obj;
  278. trunnel_wipestr(obj->username);
  279. trunnel_free(obj->username);
  280. trunnel_wipestr(obj->socks4a_addr_hostname);
  281. trunnel_free(obj->socks4a_addr_hostname);
  282. }
  283. void
  284. socks4_client_request_free(socks4_client_request_t *obj)
  285. {
  286. if (obj == NULL)
  287. return;
  288. socks4_client_request_clear(obj);
  289. trunnel_memwipe(obj, sizeof(socks4_client_request_t));
  290. trunnel_free_(obj);
  291. }
  292. uint8_t
  293. socks4_client_request_get_version(const socks4_client_request_t *inp)
  294. {
  295. return inp->version;
  296. }
  297. int
  298. socks4_client_request_set_version(socks4_client_request_t *inp, uint8_t val)
  299. {
  300. if (! ((val == 4))) {
  301. TRUNNEL_SET_ERROR_CODE(inp);
  302. return -1;
  303. }
  304. inp->version = val;
  305. return 0;
  306. }
  307. uint8_t
  308. socks4_client_request_get_command(const socks4_client_request_t *inp)
  309. {
  310. return inp->command;
  311. }
  312. int
  313. socks4_client_request_set_command(socks4_client_request_t *inp, uint8_t val)
  314. {
  315. if (! ((val == CMD_BIND || val == CMD_CONNECT || val == CMD_RESOLVE || val == CMD_RESOLVE_PTR))) {
  316. TRUNNEL_SET_ERROR_CODE(inp);
  317. return -1;
  318. }
  319. inp->command = val;
  320. return 0;
  321. }
  322. uint16_t
  323. socks4_client_request_get_port(const socks4_client_request_t *inp)
  324. {
  325. return inp->port;
  326. }
  327. int
  328. socks4_client_request_set_port(socks4_client_request_t *inp, uint16_t val)
  329. {
  330. inp->port = val;
  331. return 0;
  332. }
  333. uint32_t
  334. socks4_client_request_get_addr(const socks4_client_request_t *inp)
  335. {
  336. return inp->addr;
  337. }
  338. int
  339. socks4_client_request_set_addr(socks4_client_request_t *inp, uint32_t val)
  340. {
  341. inp->addr = val;
  342. return 0;
  343. }
  344. const char *
  345. socks4_client_request_get_username(const socks4_client_request_t *inp)
  346. {
  347. return inp->username;
  348. }
  349. int
  350. socks4_client_request_set_username(socks4_client_request_t *inp, const char *val)
  351. {
  352. trunnel_free(inp->username);
  353. if (NULL == (inp->username = trunnel_strdup(val))) {
  354. TRUNNEL_SET_ERROR_CODE(inp);
  355. return -1;
  356. }
  357. return 0;
  358. }
  359. const char *
  360. socks4_client_request_get_socks4a_addr_hostname(const socks4_client_request_t *inp)
  361. {
  362. return inp->socks4a_addr_hostname;
  363. }
  364. int
  365. socks4_client_request_set_socks4a_addr_hostname(socks4_client_request_t *inp, const char *val)
  366. {
  367. trunnel_free(inp->socks4a_addr_hostname);
  368. if (NULL == (inp->socks4a_addr_hostname = trunnel_strdup(val))) {
  369. TRUNNEL_SET_ERROR_CODE(inp);
  370. return -1;
  371. }
  372. return 0;
  373. }
  374. const char *
  375. socks4_client_request_check(const socks4_client_request_t *obj)
  376. {
  377. if (obj == NULL)
  378. return "Object was NULL";
  379. if (obj->trunnel_error_code_)
  380. return "A set function failed on this object";
  381. if (! (obj->version == 4))
  382. return "Integer out of bounds";
  383. if (! (obj->command == CMD_BIND || obj->command == CMD_CONNECT || obj->command == CMD_RESOLVE || obj->command == CMD_RESOLVE_PTR))
  384. return "Integer out of bounds";
  385. if (NULL == obj->username)
  386. return "Missing username";
  387. switch (obj->addr) {
  388. case 1:
  389. case 2:
  390. case 3:
  391. case 4:
  392. case 5:
  393. case 6:
  394. case 7:
  395. case 8:
  396. case 9:
  397. case 10:
  398. case 11:
  399. case 12:
  400. case 13:
  401. case 14:
  402. case 15:
  403. case 16:
  404. case 17:
  405. case 18:
  406. case 19:
  407. case 20:
  408. case 21:
  409. case 22:
  410. case 23:
  411. case 24:
  412. case 25:
  413. case 26:
  414. case 27:
  415. case 28:
  416. case 29:
  417. case 30:
  418. case 31:
  419. case 32:
  420. case 33:
  421. case 34:
  422. case 35:
  423. case 36:
  424. case 37:
  425. case 38:
  426. case 39:
  427. case 40:
  428. case 41:
  429. case 42:
  430. case 43:
  431. case 44:
  432. case 45:
  433. case 46:
  434. case 47:
  435. case 48:
  436. case 49:
  437. case 50:
  438. case 51:
  439. case 52:
  440. case 53:
  441. case 54:
  442. case 55:
  443. case 56:
  444. case 57:
  445. case 58:
  446. case 59:
  447. case 60:
  448. case 61:
  449. case 62:
  450. case 63:
  451. case 64:
  452. case 65:
  453. case 66:
  454. case 67:
  455. case 68:
  456. case 69:
  457. case 70:
  458. case 71:
  459. case 72:
  460. case 73:
  461. case 74:
  462. case 75:
  463. case 76:
  464. case 77:
  465. case 78:
  466. case 79:
  467. case 80:
  468. case 81:
  469. case 82:
  470. case 83:
  471. case 84:
  472. case 85:
  473. case 86:
  474. case 87:
  475. case 88:
  476. case 89:
  477. case 90:
  478. case 91:
  479. case 92:
  480. case 93:
  481. case 94:
  482. case 95:
  483. case 96:
  484. case 97:
  485. case 98:
  486. case 99:
  487. case 100:
  488. case 101:
  489. case 102:
  490. case 103:
  491. case 104:
  492. case 105:
  493. case 106:
  494. case 107:
  495. case 108:
  496. case 109:
  497. case 110:
  498. case 111:
  499. case 112:
  500. case 113:
  501. case 114:
  502. case 115:
  503. case 116:
  504. case 117:
  505. case 118:
  506. case 119:
  507. case 120:
  508. case 121:
  509. case 122:
  510. case 123:
  511. case 124:
  512. case 125:
  513. case 126:
  514. case 127:
  515. case 128:
  516. case 129:
  517. case 130:
  518. case 131:
  519. case 132:
  520. case 133:
  521. case 134:
  522. case 135:
  523. case 136:
  524. case 137:
  525. case 138:
  526. case 139:
  527. case 140:
  528. case 141:
  529. case 142:
  530. case 143:
  531. case 144:
  532. case 145:
  533. case 146:
  534. case 147:
  535. case 148:
  536. case 149:
  537. case 150:
  538. case 151:
  539. case 152:
  540. case 153:
  541. case 154:
  542. case 155:
  543. case 156:
  544. case 157:
  545. case 158:
  546. case 159:
  547. case 160:
  548. case 161:
  549. case 162:
  550. case 163:
  551. case 164:
  552. case 165:
  553. case 166:
  554. case 167:
  555. case 168:
  556. case 169:
  557. case 170:
  558. case 171:
  559. case 172:
  560. case 173:
  561. case 174:
  562. case 175:
  563. case 176:
  564. case 177:
  565. case 178:
  566. case 179:
  567. case 180:
  568. case 181:
  569. case 182:
  570. case 183:
  571. case 184:
  572. case 185:
  573. case 186:
  574. case 187:
  575. case 188:
  576. case 189:
  577. case 190:
  578. case 191:
  579. case 192:
  580. case 193:
  581. case 194:
  582. case 195:
  583. case 196:
  584. case 197:
  585. case 198:
  586. case 199:
  587. case 200:
  588. case 201:
  589. case 202:
  590. case 203:
  591. case 204:
  592. case 205:
  593. case 206:
  594. case 207:
  595. case 208:
  596. case 209:
  597. case 210:
  598. case 211:
  599. case 212:
  600. case 213:
  601. case 214:
  602. case 215:
  603. case 216:
  604. case 217:
  605. case 218:
  606. case 219:
  607. case 220:
  608. case 221:
  609. case 222:
  610. case 223:
  611. case 224:
  612. case 225:
  613. case 226:
  614. case 227:
  615. case 228:
  616. case 229:
  617. case 230:
  618. case 231:
  619. case 232:
  620. case 233:
  621. case 234:
  622. case 235:
  623. case 236:
  624. case 237:
  625. case 238:
  626. case 239:
  627. case 240:
  628. case 241:
  629. case 242:
  630. case 243:
  631. case 244:
  632. case 245:
  633. case 246:
  634. case 247:
  635. case 248:
  636. case 249:
  637. case 250:
  638. case 251:
  639. case 252:
  640. case 253:
  641. case 254:
  642. case 255:
  643. if (NULL == obj->socks4a_addr_hostname)
  644. return "Missing socks4a_addr_hostname";
  645. break;
  646. default:
  647. break;
  648. }
  649. return NULL;
  650. }
  651. ssize_t
  652. socks4_client_request_encoded_len(const socks4_client_request_t *obj)
  653. {
  654. ssize_t result = 0;
  655. if (NULL != socks4_client_request_check(obj))
  656. return -1;
  657. /* Length of u8 version IN [4] */
  658. result += 1;
  659. /* Length of u8 command IN [CMD_BIND, CMD_CONNECT, CMD_RESOLVE, CMD_RESOLVE_PTR] */
  660. result += 1;
  661. /* Length of u16 port */
  662. result += 2;
  663. /* Length of u32 addr */
  664. result += 4;
  665. /* Length of nulterm username */
  666. result += strlen(obj->username) + 1;
  667. switch (obj->addr) {
  668. case 1:
  669. case 2:
  670. case 3:
  671. case 4:
  672. case 5:
  673. case 6:
  674. case 7:
  675. case 8:
  676. case 9:
  677. case 10:
  678. case 11:
  679. case 12:
  680. case 13:
  681. case 14:
  682. case 15:
  683. case 16:
  684. case 17:
  685. case 18:
  686. case 19:
  687. case 20:
  688. case 21:
  689. case 22:
  690. case 23:
  691. case 24:
  692. case 25:
  693. case 26:
  694. case 27:
  695. case 28:
  696. case 29:
  697. case 30:
  698. case 31:
  699. case 32:
  700. case 33:
  701. case 34:
  702. case 35:
  703. case 36:
  704. case 37:
  705. case 38:
  706. case 39:
  707. case 40:
  708. case 41:
  709. case 42:
  710. case 43:
  711. case 44:
  712. case 45:
  713. case 46:
  714. case 47:
  715. case 48:
  716. case 49:
  717. case 50:
  718. case 51:
  719. case 52:
  720. case 53:
  721. case 54:
  722. case 55:
  723. case 56:
  724. case 57:
  725. case 58:
  726. case 59:
  727. case 60:
  728. case 61:
  729. case 62:
  730. case 63:
  731. case 64:
  732. case 65:
  733. case 66:
  734. case 67:
  735. case 68:
  736. case 69:
  737. case 70:
  738. case 71:
  739. case 72:
  740. case 73:
  741. case 74:
  742. case 75:
  743. case 76:
  744. case 77:
  745. case 78:
  746. case 79:
  747. case 80:
  748. case 81:
  749. case 82:
  750. case 83:
  751. case 84:
  752. case 85:
  753. case 86:
  754. case 87:
  755. case 88:
  756. case 89:
  757. case 90:
  758. case 91:
  759. case 92:
  760. case 93:
  761. case 94:
  762. case 95:
  763. case 96:
  764. case 97:
  765. case 98:
  766. case 99:
  767. case 100:
  768. case 101:
  769. case 102:
  770. case 103:
  771. case 104:
  772. case 105:
  773. case 106:
  774. case 107:
  775. case 108:
  776. case 109:
  777. case 110:
  778. case 111:
  779. case 112:
  780. case 113:
  781. case 114:
  782. case 115:
  783. case 116:
  784. case 117:
  785. case 118:
  786. case 119:
  787. case 120:
  788. case 121:
  789. case 122:
  790. case 123:
  791. case 124:
  792. case 125:
  793. case 126:
  794. case 127:
  795. case 128:
  796. case 129:
  797. case 130:
  798. case 131:
  799. case 132:
  800. case 133:
  801. case 134:
  802. case 135:
  803. case 136:
  804. case 137:
  805. case 138:
  806. case 139:
  807. case 140:
  808. case 141:
  809. case 142:
  810. case 143:
  811. case 144:
  812. case 145:
  813. case 146:
  814. case 147:
  815. case 148:
  816. case 149:
  817. case 150:
  818. case 151:
  819. case 152:
  820. case 153:
  821. case 154:
  822. case 155:
  823. case 156:
  824. case 157:
  825. case 158:
  826. case 159:
  827. case 160:
  828. case 161:
  829. case 162:
  830. case 163:
  831. case 164:
  832. case 165:
  833. case 166:
  834. case 167:
  835. case 168:
  836. case 169:
  837. case 170:
  838. case 171:
  839. case 172:
  840. case 173:
  841. case 174:
  842. case 175:
  843. case 176:
  844. case 177:
  845. case 178:
  846. case 179:
  847. case 180:
  848. case 181:
  849. case 182:
  850. case 183:
  851. case 184:
  852. case 185:
  853. case 186:
  854. case 187:
  855. case 188:
  856. case 189:
  857. case 190:
  858. case 191:
  859. case 192:
  860. case 193:
  861. case 194:
  862. case 195:
  863. case 196:
  864. case 197:
  865. case 198:
  866. case 199:
  867. case 200:
  868. case 201:
  869. case 202:
  870. case 203:
  871. case 204:
  872. case 205:
  873. case 206:
  874. case 207:
  875. case 208:
  876. case 209:
  877. case 210:
  878. case 211:
  879. case 212:
  880. case 213:
  881. case 214:
  882. case 215:
  883. case 216:
  884. case 217:
  885. case 218:
  886. case 219:
  887. case 220:
  888. case 221:
  889. case 222:
  890. case 223:
  891. case 224:
  892. case 225:
  893. case 226:
  894. case 227:
  895. case 228:
  896. case 229:
  897. case 230:
  898. case 231:
  899. case 232:
  900. case 233:
  901. case 234:
  902. case 235:
  903. case 236:
  904. case 237:
  905. case 238:
  906. case 239:
  907. case 240:
  908. case 241:
  909. case 242:
  910. case 243:
  911. case 244:
  912. case 245:
  913. case 246:
  914. case 247:
  915. case 248:
  916. case 249:
  917. case 250:
  918. case 251:
  919. case 252:
  920. case 253:
  921. case 254:
  922. case 255:
  923. /* Length of nulterm socks4a_addr_hostname */
  924. result += strlen(obj->socks4a_addr_hostname) + 1;
  925. break;
  926. default:
  927. break;
  928. }
  929. return result;
  930. }
  931. int
  932. socks4_client_request_clear_errors(socks4_client_request_t *obj)
  933. {
  934. int r = obj->trunnel_error_code_;
  935. obj->trunnel_error_code_ = 0;
  936. return r;
  937. }
  938. ssize_t
  939. socks4_client_request_encode(uint8_t *output, const size_t avail, const socks4_client_request_t *obj)
  940. {
  941. ssize_t result = 0;
  942. size_t written = 0;
  943. uint8_t *ptr = output;
  944. const char *msg;
  945. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  946. const ssize_t encoded_len = socks4_client_request_encoded_len(obj);
  947. #endif
  948. if (NULL != (msg = socks4_client_request_check(obj)))
  949. goto check_failed;
  950. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  951. trunnel_assert(encoded_len >= 0);
  952. #endif
  953. /* Encode u8 version IN [4] */
  954. trunnel_assert(written <= avail);
  955. if (avail - written < 1)
  956. goto truncated;
  957. trunnel_set_uint8(ptr, (obj->version));
  958. written += 1; ptr += 1;
  959. /* Encode u8 command IN [CMD_BIND, CMD_CONNECT, CMD_RESOLVE, CMD_RESOLVE_PTR] */
  960. trunnel_assert(written <= avail);
  961. if (avail - written < 1)
  962. goto truncated;
  963. trunnel_set_uint8(ptr, (obj->command));
  964. written += 1; ptr += 1;
  965. /* Encode u16 port */
  966. trunnel_assert(written <= avail);
  967. if (avail - written < 2)
  968. goto truncated;
  969. trunnel_set_uint16(ptr, trunnel_htons(obj->port));
  970. written += 2; ptr += 2;
  971. /* Encode u32 addr */
  972. trunnel_assert(written <= avail);
  973. if (avail - written < 4)
  974. goto truncated;
  975. trunnel_set_uint32(ptr, trunnel_htonl(obj->addr));
  976. written += 4; ptr += 4;
  977. /* Encode nulterm username */
  978. {
  979. size_t len = strlen(obj->username);
  980. trunnel_assert(written <= avail);
  981. if (avail - written < len + 1)
  982. goto truncated;
  983. memcpy(ptr, obj->username, len + 1);
  984. ptr += len + 1; written += len + 1;
  985. }
  986. /* Encode union socks4a_addr[addr] */
  987. trunnel_assert(written <= avail);
  988. switch (obj->addr) {
  989. case 1:
  990. case 2:
  991. case 3:
  992. case 4:
  993. case 5:
  994. case 6:
  995. case 7:
  996. case 8:
  997. case 9:
  998. case 10:
  999. case 11:
  1000. case 12:
  1001. case 13:
  1002. case 14:
  1003. case 15:
  1004. case 16:
  1005. case 17:
  1006. case 18:
  1007. case 19:
  1008. case 20:
  1009. case 21:
  1010. case 22:
  1011. case 23:
  1012. case 24:
  1013. case 25:
  1014. case 26:
  1015. case 27:
  1016. case 28:
  1017. case 29:
  1018. case 30:
  1019. case 31:
  1020. case 32:
  1021. case 33:
  1022. case 34:
  1023. case 35:
  1024. case 36:
  1025. case 37:
  1026. case 38:
  1027. case 39:
  1028. case 40:
  1029. case 41:
  1030. case 42:
  1031. case 43:
  1032. case 44:
  1033. case 45:
  1034. case 46:
  1035. case 47:
  1036. case 48:
  1037. case 49:
  1038. case 50:
  1039. case 51:
  1040. case 52:
  1041. case 53:
  1042. case 54:
  1043. case 55:
  1044. case 56:
  1045. case 57:
  1046. case 58:
  1047. case 59:
  1048. case 60:
  1049. case 61:
  1050. case 62:
  1051. case 63:
  1052. case 64:
  1053. case 65:
  1054. case 66:
  1055. case 67:
  1056. case 68:
  1057. case 69:
  1058. case 70:
  1059. case 71:
  1060. case 72:
  1061. case 73:
  1062. case 74:
  1063. case 75:
  1064. case 76:
  1065. case 77:
  1066. case 78:
  1067. case 79:
  1068. case 80:
  1069. case 81:
  1070. case 82:
  1071. case 83:
  1072. case 84:
  1073. case 85:
  1074. case 86:
  1075. case 87:
  1076. case 88:
  1077. case 89:
  1078. case 90:
  1079. case 91:
  1080. case 92:
  1081. case 93:
  1082. case 94:
  1083. case 95:
  1084. case 96:
  1085. case 97:
  1086. case 98:
  1087. case 99:
  1088. case 100:
  1089. case 101:
  1090. case 102:
  1091. case 103:
  1092. case 104:
  1093. case 105:
  1094. case 106:
  1095. case 107:
  1096. case 108:
  1097. case 109:
  1098. case 110:
  1099. case 111:
  1100. case 112:
  1101. case 113:
  1102. case 114:
  1103. case 115:
  1104. case 116:
  1105. case 117:
  1106. case 118:
  1107. case 119:
  1108. case 120:
  1109. case 121:
  1110. case 122:
  1111. case 123:
  1112. case 124:
  1113. case 125:
  1114. case 126:
  1115. case 127:
  1116. case 128:
  1117. case 129:
  1118. case 130:
  1119. case 131:
  1120. case 132:
  1121. case 133:
  1122. case 134:
  1123. case 135:
  1124. case 136:
  1125. case 137:
  1126. case 138:
  1127. case 139:
  1128. case 140:
  1129. case 141:
  1130. case 142:
  1131. case 143:
  1132. case 144:
  1133. case 145:
  1134. case 146:
  1135. case 147:
  1136. case 148:
  1137. case 149:
  1138. case 150:
  1139. case 151:
  1140. case 152:
  1141. case 153:
  1142. case 154:
  1143. case 155:
  1144. case 156:
  1145. case 157:
  1146. case 158:
  1147. case 159:
  1148. case 160:
  1149. case 161:
  1150. case 162:
  1151. case 163:
  1152. case 164:
  1153. case 165:
  1154. case 166:
  1155. case 167:
  1156. case 168:
  1157. case 169:
  1158. case 170:
  1159. case 171:
  1160. case 172:
  1161. case 173:
  1162. case 174:
  1163. case 175:
  1164. case 176:
  1165. case 177:
  1166. case 178:
  1167. case 179:
  1168. case 180:
  1169. case 181:
  1170. case 182:
  1171. case 183:
  1172. case 184:
  1173. case 185:
  1174. case 186:
  1175. case 187:
  1176. case 188:
  1177. case 189:
  1178. case 190:
  1179. case 191:
  1180. case 192:
  1181. case 193:
  1182. case 194:
  1183. case 195:
  1184. case 196:
  1185. case 197:
  1186. case 198:
  1187. case 199:
  1188. case 200:
  1189. case 201:
  1190. case 202:
  1191. case 203:
  1192. case 204:
  1193. case 205:
  1194. case 206:
  1195. case 207:
  1196. case 208:
  1197. case 209:
  1198. case 210:
  1199. case 211:
  1200. case 212:
  1201. case 213:
  1202. case 214:
  1203. case 215:
  1204. case 216:
  1205. case 217:
  1206. case 218:
  1207. case 219:
  1208. case 220:
  1209. case 221:
  1210. case 222:
  1211. case 223:
  1212. case 224:
  1213. case 225:
  1214. case 226:
  1215. case 227:
  1216. case 228:
  1217. case 229:
  1218. case 230:
  1219. case 231:
  1220. case 232:
  1221. case 233:
  1222. case 234:
  1223. case 235:
  1224. case 236:
  1225. case 237:
  1226. case 238:
  1227. case 239:
  1228. case 240:
  1229. case 241:
  1230. case 242:
  1231. case 243:
  1232. case 244:
  1233. case 245:
  1234. case 246:
  1235. case 247:
  1236. case 248:
  1237. case 249:
  1238. case 250:
  1239. case 251:
  1240. case 252:
  1241. case 253:
  1242. case 254:
  1243. case 255:
  1244. /* Encode nulterm socks4a_addr_hostname */
  1245. {
  1246. size_t len = strlen(obj->socks4a_addr_hostname);
  1247. trunnel_assert(written <= avail);
  1248. if (avail - written < len + 1)
  1249. goto truncated;
  1250. memcpy(ptr, obj->socks4a_addr_hostname, len + 1);
  1251. ptr += len + 1; written += len + 1;
  1252. }
  1253. break;
  1254. default:
  1255. break;
  1256. }
  1257. trunnel_assert(ptr == output + written);
  1258. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1259. {
  1260. trunnel_assert(encoded_len >= 0);
  1261. trunnel_assert((size_t)encoded_len == written);
  1262. }
  1263. #endif
  1264. return written;
  1265. truncated:
  1266. result = -2;
  1267. goto fail;
  1268. check_failed:
  1269. (void)msg;
  1270. result = -1;
  1271. goto fail;
  1272. fail:
  1273. trunnel_assert(result < 0);
  1274. return result;
  1275. }
  1276. /** As socks4_client_request_parse(), but do not allocate the output
  1277. * object.
  1278. */
  1279. static ssize_t
  1280. socks4_client_request_parse_into(socks4_client_request_t *obj, const uint8_t *input, const size_t len_in)
  1281. {
  1282. const uint8_t *ptr = input;
  1283. size_t remaining = len_in;
  1284. ssize_t result = 0;
  1285. (void)result;
  1286. /* Parse u8 version IN [4] */
  1287. CHECK_REMAINING(1, truncated);
  1288. obj->version = (trunnel_get_uint8(ptr));
  1289. remaining -= 1; ptr += 1;
  1290. if (! (obj->version == 4))
  1291. goto fail;
  1292. /* Parse u8 command IN [CMD_BIND, CMD_CONNECT, CMD_RESOLVE, CMD_RESOLVE_PTR] */
  1293. CHECK_REMAINING(1, truncated);
  1294. obj->command = (trunnel_get_uint8(ptr));
  1295. remaining -= 1; ptr += 1;
  1296. if (! (obj->command == CMD_BIND || obj->command == CMD_CONNECT || obj->command == CMD_RESOLVE || obj->command == CMD_RESOLVE_PTR))
  1297. goto fail;
  1298. /* Parse u16 port */
  1299. CHECK_REMAINING(2, truncated);
  1300. obj->port = trunnel_ntohs(trunnel_get_uint16(ptr));
  1301. remaining -= 2; ptr += 2;
  1302. /* Parse u32 addr */
  1303. CHECK_REMAINING(4, truncated);
  1304. obj->addr = trunnel_ntohl(trunnel_get_uint32(ptr));
  1305. remaining -= 4; ptr += 4;
  1306. /* Parse nulterm username */
  1307. {
  1308. uint8_t *eos = (uint8_t*)memchr(ptr, 0, remaining);
  1309. size_t memlen;
  1310. if (eos == NULL)
  1311. goto truncated;
  1312. trunnel_assert(eos >= ptr);
  1313. trunnel_assert((size_t)(eos - ptr) < SIZE_MAX - 1);
  1314. memlen = ((size_t)(eos - ptr)) + 1;
  1315. if (!(obj->username = trunnel_malloc(memlen)))
  1316. goto fail;
  1317. memcpy(obj->username, ptr, memlen);
  1318. remaining -= memlen; ptr += memlen;
  1319. }
  1320. /* Parse union socks4a_addr[addr] */
  1321. switch (obj->addr) {
  1322. case 1:
  1323. case 2:
  1324. case 3:
  1325. case 4:
  1326. case 5:
  1327. case 6:
  1328. case 7:
  1329. case 8:
  1330. case 9:
  1331. case 10:
  1332. case 11:
  1333. case 12:
  1334. case 13:
  1335. case 14:
  1336. case 15:
  1337. case 16:
  1338. case 17:
  1339. case 18:
  1340. case 19:
  1341. case 20:
  1342. case 21:
  1343. case 22:
  1344. case 23:
  1345. case 24:
  1346. case 25:
  1347. case 26:
  1348. case 27:
  1349. case 28:
  1350. case 29:
  1351. case 30:
  1352. case 31:
  1353. case 32:
  1354. case 33:
  1355. case 34:
  1356. case 35:
  1357. case 36:
  1358. case 37:
  1359. case 38:
  1360. case 39:
  1361. case 40:
  1362. case 41:
  1363. case 42:
  1364. case 43:
  1365. case 44:
  1366. case 45:
  1367. case 46:
  1368. case 47:
  1369. case 48:
  1370. case 49:
  1371. case 50:
  1372. case 51:
  1373. case 52:
  1374. case 53:
  1375. case 54:
  1376. case 55:
  1377. case 56:
  1378. case 57:
  1379. case 58:
  1380. case 59:
  1381. case 60:
  1382. case 61:
  1383. case 62:
  1384. case 63:
  1385. case 64:
  1386. case 65:
  1387. case 66:
  1388. case 67:
  1389. case 68:
  1390. case 69:
  1391. case 70:
  1392. case 71:
  1393. case 72:
  1394. case 73:
  1395. case 74:
  1396. case 75:
  1397. case 76:
  1398. case 77:
  1399. case 78:
  1400. case 79:
  1401. case 80:
  1402. case 81:
  1403. case 82:
  1404. case 83:
  1405. case 84:
  1406. case 85:
  1407. case 86:
  1408. case 87:
  1409. case 88:
  1410. case 89:
  1411. case 90:
  1412. case 91:
  1413. case 92:
  1414. case 93:
  1415. case 94:
  1416. case 95:
  1417. case 96:
  1418. case 97:
  1419. case 98:
  1420. case 99:
  1421. case 100:
  1422. case 101:
  1423. case 102:
  1424. case 103:
  1425. case 104:
  1426. case 105:
  1427. case 106:
  1428. case 107:
  1429. case 108:
  1430. case 109:
  1431. case 110:
  1432. case 111:
  1433. case 112:
  1434. case 113:
  1435. case 114:
  1436. case 115:
  1437. case 116:
  1438. case 117:
  1439. case 118:
  1440. case 119:
  1441. case 120:
  1442. case 121:
  1443. case 122:
  1444. case 123:
  1445. case 124:
  1446. case 125:
  1447. case 126:
  1448. case 127:
  1449. case 128:
  1450. case 129:
  1451. case 130:
  1452. case 131:
  1453. case 132:
  1454. case 133:
  1455. case 134:
  1456. case 135:
  1457. case 136:
  1458. case 137:
  1459. case 138:
  1460. case 139:
  1461. case 140:
  1462. case 141:
  1463. case 142:
  1464. case 143:
  1465. case 144:
  1466. case 145:
  1467. case 146:
  1468. case 147:
  1469. case 148:
  1470. case 149:
  1471. case 150:
  1472. case 151:
  1473. case 152:
  1474. case 153:
  1475. case 154:
  1476. case 155:
  1477. case 156:
  1478. case 157:
  1479. case 158:
  1480. case 159:
  1481. case 160:
  1482. case 161:
  1483. case 162:
  1484. case 163:
  1485. case 164:
  1486. case 165:
  1487. case 166:
  1488. case 167:
  1489. case 168:
  1490. case 169:
  1491. case 170:
  1492. case 171:
  1493. case 172:
  1494. case 173:
  1495. case 174:
  1496. case 175:
  1497. case 176:
  1498. case 177:
  1499. case 178:
  1500. case 179:
  1501. case 180:
  1502. case 181:
  1503. case 182:
  1504. case 183:
  1505. case 184:
  1506. case 185:
  1507. case 186:
  1508. case 187:
  1509. case 188:
  1510. case 189:
  1511. case 190:
  1512. case 191:
  1513. case 192:
  1514. case 193:
  1515. case 194:
  1516. case 195:
  1517. case 196:
  1518. case 197:
  1519. case 198:
  1520. case 199:
  1521. case 200:
  1522. case 201:
  1523. case 202:
  1524. case 203:
  1525. case 204:
  1526. case 205:
  1527. case 206:
  1528. case 207:
  1529. case 208:
  1530. case 209:
  1531. case 210:
  1532. case 211:
  1533. case 212:
  1534. case 213:
  1535. case 214:
  1536. case 215:
  1537. case 216:
  1538. case 217:
  1539. case 218:
  1540. case 219:
  1541. case 220:
  1542. case 221:
  1543. case 222:
  1544. case 223:
  1545. case 224:
  1546. case 225:
  1547. case 226:
  1548. case 227:
  1549. case 228:
  1550. case 229:
  1551. case 230:
  1552. case 231:
  1553. case 232:
  1554. case 233:
  1555. case 234:
  1556. case 235:
  1557. case 236:
  1558. case 237:
  1559. case 238:
  1560. case 239:
  1561. case 240:
  1562. case 241:
  1563. case 242:
  1564. case 243:
  1565. case 244:
  1566. case 245:
  1567. case 246:
  1568. case 247:
  1569. case 248:
  1570. case 249:
  1571. case 250:
  1572. case 251:
  1573. case 252:
  1574. case 253:
  1575. case 254:
  1576. case 255:
  1577. /* Parse nulterm socks4a_addr_hostname */
  1578. {
  1579. uint8_t *eos = (uint8_t*)memchr(ptr, 0, remaining);
  1580. size_t memlen;
  1581. if (eos == NULL)
  1582. goto truncated;
  1583. trunnel_assert(eos >= ptr);
  1584. trunnel_assert((size_t)(eos - ptr) < SIZE_MAX - 1);
  1585. memlen = ((size_t)(eos - ptr)) + 1;
  1586. if (!(obj->socks4a_addr_hostname = trunnel_malloc(memlen)))
  1587. goto fail;
  1588. memcpy(obj->socks4a_addr_hostname, ptr, memlen);
  1589. remaining -= memlen; ptr += memlen;
  1590. }
  1591. break;
  1592. default:
  1593. break;
  1594. }
  1595. trunnel_assert(ptr + remaining == input + len_in);
  1596. return len_in - remaining;
  1597. truncated:
  1598. return -2;
  1599. fail:
  1600. result = -1;
  1601. return result;
  1602. }
  1603. ssize_t
  1604. socks4_client_request_parse(socks4_client_request_t **output, const uint8_t *input, const size_t len_in)
  1605. {
  1606. ssize_t result;
  1607. *output = socks4_client_request_new();
  1608. if (NULL == *output)
  1609. return -1;
  1610. result = socks4_client_request_parse_into(*output, input, len_in);
  1611. if (result < 0) {
  1612. socks4_client_request_free(*output);
  1613. *output = NULL;
  1614. }
  1615. return result;
  1616. }
  1617. socks4_server_reply_t *
  1618. socks4_server_reply_new(void)
  1619. {
  1620. socks4_server_reply_t *val = trunnel_calloc(1, sizeof(socks4_server_reply_t));
  1621. if (NULL == val)
  1622. return NULL;
  1623. val->version = 4;
  1624. return val;
  1625. }
  1626. /** Release all storage held inside 'obj', but do not free 'obj'.
  1627. */
  1628. static void
  1629. socks4_server_reply_clear(socks4_server_reply_t *obj)
  1630. {
  1631. (void) obj;
  1632. }
  1633. void
  1634. socks4_server_reply_free(socks4_server_reply_t *obj)
  1635. {
  1636. if (obj == NULL)
  1637. return;
  1638. socks4_server_reply_clear(obj);
  1639. trunnel_memwipe(obj, sizeof(socks4_server_reply_t));
  1640. trunnel_free_(obj);
  1641. }
  1642. uint8_t
  1643. socks4_server_reply_get_version(const socks4_server_reply_t *inp)
  1644. {
  1645. return inp->version;
  1646. }
  1647. int
  1648. socks4_server_reply_set_version(socks4_server_reply_t *inp, uint8_t val)
  1649. {
  1650. if (! ((val == 4))) {
  1651. TRUNNEL_SET_ERROR_CODE(inp);
  1652. return -1;
  1653. }
  1654. inp->version = val;
  1655. return 0;
  1656. }
  1657. uint8_t
  1658. socks4_server_reply_get_status(const socks4_server_reply_t *inp)
  1659. {
  1660. return inp->status;
  1661. }
  1662. int
  1663. socks4_server_reply_set_status(socks4_server_reply_t *inp, uint8_t val)
  1664. {
  1665. inp->status = val;
  1666. return 0;
  1667. }
  1668. uint16_t
  1669. socks4_server_reply_get_port(const socks4_server_reply_t *inp)
  1670. {
  1671. return inp->port;
  1672. }
  1673. int
  1674. socks4_server_reply_set_port(socks4_server_reply_t *inp, uint16_t val)
  1675. {
  1676. inp->port = val;
  1677. return 0;
  1678. }
  1679. uint32_t
  1680. socks4_server_reply_get_addr(const socks4_server_reply_t *inp)
  1681. {
  1682. return inp->addr;
  1683. }
  1684. int
  1685. socks4_server_reply_set_addr(socks4_server_reply_t *inp, uint32_t val)
  1686. {
  1687. inp->addr = val;
  1688. return 0;
  1689. }
  1690. const char *
  1691. socks4_server_reply_check(const socks4_server_reply_t *obj)
  1692. {
  1693. if (obj == NULL)
  1694. return "Object was NULL";
  1695. if (obj->trunnel_error_code_)
  1696. return "A set function failed on this object";
  1697. if (! (obj->version == 4))
  1698. return "Integer out of bounds";
  1699. return NULL;
  1700. }
  1701. ssize_t
  1702. socks4_server_reply_encoded_len(const socks4_server_reply_t *obj)
  1703. {
  1704. ssize_t result = 0;
  1705. if (NULL != socks4_server_reply_check(obj))
  1706. return -1;
  1707. /* Length of u8 version IN [4] */
  1708. result += 1;
  1709. /* Length of u8 status */
  1710. result += 1;
  1711. /* Length of u16 port */
  1712. result += 2;
  1713. /* Length of u32 addr */
  1714. result += 4;
  1715. return result;
  1716. }
  1717. int
  1718. socks4_server_reply_clear_errors(socks4_server_reply_t *obj)
  1719. {
  1720. int r = obj->trunnel_error_code_;
  1721. obj->trunnel_error_code_ = 0;
  1722. return r;
  1723. }
  1724. ssize_t
  1725. socks4_server_reply_encode(uint8_t *output, const size_t avail, const socks4_server_reply_t *obj)
  1726. {
  1727. ssize_t result = 0;
  1728. size_t written = 0;
  1729. uint8_t *ptr = output;
  1730. const char *msg;
  1731. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1732. const ssize_t encoded_len = socks4_server_reply_encoded_len(obj);
  1733. #endif
  1734. if (NULL != (msg = socks4_server_reply_check(obj)))
  1735. goto check_failed;
  1736. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1737. trunnel_assert(encoded_len >= 0);
  1738. #endif
  1739. /* Encode u8 version IN [4] */
  1740. trunnel_assert(written <= avail);
  1741. if (avail - written < 1)
  1742. goto truncated;
  1743. trunnel_set_uint8(ptr, (obj->version));
  1744. written += 1; ptr += 1;
  1745. /* Encode u8 status */
  1746. trunnel_assert(written <= avail);
  1747. if (avail - written < 1)
  1748. goto truncated;
  1749. trunnel_set_uint8(ptr, (obj->status));
  1750. written += 1; ptr += 1;
  1751. /* Encode u16 port */
  1752. trunnel_assert(written <= avail);
  1753. if (avail - written < 2)
  1754. goto truncated;
  1755. trunnel_set_uint16(ptr, trunnel_htons(obj->port));
  1756. written += 2; ptr += 2;
  1757. /* Encode u32 addr */
  1758. trunnel_assert(written <= avail);
  1759. if (avail - written < 4)
  1760. goto truncated;
  1761. trunnel_set_uint32(ptr, trunnel_htonl(obj->addr));
  1762. written += 4; ptr += 4;
  1763. trunnel_assert(ptr == output + written);
  1764. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1765. {
  1766. trunnel_assert(encoded_len >= 0);
  1767. trunnel_assert((size_t)encoded_len == written);
  1768. }
  1769. #endif
  1770. return written;
  1771. truncated:
  1772. result = -2;
  1773. goto fail;
  1774. check_failed:
  1775. (void)msg;
  1776. result = -1;
  1777. goto fail;
  1778. fail:
  1779. trunnel_assert(result < 0);
  1780. return result;
  1781. }
  1782. /** As socks4_server_reply_parse(), but do not allocate the output
  1783. * object.
  1784. */
  1785. static ssize_t
  1786. socks4_server_reply_parse_into(socks4_server_reply_t *obj, const uint8_t *input, const size_t len_in)
  1787. {
  1788. const uint8_t *ptr = input;
  1789. size_t remaining = len_in;
  1790. ssize_t result = 0;
  1791. (void)result;
  1792. /* Parse u8 version IN [4] */
  1793. CHECK_REMAINING(1, truncated);
  1794. obj->version = (trunnel_get_uint8(ptr));
  1795. remaining -= 1; ptr += 1;
  1796. if (! (obj->version == 4))
  1797. goto fail;
  1798. /* Parse u8 status */
  1799. CHECK_REMAINING(1, truncated);
  1800. obj->status = (trunnel_get_uint8(ptr));
  1801. remaining -= 1; ptr += 1;
  1802. /* Parse u16 port */
  1803. CHECK_REMAINING(2, truncated);
  1804. obj->port = trunnel_ntohs(trunnel_get_uint16(ptr));
  1805. remaining -= 2; ptr += 2;
  1806. /* Parse u32 addr */
  1807. CHECK_REMAINING(4, truncated);
  1808. obj->addr = trunnel_ntohl(trunnel_get_uint32(ptr));
  1809. remaining -= 4; ptr += 4;
  1810. trunnel_assert(ptr + remaining == input + len_in);
  1811. return len_in - remaining;
  1812. truncated:
  1813. return -2;
  1814. fail:
  1815. result = -1;
  1816. return result;
  1817. }
  1818. ssize_t
  1819. socks4_server_reply_parse(socks4_server_reply_t **output, const uint8_t *input, const size_t len_in)
  1820. {
  1821. ssize_t result;
  1822. *output = socks4_server_reply_new();
  1823. if (NULL == *output)
  1824. return -1;
  1825. result = socks4_server_reply_parse_into(*output, input, len_in);
  1826. if (result < 0) {
  1827. socks4_server_reply_free(*output);
  1828. *output = NULL;
  1829. }
  1830. return result;
  1831. }
  1832. socks5_client_userpass_auth_t *
  1833. socks5_client_userpass_auth_new(void)
  1834. {
  1835. socks5_client_userpass_auth_t *val = trunnel_calloc(1, sizeof(socks5_client_userpass_auth_t));
  1836. if (NULL == val)
  1837. return NULL;
  1838. val->version = 1;
  1839. return val;
  1840. }
  1841. /** Release all storage held inside 'obj', but do not free 'obj'.
  1842. */
  1843. static void
  1844. socks5_client_userpass_auth_clear(socks5_client_userpass_auth_t *obj)
  1845. {
  1846. (void) obj;
  1847. TRUNNEL_DYNARRAY_WIPE(&obj->username);
  1848. TRUNNEL_DYNARRAY_CLEAR(&obj->username);
  1849. TRUNNEL_DYNARRAY_WIPE(&obj->passwd);
  1850. TRUNNEL_DYNARRAY_CLEAR(&obj->passwd);
  1851. }
  1852. void
  1853. socks5_client_userpass_auth_free(socks5_client_userpass_auth_t *obj)
  1854. {
  1855. if (obj == NULL)
  1856. return;
  1857. socks5_client_userpass_auth_clear(obj);
  1858. trunnel_memwipe(obj, sizeof(socks5_client_userpass_auth_t));
  1859. trunnel_free_(obj);
  1860. }
  1861. uint8_t
  1862. socks5_client_userpass_auth_get_version(const socks5_client_userpass_auth_t *inp)
  1863. {
  1864. return inp->version;
  1865. }
  1866. int
  1867. socks5_client_userpass_auth_set_version(socks5_client_userpass_auth_t *inp, uint8_t val)
  1868. {
  1869. if (! ((val == 1))) {
  1870. TRUNNEL_SET_ERROR_CODE(inp);
  1871. return -1;
  1872. }
  1873. inp->version = val;
  1874. return 0;
  1875. }
  1876. uint8_t
  1877. socks5_client_userpass_auth_get_username_len(const socks5_client_userpass_auth_t *inp)
  1878. {
  1879. return inp->username_len;
  1880. }
  1881. int
  1882. socks5_client_userpass_auth_set_username_len(socks5_client_userpass_auth_t *inp, uint8_t val)
  1883. {
  1884. inp->username_len = val;
  1885. return 0;
  1886. }
  1887. size_t
  1888. socks5_client_userpass_auth_getlen_username(const socks5_client_userpass_auth_t *inp)
  1889. {
  1890. return TRUNNEL_DYNARRAY_LEN(&inp->username);
  1891. }
  1892. char
  1893. socks5_client_userpass_auth_get_username(socks5_client_userpass_auth_t *inp, size_t idx)
  1894. {
  1895. return TRUNNEL_DYNARRAY_GET(&inp->username, idx);
  1896. }
  1897. char
  1898. socks5_client_userpass_auth_getconst_username(const socks5_client_userpass_auth_t *inp, size_t idx)
  1899. {
  1900. return socks5_client_userpass_auth_get_username((socks5_client_userpass_auth_t*)inp, idx);
  1901. }
  1902. int
  1903. socks5_client_userpass_auth_set_username(socks5_client_userpass_auth_t *inp, size_t idx, char elt)
  1904. {
  1905. TRUNNEL_DYNARRAY_SET(&inp->username, idx, elt);
  1906. return 0;
  1907. }
  1908. int
  1909. socks5_client_userpass_auth_add_username(socks5_client_userpass_auth_t *inp, char elt)
  1910. {
  1911. #if SIZE_MAX >= UINT8_MAX
  1912. if (inp->username.n_ == UINT8_MAX)
  1913. goto trunnel_alloc_failed;
  1914. #endif
  1915. TRUNNEL_DYNARRAY_ADD(char, &inp->username, elt, {});
  1916. return 0;
  1917. trunnel_alloc_failed:
  1918. TRUNNEL_SET_ERROR_CODE(inp);
  1919. return -1;
  1920. }
  1921. char *
  1922. socks5_client_userpass_auth_getarray_username(socks5_client_userpass_auth_t *inp)
  1923. {
  1924. return inp->username.elts_;
  1925. }
  1926. const char *
  1927. socks5_client_userpass_auth_getconstarray_username(const socks5_client_userpass_auth_t *inp)
  1928. {
  1929. return (const char *)socks5_client_userpass_auth_getarray_username((socks5_client_userpass_auth_t*)inp);
  1930. }
  1931. int
  1932. socks5_client_userpass_auth_setlen_username(socks5_client_userpass_auth_t *inp, size_t newlen)
  1933. {
  1934. #if UINT8_MAX < SIZE_MAX
  1935. if (newlen > UINT8_MAX)
  1936. goto trunnel_alloc_failed;
  1937. #endif
  1938. return trunnel_string_setlen(&inp->username, newlen,
  1939. &inp->trunnel_error_code_);
  1940. trunnel_alloc_failed:
  1941. TRUNNEL_SET_ERROR_CODE(inp);
  1942. return -1;
  1943. }
  1944. const char *
  1945. socks5_client_userpass_auth_getstr_username(socks5_client_userpass_auth_t *inp)
  1946. {
  1947. return trunnel_string_getstr(&inp->username);
  1948. }
  1949. int
  1950. socks5_client_userpass_auth_setstr0_username(socks5_client_userpass_auth_t *inp, const char *val, size_t len)
  1951. {
  1952. #if UINT8_MAX < SIZE_MAX
  1953. if (len > UINT8_MAX) {
  1954. TRUNNEL_SET_ERROR_CODE(inp);
  1955. return -1;
  1956. }
  1957. #endif
  1958. return trunnel_string_setstr0(&inp->username, val, len, &inp->trunnel_error_code_);
  1959. }
  1960. int
  1961. socks5_client_userpass_auth_setstr_username(socks5_client_userpass_auth_t *inp, const char *val)
  1962. {
  1963. return socks5_client_userpass_auth_setstr0_username(inp, val, strlen(val));
  1964. }
  1965. uint8_t
  1966. socks5_client_userpass_auth_get_passwd_len(const socks5_client_userpass_auth_t *inp)
  1967. {
  1968. return inp->passwd_len;
  1969. }
  1970. int
  1971. socks5_client_userpass_auth_set_passwd_len(socks5_client_userpass_auth_t *inp, uint8_t val)
  1972. {
  1973. inp->passwd_len = val;
  1974. return 0;
  1975. }
  1976. size_t
  1977. socks5_client_userpass_auth_getlen_passwd(const socks5_client_userpass_auth_t *inp)
  1978. {
  1979. return TRUNNEL_DYNARRAY_LEN(&inp->passwd);
  1980. }
  1981. char
  1982. socks5_client_userpass_auth_get_passwd(socks5_client_userpass_auth_t *inp, size_t idx)
  1983. {
  1984. return TRUNNEL_DYNARRAY_GET(&inp->passwd, idx);
  1985. }
  1986. char
  1987. socks5_client_userpass_auth_getconst_passwd(const socks5_client_userpass_auth_t *inp, size_t idx)
  1988. {
  1989. return socks5_client_userpass_auth_get_passwd((socks5_client_userpass_auth_t*)inp, idx);
  1990. }
  1991. int
  1992. socks5_client_userpass_auth_set_passwd(socks5_client_userpass_auth_t *inp, size_t idx, char elt)
  1993. {
  1994. TRUNNEL_DYNARRAY_SET(&inp->passwd, idx, elt);
  1995. return 0;
  1996. }
  1997. int
  1998. socks5_client_userpass_auth_add_passwd(socks5_client_userpass_auth_t *inp, char elt)
  1999. {
  2000. #if SIZE_MAX >= UINT8_MAX
  2001. if (inp->passwd.n_ == UINT8_MAX)
  2002. goto trunnel_alloc_failed;
  2003. #endif
  2004. TRUNNEL_DYNARRAY_ADD(char, &inp->passwd, elt, {});
  2005. return 0;
  2006. trunnel_alloc_failed:
  2007. TRUNNEL_SET_ERROR_CODE(inp);
  2008. return -1;
  2009. }
  2010. char *
  2011. socks5_client_userpass_auth_getarray_passwd(socks5_client_userpass_auth_t *inp)
  2012. {
  2013. return inp->passwd.elts_;
  2014. }
  2015. const char *
  2016. socks5_client_userpass_auth_getconstarray_passwd(const socks5_client_userpass_auth_t *inp)
  2017. {
  2018. return (const char *)socks5_client_userpass_auth_getarray_passwd((socks5_client_userpass_auth_t*)inp);
  2019. }
  2020. int
  2021. socks5_client_userpass_auth_setlen_passwd(socks5_client_userpass_auth_t *inp, size_t newlen)
  2022. {
  2023. #if UINT8_MAX < SIZE_MAX
  2024. if (newlen > UINT8_MAX)
  2025. goto trunnel_alloc_failed;
  2026. #endif
  2027. return trunnel_string_setlen(&inp->passwd, newlen,
  2028. &inp->trunnel_error_code_);
  2029. trunnel_alloc_failed:
  2030. TRUNNEL_SET_ERROR_CODE(inp);
  2031. return -1;
  2032. }
  2033. const char *
  2034. socks5_client_userpass_auth_getstr_passwd(socks5_client_userpass_auth_t *inp)
  2035. {
  2036. return trunnel_string_getstr(&inp->passwd);
  2037. }
  2038. int
  2039. socks5_client_userpass_auth_setstr0_passwd(socks5_client_userpass_auth_t *inp, const char *val, size_t len)
  2040. {
  2041. #if UINT8_MAX < SIZE_MAX
  2042. if (len > UINT8_MAX) {
  2043. TRUNNEL_SET_ERROR_CODE(inp);
  2044. return -1;
  2045. }
  2046. #endif
  2047. return trunnel_string_setstr0(&inp->passwd, val, len, &inp->trunnel_error_code_);
  2048. }
  2049. int
  2050. socks5_client_userpass_auth_setstr_passwd(socks5_client_userpass_auth_t *inp, const char *val)
  2051. {
  2052. return socks5_client_userpass_auth_setstr0_passwd(inp, val, strlen(val));
  2053. }
  2054. const char *
  2055. socks5_client_userpass_auth_check(const socks5_client_userpass_auth_t *obj)
  2056. {
  2057. if (obj == NULL)
  2058. return "Object was NULL";
  2059. if (obj->trunnel_error_code_)
  2060. return "A set function failed on this object";
  2061. if (! (obj->version == 1))
  2062. return "Integer out of bounds";
  2063. if (TRUNNEL_DYNARRAY_LEN(&obj->username) != obj->username_len)
  2064. return "Length mismatch for username";
  2065. if (TRUNNEL_DYNARRAY_LEN(&obj->passwd) != obj->passwd_len)
  2066. return "Length mismatch for passwd";
  2067. return NULL;
  2068. }
  2069. ssize_t
  2070. socks5_client_userpass_auth_encoded_len(const socks5_client_userpass_auth_t *obj)
  2071. {
  2072. ssize_t result = 0;
  2073. if (NULL != socks5_client_userpass_auth_check(obj))
  2074. return -1;
  2075. /* Length of u8 version IN [1] */
  2076. result += 1;
  2077. /* Length of u8 username_len */
  2078. result += 1;
  2079. /* Length of char username[username_len] */
  2080. result += TRUNNEL_DYNARRAY_LEN(&obj->username);
  2081. /* Length of u8 passwd_len */
  2082. result += 1;
  2083. /* Length of char passwd[passwd_len] */
  2084. result += TRUNNEL_DYNARRAY_LEN(&obj->passwd);
  2085. return result;
  2086. }
  2087. int
  2088. socks5_client_userpass_auth_clear_errors(socks5_client_userpass_auth_t *obj)
  2089. {
  2090. int r = obj->trunnel_error_code_;
  2091. obj->trunnel_error_code_ = 0;
  2092. return r;
  2093. }
  2094. ssize_t
  2095. socks5_client_userpass_auth_encode(uint8_t *output, const size_t avail, const socks5_client_userpass_auth_t *obj)
  2096. {
  2097. ssize_t result = 0;
  2098. size_t written = 0;
  2099. uint8_t *ptr = output;
  2100. const char *msg;
  2101. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2102. const ssize_t encoded_len = socks5_client_userpass_auth_encoded_len(obj);
  2103. #endif
  2104. if (NULL != (msg = socks5_client_userpass_auth_check(obj)))
  2105. goto check_failed;
  2106. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2107. trunnel_assert(encoded_len >= 0);
  2108. #endif
  2109. /* Encode u8 version IN [1] */
  2110. trunnel_assert(written <= avail);
  2111. if (avail - written < 1)
  2112. goto truncated;
  2113. trunnel_set_uint8(ptr, (obj->version));
  2114. written += 1; ptr += 1;
  2115. /* Encode u8 username_len */
  2116. trunnel_assert(written <= avail);
  2117. if (avail - written < 1)
  2118. goto truncated;
  2119. trunnel_set_uint8(ptr, (obj->username_len));
  2120. written += 1; ptr += 1;
  2121. /* Encode char username[username_len] */
  2122. {
  2123. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->username);
  2124. trunnel_assert(obj->username_len == elt_len);
  2125. trunnel_assert(written <= avail);
  2126. if (avail - written < elt_len)
  2127. goto truncated;
  2128. if (elt_len)
  2129. memcpy(ptr, obj->username.elts_, elt_len);
  2130. written += elt_len; ptr += elt_len;
  2131. }
  2132. /* Encode u8 passwd_len */
  2133. trunnel_assert(written <= avail);
  2134. if (avail - written < 1)
  2135. goto truncated;
  2136. trunnel_set_uint8(ptr, (obj->passwd_len));
  2137. written += 1; ptr += 1;
  2138. /* Encode char passwd[passwd_len] */
  2139. {
  2140. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->passwd);
  2141. trunnel_assert(obj->passwd_len == elt_len);
  2142. trunnel_assert(written <= avail);
  2143. if (avail - written < elt_len)
  2144. goto truncated;
  2145. if (elt_len)
  2146. memcpy(ptr, obj->passwd.elts_, elt_len);
  2147. written += elt_len; ptr += elt_len;
  2148. }
  2149. trunnel_assert(ptr == output + written);
  2150. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2151. {
  2152. trunnel_assert(encoded_len >= 0);
  2153. trunnel_assert((size_t)encoded_len == written);
  2154. }
  2155. #endif
  2156. return written;
  2157. truncated:
  2158. result = -2;
  2159. goto fail;
  2160. check_failed:
  2161. (void)msg;
  2162. result = -1;
  2163. goto fail;
  2164. fail:
  2165. trunnel_assert(result < 0);
  2166. return result;
  2167. }
  2168. /** As socks5_client_userpass_auth_parse(), but do not allocate the
  2169. * output object.
  2170. */
  2171. static ssize_t
  2172. socks5_client_userpass_auth_parse_into(socks5_client_userpass_auth_t *obj, const uint8_t *input, const size_t len_in)
  2173. {
  2174. const uint8_t *ptr = input;
  2175. size_t remaining = len_in;
  2176. ssize_t result = 0;
  2177. (void)result;
  2178. /* Parse u8 version IN [1] */
  2179. CHECK_REMAINING(1, truncated);
  2180. obj->version = (trunnel_get_uint8(ptr));
  2181. remaining -= 1; ptr += 1;
  2182. if (! (obj->version == 1))
  2183. goto fail;
  2184. /* Parse u8 username_len */
  2185. CHECK_REMAINING(1, truncated);
  2186. obj->username_len = (trunnel_get_uint8(ptr));
  2187. remaining -= 1; ptr += 1;
  2188. /* Parse char username[username_len] */
  2189. CHECK_REMAINING(obj->username_len, truncated);
  2190. if (socks5_client_userpass_auth_setstr0_username(obj, (const char*)ptr, obj->username_len))
  2191. goto fail;
  2192. ptr += obj->username_len; remaining -= obj->username_len;
  2193. /* Parse u8 passwd_len */
  2194. CHECK_REMAINING(1, truncated);
  2195. obj->passwd_len = (trunnel_get_uint8(ptr));
  2196. remaining -= 1; ptr += 1;
  2197. /* Parse char passwd[passwd_len] */
  2198. CHECK_REMAINING(obj->passwd_len, truncated);
  2199. if (socks5_client_userpass_auth_setstr0_passwd(obj, (const char*)ptr, obj->passwd_len))
  2200. goto fail;
  2201. ptr += obj->passwd_len; remaining -= obj->passwd_len;
  2202. trunnel_assert(ptr + remaining == input + len_in);
  2203. return len_in - remaining;
  2204. truncated:
  2205. return -2;
  2206. fail:
  2207. result = -1;
  2208. return result;
  2209. }
  2210. ssize_t
  2211. socks5_client_userpass_auth_parse(socks5_client_userpass_auth_t **output, const uint8_t *input, const size_t len_in)
  2212. {
  2213. ssize_t result;
  2214. *output = socks5_client_userpass_auth_new();
  2215. if (NULL == *output)
  2216. return -1;
  2217. result = socks5_client_userpass_auth_parse_into(*output, input, len_in);
  2218. if (result < 0) {
  2219. socks5_client_userpass_auth_free(*output);
  2220. *output = NULL;
  2221. }
  2222. return result;
  2223. }
  2224. socks5_client_version_t *
  2225. socks5_client_version_new(void)
  2226. {
  2227. socks5_client_version_t *val = trunnel_calloc(1, sizeof(socks5_client_version_t));
  2228. if (NULL == val)
  2229. return NULL;
  2230. val->version = 5;
  2231. return val;
  2232. }
  2233. /** Release all storage held inside 'obj', but do not free 'obj'.
  2234. */
  2235. static void
  2236. socks5_client_version_clear(socks5_client_version_t *obj)
  2237. {
  2238. (void) obj;
  2239. TRUNNEL_DYNARRAY_WIPE(&obj->methods);
  2240. TRUNNEL_DYNARRAY_CLEAR(&obj->methods);
  2241. }
  2242. void
  2243. socks5_client_version_free(socks5_client_version_t *obj)
  2244. {
  2245. if (obj == NULL)
  2246. return;
  2247. socks5_client_version_clear(obj);
  2248. trunnel_memwipe(obj, sizeof(socks5_client_version_t));
  2249. trunnel_free_(obj);
  2250. }
  2251. uint8_t
  2252. socks5_client_version_get_version(const socks5_client_version_t *inp)
  2253. {
  2254. return inp->version;
  2255. }
  2256. int
  2257. socks5_client_version_set_version(socks5_client_version_t *inp, uint8_t val)
  2258. {
  2259. if (! ((val == 5))) {
  2260. TRUNNEL_SET_ERROR_CODE(inp);
  2261. return -1;
  2262. }
  2263. inp->version = val;
  2264. return 0;
  2265. }
  2266. uint8_t
  2267. socks5_client_version_get_n_methods(const socks5_client_version_t *inp)
  2268. {
  2269. return inp->n_methods;
  2270. }
  2271. int
  2272. socks5_client_version_set_n_methods(socks5_client_version_t *inp, uint8_t val)
  2273. {
  2274. inp->n_methods = val;
  2275. return 0;
  2276. }
  2277. size_t
  2278. socks5_client_version_getlen_methods(const socks5_client_version_t *inp)
  2279. {
  2280. return TRUNNEL_DYNARRAY_LEN(&inp->methods);
  2281. }
  2282. uint8_t
  2283. socks5_client_version_get_methods(socks5_client_version_t *inp, size_t idx)
  2284. {
  2285. return TRUNNEL_DYNARRAY_GET(&inp->methods, idx);
  2286. }
  2287. uint8_t
  2288. socks5_client_version_getconst_methods(const socks5_client_version_t *inp, size_t idx)
  2289. {
  2290. return socks5_client_version_get_methods((socks5_client_version_t*)inp, idx);
  2291. }
  2292. int
  2293. socks5_client_version_set_methods(socks5_client_version_t *inp, size_t idx, uint8_t elt)
  2294. {
  2295. TRUNNEL_DYNARRAY_SET(&inp->methods, idx, elt);
  2296. return 0;
  2297. }
  2298. int
  2299. socks5_client_version_add_methods(socks5_client_version_t *inp, uint8_t elt)
  2300. {
  2301. #if SIZE_MAX >= UINT8_MAX
  2302. if (inp->methods.n_ == UINT8_MAX)
  2303. goto trunnel_alloc_failed;
  2304. #endif
  2305. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->methods, elt, {});
  2306. return 0;
  2307. trunnel_alloc_failed:
  2308. TRUNNEL_SET_ERROR_CODE(inp);
  2309. return -1;
  2310. }
  2311. uint8_t *
  2312. socks5_client_version_getarray_methods(socks5_client_version_t *inp)
  2313. {
  2314. return inp->methods.elts_;
  2315. }
  2316. const uint8_t *
  2317. socks5_client_version_getconstarray_methods(const socks5_client_version_t *inp)
  2318. {
  2319. return (const uint8_t *)socks5_client_version_getarray_methods((socks5_client_version_t*)inp);
  2320. }
  2321. int
  2322. socks5_client_version_setlen_methods(socks5_client_version_t *inp, size_t newlen)
  2323. {
  2324. uint8_t *newptr;
  2325. #if UINT8_MAX < SIZE_MAX
  2326. if (newlen > UINT8_MAX)
  2327. goto trunnel_alloc_failed;
  2328. #endif
  2329. newptr = trunnel_dynarray_setlen(&inp->methods.allocated_,
  2330. &inp->methods.n_, inp->methods.elts_, newlen,
  2331. sizeof(inp->methods.elts_[0]), (trunnel_free_fn_t) NULL,
  2332. &inp->trunnel_error_code_);
  2333. if (newlen != 0 && newptr == NULL)
  2334. goto trunnel_alloc_failed;
  2335. inp->methods.elts_ = newptr;
  2336. return 0;
  2337. trunnel_alloc_failed:
  2338. TRUNNEL_SET_ERROR_CODE(inp);
  2339. return -1;
  2340. }
  2341. const char *
  2342. socks5_client_version_check(const socks5_client_version_t *obj)
  2343. {
  2344. if (obj == NULL)
  2345. return "Object was NULL";
  2346. if (obj->trunnel_error_code_)
  2347. return "A set function failed on this object";
  2348. if (! (obj->version == 5))
  2349. return "Integer out of bounds";
  2350. if (TRUNNEL_DYNARRAY_LEN(&obj->methods) != obj->n_methods)
  2351. return "Length mismatch for methods";
  2352. return NULL;
  2353. }
  2354. ssize_t
  2355. socks5_client_version_encoded_len(const socks5_client_version_t *obj)
  2356. {
  2357. ssize_t result = 0;
  2358. if (NULL != socks5_client_version_check(obj))
  2359. return -1;
  2360. /* Length of u8 version IN [5] */
  2361. result += 1;
  2362. /* Length of u8 n_methods */
  2363. result += 1;
  2364. /* Length of u8 methods[n_methods] */
  2365. result += TRUNNEL_DYNARRAY_LEN(&obj->methods);
  2366. return result;
  2367. }
  2368. int
  2369. socks5_client_version_clear_errors(socks5_client_version_t *obj)
  2370. {
  2371. int r = obj->trunnel_error_code_;
  2372. obj->trunnel_error_code_ = 0;
  2373. return r;
  2374. }
  2375. ssize_t
  2376. socks5_client_version_encode(uint8_t *output, const size_t avail, const socks5_client_version_t *obj)
  2377. {
  2378. ssize_t result = 0;
  2379. size_t written = 0;
  2380. uint8_t *ptr = output;
  2381. const char *msg;
  2382. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2383. const ssize_t encoded_len = socks5_client_version_encoded_len(obj);
  2384. #endif
  2385. if (NULL != (msg = socks5_client_version_check(obj)))
  2386. goto check_failed;
  2387. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2388. trunnel_assert(encoded_len >= 0);
  2389. #endif
  2390. /* Encode u8 version IN [5] */
  2391. trunnel_assert(written <= avail);
  2392. if (avail - written < 1)
  2393. goto truncated;
  2394. trunnel_set_uint8(ptr, (obj->version));
  2395. written += 1; ptr += 1;
  2396. /* Encode u8 n_methods */
  2397. trunnel_assert(written <= avail);
  2398. if (avail - written < 1)
  2399. goto truncated;
  2400. trunnel_set_uint8(ptr, (obj->n_methods));
  2401. written += 1; ptr += 1;
  2402. /* Encode u8 methods[n_methods] */
  2403. {
  2404. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->methods);
  2405. trunnel_assert(obj->n_methods == elt_len);
  2406. trunnel_assert(written <= avail);
  2407. if (avail - written < elt_len)
  2408. goto truncated;
  2409. if (elt_len)
  2410. memcpy(ptr, obj->methods.elts_, elt_len);
  2411. written += elt_len; ptr += elt_len;
  2412. }
  2413. trunnel_assert(ptr == output + written);
  2414. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2415. {
  2416. trunnel_assert(encoded_len >= 0);
  2417. trunnel_assert((size_t)encoded_len == written);
  2418. }
  2419. #endif
  2420. return written;
  2421. truncated:
  2422. result = -2;
  2423. goto fail;
  2424. check_failed:
  2425. (void)msg;
  2426. result = -1;
  2427. goto fail;
  2428. fail:
  2429. trunnel_assert(result < 0);
  2430. return result;
  2431. }
  2432. /** As socks5_client_version_parse(), but do not allocate the output
  2433. * object.
  2434. */
  2435. static ssize_t
  2436. socks5_client_version_parse_into(socks5_client_version_t *obj, const uint8_t *input, const size_t len_in)
  2437. {
  2438. const uint8_t *ptr = input;
  2439. size_t remaining = len_in;
  2440. ssize_t result = 0;
  2441. (void)result;
  2442. /* Parse u8 version IN [5] */
  2443. CHECK_REMAINING(1, truncated);
  2444. obj->version = (trunnel_get_uint8(ptr));
  2445. remaining -= 1; ptr += 1;
  2446. if (! (obj->version == 5))
  2447. goto fail;
  2448. /* Parse u8 n_methods */
  2449. CHECK_REMAINING(1, truncated);
  2450. obj->n_methods = (trunnel_get_uint8(ptr));
  2451. remaining -= 1; ptr += 1;
  2452. /* Parse u8 methods[n_methods] */
  2453. CHECK_REMAINING(obj->n_methods, truncated);
  2454. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->methods, obj->n_methods, {});
  2455. obj->methods.n_ = obj->n_methods;
  2456. if (obj->n_methods)
  2457. memcpy(obj->methods.elts_, ptr, obj->n_methods);
  2458. ptr += obj->n_methods; remaining -= obj->n_methods;
  2459. trunnel_assert(ptr + remaining == input + len_in);
  2460. return len_in - remaining;
  2461. truncated:
  2462. return -2;
  2463. trunnel_alloc_failed:
  2464. return -1;
  2465. fail:
  2466. result = -1;
  2467. return result;
  2468. }
  2469. ssize_t
  2470. socks5_client_version_parse(socks5_client_version_t **output, const uint8_t *input, const size_t len_in)
  2471. {
  2472. ssize_t result;
  2473. *output = socks5_client_version_new();
  2474. if (NULL == *output)
  2475. return -1;
  2476. result = socks5_client_version_parse_into(*output, input, len_in);
  2477. if (result < 0) {
  2478. socks5_client_version_free(*output);
  2479. *output = NULL;
  2480. }
  2481. return result;
  2482. }
  2483. socks5_server_method_t *
  2484. socks5_server_method_new(void)
  2485. {
  2486. socks5_server_method_t *val = trunnel_calloc(1, sizeof(socks5_server_method_t));
  2487. if (NULL == val)
  2488. return NULL;
  2489. val->version = 5;
  2490. return val;
  2491. }
  2492. /** Release all storage held inside 'obj', but do not free 'obj'.
  2493. */
  2494. static void
  2495. socks5_server_method_clear(socks5_server_method_t *obj)
  2496. {
  2497. (void) obj;
  2498. }
  2499. void
  2500. socks5_server_method_free(socks5_server_method_t *obj)
  2501. {
  2502. if (obj == NULL)
  2503. return;
  2504. socks5_server_method_clear(obj);
  2505. trunnel_memwipe(obj, sizeof(socks5_server_method_t));
  2506. trunnel_free_(obj);
  2507. }
  2508. uint8_t
  2509. socks5_server_method_get_version(const socks5_server_method_t *inp)
  2510. {
  2511. return inp->version;
  2512. }
  2513. int
  2514. socks5_server_method_set_version(socks5_server_method_t *inp, uint8_t val)
  2515. {
  2516. if (! ((val == 5))) {
  2517. TRUNNEL_SET_ERROR_CODE(inp);
  2518. return -1;
  2519. }
  2520. inp->version = val;
  2521. return 0;
  2522. }
  2523. uint8_t
  2524. socks5_server_method_get_method(const socks5_server_method_t *inp)
  2525. {
  2526. return inp->method;
  2527. }
  2528. int
  2529. socks5_server_method_set_method(socks5_server_method_t *inp, uint8_t val)
  2530. {
  2531. inp->method = val;
  2532. return 0;
  2533. }
  2534. const char *
  2535. socks5_server_method_check(const socks5_server_method_t *obj)
  2536. {
  2537. if (obj == NULL)
  2538. return "Object was NULL";
  2539. if (obj->trunnel_error_code_)
  2540. return "A set function failed on this object";
  2541. if (! (obj->version == 5))
  2542. return "Integer out of bounds";
  2543. return NULL;
  2544. }
  2545. ssize_t
  2546. socks5_server_method_encoded_len(const socks5_server_method_t *obj)
  2547. {
  2548. ssize_t result = 0;
  2549. if (NULL != socks5_server_method_check(obj))
  2550. return -1;
  2551. /* Length of u8 version IN [5] */
  2552. result += 1;
  2553. /* Length of u8 method */
  2554. result += 1;
  2555. return result;
  2556. }
  2557. int
  2558. socks5_server_method_clear_errors(socks5_server_method_t *obj)
  2559. {
  2560. int r = obj->trunnel_error_code_;
  2561. obj->trunnel_error_code_ = 0;
  2562. return r;
  2563. }
  2564. ssize_t
  2565. socks5_server_method_encode(uint8_t *output, const size_t avail, const socks5_server_method_t *obj)
  2566. {
  2567. ssize_t result = 0;
  2568. size_t written = 0;
  2569. uint8_t *ptr = output;
  2570. const char *msg;
  2571. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2572. const ssize_t encoded_len = socks5_server_method_encoded_len(obj);
  2573. #endif
  2574. if (NULL != (msg = socks5_server_method_check(obj)))
  2575. goto check_failed;
  2576. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2577. trunnel_assert(encoded_len >= 0);
  2578. #endif
  2579. /* Encode u8 version IN [5] */
  2580. trunnel_assert(written <= avail);
  2581. if (avail - written < 1)
  2582. goto truncated;
  2583. trunnel_set_uint8(ptr, (obj->version));
  2584. written += 1; ptr += 1;
  2585. /* Encode u8 method */
  2586. trunnel_assert(written <= avail);
  2587. if (avail - written < 1)
  2588. goto truncated;
  2589. trunnel_set_uint8(ptr, (obj->method));
  2590. written += 1; ptr += 1;
  2591. trunnel_assert(ptr == output + written);
  2592. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2593. {
  2594. trunnel_assert(encoded_len >= 0);
  2595. trunnel_assert((size_t)encoded_len == written);
  2596. }
  2597. #endif
  2598. return written;
  2599. truncated:
  2600. result = -2;
  2601. goto fail;
  2602. check_failed:
  2603. (void)msg;
  2604. result = -1;
  2605. goto fail;
  2606. fail:
  2607. trunnel_assert(result < 0);
  2608. return result;
  2609. }
  2610. /** As socks5_server_method_parse(), but do not allocate the output
  2611. * object.
  2612. */
  2613. static ssize_t
  2614. socks5_server_method_parse_into(socks5_server_method_t *obj, const uint8_t *input, const size_t len_in)
  2615. {
  2616. const uint8_t *ptr = input;
  2617. size_t remaining = len_in;
  2618. ssize_t result = 0;
  2619. (void)result;
  2620. /* Parse u8 version IN [5] */
  2621. CHECK_REMAINING(1, truncated);
  2622. obj->version = (trunnel_get_uint8(ptr));
  2623. remaining -= 1; ptr += 1;
  2624. if (! (obj->version == 5))
  2625. goto fail;
  2626. /* Parse u8 method */
  2627. CHECK_REMAINING(1, truncated);
  2628. obj->method = (trunnel_get_uint8(ptr));
  2629. remaining -= 1; ptr += 1;
  2630. trunnel_assert(ptr + remaining == input + len_in);
  2631. return len_in - remaining;
  2632. truncated:
  2633. return -2;
  2634. fail:
  2635. result = -1;
  2636. return result;
  2637. }
  2638. ssize_t
  2639. socks5_server_method_parse(socks5_server_method_t **output, const uint8_t *input, const size_t len_in)
  2640. {
  2641. ssize_t result;
  2642. *output = socks5_server_method_new();
  2643. if (NULL == *output)
  2644. return -1;
  2645. result = socks5_server_method_parse_into(*output, input, len_in);
  2646. if (result < 0) {
  2647. socks5_server_method_free(*output);
  2648. *output = NULL;
  2649. }
  2650. return result;
  2651. }
  2652. socks5_server_userpass_auth_t *
  2653. socks5_server_userpass_auth_new(void)
  2654. {
  2655. socks5_server_userpass_auth_t *val = trunnel_calloc(1, sizeof(socks5_server_userpass_auth_t));
  2656. if (NULL == val)
  2657. return NULL;
  2658. val->version = 1;
  2659. return val;
  2660. }
  2661. /** Release all storage held inside 'obj', but do not free 'obj'.
  2662. */
  2663. static void
  2664. socks5_server_userpass_auth_clear(socks5_server_userpass_auth_t *obj)
  2665. {
  2666. (void) obj;
  2667. }
  2668. void
  2669. socks5_server_userpass_auth_free(socks5_server_userpass_auth_t *obj)
  2670. {
  2671. if (obj == NULL)
  2672. return;
  2673. socks5_server_userpass_auth_clear(obj);
  2674. trunnel_memwipe(obj, sizeof(socks5_server_userpass_auth_t));
  2675. trunnel_free_(obj);
  2676. }
  2677. uint8_t
  2678. socks5_server_userpass_auth_get_version(const socks5_server_userpass_auth_t *inp)
  2679. {
  2680. return inp->version;
  2681. }
  2682. int
  2683. socks5_server_userpass_auth_set_version(socks5_server_userpass_auth_t *inp, uint8_t val)
  2684. {
  2685. if (! ((val == 1))) {
  2686. TRUNNEL_SET_ERROR_CODE(inp);
  2687. return -1;
  2688. }
  2689. inp->version = val;
  2690. return 0;
  2691. }
  2692. uint8_t
  2693. socks5_server_userpass_auth_get_status(const socks5_server_userpass_auth_t *inp)
  2694. {
  2695. return inp->status;
  2696. }
  2697. int
  2698. socks5_server_userpass_auth_set_status(socks5_server_userpass_auth_t *inp, uint8_t val)
  2699. {
  2700. inp->status = val;
  2701. return 0;
  2702. }
  2703. const char *
  2704. socks5_server_userpass_auth_check(const socks5_server_userpass_auth_t *obj)
  2705. {
  2706. if (obj == NULL)
  2707. return "Object was NULL";
  2708. if (obj->trunnel_error_code_)
  2709. return "A set function failed on this object";
  2710. if (! (obj->version == 1))
  2711. return "Integer out of bounds";
  2712. return NULL;
  2713. }
  2714. ssize_t
  2715. socks5_server_userpass_auth_encoded_len(const socks5_server_userpass_auth_t *obj)
  2716. {
  2717. ssize_t result = 0;
  2718. if (NULL != socks5_server_userpass_auth_check(obj))
  2719. return -1;
  2720. /* Length of u8 version IN [1] */
  2721. result += 1;
  2722. /* Length of u8 status */
  2723. result += 1;
  2724. return result;
  2725. }
  2726. int
  2727. socks5_server_userpass_auth_clear_errors(socks5_server_userpass_auth_t *obj)
  2728. {
  2729. int r = obj->trunnel_error_code_;
  2730. obj->trunnel_error_code_ = 0;
  2731. return r;
  2732. }
  2733. ssize_t
  2734. socks5_server_userpass_auth_encode(uint8_t *output, const size_t avail, const socks5_server_userpass_auth_t *obj)
  2735. {
  2736. ssize_t result = 0;
  2737. size_t written = 0;
  2738. uint8_t *ptr = output;
  2739. const char *msg;
  2740. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2741. const ssize_t encoded_len = socks5_server_userpass_auth_encoded_len(obj);
  2742. #endif
  2743. if (NULL != (msg = socks5_server_userpass_auth_check(obj)))
  2744. goto check_failed;
  2745. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2746. trunnel_assert(encoded_len >= 0);
  2747. #endif
  2748. /* Encode u8 version IN [1] */
  2749. trunnel_assert(written <= avail);
  2750. if (avail - written < 1)
  2751. goto truncated;
  2752. trunnel_set_uint8(ptr, (obj->version));
  2753. written += 1; ptr += 1;
  2754. /* Encode u8 status */
  2755. trunnel_assert(written <= avail);
  2756. if (avail - written < 1)
  2757. goto truncated;
  2758. trunnel_set_uint8(ptr, (obj->status));
  2759. written += 1; ptr += 1;
  2760. trunnel_assert(ptr == output + written);
  2761. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2762. {
  2763. trunnel_assert(encoded_len >= 0);
  2764. trunnel_assert((size_t)encoded_len == written);
  2765. }
  2766. #endif
  2767. return written;
  2768. truncated:
  2769. result = -2;
  2770. goto fail;
  2771. check_failed:
  2772. (void)msg;
  2773. result = -1;
  2774. goto fail;
  2775. fail:
  2776. trunnel_assert(result < 0);
  2777. return result;
  2778. }
  2779. /** As socks5_server_userpass_auth_parse(), but do not allocate the
  2780. * output object.
  2781. */
  2782. static ssize_t
  2783. socks5_server_userpass_auth_parse_into(socks5_server_userpass_auth_t *obj, const uint8_t *input, const size_t len_in)
  2784. {
  2785. const uint8_t *ptr = input;
  2786. size_t remaining = len_in;
  2787. ssize_t result = 0;
  2788. (void)result;
  2789. /* Parse u8 version IN [1] */
  2790. CHECK_REMAINING(1, truncated);
  2791. obj->version = (trunnel_get_uint8(ptr));
  2792. remaining -= 1; ptr += 1;
  2793. if (! (obj->version == 1))
  2794. goto fail;
  2795. /* Parse u8 status */
  2796. CHECK_REMAINING(1, truncated);
  2797. obj->status = (trunnel_get_uint8(ptr));
  2798. remaining -= 1; ptr += 1;
  2799. trunnel_assert(ptr + remaining == input + len_in);
  2800. return len_in - remaining;
  2801. truncated:
  2802. return -2;
  2803. fail:
  2804. result = -1;
  2805. return result;
  2806. }
  2807. ssize_t
  2808. socks5_server_userpass_auth_parse(socks5_server_userpass_auth_t **output, const uint8_t *input, const size_t len_in)
  2809. {
  2810. ssize_t result;
  2811. *output = socks5_server_userpass_auth_new();
  2812. if (NULL == *output)
  2813. return -1;
  2814. result = socks5_server_userpass_auth_parse_into(*output, input, len_in);
  2815. if (result < 0) {
  2816. socks5_server_userpass_auth_free(*output);
  2817. *output = NULL;
  2818. }
  2819. return result;
  2820. }
  2821. socks5_client_request_t *
  2822. socks5_client_request_new(void)
  2823. {
  2824. socks5_client_request_t *val = trunnel_calloc(1, sizeof(socks5_client_request_t));
  2825. if (NULL == val)
  2826. return NULL;
  2827. val->version = 5;
  2828. val->command = CMD_BIND;
  2829. return val;
  2830. }
  2831. /** Release all storage held inside 'obj', but do not free 'obj'.
  2832. */
  2833. static void
  2834. socks5_client_request_clear(socks5_client_request_t *obj)
  2835. {
  2836. (void) obj;
  2837. domainname_free(obj->dest_addr_domainname);
  2838. obj->dest_addr_domainname = NULL;
  2839. }
  2840. void
  2841. socks5_client_request_free(socks5_client_request_t *obj)
  2842. {
  2843. if (obj == NULL)
  2844. return;
  2845. socks5_client_request_clear(obj);
  2846. trunnel_memwipe(obj, sizeof(socks5_client_request_t));
  2847. trunnel_free_(obj);
  2848. }
  2849. uint8_t
  2850. socks5_client_request_get_version(const socks5_client_request_t *inp)
  2851. {
  2852. return inp->version;
  2853. }
  2854. int
  2855. socks5_client_request_set_version(socks5_client_request_t *inp, uint8_t val)
  2856. {
  2857. if (! ((val == 5))) {
  2858. TRUNNEL_SET_ERROR_CODE(inp);
  2859. return -1;
  2860. }
  2861. inp->version = val;
  2862. return 0;
  2863. }
  2864. uint8_t
  2865. socks5_client_request_get_command(const socks5_client_request_t *inp)
  2866. {
  2867. return inp->command;
  2868. }
  2869. int
  2870. socks5_client_request_set_command(socks5_client_request_t *inp, uint8_t val)
  2871. {
  2872. if (! ((val == CMD_BIND || val == CMD_CONNECT || val == CMD_RESOLVE || val == CMD_RESOLVE_PTR || val == CMD_UDP_ASSOCIATE))) {
  2873. TRUNNEL_SET_ERROR_CODE(inp);
  2874. return -1;
  2875. }
  2876. inp->command = val;
  2877. return 0;
  2878. }
  2879. uint8_t
  2880. socks5_client_request_get_reserved(const socks5_client_request_t *inp)
  2881. {
  2882. return inp->reserved;
  2883. }
  2884. int
  2885. socks5_client_request_set_reserved(socks5_client_request_t *inp, uint8_t val)
  2886. {
  2887. if (! ((val == 0))) {
  2888. TRUNNEL_SET_ERROR_CODE(inp);
  2889. return -1;
  2890. }
  2891. inp->reserved = val;
  2892. return 0;
  2893. }
  2894. uint8_t
  2895. socks5_client_request_get_atype(const socks5_client_request_t *inp)
  2896. {
  2897. return inp->atype;
  2898. }
  2899. int
  2900. socks5_client_request_set_atype(socks5_client_request_t *inp, uint8_t val)
  2901. {
  2902. inp->atype = val;
  2903. return 0;
  2904. }
  2905. uint32_t
  2906. socks5_client_request_get_dest_addr_ipv4(const socks5_client_request_t *inp)
  2907. {
  2908. return inp->dest_addr_ipv4;
  2909. }
  2910. int
  2911. socks5_client_request_set_dest_addr_ipv4(socks5_client_request_t *inp, uint32_t val)
  2912. {
  2913. inp->dest_addr_ipv4 = val;
  2914. return 0;
  2915. }
  2916. size_t
  2917. socks5_client_request_getlen_dest_addr_ipv6(const socks5_client_request_t *inp)
  2918. {
  2919. (void)inp; return 16;
  2920. }
  2921. uint8_t
  2922. socks5_client_request_get_dest_addr_ipv6(socks5_client_request_t *inp, size_t idx)
  2923. {
  2924. trunnel_assert(idx < 16);
  2925. return inp->dest_addr_ipv6[idx];
  2926. }
  2927. uint8_t
  2928. socks5_client_request_getconst_dest_addr_ipv6(const socks5_client_request_t *inp, size_t idx)
  2929. {
  2930. return socks5_client_request_get_dest_addr_ipv6((socks5_client_request_t*)inp, idx);
  2931. }
  2932. int
  2933. socks5_client_request_set_dest_addr_ipv6(socks5_client_request_t *inp, size_t idx, uint8_t elt)
  2934. {
  2935. trunnel_assert(idx < 16);
  2936. inp->dest_addr_ipv6[idx] = elt;
  2937. return 0;
  2938. }
  2939. uint8_t *
  2940. socks5_client_request_getarray_dest_addr_ipv6(socks5_client_request_t *inp)
  2941. {
  2942. return inp->dest_addr_ipv6;
  2943. }
  2944. const uint8_t *
  2945. socks5_client_request_getconstarray_dest_addr_ipv6(const socks5_client_request_t *inp)
  2946. {
  2947. return (const uint8_t *)socks5_client_request_getarray_dest_addr_ipv6((socks5_client_request_t*)inp);
  2948. }
  2949. struct domainname_st *
  2950. socks5_client_request_get_dest_addr_domainname(socks5_client_request_t *inp)
  2951. {
  2952. return inp->dest_addr_domainname;
  2953. }
  2954. const struct domainname_st *
  2955. socks5_client_request_getconst_dest_addr_domainname(const socks5_client_request_t *inp)
  2956. {
  2957. return socks5_client_request_get_dest_addr_domainname((socks5_client_request_t*) inp);
  2958. }
  2959. int
  2960. socks5_client_request_set_dest_addr_domainname(socks5_client_request_t *inp, struct domainname_st *val)
  2961. {
  2962. if (inp->dest_addr_domainname && inp->dest_addr_domainname != val)
  2963. domainname_free(inp->dest_addr_domainname);
  2964. return socks5_client_request_set0_dest_addr_domainname(inp, val);
  2965. }
  2966. int
  2967. socks5_client_request_set0_dest_addr_domainname(socks5_client_request_t *inp, struct domainname_st *val)
  2968. {
  2969. inp->dest_addr_domainname = val;
  2970. return 0;
  2971. }
  2972. uint16_t
  2973. socks5_client_request_get_dest_port(const socks5_client_request_t *inp)
  2974. {
  2975. return inp->dest_port;
  2976. }
  2977. int
  2978. socks5_client_request_set_dest_port(socks5_client_request_t *inp, uint16_t val)
  2979. {
  2980. inp->dest_port = val;
  2981. return 0;
  2982. }
  2983. const char *
  2984. socks5_client_request_check(const socks5_client_request_t *obj)
  2985. {
  2986. if (obj == NULL)
  2987. return "Object was NULL";
  2988. if (obj->trunnel_error_code_)
  2989. return "A set function failed on this object";
  2990. if (! (obj->version == 5))
  2991. return "Integer out of bounds";
  2992. if (! (obj->command == CMD_BIND || obj->command == CMD_CONNECT || obj->command == CMD_RESOLVE || obj->command == CMD_RESOLVE_PTR || obj->command == CMD_UDP_ASSOCIATE))
  2993. return "Integer out of bounds";
  2994. if (! (obj->reserved == 0))
  2995. return "Integer out of bounds";
  2996. switch (obj->atype) {
  2997. case ATYPE_IPV4:
  2998. break;
  2999. case ATYPE_IPV6:
  3000. break;
  3001. case ATYPE_DOMAINNAME:
  3002. {
  3003. const char *msg;
  3004. if (NULL != (msg = domainname_check(obj->dest_addr_domainname)))
  3005. return msg;
  3006. }
  3007. break;
  3008. default:
  3009. return "Bad tag for union";
  3010. break;
  3011. }
  3012. return NULL;
  3013. }
  3014. ssize_t
  3015. socks5_client_request_encoded_len(const socks5_client_request_t *obj)
  3016. {
  3017. ssize_t result = 0;
  3018. if (NULL != socks5_client_request_check(obj))
  3019. return -1;
  3020. /* Length of u8 version IN [5] */
  3021. result += 1;
  3022. /* Length of u8 command IN [CMD_BIND, CMD_CONNECT, CMD_RESOLVE, CMD_RESOLVE_PTR, CMD_UDP_ASSOCIATE] */
  3023. result += 1;
  3024. /* Length of u8 reserved IN [0] */
  3025. result += 1;
  3026. /* Length of u8 atype */
  3027. result += 1;
  3028. switch (obj->atype) {
  3029. case ATYPE_IPV4:
  3030. /* Length of u32 dest_addr_ipv4 */
  3031. result += 4;
  3032. break;
  3033. case ATYPE_IPV6:
  3034. /* Length of u8 dest_addr_ipv6[16] */
  3035. result += 16;
  3036. break;
  3037. case ATYPE_DOMAINNAME:
  3038. /* Length of struct domainname dest_addr_domainname */
  3039. result += domainname_encoded_len(obj->dest_addr_domainname);
  3040. break;
  3041. default:
  3042. trunnel_assert(0);
  3043. break;
  3044. }
  3045. /* Length of u16 dest_port */
  3046. result += 2;
  3047. return result;
  3048. }
  3049. int
  3050. socks5_client_request_clear_errors(socks5_client_request_t *obj)
  3051. {
  3052. int r = obj->trunnel_error_code_;
  3053. obj->trunnel_error_code_ = 0;
  3054. return r;
  3055. }
  3056. ssize_t
  3057. socks5_client_request_encode(uint8_t *output, const size_t avail, const socks5_client_request_t *obj)
  3058. {
  3059. ssize_t result = 0;
  3060. size_t written = 0;
  3061. uint8_t *ptr = output;
  3062. const char *msg;
  3063. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  3064. const ssize_t encoded_len = socks5_client_request_encoded_len(obj);
  3065. #endif
  3066. if (NULL != (msg = socks5_client_request_check(obj)))
  3067. goto check_failed;
  3068. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  3069. trunnel_assert(encoded_len >= 0);
  3070. #endif
  3071. /* Encode u8 version IN [5] */
  3072. trunnel_assert(written <= avail);
  3073. if (avail - written < 1)
  3074. goto truncated;
  3075. trunnel_set_uint8(ptr, (obj->version));
  3076. written += 1; ptr += 1;
  3077. /* Encode u8 command IN [CMD_BIND, CMD_CONNECT, CMD_RESOLVE, CMD_RESOLVE_PTR, CMD_UDP_ASSOCIATE] */
  3078. trunnel_assert(written <= avail);
  3079. if (avail - written < 1)
  3080. goto truncated;
  3081. trunnel_set_uint8(ptr, (obj->command));
  3082. written += 1; ptr += 1;
  3083. /* Encode u8 reserved IN [0] */
  3084. trunnel_assert(written <= avail);
  3085. if (avail - written < 1)
  3086. goto truncated;
  3087. trunnel_set_uint8(ptr, (obj->reserved));
  3088. written += 1; ptr += 1;
  3089. /* Encode u8 atype */
  3090. trunnel_assert(written <= avail);
  3091. if (avail - written < 1)
  3092. goto truncated;
  3093. trunnel_set_uint8(ptr, (obj->atype));
  3094. written += 1; ptr += 1;
  3095. /* Encode union dest_addr[atype] */
  3096. trunnel_assert(written <= avail);
  3097. switch (obj->atype) {
  3098. case ATYPE_IPV4:
  3099. /* Encode u32 dest_addr_ipv4 */
  3100. trunnel_assert(written <= avail);
  3101. if (avail - written < 4)
  3102. goto truncated;
  3103. trunnel_set_uint32(ptr, trunnel_htonl(obj->dest_addr_ipv4));
  3104. written += 4; ptr += 4;
  3105. break;
  3106. case ATYPE_IPV6:
  3107. /* Encode u8 dest_addr_ipv6[16] */
  3108. trunnel_assert(written <= avail);
  3109. if (avail - written < 16)
  3110. goto truncated;
  3111. memcpy(ptr, obj->dest_addr_ipv6, 16);
  3112. written += 16; ptr += 16;
  3113. break;
  3114. case ATYPE_DOMAINNAME:
  3115. /* Encode struct domainname dest_addr_domainname */
  3116. trunnel_assert(written <= avail);
  3117. result = domainname_encode(ptr, avail - written, obj->dest_addr_domainname);
  3118. if (result < 0)
  3119. goto fail; /* XXXXXXX !*/
  3120. written += result; ptr += result;
  3121. break;
  3122. default:
  3123. trunnel_assert(0);
  3124. break;
  3125. }
  3126. /* Encode u16 dest_port */
  3127. trunnel_assert(written <= avail);
  3128. if (avail - written < 2)
  3129. goto truncated;
  3130. trunnel_set_uint16(ptr, trunnel_htons(obj->dest_port));
  3131. written += 2; ptr += 2;
  3132. trunnel_assert(ptr == output + written);
  3133. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  3134. {
  3135. trunnel_assert(encoded_len >= 0);
  3136. trunnel_assert((size_t)encoded_len == written);
  3137. }
  3138. #endif
  3139. return written;
  3140. truncated:
  3141. result = -2;
  3142. goto fail;
  3143. check_failed:
  3144. (void)msg;
  3145. result = -1;
  3146. goto fail;
  3147. fail:
  3148. trunnel_assert(result < 0);
  3149. return result;
  3150. }
  3151. /** As socks5_client_request_parse(), but do not allocate the output
  3152. * object.
  3153. */
  3154. static ssize_t
  3155. socks5_client_request_parse_into(socks5_client_request_t *obj, const uint8_t *input, const size_t len_in)
  3156. {
  3157. const uint8_t *ptr = input;
  3158. size_t remaining = len_in;
  3159. ssize_t result = 0;
  3160. (void)result;
  3161. /* Parse u8 version IN [5] */
  3162. CHECK_REMAINING(1, truncated);
  3163. obj->version = (trunnel_get_uint8(ptr));
  3164. remaining -= 1; ptr += 1;
  3165. if (! (obj->version == 5))
  3166. goto fail;
  3167. /* Parse u8 command IN [CMD_BIND, CMD_CONNECT, CMD_RESOLVE, CMD_RESOLVE_PTR, CMD_UDP_ASSOCIATE] */
  3168. CHECK_REMAINING(1, truncated);
  3169. obj->command = (trunnel_get_uint8(ptr));
  3170. remaining -= 1; ptr += 1;
  3171. if (! (obj->command == CMD_BIND || obj->command == CMD_CONNECT || obj->command == CMD_RESOLVE || obj->command == CMD_RESOLVE_PTR || obj->command == CMD_UDP_ASSOCIATE))
  3172. goto fail;
  3173. /* Parse u8 reserved IN [0] */
  3174. CHECK_REMAINING(1, truncated);
  3175. obj->reserved = (trunnel_get_uint8(ptr));
  3176. remaining -= 1; ptr += 1;
  3177. if (! (obj->reserved == 0))
  3178. goto fail;
  3179. /* Parse u8 atype */
  3180. CHECK_REMAINING(1, truncated);
  3181. obj->atype = (trunnel_get_uint8(ptr));
  3182. remaining -= 1; ptr += 1;
  3183. /* Parse union dest_addr[atype] */
  3184. switch (obj->atype) {
  3185. case ATYPE_IPV4:
  3186. /* Parse u32 dest_addr_ipv4 */
  3187. CHECK_REMAINING(4, truncated);
  3188. obj->dest_addr_ipv4 = trunnel_ntohl(trunnel_get_uint32(ptr));
  3189. remaining -= 4; ptr += 4;
  3190. break;
  3191. case ATYPE_IPV6:
  3192. /* Parse u8 dest_addr_ipv6[16] */
  3193. CHECK_REMAINING(16, truncated);
  3194. memcpy(obj->dest_addr_ipv6, ptr, 16);
  3195. remaining -= 16; ptr += 16;
  3196. break;
  3197. case ATYPE_DOMAINNAME:
  3198. /* Parse struct domainname dest_addr_domainname */
  3199. result = domainname_parse(&obj->dest_addr_domainname, ptr, remaining);
  3200. if (result < 0)
  3201. goto relay_fail;
  3202. trunnel_assert((size_t)result <= remaining);
  3203. remaining -= result; ptr += result;
  3204. break;
  3205. default:
  3206. goto fail;
  3207. break;
  3208. }
  3209. /* Parse u16 dest_port */
  3210. CHECK_REMAINING(2, truncated);
  3211. obj->dest_port = trunnel_ntohs(trunnel_get_uint16(ptr));
  3212. remaining -= 2; ptr += 2;
  3213. trunnel_assert(ptr + remaining == input + len_in);
  3214. return len_in - remaining;
  3215. truncated:
  3216. return -2;
  3217. relay_fail:
  3218. trunnel_assert(result < 0);
  3219. return result;
  3220. fail:
  3221. result = -1;
  3222. return result;
  3223. }
  3224. ssize_t
  3225. socks5_client_request_parse(socks5_client_request_t **output, const uint8_t *input, const size_t len_in)
  3226. {
  3227. ssize_t result;
  3228. *output = socks5_client_request_new();
  3229. if (NULL == *output)
  3230. return -1;
  3231. result = socks5_client_request_parse_into(*output, input, len_in);
  3232. if (result < 0) {
  3233. socks5_client_request_free(*output);
  3234. *output = NULL;
  3235. }
  3236. return result;
  3237. }
  3238. socks5_server_reply_t *
  3239. socks5_server_reply_new(void)
  3240. {
  3241. socks5_server_reply_t *val = trunnel_calloc(1, sizeof(socks5_server_reply_t));
  3242. if (NULL == val)
  3243. return NULL;
  3244. val->version = 5;
  3245. return val;
  3246. }
  3247. /** Release all storage held inside 'obj', but do not free 'obj'.
  3248. */
  3249. static void
  3250. socks5_server_reply_clear(socks5_server_reply_t *obj)
  3251. {
  3252. (void) obj;
  3253. domainname_free(obj->bind_addr_domainname);
  3254. obj->bind_addr_domainname = NULL;
  3255. }
  3256. void
  3257. socks5_server_reply_free(socks5_server_reply_t *obj)
  3258. {
  3259. if (obj == NULL)
  3260. return;
  3261. socks5_server_reply_clear(obj);
  3262. trunnel_memwipe(obj, sizeof(socks5_server_reply_t));
  3263. trunnel_free_(obj);
  3264. }
  3265. uint8_t
  3266. socks5_server_reply_get_version(const socks5_server_reply_t *inp)
  3267. {
  3268. return inp->version;
  3269. }
  3270. int
  3271. socks5_server_reply_set_version(socks5_server_reply_t *inp, uint8_t val)
  3272. {
  3273. if (! ((val == 5))) {
  3274. TRUNNEL_SET_ERROR_CODE(inp);
  3275. return -1;
  3276. }
  3277. inp->version = val;
  3278. return 0;
  3279. }
  3280. uint8_t
  3281. socks5_server_reply_get_reply(const socks5_server_reply_t *inp)
  3282. {
  3283. return inp->reply;
  3284. }
  3285. int
  3286. socks5_server_reply_set_reply(socks5_server_reply_t *inp, uint8_t val)
  3287. {
  3288. inp->reply = val;
  3289. return 0;
  3290. }
  3291. uint8_t
  3292. socks5_server_reply_get_reserved(const socks5_server_reply_t *inp)
  3293. {
  3294. return inp->reserved;
  3295. }
  3296. int
  3297. socks5_server_reply_set_reserved(socks5_server_reply_t *inp, uint8_t val)
  3298. {
  3299. if (! ((val == 0))) {
  3300. TRUNNEL_SET_ERROR_CODE(inp);
  3301. return -1;
  3302. }
  3303. inp->reserved = val;
  3304. return 0;
  3305. }
  3306. uint8_t
  3307. socks5_server_reply_get_atype(const socks5_server_reply_t *inp)
  3308. {
  3309. return inp->atype;
  3310. }
  3311. int
  3312. socks5_server_reply_set_atype(socks5_server_reply_t *inp, uint8_t val)
  3313. {
  3314. inp->atype = val;
  3315. return 0;
  3316. }
  3317. uint32_t
  3318. socks5_server_reply_get_bind_addr_ipv4(const socks5_server_reply_t *inp)
  3319. {
  3320. return inp->bind_addr_ipv4;
  3321. }
  3322. int
  3323. socks5_server_reply_set_bind_addr_ipv4(socks5_server_reply_t *inp, uint32_t val)
  3324. {
  3325. inp->bind_addr_ipv4 = val;
  3326. return 0;
  3327. }
  3328. size_t
  3329. socks5_server_reply_getlen_bind_addr_ipv6(const socks5_server_reply_t *inp)
  3330. {
  3331. (void)inp; return 16;
  3332. }
  3333. uint8_t
  3334. socks5_server_reply_get_bind_addr_ipv6(socks5_server_reply_t *inp, size_t idx)
  3335. {
  3336. trunnel_assert(idx < 16);
  3337. return inp->bind_addr_ipv6[idx];
  3338. }
  3339. uint8_t
  3340. socks5_server_reply_getconst_bind_addr_ipv6(const socks5_server_reply_t *inp, size_t idx)
  3341. {
  3342. return socks5_server_reply_get_bind_addr_ipv6((socks5_server_reply_t*)inp, idx);
  3343. }
  3344. int
  3345. socks5_server_reply_set_bind_addr_ipv6(socks5_server_reply_t *inp, size_t idx, uint8_t elt)
  3346. {
  3347. trunnel_assert(idx < 16);
  3348. inp->bind_addr_ipv6[idx] = elt;
  3349. return 0;
  3350. }
  3351. uint8_t *
  3352. socks5_server_reply_getarray_bind_addr_ipv6(socks5_server_reply_t *inp)
  3353. {
  3354. return inp->bind_addr_ipv6;
  3355. }
  3356. const uint8_t *
  3357. socks5_server_reply_getconstarray_bind_addr_ipv6(const socks5_server_reply_t *inp)
  3358. {
  3359. return (const uint8_t *)socks5_server_reply_getarray_bind_addr_ipv6((socks5_server_reply_t*)inp);
  3360. }
  3361. struct domainname_st *
  3362. socks5_server_reply_get_bind_addr_domainname(socks5_server_reply_t *inp)
  3363. {
  3364. return inp->bind_addr_domainname;
  3365. }
  3366. const struct domainname_st *
  3367. socks5_server_reply_getconst_bind_addr_domainname(const socks5_server_reply_t *inp)
  3368. {
  3369. return socks5_server_reply_get_bind_addr_domainname((socks5_server_reply_t*) inp);
  3370. }
  3371. int
  3372. socks5_server_reply_set_bind_addr_domainname(socks5_server_reply_t *inp, struct domainname_st *val)
  3373. {
  3374. if (inp->bind_addr_domainname && inp->bind_addr_domainname != val)
  3375. domainname_free(inp->bind_addr_domainname);
  3376. return socks5_server_reply_set0_bind_addr_domainname(inp, val);
  3377. }
  3378. int
  3379. socks5_server_reply_set0_bind_addr_domainname(socks5_server_reply_t *inp, struct domainname_st *val)
  3380. {
  3381. inp->bind_addr_domainname = val;
  3382. return 0;
  3383. }
  3384. uint16_t
  3385. socks5_server_reply_get_bind_port(const socks5_server_reply_t *inp)
  3386. {
  3387. return inp->bind_port;
  3388. }
  3389. int
  3390. socks5_server_reply_set_bind_port(socks5_server_reply_t *inp, uint16_t val)
  3391. {
  3392. inp->bind_port = val;
  3393. return 0;
  3394. }
  3395. const char *
  3396. socks5_server_reply_check(const socks5_server_reply_t *obj)
  3397. {
  3398. if (obj == NULL)
  3399. return "Object was NULL";
  3400. if (obj->trunnel_error_code_)
  3401. return "A set function failed on this object";
  3402. if (! (obj->version == 5))
  3403. return "Integer out of bounds";
  3404. if (! (obj->reserved == 0))
  3405. return "Integer out of bounds";
  3406. switch (obj->atype) {
  3407. case ATYPE_IPV4:
  3408. break;
  3409. case ATYPE_IPV6:
  3410. break;
  3411. case ATYPE_DOMAINNAME:
  3412. {
  3413. const char *msg;
  3414. if (NULL != (msg = domainname_check(obj->bind_addr_domainname)))
  3415. return msg;
  3416. }
  3417. break;
  3418. default:
  3419. return "Bad tag for union";
  3420. break;
  3421. }
  3422. return NULL;
  3423. }
  3424. ssize_t
  3425. socks5_server_reply_encoded_len(const socks5_server_reply_t *obj)
  3426. {
  3427. ssize_t result = 0;
  3428. if (NULL != socks5_server_reply_check(obj))
  3429. return -1;
  3430. /* Length of u8 version IN [5] */
  3431. result += 1;
  3432. /* Length of u8 reply */
  3433. result += 1;
  3434. /* Length of u8 reserved IN [0] */
  3435. result += 1;
  3436. /* Length of u8 atype */
  3437. result += 1;
  3438. switch (obj->atype) {
  3439. case ATYPE_IPV4:
  3440. /* Length of u32 bind_addr_ipv4 */
  3441. result += 4;
  3442. break;
  3443. case ATYPE_IPV6:
  3444. /* Length of u8 bind_addr_ipv6[16] */
  3445. result += 16;
  3446. break;
  3447. case ATYPE_DOMAINNAME:
  3448. /* Length of struct domainname bind_addr_domainname */
  3449. result += domainname_encoded_len(obj->bind_addr_domainname);
  3450. break;
  3451. default:
  3452. trunnel_assert(0);
  3453. break;
  3454. }
  3455. /* Length of u16 bind_port */
  3456. result += 2;
  3457. return result;
  3458. }
  3459. int
  3460. socks5_server_reply_clear_errors(socks5_server_reply_t *obj)
  3461. {
  3462. int r = obj->trunnel_error_code_;
  3463. obj->trunnel_error_code_ = 0;
  3464. return r;
  3465. }
  3466. ssize_t
  3467. socks5_server_reply_encode(uint8_t *output, const size_t avail, const socks5_server_reply_t *obj)
  3468. {
  3469. ssize_t result = 0;
  3470. size_t written = 0;
  3471. uint8_t *ptr = output;
  3472. const char *msg;
  3473. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  3474. const ssize_t encoded_len = socks5_server_reply_encoded_len(obj);
  3475. #endif
  3476. if (NULL != (msg = socks5_server_reply_check(obj)))
  3477. goto check_failed;
  3478. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  3479. trunnel_assert(encoded_len >= 0);
  3480. #endif
  3481. /* Encode u8 version IN [5] */
  3482. trunnel_assert(written <= avail);
  3483. if (avail - written < 1)
  3484. goto truncated;
  3485. trunnel_set_uint8(ptr, (obj->version));
  3486. written += 1; ptr += 1;
  3487. /* Encode u8 reply */
  3488. trunnel_assert(written <= avail);
  3489. if (avail - written < 1)
  3490. goto truncated;
  3491. trunnel_set_uint8(ptr, (obj->reply));
  3492. written += 1; ptr += 1;
  3493. /* Encode u8 reserved IN [0] */
  3494. trunnel_assert(written <= avail);
  3495. if (avail - written < 1)
  3496. goto truncated;
  3497. trunnel_set_uint8(ptr, (obj->reserved));
  3498. written += 1; ptr += 1;
  3499. /* Encode u8 atype */
  3500. trunnel_assert(written <= avail);
  3501. if (avail - written < 1)
  3502. goto truncated;
  3503. trunnel_set_uint8(ptr, (obj->atype));
  3504. written += 1; ptr += 1;
  3505. /* Encode union bind_addr[atype] */
  3506. trunnel_assert(written <= avail);
  3507. switch (obj->atype) {
  3508. case ATYPE_IPV4:
  3509. /* Encode u32 bind_addr_ipv4 */
  3510. trunnel_assert(written <= avail);
  3511. if (avail - written < 4)
  3512. goto truncated;
  3513. trunnel_set_uint32(ptr, trunnel_htonl(obj->bind_addr_ipv4));
  3514. written += 4; ptr += 4;
  3515. break;
  3516. case ATYPE_IPV6:
  3517. /* Encode u8 bind_addr_ipv6[16] */
  3518. trunnel_assert(written <= avail);
  3519. if (avail - written < 16)
  3520. goto truncated;
  3521. memcpy(ptr, obj->bind_addr_ipv6, 16);
  3522. written += 16; ptr += 16;
  3523. break;
  3524. case ATYPE_DOMAINNAME:
  3525. /* Encode struct domainname bind_addr_domainname */
  3526. trunnel_assert(written <= avail);
  3527. result = domainname_encode(ptr, avail - written, obj->bind_addr_domainname);
  3528. if (result < 0)
  3529. goto fail; /* XXXXXXX !*/
  3530. written += result; ptr += result;
  3531. break;
  3532. default:
  3533. trunnel_assert(0);
  3534. break;
  3535. }
  3536. /* Encode u16 bind_port */
  3537. trunnel_assert(written <= avail);
  3538. if (avail - written < 2)
  3539. goto truncated;
  3540. trunnel_set_uint16(ptr, trunnel_htons(obj->bind_port));
  3541. written += 2; ptr += 2;
  3542. trunnel_assert(ptr == output + written);
  3543. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  3544. {
  3545. trunnel_assert(encoded_len >= 0);
  3546. trunnel_assert((size_t)encoded_len == written);
  3547. }
  3548. #endif
  3549. return written;
  3550. truncated:
  3551. result = -2;
  3552. goto fail;
  3553. check_failed:
  3554. (void)msg;
  3555. result = -1;
  3556. goto fail;
  3557. fail:
  3558. trunnel_assert(result < 0);
  3559. return result;
  3560. }
  3561. /** As socks5_server_reply_parse(), but do not allocate the output
  3562. * object.
  3563. */
  3564. static ssize_t
  3565. socks5_server_reply_parse_into(socks5_server_reply_t *obj, const uint8_t *input, const size_t len_in)
  3566. {
  3567. const uint8_t *ptr = input;
  3568. size_t remaining = len_in;
  3569. ssize_t result = 0;
  3570. (void)result;
  3571. /* Parse u8 version IN [5] */
  3572. CHECK_REMAINING(1, truncated);
  3573. obj->version = (trunnel_get_uint8(ptr));
  3574. remaining -= 1; ptr += 1;
  3575. if (! (obj->version == 5))
  3576. goto fail;
  3577. /* Parse u8 reply */
  3578. CHECK_REMAINING(1, truncated);
  3579. obj->reply = (trunnel_get_uint8(ptr));
  3580. remaining -= 1; ptr += 1;
  3581. /* Parse u8 reserved IN [0] */
  3582. CHECK_REMAINING(1, truncated);
  3583. obj->reserved = (trunnel_get_uint8(ptr));
  3584. remaining -= 1; ptr += 1;
  3585. if (! (obj->reserved == 0))
  3586. goto fail;
  3587. /* Parse u8 atype */
  3588. CHECK_REMAINING(1, truncated);
  3589. obj->atype = (trunnel_get_uint8(ptr));
  3590. remaining -= 1; ptr += 1;
  3591. /* Parse union bind_addr[atype] */
  3592. switch (obj->atype) {
  3593. case ATYPE_IPV4:
  3594. /* Parse u32 bind_addr_ipv4 */
  3595. CHECK_REMAINING(4, truncated);
  3596. obj->bind_addr_ipv4 = trunnel_ntohl(trunnel_get_uint32(ptr));
  3597. remaining -= 4; ptr += 4;
  3598. break;
  3599. case ATYPE_IPV6:
  3600. /* Parse u8 bind_addr_ipv6[16] */
  3601. CHECK_REMAINING(16, truncated);
  3602. memcpy(obj->bind_addr_ipv6, ptr, 16);
  3603. remaining -= 16; ptr += 16;
  3604. break;
  3605. case ATYPE_DOMAINNAME:
  3606. /* Parse struct domainname bind_addr_domainname */
  3607. result = domainname_parse(&obj->bind_addr_domainname, ptr, remaining);
  3608. if (result < 0)
  3609. goto relay_fail;
  3610. trunnel_assert((size_t)result <= remaining);
  3611. remaining -= result; ptr += result;
  3612. break;
  3613. default:
  3614. goto fail;
  3615. break;
  3616. }
  3617. /* Parse u16 bind_port */
  3618. CHECK_REMAINING(2, truncated);
  3619. obj->bind_port = trunnel_ntohs(trunnel_get_uint16(ptr));
  3620. remaining -= 2; ptr += 2;
  3621. trunnel_assert(ptr + remaining == input + len_in);
  3622. return len_in - remaining;
  3623. truncated:
  3624. return -2;
  3625. relay_fail:
  3626. trunnel_assert(result < 0);
  3627. return result;
  3628. fail:
  3629. result = -1;
  3630. return result;
  3631. }
  3632. ssize_t
  3633. socks5_server_reply_parse(socks5_server_reply_t **output, const uint8_t *input, const size_t len_in)
  3634. {
  3635. ssize_t result;
  3636. *output = socks5_server_reply_new();
  3637. if (NULL == *output)
  3638. return -1;
  3639. result = socks5_server_reply_parse_into(*output, input, len_in);
  3640. if (result < 0) {
  3641. socks5_server_reply_free(*output);
  3642. *output = NULL;
  3643. }
  3644. return result;
  3645. }