socks5.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977
  1. /* socks5.c -- generated by Trunnel v1.5.3.
  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. return val;
  1624. }
  1625. /** Release all storage held inside 'obj', but do not free 'obj'.
  1626. */
  1627. static void
  1628. socks4_server_reply_clear(socks4_server_reply_t *obj)
  1629. {
  1630. (void) obj;
  1631. }
  1632. void
  1633. socks4_server_reply_free(socks4_server_reply_t *obj)
  1634. {
  1635. if (obj == NULL)
  1636. return;
  1637. socks4_server_reply_clear(obj);
  1638. trunnel_memwipe(obj, sizeof(socks4_server_reply_t));
  1639. trunnel_free_(obj);
  1640. }
  1641. uint8_t
  1642. socks4_server_reply_get_version(const socks4_server_reply_t *inp)
  1643. {
  1644. return inp->version;
  1645. }
  1646. int
  1647. socks4_server_reply_set_version(socks4_server_reply_t *inp, uint8_t val)
  1648. {
  1649. if (! ((val == 0 || val == 4))) {
  1650. TRUNNEL_SET_ERROR_CODE(inp);
  1651. return -1;
  1652. }
  1653. inp->version = val;
  1654. return 0;
  1655. }
  1656. uint8_t
  1657. socks4_server_reply_get_status(const socks4_server_reply_t *inp)
  1658. {
  1659. return inp->status;
  1660. }
  1661. int
  1662. socks4_server_reply_set_status(socks4_server_reply_t *inp, uint8_t val)
  1663. {
  1664. inp->status = val;
  1665. return 0;
  1666. }
  1667. uint16_t
  1668. socks4_server_reply_get_port(const socks4_server_reply_t *inp)
  1669. {
  1670. return inp->port;
  1671. }
  1672. int
  1673. socks4_server_reply_set_port(socks4_server_reply_t *inp, uint16_t val)
  1674. {
  1675. inp->port = val;
  1676. return 0;
  1677. }
  1678. uint32_t
  1679. socks4_server_reply_get_addr(const socks4_server_reply_t *inp)
  1680. {
  1681. return inp->addr;
  1682. }
  1683. int
  1684. socks4_server_reply_set_addr(socks4_server_reply_t *inp, uint32_t val)
  1685. {
  1686. inp->addr = val;
  1687. return 0;
  1688. }
  1689. const char *
  1690. socks4_server_reply_check(const socks4_server_reply_t *obj)
  1691. {
  1692. if (obj == NULL)
  1693. return "Object was NULL";
  1694. if (obj->trunnel_error_code_)
  1695. return "A set function failed on this object";
  1696. if (! (obj->version == 0 || obj->version == 4))
  1697. return "Integer out of bounds";
  1698. return NULL;
  1699. }
  1700. ssize_t
  1701. socks4_server_reply_encoded_len(const socks4_server_reply_t *obj)
  1702. {
  1703. ssize_t result = 0;
  1704. if (NULL != socks4_server_reply_check(obj))
  1705. return -1;
  1706. /* Length of u8 version IN [0, 4] */
  1707. result += 1;
  1708. /* Length of u8 status */
  1709. result += 1;
  1710. /* Length of u16 port */
  1711. result += 2;
  1712. /* Length of u32 addr */
  1713. result += 4;
  1714. return result;
  1715. }
  1716. int
  1717. socks4_server_reply_clear_errors(socks4_server_reply_t *obj)
  1718. {
  1719. int r = obj->trunnel_error_code_;
  1720. obj->trunnel_error_code_ = 0;
  1721. return r;
  1722. }
  1723. ssize_t
  1724. socks4_server_reply_encode(uint8_t *output, const size_t avail, const socks4_server_reply_t *obj)
  1725. {
  1726. ssize_t result = 0;
  1727. size_t written = 0;
  1728. uint8_t *ptr = output;
  1729. const char *msg;
  1730. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1731. const ssize_t encoded_len = socks4_server_reply_encoded_len(obj);
  1732. #endif
  1733. if (NULL != (msg = socks4_server_reply_check(obj)))
  1734. goto check_failed;
  1735. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1736. trunnel_assert(encoded_len >= 0);
  1737. #endif
  1738. /* Encode u8 version IN [0, 4] */
  1739. trunnel_assert(written <= avail);
  1740. if (avail - written < 1)
  1741. goto truncated;
  1742. trunnel_set_uint8(ptr, (obj->version));
  1743. written += 1; ptr += 1;
  1744. /* Encode u8 status */
  1745. trunnel_assert(written <= avail);
  1746. if (avail - written < 1)
  1747. goto truncated;
  1748. trunnel_set_uint8(ptr, (obj->status));
  1749. written += 1; ptr += 1;
  1750. /* Encode u16 port */
  1751. trunnel_assert(written <= avail);
  1752. if (avail - written < 2)
  1753. goto truncated;
  1754. trunnel_set_uint16(ptr, trunnel_htons(obj->port));
  1755. written += 2; ptr += 2;
  1756. /* Encode u32 addr */
  1757. trunnel_assert(written <= avail);
  1758. if (avail - written < 4)
  1759. goto truncated;
  1760. trunnel_set_uint32(ptr, trunnel_htonl(obj->addr));
  1761. written += 4; ptr += 4;
  1762. trunnel_assert(ptr == output + written);
  1763. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  1764. {
  1765. trunnel_assert(encoded_len >= 0);
  1766. trunnel_assert((size_t)encoded_len == written);
  1767. }
  1768. #endif
  1769. return written;
  1770. truncated:
  1771. result = -2;
  1772. goto fail;
  1773. check_failed:
  1774. (void)msg;
  1775. result = -1;
  1776. goto fail;
  1777. fail:
  1778. trunnel_assert(result < 0);
  1779. return result;
  1780. }
  1781. /** As socks4_server_reply_parse(), but do not allocate the output
  1782. * object.
  1783. */
  1784. static ssize_t
  1785. socks4_server_reply_parse_into(socks4_server_reply_t *obj, const uint8_t *input, const size_t len_in)
  1786. {
  1787. const uint8_t *ptr = input;
  1788. size_t remaining = len_in;
  1789. ssize_t result = 0;
  1790. (void)result;
  1791. /* Parse u8 version IN [0, 4] */
  1792. CHECK_REMAINING(1, truncated);
  1793. obj->version = (trunnel_get_uint8(ptr));
  1794. remaining -= 1; ptr += 1;
  1795. if (! (obj->version == 0 || obj->version == 4))
  1796. goto fail;
  1797. /* Parse u8 status */
  1798. CHECK_REMAINING(1, truncated);
  1799. obj->status = (trunnel_get_uint8(ptr));
  1800. remaining -= 1; ptr += 1;
  1801. /* Parse u16 port */
  1802. CHECK_REMAINING(2, truncated);
  1803. obj->port = trunnel_ntohs(trunnel_get_uint16(ptr));
  1804. remaining -= 2; ptr += 2;
  1805. /* Parse u32 addr */
  1806. CHECK_REMAINING(4, truncated);
  1807. obj->addr = trunnel_ntohl(trunnel_get_uint32(ptr));
  1808. remaining -= 4; ptr += 4;
  1809. trunnel_assert(ptr + remaining == input + len_in);
  1810. return len_in - remaining;
  1811. truncated:
  1812. return -2;
  1813. fail:
  1814. result = -1;
  1815. return result;
  1816. }
  1817. ssize_t
  1818. socks4_server_reply_parse(socks4_server_reply_t **output, const uint8_t *input, const size_t len_in)
  1819. {
  1820. ssize_t result;
  1821. *output = socks4_server_reply_new();
  1822. if (NULL == *output)
  1823. return -1;
  1824. result = socks4_server_reply_parse_into(*output, input, len_in);
  1825. if (result < 0) {
  1826. socks4_server_reply_free(*output);
  1827. *output = NULL;
  1828. }
  1829. return result;
  1830. }
  1831. socks5_client_userpass_auth_t *
  1832. socks5_client_userpass_auth_new(void)
  1833. {
  1834. socks5_client_userpass_auth_t *val = trunnel_calloc(1, sizeof(socks5_client_userpass_auth_t));
  1835. if (NULL == val)
  1836. return NULL;
  1837. val->version = 1;
  1838. return val;
  1839. }
  1840. /** Release all storage held inside 'obj', but do not free 'obj'.
  1841. */
  1842. static void
  1843. socks5_client_userpass_auth_clear(socks5_client_userpass_auth_t *obj)
  1844. {
  1845. (void) obj;
  1846. TRUNNEL_DYNARRAY_WIPE(&obj->username);
  1847. TRUNNEL_DYNARRAY_CLEAR(&obj->username);
  1848. TRUNNEL_DYNARRAY_WIPE(&obj->passwd);
  1849. TRUNNEL_DYNARRAY_CLEAR(&obj->passwd);
  1850. }
  1851. void
  1852. socks5_client_userpass_auth_free(socks5_client_userpass_auth_t *obj)
  1853. {
  1854. if (obj == NULL)
  1855. return;
  1856. socks5_client_userpass_auth_clear(obj);
  1857. trunnel_memwipe(obj, sizeof(socks5_client_userpass_auth_t));
  1858. trunnel_free_(obj);
  1859. }
  1860. uint8_t
  1861. socks5_client_userpass_auth_get_version(const socks5_client_userpass_auth_t *inp)
  1862. {
  1863. return inp->version;
  1864. }
  1865. int
  1866. socks5_client_userpass_auth_set_version(socks5_client_userpass_auth_t *inp, uint8_t val)
  1867. {
  1868. if (! ((val == 1))) {
  1869. TRUNNEL_SET_ERROR_CODE(inp);
  1870. return -1;
  1871. }
  1872. inp->version = val;
  1873. return 0;
  1874. }
  1875. uint8_t
  1876. socks5_client_userpass_auth_get_username_len(const socks5_client_userpass_auth_t *inp)
  1877. {
  1878. return inp->username_len;
  1879. }
  1880. int
  1881. socks5_client_userpass_auth_set_username_len(socks5_client_userpass_auth_t *inp, uint8_t val)
  1882. {
  1883. inp->username_len = val;
  1884. return 0;
  1885. }
  1886. size_t
  1887. socks5_client_userpass_auth_getlen_username(const socks5_client_userpass_auth_t *inp)
  1888. {
  1889. return TRUNNEL_DYNARRAY_LEN(&inp->username);
  1890. }
  1891. char
  1892. socks5_client_userpass_auth_get_username(socks5_client_userpass_auth_t *inp, size_t idx)
  1893. {
  1894. return TRUNNEL_DYNARRAY_GET(&inp->username, idx);
  1895. }
  1896. char
  1897. socks5_client_userpass_auth_getconst_username(const socks5_client_userpass_auth_t *inp, size_t idx)
  1898. {
  1899. return socks5_client_userpass_auth_get_username((socks5_client_userpass_auth_t*)inp, idx);
  1900. }
  1901. int
  1902. socks5_client_userpass_auth_set_username(socks5_client_userpass_auth_t *inp, size_t idx, char elt)
  1903. {
  1904. TRUNNEL_DYNARRAY_SET(&inp->username, idx, elt);
  1905. return 0;
  1906. }
  1907. int
  1908. socks5_client_userpass_auth_add_username(socks5_client_userpass_auth_t *inp, char elt)
  1909. {
  1910. #if SIZE_MAX >= UINT8_MAX
  1911. if (inp->username.n_ == UINT8_MAX)
  1912. goto trunnel_alloc_failed;
  1913. #endif
  1914. TRUNNEL_DYNARRAY_ADD(char, &inp->username, elt, {});
  1915. return 0;
  1916. trunnel_alloc_failed:
  1917. TRUNNEL_SET_ERROR_CODE(inp);
  1918. return -1;
  1919. }
  1920. char *
  1921. socks5_client_userpass_auth_getarray_username(socks5_client_userpass_auth_t *inp)
  1922. {
  1923. return inp->username.elts_;
  1924. }
  1925. const char *
  1926. socks5_client_userpass_auth_getconstarray_username(const socks5_client_userpass_auth_t *inp)
  1927. {
  1928. return (const char *)socks5_client_userpass_auth_getarray_username((socks5_client_userpass_auth_t*)inp);
  1929. }
  1930. int
  1931. socks5_client_userpass_auth_setlen_username(socks5_client_userpass_auth_t *inp, size_t newlen)
  1932. {
  1933. #if UINT8_MAX < SIZE_MAX
  1934. if (newlen > UINT8_MAX)
  1935. goto trunnel_alloc_failed;
  1936. #endif
  1937. return trunnel_string_setlen(&inp->username, newlen,
  1938. &inp->trunnel_error_code_);
  1939. trunnel_alloc_failed:
  1940. TRUNNEL_SET_ERROR_CODE(inp);
  1941. return -1;
  1942. }
  1943. const char *
  1944. socks5_client_userpass_auth_getstr_username(socks5_client_userpass_auth_t *inp)
  1945. {
  1946. return trunnel_string_getstr(&inp->username);
  1947. }
  1948. int
  1949. socks5_client_userpass_auth_setstr0_username(socks5_client_userpass_auth_t *inp, const char *val, size_t len)
  1950. {
  1951. #if UINT8_MAX < SIZE_MAX
  1952. if (len > UINT8_MAX) {
  1953. TRUNNEL_SET_ERROR_CODE(inp);
  1954. return -1;
  1955. }
  1956. #endif
  1957. return trunnel_string_setstr0(&inp->username, val, len, &inp->trunnel_error_code_);
  1958. }
  1959. int
  1960. socks5_client_userpass_auth_setstr_username(socks5_client_userpass_auth_t *inp, const char *val)
  1961. {
  1962. return socks5_client_userpass_auth_setstr0_username(inp, val, strlen(val));
  1963. }
  1964. uint8_t
  1965. socks5_client_userpass_auth_get_passwd_len(const socks5_client_userpass_auth_t *inp)
  1966. {
  1967. return inp->passwd_len;
  1968. }
  1969. int
  1970. socks5_client_userpass_auth_set_passwd_len(socks5_client_userpass_auth_t *inp, uint8_t val)
  1971. {
  1972. inp->passwd_len = val;
  1973. return 0;
  1974. }
  1975. size_t
  1976. socks5_client_userpass_auth_getlen_passwd(const socks5_client_userpass_auth_t *inp)
  1977. {
  1978. return TRUNNEL_DYNARRAY_LEN(&inp->passwd);
  1979. }
  1980. char
  1981. socks5_client_userpass_auth_get_passwd(socks5_client_userpass_auth_t *inp, size_t idx)
  1982. {
  1983. return TRUNNEL_DYNARRAY_GET(&inp->passwd, idx);
  1984. }
  1985. char
  1986. socks5_client_userpass_auth_getconst_passwd(const socks5_client_userpass_auth_t *inp, size_t idx)
  1987. {
  1988. return socks5_client_userpass_auth_get_passwd((socks5_client_userpass_auth_t*)inp, idx);
  1989. }
  1990. int
  1991. socks5_client_userpass_auth_set_passwd(socks5_client_userpass_auth_t *inp, size_t idx, char elt)
  1992. {
  1993. TRUNNEL_DYNARRAY_SET(&inp->passwd, idx, elt);
  1994. return 0;
  1995. }
  1996. int
  1997. socks5_client_userpass_auth_add_passwd(socks5_client_userpass_auth_t *inp, char elt)
  1998. {
  1999. #if SIZE_MAX >= UINT8_MAX
  2000. if (inp->passwd.n_ == UINT8_MAX)
  2001. goto trunnel_alloc_failed;
  2002. #endif
  2003. TRUNNEL_DYNARRAY_ADD(char, &inp->passwd, elt, {});
  2004. return 0;
  2005. trunnel_alloc_failed:
  2006. TRUNNEL_SET_ERROR_CODE(inp);
  2007. return -1;
  2008. }
  2009. char *
  2010. socks5_client_userpass_auth_getarray_passwd(socks5_client_userpass_auth_t *inp)
  2011. {
  2012. return inp->passwd.elts_;
  2013. }
  2014. const char *
  2015. socks5_client_userpass_auth_getconstarray_passwd(const socks5_client_userpass_auth_t *inp)
  2016. {
  2017. return (const char *)socks5_client_userpass_auth_getarray_passwd((socks5_client_userpass_auth_t*)inp);
  2018. }
  2019. int
  2020. socks5_client_userpass_auth_setlen_passwd(socks5_client_userpass_auth_t *inp, size_t newlen)
  2021. {
  2022. #if UINT8_MAX < SIZE_MAX
  2023. if (newlen > UINT8_MAX)
  2024. goto trunnel_alloc_failed;
  2025. #endif
  2026. return trunnel_string_setlen(&inp->passwd, newlen,
  2027. &inp->trunnel_error_code_);
  2028. trunnel_alloc_failed:
  2029. TRUNNEL_SET_ERROR_CODE(inp);
  2030. return -1;
  2031. }
  2032. const char *
  2033. socks5_client_userpass_auth_getstr_passwd(socks5_client_userpass_auth_t *inp)
  2034. {
  2035. return trunnel_string_getstr(&inp->passwd);
  2036. }
  2037. int
  2038. socks5_client_userpass_auth_setstr0_passwd(socks5_client_userpass_auth_t *inp, const char *val, size_t len)
  2039. {
  2040. #if UINT8_MAX < SIZE_MAX
  2041. if (len > UINT8_MAX) {
  2042. TRUNNEL_SET_ERROR_CODE(inp);
  2043. return -1;
  2044. }
  2045. #endif
  2046. return trunnel_string_setstr0(&inp->passwd, val, len, &inp->trunnel_error_code_);
  2047. }
  2048. int
  2049. socks5_client_userpass_auth_setstr_passwd(socks5_client_userpass_auth_t *inp, const char *val)
  2050. {
  2051. return socks5_client_userpass_auth_setstr0_passwd(inp, val, strlen(val));
  2052. }
  2053. const char *
  2054. socks5_client_userpass_auth_check(const socks5_client_userpass_auth_t *obj)
  2055. {
  2056. if (obj == NULL)
  2057. return "Object was NULL";
  2058. if (obj->trunnel_error_code_)
  2059. return "A set function failed on this object";
  2060. if (! (obj->version == 1))
  2061. return "Integer out of bounds";
  2062. if (TRUNNEL_DYNARRAY_LEN(&obj->username) != obj->username_len)
  2063. return "Length mismatch for username";
  2064. if (TRUNNEL_DYNARRAY_LEN(&obj->passwd) != obj->passwd_len)
  2065. return "Length mismatch for passwd";
  2066. return NULL;
  2067. }
  2068. ssize_t
  2069. socks5_client_userpass_auth_encoded_len(const socks5_client_userpass_auth_t *obj)
  2070. {
  2071. ssize_t result = 0;
  2072. if (NULL != socks5_client_userpass_auth_check(obj))
  2073. return -1;
  2074. /* Length of u8 version IN [1] */
  2075. result += 1;
  2076. /* Length of u8 username_len */
  2077. result += 1;
  2078. /* Length of char username[username_len] */
  2079. result += TRUNNEL_DYNARRAY_LEN(&obj->username);
  2080. /* Length of u8 passwd_len */
  2081. result += 1;
  2082. /* Length of char passwd[passwd_len] */
  2083. result += TRUNNEL_DYNARRAY_LEN(&obj->passwd);
  2084. return result;
  2085. }
  2086. int
  2087. socks5_client_userpass_auth_clear_errors(socks5_client_userpass_auth_t *obj)
  2088. {
  2089. int r = obj->trunnel_error_code_;
  2090. obj->trunnel_error_code_ = 0;
  2091. return r;
  2092. }
  2093. ssize_t
  2094. socks5_client_userpass_auth_encode(uint8_t *output, const size_t avail, const socks5_client_userpass_auth_t *obj)
  2095. {
  2096. ssize_t result = 0;
  2097. size_t written = 0;
  2098. uint8_t *ptr = output;
  2099. const char *msg;
  2100. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2101. const ssize_t encoded_len = socks5_client_userpass_auth_encoded_len(obj);
  2102. #endif
  2103. if (NULL != (msg = socks5_client_userpass_auth_check(obj)))
  2104. goto check_failed;
  2105. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2106. trunnel_assert(encoded_len >= 0);
  2107. #endif
  2108. /* Encode u8 version IN [1] */
  2109. trunnel_assert(written <= avail);
  2110. if (avail - written < 1)
  2111. goto truncated;
  2112. trunnel_set_uint8(ptr, (obj->version));
  2113. written += 1; ptr += 1;
  2114. /* Encode u8 username_len */
  2115. trunnel_assert(written <= avail);
  2116. if (avail - written < 1)
  2117. goto truncated;
  2118. trunnel_set_uint8(ptr, (obj->username_len));
  2119. written += 1; ptr += 1;
  2120. /* Encode char username[username_len] */
  2121. {
  2122. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->username);
  2123. trunnel_assert(obj->username_len == elt_len);
  2124. trunnel_assert(written <= avail);
  2125. if (avail - written < elt_len)
  2126. goto truncated;
  2127. if (elt_len)
  2128. memcpy(ptr, obj->username.elts_, elt_len);
  2129. written += elt_len; ptr += elt_len;
  2130. }
  2131. /* Encode u8 passwd_len */
  2132. trunnel_assert(written <= avail);
  2133. if (avail - written < 1)
  2134. goto truncated;
  2135. trunnel_set_uint8(ptr, (obj->passwd_len));
  2136. written += 1; ptr += 1;
  2137. /* Encode char passwd[passwd_len] */
  2138. {
  2139. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->passwd);
  2140. trunnel_assert(obj->passwd_len == elt_len);
  2141. trunnel_assert(written <= avail);
  2142. if (avail - written < elt_len)
  2143. goto truncated;
  2144. if (elt_len)
  2145. memcpy(ptr, obj->passwd.elts_, elt_len);
  2146. written += elt_len; ptr += elt_len;
  2147. }
  2148. trunnel_assert(ptr == output + written);
  2149. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2150. {
  2151. trunnel_assert(encoded_len >= 0);
  2152. trunnel_assert((size_t)encoded_len == written);
  2153. }
  2154. #endif
  2155. return written;
  2156. truncated:
  2157. result = -2;
  2158. goto fail;
  2159. check_failed:
  2160. (void)msg;
  2161. result = -1;
  2162. goto fail;
  2163. fail:
  2164. trunnel_assert(result < 0);
  2165. return result;
  2166. }
  2167. /** As socks5_client_userpass_auth_parse(), but do not allocate the
  2168. * output object.
  2169. */
  2170. static ssize_t
  2171. socks5_client_userpass_auth_parse_into(socks5_client_userpass_auth_t *obj, const uint8_t *input, const size_t len_in)
  2172. {
  2173. const uint8_t *ptr = input;
  2174. size_t remaining = len_in;
  2175. ssize_t result = 0;
  2176. (void)result;
  2177. /* Parse u8 version IN [1] */
  2178. CHECK_REMAINING(1, truncated);
  2179. obj->version = (trunnel_get_uint8(ptr));
  2180. remaining -= 1; ptr += 1;
  2181. if (! (obj->version == 1))
  2182. goto fail;
  2183. /* Parse u8 username_len */
  2184. CHECK_REMAINING(1, truncated);
  2185. obj->username_len = (trunnel_get_uint8(ptr));
  2186. remaining -= 1; ptr += 1;
  2187. /* Parse char username[username_len] */
  2188. CHECK_REMAINING(obj->username_len, truncated);
  2189. if (socks5_client_userpass_auth_setstr0_username(obj, (const char*)ptr, obj->username_len))
  2190. goto fail;
  2191. ptr += obj->username_len; remaining -= obj->username_len;
  2192. /* Parse u8 passwd_len */
  2193. CHECK_REMAINING(1, truncated);
  2194. obj->passwd_len = (trunnel_get_uint8(ptr));
  2195. remaining -= 1; ptr += 1;
  2196. /* Parse char passwd[passwd_len] */
  2197. CHECK_REMAINING(obj->passwd_len, truncated);
  2198. if (socks5_client_userpass_auth_setstr0_passwd(obj, (const char*)ptr, obj->passwd_len))
  2199. goto fail;
  2200. ptr += obj->passwd_len; remaining -= obj->passwd_len;
  2201. trunnel_assert(ptr + remaining == input + len_in);
  2202. return len_in - remaining;
  2203. truncated:
  2204. return -2;
  2205. fail:
  2206. result = -1;
  2207. return result;
  2208. }
  2209. ssize_t
  2210. socks5_client_userpass_auth_parse(socks5_client_userpass_auth_t **output, const uint8_t *input, const size_t len_in)
  2211. {
  2212. ssize_t result;
  2213. *output = socks5_client_userpass_auth_new();
  2214. if (NULL == *output)
  2215. return -1;
  2216. result = socks5_client_userpass_auth_parse_into(*output, input, len_in);
  2217. if (result < 0) {
  2218. socks5_client_userpass_auth_free(*output);
  2219. *output = NULL;
  2220. }
  2221. return result;
  2222. }
  2223. socks5_client_version_t *
  2224. socks5_client_version_new(void)
  2225. {
  2226. socks5_client_version_t *val = trunnel_calloc(1, sizeof(socks5_client_version_t));
  2227. if (NULL == val)
  2228. return NULL;
  2229. val->version = 5;
  2230. return val;
  2231. }
  2232. /** Release all storage held inside 'obj', but do not free 'obj'.
  2233. */
  2234. static void
  2235. socks5_client_version_clear(socks5_client_version_t *obj)
  2236. {
  2237. (void) obj;
  2238. TRUNNEL_DYNARRAY_WIPE(&obj->methods);
  2239. TRUNNEL_DYNARRAY_CLEAR(&obj->methods);
  2240. }
  2241. void
  2242. socks5_client_version_free(socks5_client_version_t *obj)
  2243. {
  2244. if (obj == NULL)
  2245. return;
  2246. socks5_client_version_clear(obj);
  2247. trunnel_memwipe(obj, sizeof(socks5_client_version_t));
  2248. trunnel_free_(obj);
  2249. }
  2250. uint8_t
  2251. socks5_client_version_get_version(const socks5_client_version_t *inp)
  2252. {
  2253. return inp->version;
  2254. }
  2255. int
  2256. socks5_client_version_set_version(socks5_client_version_t *inp, uint8_t val)
  2257. {
  2258. if (! ((val == 5))) {
  2259. TRUNNEL_SET_ERROR_CODE(inp);
  2260. return -1;
  2261. }
  2262. inp->version = val;
  2263. return 0;
  2264. }
  2265. uint8_t
  2266. socks5_client_version_get_n_methods(const socks5_client_version_t *inp)
  2267. {
  2268. return inp->n_methods;
  2269. }
  2270. int
  2271. socks5_client_version_set_n_methods(socks5_client_version_t *inp, uint8_t val)
  2272. {
  2273. inp->n_methods = val;
  2274. return 0;
  2275. }
  2276. size_t
  2277. socks5_client_version_getlen_methods(const socks5_client_version_t *inp)
  2278. {
  2279. return TRUNNEL_DYNARRAY_LEN(&inp->methods);
  2280. }
  2281. uint8_t
  2282. socks5_client_version_get_methods(socks5_client_version_t *inp, size_t idx)
  2283. {
  2284. return TRUNNEL_DYNARRAY_GET(&inp->methods, idx);
  2285. }
  2286. uint8_t
  2287. socks5_client_version_getconst_methods(const socks5_client_version_t *inp, size_t idx)
  2288. {
  2289. return socks5_client_version_get_methods((socks5_client_version_t*)inp, idx);
  2290. }
  2291. int
  2292. socks5_client_version_set_methods(socks5_client_version_t *inp, size_t idx, uint8_t elt)
  2293. {
  2294. TRUNNEL_DYNARRAY_SET(&inp->methods, idx, elt);
  2295. return 0;
  2296. }
  2297. int
  2298. socks5_client_version_add_methods(socks5_client_version_t *inp, uint8_t elt)
  2299. {
  2300. #if SIZE_MAX >= UINT8_MAX
  2301. if (inp->methods.n_ == UINT8_MAX)
  2302. goto trunnel_alloc_failed;
  2303. #endif
  2304. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->methods, elt, {});
  2305. return 0;
  2306. trunnel_alloc_failed:
  2307. TRUNNEL_SET_ERROR_CODE(inp);
  2308. return -1;
  2309. }
  2310. uint8_t *
  2311. socks5_client_version_getarray_methods(socks5_client_version_t *inp)
  2312. {
  2313. return inp->methods.elts_;
  2314. }
  2315. const uint8_t *
  2316. socks5_client_version_getconstarray_methods(const socks5_client_version_t *inp)
  2317. {
  2318. return (const uint8_t *)socks5_client_version_getarray_methods((socks5_client_version_t*)inp);
  2319. }
  2320. int
  2321. socks5_client_version_setlen_methods(socks5_client_version_t *inp, size_t newlen)
  2322. {
  2323. uint8_t *newptr;
  2324. #if UINT8_MAX < SIZE_MAX
  2325. if (newlen > UINT8_MAX)
  2326. goto trunnel_alloc_failed;
  2327. #endif
  2328. newptr = trunnel_dynarray_setlen(&inp->methods.allocated_,
  2329. &inp->methods.n_, inp->methods.elts_, newlen,
  2330. sizeof(inp->methods.elts_[0]), (trunnel_free_fn_t) NULL,
  2331. &inp->trunnel_error_code_);
  2332. if (newlen != 0 && newptr == NULL)
  2333. goto trunnel_alloc_failed;
  2334. inp->methods.elts_ = newptr;
  2335. return 0;
  2336. trunnel_alloc_failed:
  2337. TRUNNEL_SET_ERROR_CODE(inp);
  2338. return -1;
  2339. }
  2340. const char *
  2341. socks5_client_version_check(const socks5_client_version_t *obj)
  2342. {
  2343. if (obj == NULL)
  2344. return "Object was NULL";
  2345. if (obj->trunnel_error_code_)
  2346. return "A set function failed on this object";
  2347. if (! (obj->version == 5))
  2348. return "Integer out of bounds";
  2349. if (TRUNNEL_DYNARRAY_LEN(&obj->methods) != obj->n_methods)
  2350. return "Length mismatch for methods";
  2351. return NULL;
  2352. }
  2353. ssize_t
  2354. socks5_client_version_encoded_len(const socks5_client_version_t *obj)
  2355. {
  2356. ssize_t result = 0;
  2357. if (NULL != socks5_client_version_check(obj))
  2358. return -1;
  2359. /* Length of u8 version IN [5] */
  2360. result += 1;
  2361. /* Length of u8 n_methods */
  2362. result += 1;
  2363. /* Length of u8 methods[n_methods] */
  2364. result += TRUNNEL_DYNARRAY_LEN(&obj->methods);
  2365. return result;
  2366. }
  2367. int
  2368. socks5_client_version_clear_errors(socks5_client_version_t *obj)
  2369. {
  2370. int r = obj->trunnel_error_code_;
  2371. obj->trunnel_error_code_ = 0;
  2372. return r;
  2373. }
  2374. ssize_t
  2375. socks5_client_version_encode(uint8_t *output, const size_t avail, const socks5_client_version_t *obj)
  2376. {
  2377. ssize_t result = 0;
  2378. size_t written = 0;
  2379. uint8_t *ptr = output;
  2380. const char *msg;
  2381. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2382. const ssize_t encoded_len = socks5_client_version_encoded_len(obj);
  2383. #endif
  2384. if (NULL != (msg = socks5_client_version_check(obj)))
  2385. goto check_failed;
  2386. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2387. trunnel_assert(encoded_len >= 0);
  2388. #endif
  2389. /* Encode u8 version IN [5] */
  2390. trunnel_assert(written <= avail);
  2391. if (avail - written < 1)
  2392. goto truncated;
  2393. trunnel_set_uint8(ptr, (obj->version));
  2394. written += 1; ptr += 1;
  2395. /* Encode u8 n_methods */
  2396. trunnel_assert(written <= avail);
  2397. if (avail - written < 1)
  2398. goto truncated;
  2399. trunnel_set_uint8(ptr, (obj->n_methods));
  2400. written += 1; ptr += 1;
  2401. /* Encode u8 methods[n_methods] */
  2402. {
  2403. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->methods);
  2404. trunnel_assert(obj->n_methods == elt_len);
  2405. trunnel_assert(written <= avail);
  2406. if (avail - written < elt_len)
  2407. goto truncated;
  2408. if (elt_len)
  2409. memcpy(ptr, obj->methods.elts_, elt_len);
  2410. written += elt_len; ptr += elt_len;
  2411. }
  2412. trunnel_assert(ptr == output + written);
  2413. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2414. {
  2415. trunnel_assert(encoded_len >= 0);
  2416. trunnel_assert((size_t)encoded_len == written);
  2417. }
  2418. #endif
  2419. return written;
  2420. truncated:
  2421. result = -2;
  2422. goto fail;
  2423. check_failed:
  2424. (void)msg;
  2425. result = -1;
  2426. goto fail;
  2427. fail:
  2428. trunnel_assert(result < 0);
  2429. return result;
  2430. }
  2431. /** As socks5_client_version_parse(), but do not allocate the output
  2432. * object.
  2433. */
  2434. static ssize_t
  2435. socks5_client_version_parse_into(socks5_client_version_t *obj, const uint8_t *input, const size_t len_in)
  2436. {
  2437. const uint8_t *ptr = input;
  2438. size_t remaining = len_in;
  2439. ssize_t result = 0;
  2440. (void)result;
  2441. /* Parse u8 version IN [5] */
  2442. CHECK_REMAINING(1, truncated);
  2443. obj->version = (trunnel_get_uint8(ptr));
  2444. remaining -= 1; ptr += 1;
  2445. if (! (obj->version == 5))
  2446. goto fail;
  2447. /* Parse u8 n_methods */
  2448. CHECK_REMAINING(1, truncated);
  2449. obj->n_methods = (trunnel_get_uint8(ptr));
  2450. remaining -= 1; ptr += 1;
  2451. /* Parse u8 methods[n_methods] */
  2452. CHECK_REMAINING(obj->n_methods, truncated);
  2453. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->methods, obj->n_methods, {});
  2454. obj->methods.n_ = obj->n_methods;
  2455. if (obj->n_methods)
  2456. memcpy(obj->methods.elts_, ptr, obj->n_methods);
  2457. ptr += obj->n_methods; remaining -= obj->n_methods;
  2458. trunnel_assert(ptr + remaining == input + len_in);
  2459. return len_in - remaining;
  2460. truncated:
  2461. return -2;
  2462. trunnel_alloc_failed:
  2463. return -1;
  2464. fail:
  2465. result = -1;
  2466. return result;
  2467. }
  2468. ssize_t
  2469. socks5_client_version_parse(socks5_client_version_t **output, const uint8_t *input, const size_t len_in)
  2470. {
  2471. ssize_t result;
  2472. *output = socks5_client_version_new();
  2473. if (NULL == *output)
  2474. return -1;
  2475. result = socks5_client_version_parse_into(*output, input, len_in);
  2476. if (result < 0) {
  2477. socks5_client_version_free(*output);
  2478. *output = NULL;
  2479. }
  2480. return result;
  2481. }
  2482. socks5_server_method_t *
  2483. socks5_server_method_new(void)
  2484. {
  2485. socks5_server_method_t *val = trunnel_calloc(1, sizeof(socks5_server_method_t));
  2486. if (NULL == val)
  2487. return NULL;
  2488. val->version = 5;
  2489. return val;
  2490. }
  2491. /** Release all storage held inside 'obj', but do not free 'obj'.
  2492. */
  2493. static void
  2494. socks5_server_method_clear(socks5_server_method_t *obj)
  2495. {
  2496. (void) obj;
  2497. }
  2498. void
  2499. socks5_server_method_free(socks5_server_method_t *obj)
  2500. {
  2501. if (obj == NULL)
  2502. return;
  2503. socks5_server_method_clear(obj);
  2504. trunnel_memwipe(obj, sizeof(socks5_server_method_t));
  2505. trunnel_free_(obj);
  2506. }
  2507. uint8_t
  2508. socks5_server_method_get_version(const socks5_server_method_t *inp)
  2509. {
  2510. return inp->version;
  2511. }
  2512. int
  2513. socks5_server_method_set_version(socks5_server_method_t *inp, uint8_t val)
  2514. {
  2515. if (! ((val == 5))) {
  2516. TRUNNEL_SET_ERROR_CODE(inp);
  2517. return -1;
  2518. }
  2519. inp->version = val;
  2520. return 0;
  2521. }
  2522. uint8_t
  2523. socks5_server_method_get_method(const socks5_server_method_t *inp)
  2524. {
  2525. return inp->method;
  2526. }
  2527. int
  2528. socks5_server_method_set_method(socks5_server_method_t *inp, uint8_t val)
  2529. {
  2530. inp->method = val;
  2531. return 0;
  2532. }
  2533. const char *
  2534. socks5_server_method_check(const socks5_server_method_t *obj)
  2535. {
  2536. if (obj == NULL)
  2537. return "Object was NULL";
  2538. if (obj->trunnel_error_code_)
  2539. return "A set function failed on this object";
  2540. if (! (obj->version == 5))
  2541. return "Integer out of bounds";
  2542. return NULL;
  2543. }
  2544. ssize_t
  2545. socks5_server_method_encoded_len(const socks5_server_method_t *obj)
  2546. {
  2547. ssize_t result = 0;
  2548. if (NULL != socks5_server_method_check(obj))
  2549. return -1;
  2550. /* Length of u8 version IN [5] */
  2551. result += 1;
  2552. /* Length of u8 method */
  2553. result += 1;
  2554. return result;
  2555. }
  2556. int
  2557. socks5_server_method_clear_errors(socks5_server_method_t *obj)
  2558. {
  2559. int r = obj->trunnel_error_code_;
  2560. obj->trunnel_error_code_ = 0;
  2561. return r;
  2562. }
  2563. ssize_t
  2564. socks5_server_method_encode(uint8_t *output, const size_t avail, const socks5_server_method_t *obj)
  2565. {
  2566. ssize_t result = 0;
  2567. size_t written = 0;
  2568. uint8_t *ptr = output;
  2569. const char *msg;
  2570. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2571. const ssize_t encoded_len = socks5_server_method_encoded_len(obj);
  2572. #endif
  2573. if (NULL != (msg = socks5_server_method_check(obj)))
  2574. goto check_failed;
  2575. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2576. trunnel_assert(encoded_len >= 0);
  2577. #endif
  2578. /* Encode u8 version IN [5] */
  2579. trunnel_assert(written <= avail);
  2580. if (avail - written < 1)
  2581. goto truncated;
  2582. trunnel_set_uint8(ptr, (obj->version));
  2583. written += 1; ptr += 1;
  2584. /* Encode u8 method */
  2585. trunnel_assert(written <= avail);
  2586. if (avail - written < 1)
  2587. goto truncated;
  2588. trunnel_set_uint8(ptr, (obj->method));
  2589. written += 1; ptr += 1;
  2590. trunnel_assert(ptr == output + written);
  2591. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2592. {
  2593. trunnel_assert(encoded_len >= 0);
  2594. trunnel_assert((size_t)encoded_len == written);
  2595. }
  2596. #endif
  2597. return written;
  2598. truncated:
  2599. result = -2;
  2600. goto fail;
  2601. check_failed:
  2602. (void)msg;
  2603. result = -1;
  2604. goto fail;
  2605. fail:
  2606. trunnel_assert(result < 0);
  2607. return result;
  2608. }
  2609. /** As socks5_server_method_parse(), but do not allocate the output
  2610. * object.
  2611. */
  2612. static ssize_t
  2613. socks5_server_method_parse_into(socks5_server_method_t *obj, const uint8_t *input, const size_t len_in)
  2614. {
  2615. const uint8_t *ptr = input;
  2616. size_t remaining = len_in;
  2617. ssize_t result = 0;
  2618. (void)result;
  2619. /* Parse u8 version IN [5] */
  2620. CHECK_REMAINING(1, truncated);
  2621. obj->version = (trunnel_get_uint8(ptr));
  2622. remaining -= 1; ptr += 1;
  2623. if (! (obj->version == 5))
  2624. goto fail;
  2625. /* Parse u8 method */
  2626. CHECK_REMAINING(1, truncated);
  2627. obj->method = (trunnel_get_uint8(ptr));
  2628. remaining -= 1; ptr += 1;
  2629. trunnel_assert(ptr + remaining == input + len_in);
  2630. return len_in - remaining;
  2631. truncated:
  2632. return -2;
  2633. fail:
  2634. result = -1;
  2635. return result;
  2636. }
  2637. ssize_t
  2638. socks5_server_method_parse(socks5_server_method_t **output, const uint8_t *input, const size_t len_in)
  2639. {
  2640. ssize_t result;
  2641. *output = socks5_server_method_new();
  2642. if (NULL == *output)
  2643. return -1;
  2644. result = socks5_server_method_parse_into(*output, input, len_in);
  2645. if (result < 0) {
  2646. socks5_server_method_free(*output);
  2647. *output = NULL;
  2648. }
  2649. return result;
  2650. }
  2651. socks5_server_userpass_auth_t *
  2652. socks5_server_userpass_auth_new(void)
  2653. {
  2654. socks5_server_userpass_auth_t *val = trunnel_calloc(1, sizeof(socks5_server_userpass_auth_t));
  2655. if (NULL == val)
  2656. return NULL;
  2657. val->version = 1;
  2658. return val;
  2659. }
  2660. /** Release all storage held inside 'obj', but do not free 'obj'.
  2661. */
  2662. static void
  2663. socks5_server_userpass_auth_clear(socks5_server_userpass_auth_t *obj)
  2664. {
  2665. (void) obj;
  2666. }
  2667. void
  2668. socks5_server_userpass_auth_free(socks5_server_userpass_auth_t *obj)
  2669. {
  2670. if (obj == NULL)
  2671. return;
  2672. socks5_server_userpass_auth_clear(obj);
  2673. trunnel_memwipe(obj, sizeof(socks5_server_userpass_auth_t));
  2674. trunnel_free_(obj);
  2675. }
  2676. uint8_t
  2677. socks5_server_userpass_auth_get_version(const socks5_server_userpass_auth_t *inp)
  2678. {
  2679. return inp->version;
  2680. }
  2681. int
  2682. socks5_server_userpass_auth_set_version(socks5_server_userpass_auth_t *inp, uint8_t val)
  2683. {
  2684. if (! ((val == 1))) {
  2685. TRUNNEL_SET_ERROR_CODE(inp);
  2686. return -1;
  2687. }
  2688. inp->version = val;
  2689. return 0;
  2690. }
  2691. uint8_t
  2692. socks5_server_userpass_auth_get_status(const socks5_server_userpass_auth_t *inp)
  2693. {
  2694. return inp->status;
  2695. }
  2696. int
  2697. socks5_server_userpass_auth_set_status(socks5_server_userpass_auth_t *inp, uint8_t val)
  2698. {
  2699. inp->status = val;
  2700. return 0;
  2701. }
  2702. const char *
  2703. socks5_server_userpass_auth_check(const socks5_server_userpass_auth_t *obj)
  2704. {
  2705. if (obj == NULL)
  2706. return "Object was NULL";
  2707. if (obj->trunnel_error_code_)
  2708. return "A set function failed on this object";
  2709. if (! (obj->version == 1))
  2710. return "Integer out of bounds";
  2711. return NULL;
  2712. }
  2713. ssize_t
  2714. socks5_server_userpass_auth_encoded_len(const socks5_server_userpass_auth_t *obj)
  2715. {
  2716. ssize_t result = 0;
  2717. if (NULL != socks5_server_userpass_auth_check(obj))
  2718. return -1;
  2719. /* Length of u8 version IN [1] */
  2720. result += 1;
  2721. /* Length of u8 status */
  2722. result += 1;
  2723. return result;
  2724. }
  2725. int
  2726. socks5_server_userpass_auth_clear_errors(socks5_server_userpass_auth_t *obj)
  2727. {
  2728. int r = obj->trunnel_error_code_;
  2729. obj->trunnel_error_code_ = 0;
  2730. return r;
  2731. }
  2732. ssize_t
  2733. socks5_server_userpass_auth_encode(uint8_t *output, const size_t avail, const socks5_server_userpass_auth_t *obj)
  2734. {
  2735. ssize_t result = 0;
  2736. size_t written = 0;
  2737. uint8_t *ptr = output;
  2738. const char *msg;
  2739. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2740. const ssize_t encoded_len = socks5_server_userpass_auth_encoded_len(obj);
  2741. #endif
  2742. if (NULL != (msg = socks5_server_userpass_auth_check(obj)))
  2743. goto check_failed;
  2744. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2745. trunnel_assert(encoded_len >= 0);
  2746. #endif
  2747. /* Encode u8 version IN [1] */
  2748. trunnel_assert(written <= avail);
  2749. if (avail - written < 1)
  2750. goto truncated;
  2751. trunnel_set_uint8(ptr, (obj->version));
  2752. written += 1; ptr += 1;
  2753. /* Encode u8 status */
  2754. trunnel_assert(written <= avail);
  2755. if (avail - written < 1)
  2756. goto truncated;
  2757. trunnel_set_uint8(ptr, (obj->status));
  2758. written += 1; ptr += 1;
  2759. trunnel_assert(ptr == output + written);
  2760. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  2761. {
  2762. trunnel_assert(encoded_len >= 0);
  2763. trunnel_assert((size_t)encoded_len == written);
  2764. }
  2765. #endif
  2766. return written;
  2767. truncated:
  2768. result = -2;
  2769. goto fail;
  2770. check_failed:
  2771. (void)msg;
  2772. result = -1;
  2773. goto fail;
  2774. fail:
  2775. trunnel_assert(result < 0);
  2776. return result;
  2777. }
  2778. /** As socks5_server_userpass_auth_parse(), but do not allocate the
  2779. * output object.
  2780. */
  2781. static ssize_t
  2782. socks5_server_userpass_auth_parse_into(socks5_server_userpass_auth_t *obj, const uint8_t *input, const size_t len_in)
  2783. {
  2784. const uint8_t *ptr = input;
  2785. size_t remaining = len_in;
  2786. ssize_t result = 0;
  2787. (void)result;
  2788. /* Parse u8 version IN [1] */
  2789. CHECK_REMAINING(1, truncated);
  2790. obj->version = (trunnel_get_uint8(ptr));
  2791. remaining -= 1; ptr += 1;
  2792. if (! (obj->version == 1))
  2793. goto fail;
  2794. /* Parse u8 status */
  2795. CHECK_REMAINING(1, truncated);
  2796. obj->status = (trunnel_get_uint8(ptr));
  2797. remaining -= 1; ptr += 1;
  2798. trunnel_assert(ptr + remaining == input + len_in);
  2799. return len_in - remaining;
  2800. truncated:
  2801. return -2;
  2802. fail:
  2803. result = -1;
  2804. return result;
  2805. }
  2806. ssize_t
  2807. socks5_server_userpass_auth_parse(socks5_server_userpass_auth_t **output, const uint8_t *input, const size_t len_in)
  2808. {
  2809. ssize_t result;
  2810. *output = socks5_server_userpass_auth_new();
  2811. if (NULL == *output)
  2812. return -1;
  2813. result = socks5_server_userpass_auth_parse_into(*output, input, len_in);
  2814. if (result < 0) {
  2815. socks5_server_userpass_auth_free(*output);
  2816. *output = NULL;
  2817. }
  2818. return result;
  2819. }
  2820. socks5_client_request_t *
  2821. socks5_client_request_new(void)
  2822. {
  2823. socks5_client_request_t *val = trunnel_calloc(1, sizeof(socks5_client_request_t));
  2824. if (NULL == val)
  2825. return NULL;
  2826. val->version = 5;
  2827. val->command = CMD_BIND;
  2828. return val;
  2829. }
  2830. /** Release all storage held inside 'obj', but do not free 'obj'.
  2831. */
  2832. static void
  2833. socks5_client_request_clear(socks5_client_request_t *obj)
  2834. {
  2835. (void) obj;
  2836. domainname_free(obj->dest_addr_domainname);
  2837. obj->dest_addr_domainname = NULL;
  2838. }
  2839. void
  2840. socks5_client_request_free(socks5_client_request_t *obj)
  2841. {
  2842. if (obj == NULL)
  2843. return;
  2844. socks5_client_request_clear(obj);
  2845. trunnel_memwipe(obj, sizeof(socks5_client_request_t));
  2846. trunnel_free_(obj);
  2847. }
  2848. uint8_t
  2849. socks5_client_request_get_version(const socks5_client_request_t *inp)
  2850. {
  2851. return inp->version;
  2852. }
  2853. int
  2854. socks5_client_request_set_version(socks5_client_request_t *inp, uint8_t val)
  2855. {
  2856. if (! ((val == 5))) {
  2857. TRUNNEL_SET_ERROR_CODE(inp);
  2858. return -1;
  2859. }
  2860. inp->version = val;
  2861. return 0;
  2862. }
  2863. uint8_t
  2864. socks5_client_request_get_command(const socks5_client_request_t *inp)
  2865. {
  2866. return inp->command;
  2867. }
  2868. int
  2869. socks5_client_request_set_command(socks5_client_request_t *inp, uint8_t val)
  2870. {
  2871. if (! ((val == CMD_BIND || val == CMD_CONNECT || val == CMD_RESOLVE || val == CMD_RESOLVE_PTR || val == CMD_UDP_ASSOCIATE))) {
  2872. TRUNNEL_SET_ERROR_CODE(inp);
  2873. return -1;
  2874. }
  2875. inp->command = val;
  2876. return 0;
  2877. }
  2878. uint8_t
  2879. socks5_client_request_get_reserved(const socks5_client_request_t *inp)
  2880. {
  2881. return inp->reserved;
  2882. }
  2883. int
  2884. socks5_client_request_set_reserved(socks5_client_request_t *inp, uint8_t val)
  2885. {
  2886. if (! ((val == 0))) {
  2887. TRUNNEL_SET_ERROR_CODE(inp);
  2888. return -1;
  2889. }
  2890. inp->reserved = val;
  2891. return 0;
  2892. }
  2893. uint8_t
  2894. socks5_client_request_get_atype(const socks5_client_request_t *inp)
  2895. {
  2896. return inp->atype;
  2897. }
  2898. int
  2899. socks5_client_request_set_atype(socks5_client_request_t *inp, uint8_t val)
  2900. {
  2901. inp->atype = val;
  2902. return 0;
  2903. }
  2904. uint32_t
  2905. socks5_client_request_get_dest_addr_ipv4(const socks5_client_request_t *inp)
  2906. {
  2907. return inp->dest_addr_ipv4;
  2908. }
  2909. int
  2910. socks5_client_request_set_dest_addr_ipv4(socks5_client_request_t *inp, uint32_t val)
  2911. {
  2912. inp->dest_addr_ipv4 = val;
  2913. return 0;
  2914. }
  2915. size_t
  2916. socks5_client_request_getlen_dest_addr_ipv6(const socks5_client_request_t *inp)
  2917. {
  2918. (void)inp; return 16;
  2919. }
  2920. uint8_t
  2921. socks5_client_request_get_dest_addr_ipv6(socks5_client_request_t *inp, size_t idx)
  2922. {
  2923. trunnel_assert(idx < 16);
  2924. return inp->dest_addr_ipv6[idx];
  2925. }
  2926. uint8_t
  2927. socks5_client_request_getconst_dest_addr_ipv6(const socks5_client_request_t *inp, size_t idx)
  2928. {
  2929. return socks5_client_request_get_dest_addr_ipv6((socks5_client_request_t*)inp, idx);
  2930. }
  2931. int
  2932. socks5_client_request_set_dest_addr_ipv6(socks5_client_request_t *inp, size_t idx, uint8_t elt)
  2933. {
  2934. trunnel_assert(idx < 16);
  2935. inp->dest_addr_ipv6[idx] = elt;
  2936. return 0;
  2937. }
  2938. uint8_t *
  2939. socks5_client_request_getarray_dest_addr_ipv6(socks5_client_request_t *inp)
  2940. {
  2941. return inp->dest_addr_ipv6;
  2942. }
  2943. const uint8_t *
  2944. socks5_client_request_getconstarray_dest_addr_ipv6(const socks5_client_request_t *inp)
  2945. {
  2946. return (const uint8_t *)socks5_client_request_getarray_dest_addr_ipv6((socks5_client_request_t*)inp);
  2947. }
  2948. struct domainname_st *
  2949. socks5_client_request_get_dest_addr_domainname(socks5_client_request_t *inp)
  2950. {
  2951. return inp->dest_addr_domainname;
  2952. }
  2953. const struct domainname_st *
  2954. socks5_client_request_getconst_dest_addr_domainname(const socks5_client_request_t *inp)
  2955. {
  2956. return socks5_client_request_get_dest_addr_domainname((socks5_client_request_t*) inp);
  2957. }
  2958. int
  2959. socks5_client_request_set_dest_addr_domainname(socks5_client_request_t *inp, struct domainname_st *val)
  2960. {
  2961. if (inp->dest_addr_domainname && inp->dest_addr_domainname != val)
  2962. domainname_free(inp->dest_addr_domainname);
  2963. return socks5_client_request_set0_dest_addr_domainname(inp, val);
  2964. }
  2965. int
  2966. socks5_client_request_set0_dest_addr_domainname(socks5_client_request_t *inp, struct domainname_st *val)
  2967. {
  2968. inp->dest_addr_domainname = val;
  2969. return 0;
  2970. }
  2971. uint16_t
  2972. socks5_client_request_get_dest_port(const socks5_client_request_t *inp)
  2973. {
  2974. return inp->dest_port;
  2975. }
  2976. int
  2977. socks5_client_request_set_dest_port(socks5_client_request_t *inp, uint16_t val)
  2978. {
  2979. inp->dest_port = val;
  2980. return 0;
  2981. }
  2982. const char *
  2983. socks5_client_request_check(const socks5_client_request_t *obj)
  2984. {
  2985. if (obj == NULL)
  2986. return "Object was NULL";
  2987. if (obj->trunnel_error_code_)
  2988. return "A set function failed on this object";
  2989. if (! (obj->version == 5))
  2990. return "Integer out of bounds";
  2991. if (! (obj->command == CMD_BIND || obj->command == CMD_CONNECT || obj->command == CMD_RESOLVE || obj->command == CMD_RESOLVE_PTR || obj->command == CMD_UDP_ASSOCIATE))
  2992. return "Integer out of bounds";
  2993. if (! (obj->reserved == 0))
  2994. return "Integer out of bounds";
  2995. switch (obj->atype) {
  2996. case ATYPE_IPV4:
  2997. break;
  2998. case ATYPE_IPV6:
  2999. break;
  3000. case ATYPE_DOMAINNAME:
  3001. {
  3002. const char *msg;
  3003. if (NULL != (msg = domainname_check(obj->dest_addr_domainname)))
  3004. return msg;
  3005. }
  3006. break;
  3007. default:
  3008. return "Bad tag for union";
  3009. break;
  3010. }
  3011. return NULL;
  3012. }
  3013. ssize_t
  3014. socks5_client_request_encoded_len(const socks5_client_request_t *obj)
  3015. {
  3016. ssize_t result = 0;
  3017. if (NULL != socks5_client_request_check(obj))
  3018. return -1;
  3019. /* Length of u8 version IN [5] */
  3020. result += 1;
  3021. /* Length of u8 command IN [CMD_BIND, CMD_CONNECT, CMD_RESOLVE, CMD_RESOLVE_PTR, CMD_UDP_ASSOCIATE] */
  3022. result += 1;
  3023. /* Length of u8 reserved IN [0] */
  3024. result += 1;
  3025. /* Length of u8 atype */
  3026. result += 1;
  3027. switch (obj->atype) {
  3028. case ATYPE_IPV4:
  3029. /* Length of u32 dest_addr_ipv4 */
  3030. result += 4;
  3031. break;
  3032. case ATYPE_IPV6:
  3033. /* Length of u8 dest_addr_ipv6[16] */
  3034. result += 16;
  3035. break;
  3036. case ATYPE_DOMAINNAME:
  3037. /* Length of struct domainname dest_addr_domainname */
  3038. result += domainname_encoded_len(obj->dest_addr_domainname);
  3039. break;
  3040. default:
  3041. trunnel_assert(0);
  3042. break;
  3043. }
  3044. /* Length of u16 dest_port */
  3045. result += 2;
  3046. return result;
  3047. }
  3048. int
  3049. socks5_client_request_clear_errors(socks5_client_request_t *obj)
  3050. {
  3051. int r = obj->trunnel_error_code_;
  3052. obj->trunnel_error_code_ = 0;
  3053. return r;
  3054. }
  3055. ssize_t
  3056. socks5_client_request_encode(uint8_t *output, const size_t avail, const socks5_client_request_t *obj)
  3057. {
  3058. ssize_t result = 0;
  3059. size_t written = 0;
  3060. uint8_t *ptr = output;
  3061. const char *msg;
  3062. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  3063. const ssize_t encoded_len = socks5_client_request_encoded_len(obj);
  3064. #endif
  3065. if (NULL != (msg = socks5_client_request_check(obj)))
  3066. goto check_failed;
  3067. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  3068. trunnel_assert(encoded_len >= 0);
  3069. #endif
  3070. /* Encode u8 version IN [5] */
  3071. trunnel_assert(written <= avail);
  3072. if (avail - written < 1)
  3073. goto truncated;
  3074. trunnel_set_uint8(ptr, (obj->version));
  3075. written += 1; ptr += 1;
  3076. /* Encode u8 command IN [CMD_BIND, CMD_CONNECT, CMD_RESOLVE, CMD_RESOLVE_PTR, CMD_UDP_ASSOCIATE] */
  3077. trunnel_assert(written <= avail);
  3078. if (avail - written < 1)
  3079. goto truncated;
  3080. trunnel_set_uint8(ptr, (obj->command));
  3081. written += 1; ptr += 1;
  3082. /* Encode u8 reserved IN [0] */
  3083. trunnel_assert(written <= avail);
  3084. if (avail - written < 1)
  3085. goto truncated;
  3086. trunnel_set_uint8(ptr, (obj->reserved));
  3087. written += 1; ptr += 1;
  3088. /* Encode u8 atype */
  3089. trunnel_assert(written <= avail);
  3090. if (avail - written < 1)
  3091. goto truncated;
  3092. trunnel_set_uint8(ptr, (obj->atype));
  3093. written += 1; ptr += 1;
  3094. /* Encode union dest_addr[atype] */
  3095. trunnel_assert(written <= avail);
  3096. switch (obj->atype) {
  3097. case ATYPE_IPV4:
  3098. /* Encode u32 dest_addr_ipv4 */
  3099. trunnel_assert(written <= avail);
  3100. if (avail - written < 4)
  3101. goto truncated;
  3102. trunnel_set_uint32(ptr, trunnel_htonl(obj->dest_addr_ipv4));
  3103. written += 4; ptr += 4;
  3104. break;
  3105. case ATYPE_IPV6:
  3106. /* Encode u8 dest_addr_ipv6[16] */
  3107. trunnel_assert(written <= avail);
  3108. if (avail - written < 16)
  3109. goto truncated;
  3110. memcpy(ptr, obj->dest_addr_ipv6, 16);
  3111. written += 16; ptr += 16;
  3112. break;
  3113. case ATYPE_DOMAINNAME:
  3114. /* Encode struct domainname dest_addr_domainname */
  3115. trunnel_assert(written <= avail);
  3116. result = domainname_encode(ptr, avail - written, obj->dest_addr_domainname);
  3117. if (result < 0)
  3118. goto fail; /* XXXXXXX !*/
  3119. written += result; ptr += result;
  3120. break;
  3121. default:
  3122. trunnel_assert(0);
  3123. break;
  3124. }
  3125. /* Encode u16 dest_port */
  3126. trunnel_assert(written <= avail);
  3127. if (avail - written < 2)
  3128. goto truncated;
  3129. trunnel_set_uint16(ptr, trunnel_htons(obj->dest_port));
  3130. written += 2; ptr += 2;
  3131. trunnel_assert(ptr == output + written);
  3132. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  3133. {
  3134. trunnel_assert(encoded_len >= 0);
  3135. trunnel_assert((size_t)encoded_len == written);
  3136. }
  3137. #endif
  3138. return written;
  3139. truncated:
  3140. result = -2;
  3141. goto fail;
  3142. check_failed:
  3143. (void)msg;
  3144. result = -1;
  3145. goto fail;
  3146. fail:
  3147. trunnel_assert(result < 0);
  3148. return result;
  3149. }
  3150. /** As socks5_client_request_parse(), but do not allocate the output
  3151. * object.
  3152. */
  3153. static ssize_t
  3154. socks5_client_request_parse_into(socks5_client_request_t *obj, const uint8_t *input, const size_t len_in)
  3155. {
  3156. const uint8_t *ptr = input;
  3157. size_t remaining = len_in;
  3158. ssize_t result = 0;
  3159. (void)result;
  3160. /* Parse u8 version IN [5] */
  3161. CHECK_REMAINING(1, truncated);
  3162. obj->version = (trunnel_get_uint8(ptr));
  3163. remaining -= 1; ptr += 1;
  3164. if (! (obj->version == 5))
  3165. goto fail;
  3166. /* Parse u8 command IN [CMD_BIND, CMD_CONNECT, CMD_RESOLVE, CMD_RESOLVE_PTR, CMD_UDP_ASSOCIATE] */
  3167. CHECK_REMAINING(1, truncated);
  3168. obj->command = (trunnel_get_uint8(ptr));
  3169. remaining -= 1; ptr += 1;
  3170. if (! (obj->command == CMD_BIND || obj->command == CMD_CONNECT || obj->command == CMD_RESOLVE || obj->command == CMD_RESOLVE_PTR || obj->command == CMD_UDP_ASSOCIATE))
  3171. goto fail;
  3172. /* Parse u8 reserved IN [0] */
  3173. CHECK_REMAINING(1, truncated);
  3174. obj->reserved = (trunnel_get_uint8(ptr));
  3175. remaining -= 1; ptr += 1;
  3176. if (! (obj->reserved == 0))
  3177. goto fail;
  3178. /* Parse u8 atype */
  3179. CHECK_REMAINING(1, truncated);
  3180. obj->atype = (trunnel_get_uint8(ptr));
  3181. remaining -= 1; ptr += 1;
  3182. /* Parse union dest_addr[atype] */
  3183. switch (obj->atype) {
  3184. case ATYPE_IPV4:
  3185. /* Parse u32 dest_addr_ipv4 */
  3186. CHECK_REMAINING(4, truncated);
  3187. obj->dest_addr_ipv4 = trunnel_ntohl(trunnel_get_uint32(ptr));
  3188. remaining -= 4; ptr += 4;
  3189. break;
  3190. case ATYPE_IPV6:
  3191. /* Parse u8 dest_addr_ipv6[16] */
  3192. CHECK_REMAINING(16, truncated);
  3193. memcpy(obj->dest_addr_ipv6, ptr, 16);
  3194. remaining -= 16; ptr += 16;
  3195. break;
  3196. case ATYPE_DOMAINNAME:
  3197. /* Parse struct domainname dest_addr_domainname */
  3198. result = domainname_parse(&obj->dest_addr_domainname, ptr, remaining);
  3199. if (result < 0)
  3200. goto relay_fail;
  3201. trunnel_assert((size_t)result <= remaining);
  3202. remaining -= result; ptr += result;
  3203. break;
  3204. default:
  3205. goto fail;
  3206. break;
  3207. }
  3208. /* Parse u16 dest_port */
  3209. CHECK_REMAINING(2, truncated);
  3210. obj->dest_port = trunnel_ntohs(trunnel_get_uint16(ptr));
  3211. remaining -= 2; ptr += 2;
  3212. trunnel_assert(ptr + remaining == input + len_in);
  3213. return len_in - remaining;
  3214. truncated:
  3215. return -2;
  3216. relay_fail:
  3217. trunnel_assert(result < 0);
  3218. return result;
  3219. fail:
  3220. result = -1;
  3221. return result;
  3222. }
  3223. ssize_t
  3224. socks5_client_request_parse(socks5_client_request_t **output, const uint8_t *input, const size_t len_in)
  3225. {
  3226. ssize_t result;
  3227. *output = socks5_client_request_new();
  3228. if (NULL == *output)
  3229. return -1;
  3230. result = socks5_client_request_parse_into(*output, input, len_in);
  3231. if (result < 0) {
  3232. socks5_client_request_free(*output);
  3233. *output = NULL;
  3234. }
  3235. return result;
  3236. }
  3237. socks5_server_reply_t *
  3238. socks5_server_reply_new(void)
  3239. {
  3240. socks5_server_reply_t *val = trunnel_calloc(1, sizeof(socks5_server_reply_t));
  3241. if (NULL == val)
  3242. return NULL;
  3243. val->version = 5;
  3244. return val;
  3245. }
  3246. /** Release all storage held inside 'obj', but do not free 'obj'.
  3247. */
  3248. static void
  3249. socks5_server_reply_clear(socks5_server_reply_t *obj)
  3250. {
  3251. (void) obj;
  3252. domainname_free(obj->bind_addr_domainname);
  3253. obj->bind_addr_domainname = NULL;
  3254. }
  3255. void
  3256. socks5_server_reply_free(socks5_server_reply_t *obj)
  3257. {
  3258. if (obj == NULL)
  3259. return;
  3260. socks5_server_reply_clear(obj);
  3261. trunnel_memwipe(obj, sizeof(socks5_server_reply_t));
  3262. trunnel_free_(obj);
  3263. }
  3264. uint8_t
  3265. socks5_server_reply_get_version(const socks5_server_reply_t *inp)
  3266. {
  3267. return inp->version;
  3268. }
  3269. int
  3270. socks5_server_reply_set_version(socks5_server_reply_t *inp, uint8_t val)
  3271. {
  3272. if (! ((val == 5))) {
  3273. TRUNNEL_SET_ERROR_CODE(inp);
  3274. return -1;
  3275. }
  3276. inp->version = val;
  3277. return 0;
  3278. }
  3279. uint8_t
  3280. socks5_server_reply_get_reply(const socks5_server_reply_t *inp)
  3281. {
  3282. return inp->reply;
  3283. }
  3284. int
  3285. socks5_server_reply_set_reply(socks5_server_reply_t *inp, uint8_t val)
  3286. {
  3287. inp->reply = val;
  3288. return 0;
  3289. }
  3290. uint8_t
  3291. socks5_server_reply_get_reserved(const socks5_server_reply_t *inp)
  3292. {
  3293. return inp->reserved;
  3294. }
  3295. int
  3296. socks5_server_reply_set_reserved(socks5_server_reply_t *inp, uint8_t val)
  3297. {
  3298. if (! ((val == 0))) {
  3299. TRUNNEL_SET_ERROR_CODE(inp);
  3300. return -1;
  3301. }
  3302. inp->reserved = val;
  3303. return 0;
  3304. }
  3305. uint8_t
  3306. socks5_server_reply_get_atype(const socks5_server_reply_t *inp)
  3307. {
  3308. return inp->atype;
  3309. }
  3310. int
  3311. socks5_server_reply_set_atype(socks5_server_reply_t *inp, uint8_t val)
  3312. {
  3313. inp->atype = val;
  3314. return 0;
  3315. }
  3316. uint32_t
  3317. socks5_server_reply_get_bind_addr_ipv4(const socks5_server_reply_t *inp)
  3318. {
  3319. return inp->bind_addr_ipv4;
  3320. }
  3321. int
  3322. socks5_server_reply_set_bind_addr_ipv4(socks5_server_reply_t *inp, uint32_t val)
  3323. {
  3324. inp->bind_addr_ipv4 = val;
  3325. return 0;
  3326. }
  3327. size_t
  3328. socks5_server_reply_getlen_bind_addr_ipv6(const socks5_server_reply_t *inp)
  3329. {
  3330. (void)inp; return 16;
  3331. }
  3332. uint8_t
  3333. socks5_server_reply_get_bind_addr_ipv6(socks5_server_reply_t *inp, size_t idx)
  3334. {
  3335. trunnel_assert(idx < 16);
  3336. return inp->bind_addr_ipv6[idx];
  3337. }
  3338. uint8_t
  3339. socks5_server_reply_getconst_bind_addr_ipv6(const socks5_server_reply_t *inp, size_t idx)
  3340. {
  3341. return socks5_server_reply_get_bind_addr_ipv6((socks5_server_reply_t*)inp, idx);
  3342. }
  3343. int
  3344. socks5_server_reply_set_bind_addr_ipv6(socks5_server_reply_t *inp, size_t idx, uint8_t elt)
  3345. {
  3346. trunnel_assert(idx < 16);
  3347. inp->bind_addr_ipv6[idx] = elt;
  3348. return 0;
  3349. }
  3350. uint8_t *
  3351. socks5_server_reply_getarray_bind_addr_ipv6(socks5_server_reply_t *inp)
  3352. {
  3353. return inp->bind_addr_ipv6;
  3354. }
  3355. const uint8_t *
  3356. socks5_server_reply_getconstarray_bind_addr_ipv6(const socks5_server_reply_t *inp)
  3357. {
  3358. return (const uint8_t *)socks5_server_reply_getarray_bind_addr_ipv6((socks5_server_reply_t*)inp);
  3359. }
  3360. struct domainname_st *
  3361. socks5_server_reply_get_bind_addr_domainname(socks5_server_reply_t *inp)
  3362. {
  3363. return inp->bind_addr_domainname;
  3364. }
  3365. const struct domainname_st *
  3366. socks5_server_reply_getconst_bind_addr_domainname(const socks5_server_reply_t *inp)
  3367. {
  3368. return socks5_server_reply_get_bind_addr_domainname((socks5_server_reply_t*) inp);
  3369. }
  3370. int
  3371. socks5_server_reply_set_bind_addr_domainname(socks5_server_reply_t *inp, struct domainname_st *val)
  3372. {
  3373. if (inp->bind_addr_domainname && inp->bind_addr_domainname != val)
  3374. domainname_free(inp->bind_addr_domainname);
  3375. return socks5_server_reply_set0_bind_addr_domainname(inp, val);
  3376. }
  3377. int
  3378. socks5_server_reply_set0_bind_addr_domainname(socks5_server_reply_t *inp, struct domainname_st *val)
  3379. {
  3380. inp->bind_addr_domainname = val;
  3381. return 0;
  3382. }
  3383. uint16_t
  3384. socks5_server_reply_get_bind_port(const socks5_server_reply_t *inp)
  3385. {
  3386. return inp->bind_port;
  3387. }
  3388. int
  3389. socks5_server_reply_set_bind_port(socks5_server_reply_t *inp, uint16_t val)
  3390. {
  3391. inp->bind_port = val;
  3392. return 0;
  3393. }
  3394. const char *
  3395. socks5_server_reply_check(const socks5_server_reply_t *obj)
  3396. {
  3397. if (obj == NULL)
  3398. return "Object was NULL";
  3399. if (obj->trunnel_error_code_)
  3400. return "A set function failed on this object";
  3401. if (! (obj->version == 5))
  3402. return "Integer out of bounds";
  3403. if (! (obj->reserved == 0))
  3404. return "Integer out of bounds";
  3405. switch (obj->atype) {
  3406. case ATYPE_IPV4:
  3407. break;
  3408. case ATYPE_IPV6:
  3409. break;
  3410. case ATYPE_DOMAINNAME:
  3411. {
  3412. const char *msg;
  3413. if (NULL != (msg = domainname_check(obj->bind_addr_domainname)))
  3414. return msg;
  3415. }
  3416. break;
  3417. default:
  3418. return "Bad tag for union";
  3419. break;
  3420. }
  3421. return NULL;
  3422. }
  3423. ssize_t
  3424. socks5_server_reply_encoded_len(const socks5_server_reply_t *obj)
  3425. {
  3426. ssize_t result = 0;
  3427. if (NULL != socks5_server_reply_check(obj))
  3428. return -1;
  3429. /* Length of u8 version IN [5] */
  3430. result += 1;
  3431. /* Length of u8 reply */
  3432. result += 1;
  3433. /* Length of u8 reserved IN [0] */
  3434. result += 1;
  3435. /* Length of u8 atype */
  3436. result += 1;
  3437. switch (obj->atype) {
  3438. case ATYPE_IPV4:
  3439. /* Length of u32 bind_addr_ipv4 */
  3440. result += 4;
  3441. break;
  3442. case ATYPE_IPV6:
  3443. /* Length of u8 bind_addr_ipv6[16] */
  3444. result += 16;
  3445. break;
  3446. case ATYPE_DOMAINNAME:
  3447. /* Length of struct domainname bind_addr_domainname */
  3448. result += domainname_encoded_len(obj->bind_addr_domainname);
  3449. break;
  3450. default:
  3451. trunnel_assert(0);
  3452. break;
  3453. }
  3454. /* Length of u16 bind_port */
  3455. result += 2;
  3456. return result;
  3457. }
  3458. int
  3459. socks5_server_reply_clear_errors(socks5_server_reply_t *obj)
  3460. {
  3461. int r = obj->trunnel_error_code_;
  3462. obj->trunnel_error_code_ = 0;
  3463. return r;
  3464. }
  3465. ssize_t
  3466. socks5_server_reply_encode(uint8_t *output, const size_t avail, const socks5_server_reply_t *obj)
  3467. {
  3468. ssize_t result = 0;
  3469. size_t written = 0;
  3470. uint8_t *ptr = output;
  3471. const char *msg;
  3472. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  3473. const ssize_t encoded_len = socks5_server_reply_encoded_len(obj);
  3474. #endif
  3475. if (NULL != (msg = socks5_server_reply_check(obj)))
  3476. goto check_failed;
  3477. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  3478. trunnel_assert(encoded_len >= 0);
  3479. #endif
  3480. /* Encode u8 version IN [5] */
  3481. trunnel_assert(written <= avail);
  3482. if (avail - written < 1)
  3483. goto truncated;
  3484. trunnel_set_uint8(ptr, (obj->version));
  3485. written += 1; ptr += 1;
  3486. /* Encode u8 reply */
  3487. trunnel_assert(written <= avail);
  3488. if (avail - written < 1)
  3489. goto truncated;
  3490. trunnel_set_uint8(ptr, (obj->reply));
  3491. written += 1; ptr += 1;
  3492. /* Encode u8 reserved IN [0] */
  3493. trunnel_assert(written <= avail);
  3494. if (avail - written < 1)
  3495. goto truncated;
  3496. trunnel_set_uint8(ptr, (obj->reserved));
  3497. written += 1; ptr += 1;
  3498. /* Encode u8 atype */
  3499. trunnel_assert(written <= avail);
  3500. if (avail - written < 1)
  3501. goto truncated;
  3502. trunnel_set_uint8(ptr, (obj->atype));
  3503. written += 1; ptr += 1;
  3504. /* Encode union bind_addr[atype] */
  3505. trunnel_assert(written <= avail);
  3506. switch (obj->atype) {
  3507. case ATYPE_IPV4:
  3508. /* Encode u32 bind_addr_ipv4 */
  3509. trunnel_assert(written <= avail);
  3510. if (avail - written < 4)
  3511. goto truncated;
  3512. trunnel_set_uint32(ptr, trunnel_htonl(obj->bind_addr_ipv4));
  3513. written += 4; ptr += 4;
  3514. break;
  3515. case ATYPE_IPV6:
  3516. /* Encode u8 bind_addr_ipv6[16] */
  3517. trunnel_assert(written <= avail);
  3518. if (avail - written < 16)
  3519. goto truncated;
  3520. memcpy(ptr, obj->bind_addr_ipv6, 16);
  3521. written += 16; ptr += 16;
  3522. break;
  3523. case ATYPE_DOMAINNAME:
  3524. /* Encode struct domainname bind_addr_domainname */
  3525. trunnel_assert(written <= avail);
  3526. result = domainname_encode(ptr, avail - written, obj->bind_addr_domainname);
  3527. if (result < 0)
  3528. goto fail; /* XXXXXXX !*/
  3529. written += result; ptr += result;
  3530. break;
  3531. default:
  3532. trunnel_assert(0);
  3533. break;
  3534. }
  3535. /* Encode u16 bind_port */
  3536. trunnel_assert(written <= avail);
  3537. if (avail - written < 2)
  3538. goto truncated;
  3539. trunnel_set_uint16(ptr, trunnel_htons(obj->bind_port));
  3540. written += 2; ptr += 2;
  3541. trunnel_assert(ptr == output + written);
  3542. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  3543. {
  3544. trunnel_assert(encoded_len >= 0);
  3545. trunnel_assert((size_t)encoded_len == written);
  3546. }
  3547. #endif
  3548. return written;
  3549. truncated:
  3550. result = -2;
  3551. goto fail;
  3552. check_failed:
  3553. (void)msg;
  3554. result = -1;
  3555. goto fail;
  3556. fail:
  3557. trunnel_assert(result < 0);
  3558. return result;
  3559. }
  3560. /** As socks5_server_reply_parse(), but do not allocate the output
  3561. * object.
  3562. */
  3563. static ssize_t
  3564. socks5_server_reply_parse_into(socks5_server_reply_t *obj, const uint8_t *input, const size_t len_in)
  3565. {
  3566. const uint8_t *ptr = input;
  3567. size_t remaining = len_in;
  3568. ssize_t result = 0;
  3569. (void)result;
  3570. /* Parse u8 version IN [5] */
  3571. CHECK_REMAINING(1, truncated);
  3572. obj->version = (trunnel_get_uint8(ptr));
  3573. remaining -= 1; ptr += 1;
  3574. if (! (obj->version == 5))
  3575. goto fail;
  3576. /* Parse u8 reply */
  3577. CHECK_REMAINING(1, truncated);
  3578. obj->reply = (trunnel_get_uint8(ptr));
  3579. remaining -= 1; ptr += 1;
  3580. /* Parse u8 reserved IN [0] */
  3581. CHECK_REMAINING(1, truncated);
  3582. obj->reserved = (trunnel_get_uint8(ptr));
  3583. remaining -= 1; ptr += 1;
  3584. if (! (obj->reserved == 0))
  3585. goto fail;
  3586. /* Parse u8 atype */
  3587. CHECK_REMAINING(1, truncated);
  3588. obj->atype = (trunnel_get_uint8(ptr));
  3589. remaining -= 1; ptr += 1;
  3590. /* Parse union bind_addr[atype] */
  3591. switch (obj->atype) {
  3592. case ATYPE_IPV4:
  3593. /* Parse u32 bind_addr_ipv4 */
  3594. CHECK_REMAINING(4, truncated);
  3595. obj->bind_addr_ipv4 = trunnel_ntohl(trunnel_get_uint32(ptr));
  3596. remaining -= 4; ptr += 4;
  3597. break;
  3598. case ATYPE_IPV6:
  3599. /* Parse u8 bind_addr_ipv6[16] */
  3600. CHECK_REMAINING(16, truncated);
  3601. memcpy(obj->bind_addr_ipv6, ptr, 16);
  3602. remaining -= 16; ptr += 16;
  3603. break;
  3604. case ATYPE_DOMAINNAME:
  3605. /* Parse struct domainname bind_addr_domainname */
  3606. result = domainname_parse(&obj->bind_addr_domainname, ptr, remaining);
  3607. if (result < 0)
  3608. goto relay_fail;
  3609. trunnel_assert((size_t)result <= remaining);
  3610. remaining -= result; ptr += result;
  3611. break;
  3612. default:
  3613. goto fail;
  3614. break;
  3615. }
  3616. /* Parse u16 bind_port */
  3617. CHECK_REMAINING(2, truncated);
  3618. obj->bind_port = trunnel_ntohs(trunnel_get_uint16(ptr));
  3619. remaining -= 2; ptr += 2;
  3620. trunnel_assert(ptr + remaining == input + len_in);
  3621. return len_in - remaining;
  3622. truncated:
  3623. return -2;
  3624. relay_fail:
  3625. trunnel_assert(result < 0);
  3626. return result;
  3627. fail:
  3628. result = -1;
  3629. return result;
  3630. }
  3631. ssize_t
  3632. socks5_server_reply_parse(socks5_server_reply_t **output, const uint8_t *input, const size_t len_in)
  3633. {
  3634. ssize_t result;
  3635. *output = socks5_server_reply_new();
  3636. if (NULL == *output)
  3637. return -1;
  3638. result = socks5_server_reply_parse_into(*output, input, len_in);
  3639. if (result < 0) {
  3640. socks5_server_reply_free(*output);
  3641. *output = NULL;
  3642. }
  3643. return result;
  3644. }