2
0

500-add-pptp-plugin.patch 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065
  1. --- a/configure
  2. +++ b/configure
  3. @@ -195,7 +195,7 @@ if [ -d "$ksrc" ]; then
  4. mkmkf $ksrc/Makedefs$compiletype Makedefs.com
  5. for dir in pppd pppstats chat pppdump pppd/plugins pppd/plugins/rp-pppoe \
  6. pppd/plugins/radius pppd/plugins/pppoatm \
  7. - pppd/plugins/pppol2tp; do
  8. + pppd/plugins/pppol2tp pppd/plugins/pptp ; do
  9. mkmkf $dir/Makefile.$makext $dir/Makefile
  10. done
  11. if [ -f $ksrc/Makefile.$makext$archvariant ]; then
  12. --- a/pppd/plugins/Makefile.linux
  13. +++ b/pppd/plugins/Makefile.linux
  14. @@ -9,7 +9,7 @@ BINDIR = $(DESTDIR)/sbin
  15. MANDIR = $(DESTDIR)/share/man/man8
  16. LIBDIR = $(DESTDIR)/lib/pppd/$(VERSION)
  17. -SUBDIRS := rp-pppoe pppoatm pppol2tp
  18. +SUBDIRS := rp-pppoe pppoatm pppol2tp pptp
  19. # Uncomment the next line to include the radius authentication plugin
  20. SUBDIRS += radius
  21. PLUGINS := minconn.so passprompt.so passwordfd.so winbind.so
  22. --- /dev/null
  23. +++ b/pppd/plugins/pptp/Makefile.linux
  24. @@ -0,0 +1,31 @@
  25. +#
  26. +# This program may be distributed according to the terms of the GNU
  27. +# General Public License, version 2 or (at your option) any later version.
  28. +#
  29. +# $Id: Makefile.linux,v 1.9 2012/05/04 21:48:00 dgolle Exp $
  30. +#***********************************************************************
  31. +
  32. +DESTDIR = $(INSTROOT)@DESTDIR@
  33. +LIBDIR = $(DESTDIR)/lib/pppd/$(PPPDVERSION)
  34. +
  35. +PPPDVERSION = $(shell awk -F '"' '/VERSION/ { print $$2; }' ../../patchlevel.h)
  36. +
  37. +INSTALL = install
  38. +
  39. +COPTS=-O2 -g
  40. +CFLAGS = $(COPTS) -I. -I../.. -I../../../include -fPIC -DPPPD_VERSION=\"$(PPPDVERSION)\"
  41. +all: pptp.so
  42. +
  43. +%.o: %.c
  44. + $(CC) $(CFLAGS) -c -o $@ $<
  45. +
  46. +pptp.so: dirutil.o orckit_quirks.o pptp.o pptp_callmgr.o pptp_ctrl.o pptp_quirks.o util.o vector.o
  47. + $(CC) -o pptp.so -shared dirutil.o orckit_quirks.o pptp.o pptp_callmgr.o pptp_ctrl.o pptp_quirks.o util.o vector.o
  48. +
  49. +install: all
  50. + $(INSTALL) -d -m 755 $(LIBDIR)
  51. + $(INSTALL) -c -m 4550 pptp.so $(LIBDIR)
  52. +
  53. +clean:
  54. + rm -f *.o *.so
  55. +
  56. --- /dev/null
  57. +++ b/pppd/plugins/pptp/dirutil.c
  58. @@ -0,0 +1,68 @@
  59. +/* dirutil.c ... directory utilities.
  60. + * C. Scott Ananian <cananian@alumni.princeton.edu>
  61. + *
  62. + * $Id: dirutil.c,v 1.2 2003/06/17 17:25:47 reink Exp $
  63. + */
  64. +
  65. +#include <sys/stat.h>
  66. +#include <sys/types.h>
  67. +#include <unistd.h>
  68. +#include <string.h>
  69. +#include <stdlib.h>
  70. +#include "dirutil.h"
  71. +
  72. +/* Returned malloc'ed string representing basename */
  73. +char *basenamex(char *pathname)
  74. +{
  75. + char *dup = strdup(pathname);
  76. + char *ptr = strrchr(stripslash(dup), '/');
  77. + if (ptr == NULL) return dup;
  78. + ptr = strdup(ptr+1);
  79. + free(dup);
  80. + return ptr;
  81. +}
  82. +
  83. +/* Return malloc'ed string representing directory name (no trailing slash) */
  84. +char *dirnamex(char *pathname)
  85. +{
  86. + char *dup = strdup(pathname);
  87. + char *ptr = strrchr(stripslash(dup), '/');
  88. + if (ptr == NULL) { free(dup); return strdup("."); }
  89. + if (ptr == dup && dup[0] == '/') ptr++;
  90. + *ptr = '\0';
  91. + return dup;
  92. +}
  93. +
  94. +/* In-place modify a string to remove trailing slashes. Returns arg.
  95. + * stripslash("/") returns "/";
  96. + */
  97. +char *stripslash(char *pathname) {
  98. + int len = strlen(pathname);
  99. + while (len > 1 && pathname[len - 1] == '/')
  100. + pathname[--len] = '\0';
  101. + return pathname;
  102. +}
  103. +
  104. +/* ensure dirname exists, creating it if necessary. */
  105. +int make_valid_path(char *dir, mode_t mode)
  106. +{
  107. + struct stat st;
  108. + char *tmp = NULL, *path = stripslash(strdup(dir));
  109. + int retval;
  110. + if (stat(path, &st) == 0) { /* file exists */
  111. + if (S_ISDIR(st.st_mode)) { retval = 1; goto end; }
  112. + else { retval = 0; goto end; } /* not a directory. Oops. */
  113. + }
  114. + /* Directory doesn't exist. Let's make it. */
  115. + /* Make parent first. */
  116. + if (!make_valid_path(tmp = dirnamex(path), mode)) { retval = 0; goto end; }
  117. + /* Now make this 'un. */
  118. + if (mkdir(path, mode) < 0) { retval = 0; goto end; }
  119. + /* Success. */
  120. + retval = 1;
  121. +
  122. +end:
  123. + if (tmp != NULL) free(tmp);
  124. + if (path != NULL) free(path);
  125. + return retval;
  126. +}
  127. --- /dev/null
  128. +++ b/pppd/plugins/pptp/dirutil.h
  129. @@ -0,0 +1,14 @@
  130. +/* dirutil.h ... directory utilities.
  131. + * C. Scott Ananian <cananian@alumni.princeton.edu>
  132. + *
  133. + * $Id: dirutil.h,v 1.1.1.1 2000/12/23 08:19:51 scott Exp $
  134. + */
  135. +
  136. +/* Returned malloc'ed string representing basename */
  137. +char *basenamex(char *pathname);
  138. +/* Return malloc'ed string representing directory name (no trailing slash) */
  139. +char *dirnamex(char *pathname);
  140. +/* In-place modify a string to remove trailing slashes. Returns arg. */
  141. +char *stripslash(char *pathname);
  142. +/* ensure dirname exists, creating it if necessary. */
  143. +int make_valid_path(char *dirname, mode_t mode);
  144. --- /dev/null
  145. +++ b/pppd/plugins/pptp/orckit_quirks.c
  146. @@ -0,0 +1,86 @@
  147. +/* orckit_quirks.c ...... fix quirks in orckit adsl modems
  148. + * mulix <mulix@actcom.co.il>
  149. + *
  150. + * $Id: orckit_quirks.c,v 1.3 2002/03/01 01:23:36 quozl Exp $
  151. + */
  152. +
  153. +#include <string.h>
  154. +#include <sys/types.h>
  155. +#include <netinet/in.h>
  156. +#include "pptp_msg.h"
  157. +#include "pptp_options.h"
  158. +#include "pptp_ctrl.h"
  159. +#include "util.h"
  160. +
  161. +
  162. +
  163. +/* return 0 on success, non zero otherwise */
  164. +int
  165. +orckit_atur3_build_hook(struct pptp_out_call_rqst* packet)
  166. +{
  167. + unsigned int name_length = 10;
  168. +
  169. + struct pptp_out_call_rqst fixed_packet = {
  170. + PPTP_HEADER_CTRL(PPTP_OUT_CALL_RQST),
  171. + 0, /* hton16(call->callid) */
  172. + 0, /* hton16(call->sernum) */
  173. + hton32(PPTP_BPS_MIN), hton32(PPTP_BPS_MAX),
  174. + hton32(PPTP_BEARER_DIGITAL), hton32(PPTP_FRAME_ANY),
  175. + hton16(PPTP_WINDOW), 0, hton16(name_length), 0,
  176. + {'R','E','L','A','Y','_','P','P','P','1',0}, {0}
  177. + };
  178. +
  179. + if (!packet)
  180. + return -1;
  181. +
  182. + memcpy(packet, &fixed_packet, sizeof(*packet));
  183. +
  184. + return 0;
  185. +}
  186. +
  187. +/* return 0 on success, non zero otherwise */
  188. +int
  189. +orckit_atur3_set_link_hook(struct pptp_set_link_info* packet,
  190. + int peer_call_id)
  191. +{
  192. + struct pptp_set_link_info fixed_packet = {
  193. + PPTP_HEADER_CTRL(PPTP_SET_LINK_INFO),
  194. + hton16(peer_call_id),
  195. + 0,
  196. + 0xffffffff,
  197. + 0xffffffff};
  198. +
  199. + if (!packet)
  200. + return -1;
  201. +
  202. + memcpy(packet, &fixed_packet, sizeof(*packet));
  203. + return 0;
  204. +}
  205. +
  206. +/* return 0 on success, non 0 otherwise */
  207. +int
  208. +orckit_atur3_start_ctrl_conn_hook(struct pptp_start_ctrl_conn* packet)
  209. +{
  210. + struct pptp_start_ctrl_conn fixed_packet = {
  211. + {0}, /* we'll set the header later */
  212. + hton16(PPTP_VERSION), 0, 0,
  213. + hton32(PPTP_FRAME_ASYNC), hton32(PPTP_BEARER_ANALOG),
  214. + hton16(0) /* max channels */,
  215. + hton16(0x6021),
  216. + {'R','E','L','A','Y','_','P','P','P','1',0}, /* hostname */
  217. + {'M','S',' ','W','i','n',' ','N','T',0} /* vendor */
  218. + };
  219. +
  220. + if (!packet)
  221. + return -1;
  222. +
  223. + /* grab the header from the original packet, since we dont
  224. + know if this is a request or a reply */
  225. + memcpy(&fixed_packet.header, &packet->header, sizeof(struct pptp_header));
  226. +
  227. + /* and now overwrite the full packet, effectively preserving the header */
  228. + memcpy(packet, &fixed_packet, sizeof(*packet));
  229. + return 0;
  230. +}
  231. +
  232. +
  233. --- /dev/null
  234. +++ b/pppd/plugins/pptp/orckit_quirks.h
  235. @@ -0,0 +1,27 @@
  236. +/* orckit_quirks.h ...... fix quirks in orckit adsl modems
  237. + * mulix <mulix@actcom.co.il>
  238. + *
  239. + * $Id: orckit_quirks.h,v 1.2 2001/11/23 03:42:51 quozl Exp $
  240. + */
  241. +
  242. +#ifndef INC_ORCKIT_QUIRKS_H_
  243. +#define INC_ORCKIT_QUIRKS_H_
  244. +
  245. +#include "pptp_options.h"
  246. +#include "pptp_ctrl.h"
  247. +#include "pptp_msg.h"
  248. +
  249. +/* return 0 on success, non zero otherwise */
  250. +int
  251. +orckit_atur3_build_hook(struct pptp_out_call_rqst* packt);
  252. +
  253. +/* return 0 on success, non zero otherwise */
  254. +int
  255. +orckit_atur3_set_link_hook(struct pptp_set_link_info* packet,
  256. + int peer_call_id);
  257. +
  258. +/* return 0 on success, non zero otherwise */
  259. +int
  260. +orckit_atur3_start_ctrl_conn_hook(struct pptp_start_ctrl_conn* packet);
  261. +
  262. +#endif /* INC_ORCKIT_QUIRKS_H_ */
  263. --- /dev/null
  264. +++ b/pppd/plugins/pptp/pppd-pptp.8
  265. @@ -0,0 +1,68 @@
  266. +.\" manual page [] for PPTP plugin for pppd 2.4
  267. +.\" $Id: pppd-pptp.8,v 1.0 2007/10/17 13:27:17 kad Exp $
  268. +.\" SH section heading
  269. +.\" SS subsection heading
  270. +.\" LP paragraph
  271. +.\" IP indented paragraph
  272. +.\" TP hanging label
  273. +.TH PPPD-PPTP 8
  274. +.SH NAME
  275. +pptp.so \- PPTP VPN plugin for
  276. +.BR pppd (8)
  277. +.SH SYNOPSIS
  278. +.B pppd
  279. +[
  280. +.I options
  281. +]
  282. +plugin pptp.so
  283. +.SH DESCRIPTION
  284. +.LP
  285. +The PPTP plugin for pppd performs interaction with pptp kernel module
  286. +and has built-in call manager (client part of PPTP).
  287. +It pasees necessary paremeters from \fIoptions\fR into kernel module
  288. +to configure ppp-pptp channel. If it runs in client mode, then additionally
  289. +call manager starts up. PPTPD daemon automaticaly invokes this plugin
  290. +in server mode and passes necessary options, so additional configuration
  291. +is not needed.
  292. +
  293. +.SH OPTIONS for client mode
  294. +The PPTP plugin introduces one additional pppd option:
  295. +.TP
  296. +.BI "pptp_server " server " (required)"
  297. +Specifies ip address or hostname of pptp server.
  298. +.TP
  299. +.BI "pptp_window " packets " (optional)"
  300. +The amount of sliding window size.
  301. +Set to 0 to turn off sliding window.
  302. + to 3-10 for low speed connections.
  303. + to >10 for hi speed connections.
  304. +Default is 50
  305. +.TP
  306. +.BI "pptp_phone " phone " (optional)"
  307. +The phone string that sended to pptp server.
  308. +.SH USAGE
  309. +Sample configuration file:
  310. +.nf
  311. +plugin "pptp.so"
  312. +pptp_server 192.168.0.1
  313. +pptp_window 100
  314. +name myname
  315. +remotename pptp
  316. +noauth
  317. +refuse-eap
  318. +refuse-chap
  319. +refuse-mschap
  320. +nobsdcomp
  321. +nodeflate
  322. +novj
  323. +novjccomp
  324. +require-mppe-128
  325. +lcp-echo-interval 20
  326. +lcp-echo-failure 3
  327. +.fi
  328. +
  329. +.SH SEE ALSO
  330. +.BR pppd (8) " " pptpd (8) " " pptpd.conf (5)
  331. +
  332. +.SH AUTHOR
  333. +xeb xeb@mail.ru
  334. --- /dev/null
  335. +++ b/pppd/plugins/pptp/pptp.c
  336. @@ -0,0 +1,323 @@
  337. +/***************************************************************************
  338. + * Copyright (C) 2006 by Kozlov D. <xeb@mail.ru> *
  339. + * some cleanup done (C) 2012 by Daniel Golle <dgolle@allnet.de> *
  340. + * *
  341. + * This program is free software; you can redistribute it and/or modify *
  342. + * it under the terms of the GNU General Public License as published by *
  343. + * the Free Software Foundation; either version 2 of the License, or *
  344. + * (at your option) any later version. *
  345. + * *
  346. + * This program is distributed in the hope that it will be useful, *
  347. + * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  348. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  349. + * GNU General Public License for more details. *
  350. + * *
  351. + * You should have received a copy of the GNU General Public License *
  352. + * along with this program; if not, write to the *
  353. + * Free Software Foundation, Inc., *
  354. + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  355. + ***************************************************************************/
  356. +
  357. +#define PPTP_VERSION "1.00"
  358. +
  359. +#ifdef HAVE_CONFIG_H
  360. +#include <config.h>
  361. +#endif
  362. +
  363. +#include <netinet/in.h>
  364. +#include <arpa/inet.h>
  365. +#include <sys/un.h>
  366. +#include <netdb.h>
  367. +#include <stdio.h>
  368. +#include <string.h>
  369. +#include <stdlib.h>
  370. +#include <syslog.h>
  371. +#include <unistd.h>
  372. +#include <signal.h>
  373. +#include <errno.h>
  374. +#include <fcntl.h>
  375. +#include <sys/wait.h>
  376. +#include <sys/ioctl.h>
  377. +
  378. +#include "pppd.h"
  379. +#include "fsm.h"
  380. +#include "lcp.h"
  381. +#include "ipcp.h"
  382. +#include "ccp.h"
  383. +#include "pathnames.h"
  384. +
  385. +#include "pptp_callmgr.h"
  386. +#include <net/if.h>
  387. +#include <net/ethernet.h>
  388. +#include <linux/if_pppox.h>
  389. +
  390. +#include <stdio.h>
  391. +#include <stdlib.h>
  392. +
  393. +
  394. +
  395. +extern char** environ;
  396. +
  397. +char pppd_version[] = PPPD_VERSION;
  398. +extern int new_style_driver;
  399. +
  400. +
  401. +char *pptp_server = NULL;
  402. +char *pptp_client = NULL;
  403. +char *pptp_phone = NULL;
  404. +int pptp_window=50;
  405. +int pptp_sock=-1;
  406. +struct in_addr localbind = { INADDR_NONE };
  407. +
  408. +static int callmgr_sock;
  409. +static int pptp_fd;
  410. +int call_ID;
  411. +
  412. +static int open_callmgr(int call_id,struct in_addr inetaddr, char *phonenr,int window);
  413. +static void launch_callmgr(int call_is,struct in_addr inetaddr, char *phonenr,int window);
  414. +static int get_call_id(int sock, pid_t gre, pid_t pppd, u_int16_t *peer_call_id);
  415. +
  416. +static option_t Options[] =
  417. +{
  418. + { "pptp_server", o_string, &pptp_server,
  419. + "PPTP Server" },
  420. + { "pptp_client", o_string, &pptp_client,
  421. + "PPTP Client" },
  422. + { "pptp_sock",o_int, &pptp_sock,
  423. + "PPTP socket" },
  424. + { "pptp_phone", o_string, &pptp_phone,
  425. + "PPTP Phone number" },
  426. + { "pptp_window",o_int, &pptp_window,
  427. + "PPTP window" },
  428. + { NULL }
  429. +};
  430. +
  431. +static int pptp_connect(void);
  432. +static void pptp_disconnect(void);
  433. +
  434. +struct channel pptp_channel = {
  435. + options: Options,
  436. + check_options: NULL,
  437. + connect: &pptp_connect,
  438. + disconnect: &pptp_disconnect,
  439. + establish_ppp: &generic_establish_ppp,
  440. + disestablish_ppp: &generic_disestablish_ppp,
  441. + close: NULL,
  442. + cleanup: NULL
  443. +};
  444. +
  445. +static int pptp_start_server(void)
  446. +{
  447. + pptp_fd=pptp_sock;
  448. + sprintf(ppp_devnam,"pptp (%s)",pptp_client);
  449. +
  450. + return pptp_fd;
  451. +}
  452. +static int pptp_start_client(void)
  453. +{
  454. + socklen_t len;
  455. + struct sockaddr_pppox src_addr,dst_addr;
  456. + struct hostent *hostinfo;
  457. +
  458. + hostinfo=gethostbyname(pptp_server);
  459. + if (!hostinfo)
  460. + {
  461. + error("PPTP: Unknown host %s\n", pptp_server);
  462. + return -1;
  463. + }
  464. + dst_addr.sa_addr.pptp.sin_addr=*(struct in_addr*)hostinfo->h_addr;
  465. + {
  466. + int sock;
  467. + struct sockaddr_in addr;
  468. + len=sizeof(addr);
  469. + addr.sin_addr=dst_addr.sa_addr.pptp.sin_addr;
  470. + addr.sin_family=AF_INET;
  471. + addr.sin_port=htons(1700);
  472. + sock=socket(AF_INET,SOCK_DGRAM,0);
  473. + if (connect(sock,(struct sockaddr*)&addr,sizeof(addr)))
  474. + {
  475. + close(sock);
  476. + error("PPTP: connect failed (%s)\n",strerror(errno));
  477. + return -1;
  478. + }
  479. + getsockname(sock,(struct sockaddr*)&addr,&len);
  480. + src_addr.sa_addr.pptp.sin_addr=addr.sin_addr;
  481. + close(sock);
  482. + }
  483. +
  484. + src_addr.sa_family=AF_PPPOX;
  485. + src_addr.sa_protocol=PX_PROTO_PPTP;
  486. + src_addr.sa_addr.pptp.call_id=0;
  487. +
  488. + dst_addr.sa_family=AF_PPPOX;
  489. + dst_addr.sa_protocol=PX_PROTO_PPTP;
  490. + dst_addr.sa_addr.pptp.call_id=0;
  491. +
  492. + pptp_fd=socket(AF_PPPOX,SOCK_STREAM,PX_PROTO_PPTP);
  493. + if (pptp_fd<0)
  494. + {
  495. + error("PPTP: failed to create PPTP socket (%s)\n",strerror(errno));
  496. + return -1;
  497. + }
  498. + if (bind(pptp_fd,(struct sockaddr*)&src_addr,sizeof(src_addr)))
  499. + {
  500. + close(pptp_fd);
  501. + error("PPTP: failed to bind PPTP socket (%s)\n",strerror(errno));
  502. + return -1;
  503. + }
  504. + len=sizeof(src_addr);
  505. + getsockname(pptp_fd,(struct sockaddr*)&src_addr,&len);
  506. + call_ID=src_addr.sa_addr.pptp.call_id;
  507. +
  508. + do {
  509. + /*
  510. + * Open connection to call manager (Launch call manager if necessary.)
  511. + */
  512. + callmgr_sock = open_callmgr(src_addr.sa_addr.pptp.call_id,dst_addr.sa_addr.pptp.sin_addr, pptp_phone, pptp_window);
  513. + if (callmgr_sock<0)
  514. + {
  515. + close(pptp_fd);
  516. + return -1;
  517. + }
  518. + /* Exchange PIDs, get call ID */
  519. + } while (get_call_id(callmgr_sock, getpid(), getpid(), &dst_addr.sa_addr.pptp.call_id) < 0);
  520. +
  521. + if (connect(pptp_fd,(struct sockaddr*)&dst_addr,sizeof(dst_addr)))
  522. + {
  523. + close(callmgr_sock);
  524. + close(pptp_fd);
  525. + error("PPTP: failed to connect PPTP socket (%s)\n",strerror(errno));
  526. + return -1;
  527. + }
  528. +
  529. + sprintf(ppp_devnam,"pptp (%s)",pptp_server);
  530. +
  531. + return pptp_fd;
  532. +}
  533. +static int pptp_connect(void)
  534. +{
  535. + if ((!pptp_server && !pptp_client) || (pptp_server && pptp_client))
  536. + {
  537. + fatal("PPTP: unknown mode (you must specify pptp_server or pptp_client option)");
  538. + return -1;
  539. + }
  540. +
  541. + if (pptp_server) return pptp_start_client();
  542. + return pptp_start_server();
  543. +}
  544. +
  545. +static void pptp_disconnect(void)
  546. +{
  547. + if (pptp_server) close(callmgr_sock);
  548. + close(pptp_fd);
  549. +}
  550. +
  551. +static int open_callmgr(int call_id,struct in_addr inetaddr, char *phonenr,int window)
  552. +{
  553. + /* Try to open unix domain socket to call manager. */
  554. + struct sockaddr_un where;
  555. + const int NUM_TRIES = 3;
  556. + int i, fd;
  557. + pid_t pid;
  558. + int status;
  559. + /* Open socket */
  560. + if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
  561. + {
  562. + fatal("Could not create unix domain socket: %s", strerror(errno));
  563. + }
  564. + /* Make address */
  565. + callmgr_name_unixsock(&where, inetaddr, localbind);
  566. + for (i = 0; i < NUM_TRIES; i++)
  567. + {
  568. + if (connect(fd, (struct sockaddr *) &where, sizeof(where)) < 0)
  569. + {
  570. + /* couldn't connect. We'll have to launch this guy. */
  571. +
  572. + unlink (where.sun_path);
  573. +
  574. + /* fork and launch call manager process */
  575. + switch (pid = fork())
  576. + {
  577. + case -1: /* failure */
  578. + fatal("fork() to launch call manager failed.");
  579. + case 0: /* child */
  580. + {
  581. + /* close the pty and gre in the call manager */
  582. + close(fd);
  583. + close(pptp_fd);
  584. + launch_callmgr(call_id,inetaddr,phonenr,window);
  585. + }
  586. + default: /* parent */
  587. + waitpid(pid, &status, 0);
  588. + if (status!= 0)
  589. + {
  590. + close(fd);
  591. + error("Call manager exited with error %d", status);
  592. + return -1;
  593. + }
  594. + break;
  595. + }
  596. + sleep(1);
  597. + }
  598. + else return fd;
  599. + }
  600. + close(fd);
  601. + error("Could not launch call manager after %d tries.", i);
  602. + return -1; /* make gcc happy */
  603. +}
  604. +
  605. +/*** call the call manager main ***********************************************/
  606. +static void launch_callmgr(int call_id,struct in_addr inetaddr, char *phonenr,int window)
  607. +{
  608. + dbglog("pptp: call manager for %s\n", inet_ntoa(inetaddr));
  609. + dbglog("window size:\t%d\n",window);
  610. + if (phonenr) dbglog("phone number:\t'%s'\n",phonenr);
  611. + dbglog("call id:\t%d\n",call_id);
  612. + exit(callmgr_main(inetaddr, phonenr, window, call_id));
  613. +}
  614. +
  615. +/*** exchange data with the call manager *************************************/
  616. +/* XXX need better error checking XXX */
  617. +static int get_call_id(int sock, pid_t gre, pid_t pppd,
  618. + u_int16_t *peer_call_id)
  619. +{
  620. + u_int16_t m_call_id, m_peer_call_id;
  621. + /* write pid's to socket */
  622. + /* don't bother with network byte order, because pid's are meaningless
  623. + * outside the local host.
  624. + */
  625. + int rc;
  626. + rc = write(sock, &gre, sizeof(gre));
  627. + if (rc != sizeof(gre))
  628. + return -1;
  629. + rc = write(sock, &pppd, sizeof(pppd));
  630. + if (rc != sizeof(pppd))
  631. + return -1;
  632. + rc = read(sock, &m_call_id, sizeof(m_call_id));
  633. + if (rc != sizeof(m_call_id))
  634. + return -1;
  635. + rc = read(sock, &m_peer_call_id, sizeof(m_peer_call_id));
  636. + if (rc != sizeof(m_peer_call_id))
  637. + return -1;
  638. + /*
  639. + * XXX FIXME ... DO ERROR CHECKING & TIME-OUTS XXX
  640. + * (Rhialto: I am assuming for now that timeouts are not relevant
  641. + * here, because the read and write calls would return -1 (fail) when
  642. + * the peer goes away during the process. We know it is (or was)
  643. + * running because the connect() call succeeded.)
  644. + * (James: on the other hand, if the route to the peer goes away, we
  645. + * wouldn't get told by read() or write() for quite some time.)
  646. + */
  647. + *peer_call_id = m_peer_call_id;
  648. + return 0;
  649. +}
  650. +
  651. +void plugin_init(void)
  652. +{
  653. + add_options(Options);
  654. +
  655. + info("PPTP plugin version %s", PPTP_VERSION);
  656. +
  657. + the_channel = &pptp_channel;
  658. + modem = 0;
  659. +}
  660. --- /dev/null
  661. +++ b/pppd/plugins/pptp/pptp_callmgr.c
  662. @@ -0,0 +1,381 @@
  663. +/* pptp_callmgr.c ... Call manager for PPTP connections.
  664. + * Handles TCP port 1723 protocol.
  665. + * C. Scott Ananian <cananian@alumni.princeton.edu>
  666. + *
  667. + * $Id: pptp_callmgr.c,v 1.20 2005/03/31 07:42:39 quozl Exp $
  668. + */
  669. +#include <signal.h>
  670. +#include <sys/time.h>
  671. +#include <sys/types.h>
  672. +#include <sys/stat.h>
  673. +#include <sys/socket.h>
  674. +#include <netinet/in.h>
  675. +#include <arpa/inet.h>
  676. +#include <sys/un.h>
  677. +#include <unistd.h>
  678. +#include <stdlib.h>
  679. +#include <string.h>
  680. +#include <assert.h>
  681. +#include <setjmp.h>
  682. +#include <stdio.h>
  683. +#include <errno.h>
  684. +#include "pptp_callmgr.h"
  685. +#include "pptp_ctrl.h"
  686. +#include "pptp_msg.h"
  687. +#include "dirutil.h"
  688. +#include "vector.h"
  689. +#include "util.h"
  690. +#include "pppd.h"
  691. +
  692. +extern struct in_addr localbind; /* from pptp.c */
  693. +extern int call_ID;
  694. +
  695. +int open_inetsock(struct in_addr inetaddr);
  696. +int open_unixsock(struct in_addr inetaddr);
  697. +void close_inetsock(int fd, struct in_addr inetaddr);
  698. +void close_unixsock(int fd, struct in_addr inetaddr);
  699. +
  700. +sigjmp_buf callmgr_env;
  701. +
  702. +void callmgr_sighandler(int sig) {
  703. + /* TODO: according to signal(2), siglongjmp() is unsafe used here */
  704. + siglongjmp (callmgr_env, 1);
  705. +}
  706. +
  707. +void callmgr_do_nothing(int sig) {
  708. + /* do nothing signal handler */
  709. +}
  710. +
  711. +struct local_callinfo {
  712. + int unix_sock;
  713. + pid_t pid[2];
  714. +};
  715. +
  716. +struct local_conninfo {
  717. + VECTOR * call_list;
  718. + fd_set * call_set;
  719. +};
  720. +
  721. +/* Call callback */
  722. +void call_callback(PPTP_CONN *conn, PPTP_CALL *call, enum call_state state)
  723. +{
  724. + struct local_callinfo *lci;
  725. + struct local_conninfo *conninfo;
  726. + u_int16_t call_id[2];
  727. + switch(state) {
  728. + case CALL_OPEN_DONE:
  729. + /* okey dokey. This means that the call_id and peer_call_id are
  730. + * now valid, so lets send them on to our friends who requested
  731. + * this call. */
  732. + lci = pptp_call_closure_get(conn, call); assert(lci != NULL);
  733. + pptp_call_get_ids(conn, call, &call_id[0], &call_id[1]);
  734. + write(lci->unix_sock, &call_id, sizeof(call_id));
  735. + /* Our duty to the fatherland is now complete. */
  736. + break;
  737. + case CALL_OPEN_FAIL:
  738. + case CALL_CLOSE_RQST:
  739. + case CALL_CLOSE_DONE:
  740. + /* don't need to do anything here, except make sure tables
  741. + * are sync'ed */
  742. + dbglog("Closing connection (call state)");
  743. + conninfo = pptp_conn_closure_get(conn);
  744. + lci = pptp_call_closure_get(conn, call);
  745. + assert(lci != NULL && conninfo != NULL);
  746. + if (vector_contains(conninfo->call_list, lci->unix_sock)) {
  747. + vector_remove(conninfo->call_list, lci->unix_sock);
  748. + close(lci->unix_sock);
  749. + FD_CLR(lci->unix_sock, conninfo->call_set);
  750. + }
  751. + break;
  752. + default:
  753. + dbglog("Unhandled call callback state [%d].", (int) state);
  754. + break;
  755. + }
  756. +}
  757. +
  758. +/******************************************************************************
  759. + * NOTE ABOUT 'VOLATILE':
  760. + * several variables here get a volatile qualifier to silence warnings
  761. + * from older (before 3.0) gccs. if the longjmp stuff is removed,
  762. + * the volatile qualifiers should be removed as well.
  763. + *****************************************************************************/
  764. +
  765. +/*** Call Manager *************************************************************/
  766. +int callmgr_main(struct in_addr inetaddr, char phonenr[], int window, int pcallid)
  767. +{
  768. + int inet_sock, unix_sock;
  769. + fd_set call_set;
  770. + PPTP_CONN * conn;
  771. + VECTOR * call_list;
  772. + int max_fd = 0;
  773. + volatile int first = 1;
  774. + int retval;
  775. + int i;
  776. + if (pcallid>0) call_ID=pcallid;
  777. +
  778. + /* Step 1: Open sockets. */
  779. + if ((inet_sock = open_inetsock(inetaddr)) < 0)
  780. + fatal("Could not open control connection to %s", inet_ntoa(inetaddr));
  781. + dbglog("control connection");
  782. + if ((unix_sock = open_unixsock(inetaddr)) < 0)
  783. + fatal("Could not open unix socket for %s", inet_ntoa(inetaddr));
  784. + /* Step 1b: FORK and return status to calling process. */
  785. + dbglog("unix_sock");
  786. +
  787. + switch (fork()) {
  788. + case 0: /* child. stick around. */
  789. + break;
  790. + case -1: /* failure. Fatal. */
  791. + fatal("Could not fork.");
  792. + default: /* Parent. Return status to caller. */
  793. + exit(0);
  794. + }
  795. + /* re-open stderr as /dev/null to release it */
  796. + file2fd("/dev/null", "wb", STDERR_FILENO);
  797. + /* Step 1c: Clean up unix socket on TERM */
  798. + if (sigsetjmp(callmgr_env, 1) != 0)
  799. + goto cleanup;
  800. + signal(SIGINT, callmgr_sighandler);
  801. + signal(SIGTERM, callmgr_sighandler);
  802. + signal(SIGPIPE, callmgr_do_nothing);
  803. + signal(SIGUSR1, callmgr_do_nothing); /* signal state change
  804. + wake up accept */
  805. + /* Step 2: Open control connection and register callback */
  806. + if ((conn = pptp_conn_open(inet_sock, 1, NULL/* callback */)) == NULL) {
  807. + close(unix_sock); close(inet_sock); fatal("Could not open connection.");
  808. + }
  809. + FD_ZERO(&call_set);
  810. + call_list = vector_create();
  811. + {
  812. + struct local_conninfo *conninfo = malloc(sizeof(*conninfo));
  813. + if (conninfo == NULL) {
  814. + close(unix_sock); close(inet_sock); fatal("No memory.");
  815. + }
  816. + conninfo->call_list = call_list;
  817. + conninfo->call_set = &call_set;
  818. + pptp_conn_closure_put(conn, conninfo);
  819. + }
  820. + if (sigsetjmp(callmgr_env, 1) != 0) goto shutdown;
  821. + /* Step 3: Get FD_SETs */
  822. + max_fd = unix_sock;
  823. + do {
  824. + int rc;
  825. + fd_set read_set = call_set, write_set;
  826. + FD_ZERO (&write_set);
  827. + if (pptp_conn_established(conn)) {
  828. + FD_SET (unix_sock, &read_set);
  829. + if (unix_sock > max_fd) max_fd = unix_sock;
  830. + }
  831. + pptp_fd_set(conn, &read_set, &write_set, &max_fd);
  832. + for (; max_fd > 0 ; max_fd--) {
  833. + if (FD_ISSET (max_fd, &read_set) ||
  834. + FD_ISSET (max_fd, &write_set))
  835. + break;
  836. + }
  837. + /* Step 4: Wait on INET or UNIX event */
  838. + if ((rc = select(max_fd + 1, &read_set, &write_set, NULL, NULL)) <0) {
  839. + if (errno == EBADF) break;
  840. + /* a signal or somesuch. */
  841. + continue;
  842. + }
  843. + /* Step 5a: Handle INET events */
  844. + rc = pptp_dispatch(conn, &read_set, &write_set);
  845. + if (rc < 0)
  846. + break;
  847. + /* Step 5b: Handle new connection to UNIX socket */
  848. + if (FD_ISSET(unix_sock, &read_set)) {
  849. + /* New call! */
  850. + struct sockaddr_un from;
  851. + int len = sizeof(from);
  852. + PPTP_CALL * call;
  853. + struct local_callinfo *lci;
  854. + int s;
  855. + /* Accept the socket */
  856. + FD_CLR (unix_sock, &read_set);
  857. + if ((s = accept(unix_sock, (struct sockaddr *) &from, &len)) < 0) {
  858. + warn("Socket not accepted: %s", strerror(errno));
  859. + goto skip_accept;
  860. + }
  861. + /* Allocate memory for local call information structure. */
  862. + if ((lci = malloc(sizeof(*lci))) == NULL) {
  863. + warn("Out of memory."); close(s); goto skip_accept;
  864. + }
  865. + lci->unix_sock = s;
  866. + /* Give the initiator time to write the PIDs while we open
  867. + * the call */
  868. + call = pptp_call_open(conn, call_ID,call_callback, phonenr,window);
  869. + /* Read and store the associated pids */
  870. + read(s, &lci->pid[0], sizeof(lci->pid[0]));
  871. + read(s, &lci->pid[1], sizeof(lci->pid[1]));
  872. + /* associate the local information with the call */
  873. + pptp_call_closure_put(conn, call, (void *) lci);
  874. + /* The rest is done on callback. */
  875. + /* Keep alive; wait for close */
  876. + retval = vector_insert(call_list, s, call); assert(retval);
  877. + if (s > max_fd) max_fd = s;
  878. + FD_SET(s, &call_set);
  879. + first = 0;
  880. + }
  881. +skip_accept: /* Step 5c: Handle socket close */
  882. + for (i = 0; i < max_fd + 1; i++)
  883. + if (FD_ISSET(i, &read_set)) {
  884. + /* close it */
  885. + PPTP_CALL * call;
  886. + retval = vector_search(call_list, i, &call);
  887. + if (retval) {
  888. + struct local_callinfo *lci =
  889. + pptp_call_closure_get(conn, call);
  890. + dbglog("Closing connection (unhandled)");
  891. + free(lci);
  892. + /* soft shutdown. Callback will do hard shutdown later */
  893. + pptp_call_close(conn, call);
  894. + vector_remove(call_list, i);
  895. + }
  896. + FD_CLR(i, &call_set);
  897. + close(i);
  898. + }
  899. + } while (vector_size(call_list) > 0 || first);
  900. +shutdown:
  901. + {
  902. + int rc;
  903. + fd_set read_set, write_set;
  904. + struct timeval tv;
  905. + signal(SIGINT, callmgr_do_nothing);
  906. + signal(SIGTERM, callmgr_do_nothing);
  907. + /* warn("Shutdown"); */
  908. + /* kill all open calls */
  909. + for (i = 0; i < vector_size(call_list); i++) {
  910. + PPTP_CALL *call = vector_get_Nth(call_list, i);
  911. + dbglog("Closing connection (shutdown)");
  912. + pptp_call_close(conn, call);
  913. + }
  914. + /* attempt to dispatch these messages */
  915. + FD_ZERO(&read_set);
  916. + FD_ZERO(&write_set);
  917. + pptp_fd_set(conn, &read_set, &write_set, &max_fd);
  918. + tv.tv_sec = 0;
  919. + tv.tv_usec = 0;
  920. + select(max_fd + 1, &read_set, &write_set, NULL, &tv);
  921. + rc = pptp_dispatch(conn, &read_set, &write_set);
  922. + if (rc > 0) {
  923. + /* wait for a respond, a timeout because there might not be one */
  924. + FD_ZERO(&read_set);
  925. + FD_ZERO(&write_set);
  926. + pptp_fd_set(conn, &read_set, &write_set, &max_fd);
  927. + tv.tv_sec = 2;
  928. + tv.tv_usec = 0;
  929. + select(max_fd + 1, &read_set, &write_set, NULL, &tv);
  930. + rc = pptp_dispatch(conn, &read_set, &write_set);
  931. + if (rc > 0) {
  932. + if (i > 0) sleep(2);
  933. + /* no more open calls. Close the connection. */
  934. + pptp_conn_close(conn, PPTP_STOP_LOCAL_SHUTDOWN);
  935. + /* wait for a respond, a timeout because there might not be one */
  936. + FD_ZERO(&read_set);
  937. + FD_ZERO(&write_set);
  938. + pptp_fd_set(conn, &read_set, &write_set, &max_fd);
  939. + tv.tv_sec = 2;
  940. + tv.tv_usec = 0;
  941. + select(max_fd + 1, &read_set, &write_set, NULL, &tv);
  942. + pptp_dispatch(conn, &read_set, &write_set);
  943. + if (rc > 0) sleep(2);
  944. + }
  945. + }
  946. + /* with extreme prejudice */
  947. + pptp_conn_destroy(conn);
  948. + vector_destroy(call_list);
  949. + }
  950. +cleanup:
  951. + signal(SIGINT, callmgr_do_nothing);
  952. + signal(SIGTERM, callmgr_do_nothing);
  953. + close_inetsock(inet_sock, inetaddr);
  954. + close_unixsock(unix_sock, inetaddr);
  955. + return 0;
  956. +}
  957. +
  958. +/*** open_inetsock ************************************************************/
  959. +int open_inetsock(struct in_addr inetaddr)
  960. +{
  961. + struct sockaddr_in dest, src;
  962. + int s;
  963. + dest.sin_family = AF_INET;
  964. + dest.sin_port = htons(PPTP_PORT);
  965. + dest.sin_addr = inetaddr;
  966. + if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  967. + warn("socket: %s", strerror(errno));
  968. + return s;
  969. + }
  970. + if (localbind.s_addr != INADDR_NONE) {
  971. + bzero(&src, sizeof(src));
  972. + src.sin_family = AF_INET;
  973. + src.sin_addr = localbind;
  974. + if (bind(s, (struct sockaddr *) &src, sizeof(src)) != 0) {
  975. + warn("bind: %s", strerror(errno));
  976. + close(s); return -1;
  977. + }
  978. + }
  979. + if (connect(s, (struct sockaddr *) &dest, sizeof(dest)) < 0) {
  980. + warn("connect: %s", strerror(errno));
  981. + close(s); return -1;
  982. + }
  983. + return s;
  984. +}
  985. +
  986. +/*** open_unixsock ************************************************************/
  987. +int open_unixsock(struct in_addr inetaddr)
  988. +{
  989. + struct sockaddr_un where;
  990. + struct stat st;
  991. + char *dir;
  992. + int s;
  993. + if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
  994. + warn("socket: %s", strerror(errno));
  995. + return s;
  996. + }
  997. + callmgr_name_unixsock( &where, inetaddr, localbind);
  998. + if (stat(where.sun_path, &st) >= 0)
  999. + {
  1000. + warn("Call manager for %s is already running.", inet_ntoa(inetaddr));
  1001. + close(s); return -1;
  1002. + }
  1003. + /* Make sure path is valid. */
  1004. + dir = dirnamex(where.sun_path);
  1005. + if (!make_valid_path(dir, 0770))
  1006. + fatal("Could not make path to %s: %s", where.sun_path, strerror(errno));
  1007. + free(dir);
  1008. + if (bind(s, (struct sockaddr *) &where, sizeof(where)) < 0) {
  1009. + warn("bind: %s", strerror(errno));
  1010. + close(s); return -1;
  1011. + }
  1012. + chmod(where.sun_path, 0777);
  1013. + listen(s, 127);
  1014. + return s;
  1015. +}
  1016. +
  1017. +/*** close_inetsock ***********************************************************/
  1018. +void close_inetsock(int fd, struct in_addr inetaddr)
  1019. +{
  1020. + close(fd);
  1021. +}
  1022. +
  1023. +/*** close_unixsock ***********************************************************/
  1024. +void close_unixsock(int fd, struct in_addr inetaddr)
  1025. +{
  1026. + struct sockaddr_un where;
  1027. + close(fd);
  1028. + callmgr_name_unixsock(&where, inetaddr, localbind);
  1029. + unlink(where.sun_path);
  1030. +}
  1031. +
  1032. +/*** make a unix socket address ***********************************************/
  1033. +void callmgr_name_unixsock(struct sockaddr_un *where,
  1034. + struct in_addr inetaddr,
  1035. + struct in_addr localbind)
  1036. +{
  1037. + char localaddr[16], remoteaddr[16];
  1038. + where->sun_family = AF_UNIX;
  1039. + strncpy(localaddr, inet_ntoa(localbind), 16);
  1040. + strncpy(remoteaddr, inet_ntoa(inetaddr), 16);
  1041. + snprintf(where->sun_path, sizeof(where->sun_path),
  1042. + PPTP_SOCKET_PREFIX "%s:%i", remoteaddr,call_ID);
  1043. +}
  1044. --- /dev/null
  1045. +++ b/pppd/plugins/pptp/pptp_callmgr.h
  1046. @@ -0,0 +1,17 @@
  1047. +/* pptp_callmgr.h ... Call manager for PPTP connections.
  1048. + * Handles TCP port 1723 protocol.
  1049. + * C. Scott Ananian <cananian@alumni.princeton.edu>
  1050. + *
  1051. + * $Id: pptp_callmgr.h,v 1.3 2003/02/17 00:22:17 quozl Exp $
  1052. + */
  1053. +
  1054. +#define PPTP_SOCKET_PREFIX "/var/run/pptp/"
  1055. +
  1056. +int callmgr_main(struct in_addr inetaddr,
  1057. + char phonenr[],
  1058. + int window,
  1059. + int pcallid);
  1060. +
  1061. +void callmgr_name_unixsock(struct sockaddr_un *where,
  1062. + struct in_addr inetaddr,
  1063. + struct in_addr localbind);
  1064. --- /dev/null
  1065. +++ b/pppd/plugins/pptp/pptp_ctrl.c
  1066. @@ -0,0 +1,1077 @@
  1067. +/* pptp_ctrl.c ... handle PPTP control connection.
  1068. + * C. Scott Ananian <cananian@alumni.princeton.edu>
  1069. + *
  1070. + * $Id: pptp_ctrl.c,v 1.31 2005/03/31 07:42:39 quozl Exp $
  1071. + */
  1072. +
  1073. +#include <errno.h>
  1074. +#include <sys/time.h>
  1075. +#include <sys/types.h>
  1076. +#include <sys/socket.h>
  1077. +#include <netinet/in.h>
  1078. +#include <unistd.h>
  1079. +#include <stdlib.h>
  1080. +#include <assert.h>
  1081. +#include <signal.h>
  1082. +#include <string.h>
  1083. +#include <ctype.h>
  1084. +#include <fcntl.h>
  1085. +#include "pptp_msg.h"
  1086. +#include "pptp_ctrl.h"
  1087. +#include "pptp_options.h"
  1088. +#include "vector.h"
  1089. +#include "util.h"
  1090. +#include "pptp_quirks.h"
  1091. +
  1092. +/* BECAUSE OF SIGNAL LIMITATIONS, EACH PROCESS CAN ONLY MANAGE ONE
  1093. + * CONNECTION. SO THIS 'PPTP_CONN' STRUCTURE IS A BIT MISLEADING.
  1094. + * WE'LL KEEP CONNECTION-SPECIFIC INFORMATION IN THERE ANYWAY (AS
  1095. + * OPPOSED TO USING GLOBAL VARIABLES), BUT BEWARE THAT THE ENTIRE
  1096. + * UNIX SIGNAL-HANDLING SEMANTICS WOULD HAVE TO CHANGE (OR THE
  1097. + * TIME-OUT CODE DRASTICALLY REWRITTEN) BEFORE YOU COULD DO A
  1098. + * PPTP_CONN_OPEN MORE THAN ONCE PER PROCESS AND GET AWAY WITH IT.
  1099. + */
  1100. +
  1101. +/* This structure contains connection-specific information that the
  1102. + * signal handler needs to see. Thus, it needs to be in a global
  1103. + * variable. If you end up using pthreads or something (why not
  1104. + * just processes?), this would have to be placed in a thread-specific
  1105. + * data area, using pthread_get|set_specific, etc., so I've
  1106. + * conveniently encapsulated it for you.
  1107. + * [linux threads will have to support thread-specific signals
  1108. + * before this would work at all, which, as of this writing
  1109. + * (linux-threads v0.6, linux kernel 2.1.72), it does not.]
  1110. + */
  1111. +
  1112. +/* Globals */
  1113. +
  1114. +/* control the number of times echo packets will be logged */
  1115. +static int nlogecho = 10;
  1116. +
  1117. +static struct thread_specific {
  1118. + struct sigaction old_sigaction; /* evil signals */
  1119. + PPTP_CONN * conn;
  1120. +} global;
  1121. +
  1122. +#define INITIAL_BUFSIZE 512 /* initial i/o buffer size. */
  1123. +
  1124. +struct PPTP_CONN {
  1125. + int inet_sock;
  1126. + /* Connection States */
  1127. + enum {
  1128. + CONN_IDLE, CONN_WAIT_CTL_REPLY, CONN_WAIT_STOP_REPLY, CONN_ESTABLISHED
  1129. + } conn_state; /* on startup: CONN_IDLE */
  1130. + /* Keep-alive states */
  1131. + enum {
  1132. + KA_NONE, KA_OUTSTANDING
  1133. + } ka_state; /* on startup: KA_NONE */
  1134. + /* Keep-alive ID; monotonically increasing (watch wrap-around!) */
  1135. + u_int32_t ka_id; /* on startup: 1 */
  1136. + /* Other properties. */
  1137. + u_int16_t version;
  1138. + u_int16_t firmware_rev;
  1139. + u_int8_t hostname[64], vendor[64];
  1140. + /* XXX these are only PNS properties, currently XXX */
  1141. + /* Call assignment information. */
  1142. + u_int16_t call_serial_number;
  1143. + VECTOR *call;
  1144. + void * closure;
  1145. + pptp_conn_cb callback;
  1146. + /******* IO buffers ******/
  1147. + char * read_buffer, *write_buffer;
  1148. + size_t read_alloc, write_alloc;
  1149. + size_t read_size, write_size;
  1150. +};
  1151. +
  1152. +struct PPTP_CALL {
  1153. + /* Call properties */
  1154. + enum {
  1155. + PPTP_CALL_PAC, PPTP_CALL_PNS
  1156. + } call_type;
  1157. + union {
  1158. + enum pptp_pac_state {
  1159. + PAC_IDLE, PAC_WAIT_REPLY, PAC_ESTABLISHED, PAC_WAIT_CS_ANS
  1160. + } pac;
  1161. + enum pptp_pns_state {
  1162. + PNS_IDLE, PNS_WAIT_REPLY, PNS_ESTABLISHED, PNS_WAIT_DISCONNECT
  1163. + } pns;
  1164. + } state;
  1165. + u_int16_t call_id, peer_call_id;
  1166. + u_int16_t sernum;
  1167. + u_int32_t speed;
  1168. + /* For user data: */
  1169. + pptp_call_cb callback;
  1170. + void * closure;
  1171. +};
  1172. +
  1173. +
  1174. +/* PPTP error codes: ----------------------------------------------*/
  1175. +
  1176. +/* (General Error Codes) */
  1177. +static const struct {
  1178. + const char *name, *desc;
  1179. +} pptp_general_errors[] = {
  1180. +#define PPTP_GENERAL_ERROR_NONE 0
  1181. + { "(None)", "No general error" },
  1182. +#define PPTP_GENERAL_ERROR_NOT_CONNECTED 1
  1183. + { "(Not-Connected)", "No control connection exists yet for this "
  1184. + "PAC-PNS pair" },
  1185. +#define PPTP_GENERAL_ERROR_BAD_FORMAT 2
  1186. + { "(Bad-Format)", "Length is wrong or Magic Cookie value is incorrect" },
  1187. +#define PPTP_GENERAL_ERROR_BAD_VALUE 3
  1188. + { "(Bad-Value)", "One of the field values was out of range or "
  1189. + "reserved field was non-zero" },
  1190. +#define PPTP_GENERAL_ERROR_NO_RESOURCE 4
  1191. + { "(No-Resource)", "Insufficient resources to handle this command now" },
  1192. +#define PPTP_GENERAL_ERROR_BAD_CALLID 5
  1193. + { "(Bad-Call ID)", "The Call ID is invalid in this context" },
  1194. +#define PPTP_GENERAL_ERROR_PAC_ERROR 6
  1195. + { "(PAC-Error)", "A generic vendor-specific error occured in the PAC" }
  1196. +};
  1197. +
  1198. +#define MAX_GENERAL_ERROR ( sizeof(pptp_general_errors) / \
  1199. + sizeof(pptp_general_errors[0]) - 1)
  1200. +
  1201. +/* Outgoing Call Reply Result Codes */
  1202. +static const char *pptp_out_call_reply_result[] = {
  1203. +/* 0 */ "Unknown Result Code",
  1204. +/* 1 */ "Connected",
  1205. +/* 2 */ "General Error",
  1206. +/* 3 */ "No Carrier Detected",
  1207. +/* 4 */ "Busy Signal",
  1208. +/* 5 */ "No Dial Tone",
  1209. +/* 6 */ "Time Out",
  1210. +/* 7 */ "Not Accepted, Call is administratively prohibited" };
  1211. +
  1212. +#define MAX_OUT_CALL_REPLY_RESULT 7
  1213. +
  1214. +/* Call Disconnect Notify Result Codes */
  1215. +static const char *pptp_call_disc_ntfy[] = {
  1216. +/* 0 */ "Unknown Result Code",
  1217. +/* 1 */ "Lost Carrier",
  1218. +/* 2 */ "General Error",
  1219. +/* 3 */ "Administrative Shutdown",
  1220. +/* 4 */ "(your) Request" };
  1221. +
  1222. +#define MAX_CALL_DISC_NTFY 4
  1223. +
  1224. +/* Call Disconnect Notify Result Codes */
  1225. +static const char *pptp_start_ctrl_conn_rply[] = {
  1226. +/* 0 */ "Unknown Result Code",
  1227. +/* 1 */ "Successful Channel Establishment",
  1228. +/* 2 */ "General Error",
  1229. +/* 3 */ "Command Channel Already Exists",
  1230. +/* 4 */ "Requester is not Authorized" };
  1231. +
  1232. +#define MAX_START_CTRL_CONN_REPLY 4
  1233. +
  1234. +/* timing options */
  1235. +int idle_wait = PPTP_TIMEOUT;
  1236. +int max_echo_wait = PPTP_TIMEOUT;
  1237. +
  1238. +/* Local prototypes */
  1239. +static void pptp_reset_timer(void);
  1240. +static void pptp_handle_timer();
  1241. +/* Write/read as much as we can without blocking. */
  1242. +int pptp_write_some(PPTP_CONN * conn);
  1243. +int pptp_read_some(PPTP_CONN * conn);
  1244. +/* Make valid packets from read_buffer */
  1245. +int pptp_make_packet(PPTP_CONN * conn, void **buf, size_t *size);
  1246. +/* Add packet to write_buffer */
  1247. +int pptp_send_ctrl_packet(PPTP_CONN * conn, void * buffer, size_t size);
  1248. +/* Dispatch packets (general) */
  1249. +int pptp_dispatch_packet(PPTP_CONN * conn, void * buffer, size_t size);
  1250. +/* Dispatch packets (control messages) */
  1251. +int ctrlp_disp(PPTP_CONN * conn, void * buffer, size_t size);
  1252. +/* Set link info, for pptp servers that need it.
  1253. + this is a noop, unless the user specified a quirk and
  1254. + there's a set_link hook defined in the quirks table
  1255. + for that quirk */
  1256. +void pptp_set_link(PPTP_CONN * conn, int peer_call_id);
  1257. +
  1258. +/*** log error information in control packets *********************************/
  1259. +static void ctrlp_error( int result, int error, int cause,
  1260. + const char *result_text[], int max_result)
  1261. +{
  1262. + if( cause >= 0)
  1263. + warn("Result code is %d '%s'. Error code is %d, Cause code is %d",
  1264. + result, result_text[result <= max_result ? result : 0], error,
  1265. + cause );
  1266. + else
  1267. + warn("Reply result code is %d '%s'. Error code is %d",
  1268. + result, result_text[result <= max_result ? result : 0], error);
  1269. + if ((error > 0) && (error <= MAX_GENERAL_ERROR)){
  1270. + if( result != PPTP_RESULT_GENERAL_ERROR )
  1271. + warn("Result code is something else then \"general error\", "
  1272. + "so the following error is probably bogus.");
  1273. + warn("Error is '%s', Error message: '%s'",
  1274. + pptp_general_errors[error].name,
  1275. + pptp_general_errors[error].desc);
  1276. + }
  1277. +}
  1278. +
  1279. +static const char *ctrl_msg_types[] = {
  1280. + "invalid control message type",
  1281. +/* (Control Connection Management) */
  1282. + "Start-Control-Connection-Request", /* 1 */
  1283. + "Start-Control-Connection-Reply", /* 2 */
  1284. + "Stop-Control-Connection-Request", /* 3 */
  1285. + "Stop-Control-Connection-Reply", /* 4 */
  1286. + "Echo-Request", /* 5 */
  1287. + "Echo-Reply", /* 6 */
  1288. +/* (Call Management) */
  1289. + "Outgoing-Call-Request", /* 7 */
  1290. + "Outgoing-Call-Reply", /* 8 */
  1291. + "Incoming-Call-Request", /* 9 */
  1292. + "Incoming-Call-Reply", /* 10 */
  1293. + "Incoming-Call-Connected", /* 11 */
  1294. + "Call-Clear-Request", /* 12 */
  1295. + "Call-Disconnect-Notify", /* 13 */
  1296. +/* (Error Reporting) */
  1297. + "WAN-Error-Notify", /* 14 */
  1298. +/* (PPP Session Control) */
  1299. + "Set-Link-Info" /* 15 */
  1300. +};
  1301. +#define MAX_CTRLMSG_TYPE 15
  1302. +
  1303. +/*** report a sent packet ****************************************************/
  1304. +static void ctrlp_rep( void * buffer, int size, int isbuff)
  1305. +{
  1306. + struct pptp_header *packet = buffer;
  1307. + unsigned int type;
  1308. + if(size < sizeof(struct pptp_header)) return;
  1309. + type = ntoh16(packet->ctrl_type);
  1310. + /* FIXME: do not report sending echo requests as long as they are
  1311. + * sent in a signal handler. This may dead lock as the syslog call
  1312. + * is not reentrant */
  1313. + if( type == PPTP_ECHO_RQST ) return;
  1314. + /* don't keep reporting sending of echo's */
  1315. + if( (type == PPTP_ECHO_RQST || type == PPTP_ECHO_RPLY) && nlogecho <= 0 ) return;
  1316. + dbglog("%s control packet type is %d '%s'\n",isbuff ? "Buffered" : "Sent",
  1317. + type, ctrl_msg_types[type <= MAX_CTRLMSG_TYPE ? type : 0]);
  1318. +
  1319. +}
  1320. +
  1321. +
  1322. +
  1323. +/* Open new pptp_connection. Returns NULL on failure. */
  1324. +PPTP_CONN * pptp_conn_open(int inet_sock, int isclient, pptp_conn_cb callback)
  1325. +{
  1326. + PPTP_CONN *conn;
  1327. + /* Allocate structure */
  1328. + if ((conn = malloc(sizeof(*conn))) == NULL) return NULL;
  1329. + if ((conn->call = vector_create()) == NULL) { free(conn); return NULL; }
  1330. + /* Initialize */
  1331. + conn->inet_sock = inet_sock;
  1332. + conn->conn_state = CONN_IDLE;
  1333. + conn->ka_state = KA_NONE;
  1334. + conn->ka_id = 1;
  1335. + conn->call_serial_number = 0;
  1336. + conn->callback = callback;
  1337. + /* Create I/O buffers */
  1338. + conn->read_size = conn->write_size = 0;
  1339. + conn->read_alloc = conn->write_alloc = INITIAL_BUFSIZE;
  1340. + conn->read_buffer =
  1341. + malloc(sizeof(*(conn->read_buffer)) * conn->read_alloc);
  1342. + conn->write_buffer =
  1343. + malloc(sizeof(*(conn->write_buffer)) * conn->write_alloc);
  1344. + if (conn->read_buffer == NULL || conn->write_buffer == NULL) {
  1345. + if (conn->read_buffer != NULL) free(conn->read_buffer);
  1346. + if (conn->write_buffer != NULL) free(conn->write_buffer);
  1347. + vector_destroy(conn->call); free(conn); return NULL;
  1348. + }
  1349. + /* Make this socket non-blocking. */
  1350. + fcntl(conn->inet_sock, F_SETFL, O_NONBLOCK);
  1351. + /* Request connection from server, if this is a client */
  1352. + if (isclient) {
  1353. + struct pptp_start_ctrl_conn packet = {
  1354. + PPTP_HEADER_CTRL(PPTP_START_CTRL_CONN_RQST),
  1355. + hton16(PPTP_VERSION), 0, 0,
  1356. + hton32(PPTP_FRAME_CAP), hton32(PPTP_BEARER_CAP),
  1357. + hton16(PPTP_MAX_CHANNELS), hton16(PPTP_FIRMWARE_VERSION),
  1358. + PPTP_HOSTNAME, PPTP_VENDOR
  1359. + };
  1360. + /* fix this packet, if necessary */
  1361. + int idx, rc;
  1362. + idx = get_quirk_index();
  1363. + if (idx != -1 && pptp_fixups[idx].start_ctrl_conn) {
  1364. + if ((rc = pptp_fixups[idx].start_ctrl_conn(&packet)))
  1365. + warn("calling the start_ctrl_conn hook failed (%d)", rc);
  1366. + }
  1367. + if (pptp_send_ctrl_packet(conn, &packet, sizeof(packet)))
  1368. + conn->conn_state = CONN_WAIT_CTL_REPLY;
  1369. + else
  1370. + return NULL; /* could not send initial start request. */
  1371. + }
  1372. + /* Set up interval/keep-alive timer */
  1373. + /* First, register handler for SIGALRM */
  1374. + sigpipe_create();
  1375. + sigpipe_assign(SIGALRM);
  1376. + global.conn = conn;
  1377. + /* Reset event timer */
  1378. + pptp_reset_timer();
  1379. + /* all done. */
  1380. + return conn;
  1381. +}
  1382. +
  1383. +int pptp_conn_established(PPTP_CONN *conn) {
  1384. + return (conn->conn_state == CONN_ESTABLISHED);
  1385. +}
  1386. +
  1387. +/* This currently *only* works for client call requests.
  1388. + * We need to do something else to allocate calls for incoming requests.
  1389. + */
  1390. +PPTP_CALL * pptp_call_open(PPTP_CONN * conn, int call_id,pptp_call_cb callback,
  1391. + char *phonenr,int window)
  1392. +{
  1393. + PPTP_CALL * call;
  1394. + int idx, rc;
  1395. + /* Send off the call request */
  1396. + struct pptp_out_call_rqst packet = {
  1397. + PPTP_HEADER_CTRL(PPTP_OUT_CALL_RQST),
  1398. + 0,0, /*call_id, sernum */
  1399. + hton32(PPTP_BPS_MIN), hton32(PPTP_BPS_MAX),
  1400. + hton32(PPTP_BEARER_CAP), hton32(PPTP_FRAME_CAP),
  1401. + hton16(window), 0, 0, 0, {0}, {0}
  1402. + };
  1403. + assert(conn && conn->call);
  1404. + assert(conn->conn_state == CONN_ESTABLISHED);
  1405. + /* Assign call id */
  1406. + if (!call_id && !vector_scan(conn->call, 0, PPTP_MAX_CHANNELS - 1, &call_id))
  1407. + /* no more calls available! */
  1408. + return NULL;
  1409. + /* allocate structure. */
  1410. + if ((call = malloc(sizeof(*call))) == NULL) return NULL;
  1411. + /* Initialize call structure */
  1412. + call->call_type = PPTP_CALL_PNS;
  1413. + call->state.pns = PNS_IDLE;
  1414. + call->call_id = (u_int16_t) call_id;
  1415. + call->sernum = conn->call_serial_number++;
  1416. + call->callback = callback;
  1417. + call->closure = NULL;
  1418. + packet.call_id = htons(call->call_id);
  1419. + packet.call_sernum = htons(call->sernum);
  1420. + /* if we have a quirk, build a new packet to fit it */
  1421. + idx = get_quirk_index();
  1422. + if (idx != -1 && pptp_fixups[idx].out_call_rqst_hook) {
  1423. + if ((rc = pptp_fixups[idx].out_call_rqst_hook(&packet)))
  1424. + warn("calling the out_call_rqst hook failed (%d)", rc);
  1425. + }
  1426. + /* fill in the phone number if it was specified */
  1427. + if (phonenr) {
  1428. + strncpy(packet.phone_num, phonenr, sizeof(packet.phone_num));
  1429. + packet.phone_len = strlen(phonenr);
  1430. + if( packet.phone_len > sizeof(packet.phone_num))
  1431. + packet.phone_len = sizeof(packet.phone_num);
  1432. + packet.phone_len = hton16 (packet.phone_len);
  1433. + }
  1434. + if (pptp_send_ctrl_packet(conn, &packet, sizeof(packet))) {
  1435. + pptp_reset_timer();
  1436. + call->state.pns = PNS_WAIT_REPLY;
  1437. + /* and add it to the call vector */
  1438. + vector_insert(conn->call, call_id, call);
  1439. + return call;
  1440. + } else { /* oops, unsuccessful. Deallocate. */
  1441. + free(call);
  1442. + return NULL;
  1443. + }
  1444. +}
  1445. +
  1446. +/*** pptp_call_close **********************************************************/
  1447. +void pptp_call_close(PPTP_CONN * conn, PPTP_CALL * call)
  1448. +{
  1449. + struct pptp_call_clear_rqst rqst = {
  1450. + PPTP_HEADER_CTRL(PPTP_CALL_CLEAR_RQST), 0, 0
  1451. + };
  1452. + assert(conn && conn->call); assert(call);
  1453. + assert(vector_contains(conn->call, call->call_id));
  1454. + /* haven't thought about PAC yet */
  1455. + assert(call->call_type == PPTP_CALL_PNS);
  1456. + assert(call->state.pns != PNS_IDLE);
  1457. + rqst.call_id = hton16(call->call_id);
  1458. + /* don't check state against WAIT_DISCONNECT... allow multiple disconnect
  1459. + * requests to be made.
  1460. + */
  1461. + pptp_send_ctrl_packet(conn, &rqst, sizeof(rqst));
  1462. + pptp_reset_timer();
  1463. + call->state.pns = PNS_WAIT_DISCONNECT;
  1464. + /* call structure will be freed when we have confirmation of disconnect. */
  1465. +}
  1466. +
  1467. +/*** hard close ***************************************************************/
  1468. +void pptp_call_destroy(PPTP_CONN *conn, PPTP_CALL *call)
  1469. +{
  1470. + assert(conn && conn->call); assert(call);
  1471. + assert(vector_contains(conn->call, call->call_id));
  1472. + /* notify */
  1473. + if (call->callback != NULL) call->callback(conn, call, CALL_CLOSE_DONE);
  1474. + /* deallocate */
  1475. + vector_remove(conn->call, call->call_id);
  1476. + free(call);
  1477. +}
  1478. +
  1479. +/*** this is a soft close *****************************************************/
  1480. +void pptp_conn_close(PPTP_CONN * conn, u_int8_t close_reason)
  1481. +{
  1482. + struct pptp_stop_ctrl_conn rqst = {
  1483. + PPTP_HEADER_CTRL(PPTP_STOP_CTRL_CONN_RQST),
  1484. + hton8(close_reason), 0, 0
  1485. + };
  1486. + int i;
  1487. + assert(conn && conn->call);
  1488. + /* avoid repeated close attempts */
  1489. + if (conn->conn_state == CONN_IDLE || conn->conn_state == CONN_WAIT_STOP_REPLY)
  1490. + return;
  1491. + /* close open calls, if any */
  1492. + for (i = 0; i < vector_size(conn->call); i++)
  1493. + pptp_call_close(conn, vector_get_Nth(conn->call, i));
  1494. + /* now close connection */
  1495. + info("Closing PPTP connection");
  1496. + pptp_send_ctrl_packet(conn, &rqst, sizeof(rqst));
  1497. + pptp_reset_timer(); /* wait 60 seconds for reply */
  1498. + conn->conn_state = CONN_WAIT_STOP_REPLY;
  1499. + return;
  1500. +}
  1501. +
  1502. +/*** this is a hard close *****************************************************/
  1503. +void pptp_conn_destroy(PPTP_CONN * conn)
  1504. +{
  1505. + int i;
  1506. + assert(conn != NULL); assert(conn->call != NULL);
  1507. + /* destroy all open calls */
  1508. + for (i = 0; i < vector_size(conn->call); i++)
  1509. + pptp_call_destroy(conn, vector_get_Nth(conn->call, i));
  1510. + /* notify */
  1511. + if (conn->callback != NULL) conn->callback(conn, CONN_CLOSE_DONE);
  1512. + sigpipe_close();
  1513. + close(conn->inet_sock);
  1514. + /* deallocate */
  1515. + vector_destroy(conn->call);
  1516. + free(conn);
  1517. +}
  1518. +
  1519. +/*** Deal with messages, in a non-blocking manner
  1520. + * Add file descriptors used by pptp to fd_set.
  1521. + */
  1522. +void pptp_fd_set(PPTP_CONN * conn, fd_set * read_set, fd_set * write_set,
  1523. + int * max_fd)
  1524. +{
  1525. + assert(conn && conn->call);
  1526. + /* Add fd to write_set if there are outstanding writes. */
  1527. + if (conn->write_size > 0)
  1528. + FD_SET(conn->inet_sock, write_set);
  1529. + /* Always add fd to read_set. (always want something to read) */
  1530. + FD_SET(conn->inet_sock, read_set);
  1531. + if (*max_fd < conn->inet_sock) *max_fd = conn->inet_sock;
  1532. + /* Add signal pipe file descriptor to set */
  1533. + int sig_fd = sigpipe_fd();
  1534. + FD_SET(sig_fd, read_set);
  1535. + if (*max_fd < sig_fd) *max_fd = sig_fd;
  1536. +}
  1537. +
  1538. +/*** handle any pptp file descriptors set in fd_set, and clear them ***********/
  1539. +int pptp_dispatch(PPTP_CONN * conn, fd_set * read_set, fd_set * write_set)
  1540. +{
  1541. + int r = 0;
  1542. + assert(conn && conn->call);
  1543. + /* Check for signals */
  1544. + if (FD_ISSET(sigpipe_fd(), read_set)) {
  1545. + if (sigpipe_read() == SIGALRM) pptp_handle_timer();
  1546. + FD_CLR(sigpipe_fd(), read_set);
  1547. + }
  1548. + /* Check write_set could be set. */
  1549. + if (FD_ISSET(conn->inet_sock, write_set)) {
  1550. + FD_CLR(conn->inet_sock, write_set);
  1551. + if (conn->write_size > 0)
  1552. + r = pptp_write_some(conn);/* write as much as we can without blocking */
  1553. + }
  1554. + /* Check read_set */
  1555. + if (r >= 0 && FD_ISSET(conn->inet_sock, read_set)) {
  1556. + void *buffer; size_t size;
  1557. + FD_CLR(conn->inet_sock, read_set);
  1558. + r = pptp_read_some(conn); /* read as much as we can without blocking */
  1559. + if (r < 0)
  1560. + return r;
  1561. + /* make packets of the buffer, while we can. */
  1562. + while (r >= 0 && pptp_make_packet(conn, &buffer, &size)) {
  1563. + r = pptp_dispatch_packet(conn, buffer, size);
  1564. + free(buffer);
  1565. + }
  1566. + }
  1567. + /* That's all, folks. Simple, eh? */
  1568. + return r;
  1569. +}
  1570. +
  1571. +/*** Non-blocking write *******************************************************/
  1572. +int pptp_write_some(PPTP_CONN * conn) {
  1573. + ssize_t retval;
  1574. + assert(conn && conn->call);
  1575. + retval = write(conn->inet_sock, conn->write_buffer, conn->write_size);
  1576. + if (retval < 0) { /* error. */
  1577. + if (errno == EAGAIN || errno == EINTR) {
  1578. + return 0;
  1579. + } else { /* a real error */
  1580. + warn("write error: %s", strerror(errno));
  1581. + return -1;
  1582. + }
  1583. + }
  1584. + assert(retval <= conn->write_size);
  1585. + conn->write_size -= retval;
  1586. + memmove(conn->write_buffer, conn->write_buffer + retval, conn->write_size);
  1587. + ctrlp_rep(conn->write_buffer, retval, 0);
  1588. + return 0;
  1589. +}
  1590. +
  1591. +/*** Non-blocking read ********************************************************/
  1592. +int pptp_read_some(PPTP_CONN * conn)
  1593. +{
  1594. + ssize_t retval;
  1595. + assert(conn && conn->call);
  1596. + if (conn->read_size == conn->read_alloc) { /* need to alloc more memory */
  1597. + char *new_buffer = realloc(conn->read_buffer,
  1598. + sizeof(*(conn->read_buffer)) * conn->read_alloc * 2);
  1599. + if (new_buffer == NULL) {
  1600. + warn("Out of memory"); return -1;
  1601. + }
  1602. + conn->read_alloc *= 2;
  1603. + conn->read_buffer = new_buffer;
  1604. + }
  1605. + retval = read(conn->inet_sock, conn->read_buffer + conn->read_size,
  1606. + conn->read_alloc - conn->read_size);
  1607. + if (retval == 0) {
  1608. + warn("read returned zero, peer has closed");
  1609. + return -1;
  1610. + }
  1611. + if (retval < 0) {
  1612. + if (errno == EINTR || errno == EAGAIN)
  1613. + return 0;
  1614. + else { /* a real error */
  1615. + warn("read error: %s", strerror(errno));
  1616. + return -1;
  1617. + }
  1618. + }
  1619. + conn->read_size += retval;
  1620. + assert(conn->read_size <= conn->read_alloc);
  1621. + return 0;
  1622. +}
  1623. +
  1624. +/*** Packet formation *********************************************************/
  1625. +int pptp_make_packet(PPTP_CONN * conn, void **buf, size_t *size)
  1626. +{
  1627. + struct pptp_header *header;
  1628. + size_t bad_bytes = 0;
  1629. + assert(conn && conn->call); assert(buf != NULL); assert(size != NULL);
  1630. + /* Give up unless there are at least sizeof(pptp_header) bytes */
  1631. + while ((conn->read_size-bad_bytes) >= sizeof(struct pptp_header)) {
  1632. + /* Throw out bytes until we have a valid header. */
  1633. + header = (struct pptp_header *) (conn->read_buffer + bad_bytes);
  1634. + if (ntoh32(header->magic) != PPTP_MAGIC) goto throwitout;
  1635. + if (ntoh16(header->reserved0) != 0)
  1636. + warn("reserved0 field is not zero! (0x%x) Cisco feature? \n",
  1637. + ntoh16(header->reserved0));
  1638. + if (ntoh16(header->length) < sizeof(struct pptp_header)) goto throwitout;
  1639. + if (ntoh16(header->length) > PPTP_CTRL_SIZE_MAX) goto throwitout;
  1640. + /* well. I guess it's good. Let's see if we've got it all. */
  1641. + if (ntoh16(header->length) > (conn->read_size-bad_bytes))
  1642. + /* nope. Let's wait until we've got it, then. */
  1643. + goto flushbadbytes;
  1644. + /* One last check: */
  1645. + if ((ntoh16(header->pptp_type) == PPTP_MESSAGE_CONTROL) &&
  1646. + (ntoh16(header->length) !=
  1647. + PPTP_CTRL_SIZE(ntoh16(header->ctrl_type))))
  1648. + goto throwitout;
  1649. + /* well, I guess we've got it. */
  1650. + *size = ntoh16(header->length);
  1651. + *buf = malloc(*size);
  1652. + if (*buf == NULL) { warn("Out of memory."); return 0; /* ack! */ }
  1653. + memcpy(*buf, conn->read_buffer + bad_bytes, *size);
  1654. + /* Delete this packet from the read_buffer. */
  1655. + conn->read_size -= (bad_bytes + *size);
  1656. + memmove(conn->read_buffer, conn->read_buffer + bad_bytes + *size,
  1657. + conn->read_size);
  1658. + if (bad_bytes > 0)
  1659. + warn("%lu bad bytes thrown away.", (unsigned long) bad_bytes);
  1660. + return 1;
  1661. +throwitout:
  1662. + bad_bytes++;
  1663. + }
  1664. +flushbadbytes:
  1665. + /* no more packets. Let's get rid of those bad bytes */
  1666. + conn->read_size -= bad_bytes;
  1667. + memmove(conn->read_buffer, conn->read_buffer + bad_bytes, conn->read_size);
  1668. + if (bad_bytes > 0)
  1669. + warn("%lu bad bytes thrown away.", (unsigned long) bad_bytes);
  1670. + return 0;
  1671. +}
  1672. +
  1673. +/*** pptp_send_ctrl_packet ****************************************************/
  1674. +int pptp_send_ctrl_packet(PPTP_CONN * conn, void * buffer, size_t size)
  1675. +{
  1676. + assert(conn && conn->call); assert(buffer);
  1677. + if( conn->write_size > 0) pptp_write_some( conn);
  1678. + if( conn->write_size == 0) {
  1679. + ssize_t retval;
  1680. + retval = write(conn->inet_sock, buffer, size);
  1681. + if (retval < 0) { /* error. */
  1682. + if (errno == EAGAIN || errno == EINTR) {
  1683. + /* ignore */;
  1684. + retval = 0;
  1685. + } else { /* a real error */
  1686. + warn("write error: %s", strerror(errno));
  1687. + pptp_conn_destroy(conn); /* shut down fast. */
  1688. + return 0;
  1689. + }
  1690. + }
  1691. + ctrlp_rep( buffer, retval, 0);
  1692. + size -= retval;
  1693. + if( size <= 0) return 1;
  1694. + }
  1695. + /* Shove anything not written into the write buffer */
  1696. + if (conn->write_size + size > conn->write_alloc) { /* need more memory */
  1697. + char *new_buffer = realloc(conn->write_buffer,
  1698. + sizeof(*(conn->write_buffer)) * conn->write_alloc * 2);
  1699. + if (new_buffer == NULL) {
  1700. + warn("Out of memory"); return 0;
  1701. + }
  1702. + conn->write_alloc *= 2;
  1703. + conn->write_buffer = new_buffer;
  1704. + }
  1705. + memcpy(conn->write_buffer + conn->write_size, buffer, size);
  1706. + conn->write_size += size;
  1707. + ctrlp_rep( buffer,size,1);
  1708. + return 1;
  1709. +}
  1710. +
  1711. +/*** Packet Dispatch **********************************************************/
  1712. +int pptp_dispatch_packet(PPTP_CONN * conn, void * buffer, size_t size)
  1713. +{
  1714. + int r = 0;
  1715. + struct pptp_header *header = (struct pptp_header *)buffer;
  1716. + assert(conn && conn->call); assert(buffer);
  1717. + assert(ntoh32(header->magic) == PPTP_MAGIC);
  1718. + assert(ntoh16(header->length) == size);
  1719. + switch (ntoh16(header->pptp_type)) {
  1720. + case PPTP_MESSAGE_CONTROL:
  1721. + r = ctrlp_disp(conn, buffer, size);
  1722. + break;
  1723. + case PPTP_MESSAGE_MANAGE:
  1724. + /* MANAGEMENT messages aren't even part of the spec right now. */
  1725. + dbglog("PPTP management message received, but not understood.");
  1726. + break;
  1727. + default:
  1728. + dbglog("Unknown PPTP control message type received: %u",
  1729. + (unsigned int) ntoh16(header->pptp_type));
  1730. + break;
  1731. + }
  1732. + return r;
  1733. +}
  1734. +
  1735. +/*** log echo request/replies *************************************************/
  1736. +static void logecho( int type)
  1737. +{
  1738. + /* hack to stop flooding the log files (the most interesting part is right
  1739. + * after the connection built-up) */
  1740. + if( nlogecho > 0) {
  1741. + dbglog("Echo Re%s received.", type == PPTP_ECHO_RQST ? "quest" :"ply");
  1742. + if( --nlogecho == 0)
  1743. + dbglog("no more Echo Reply/Request packets will be reported.");
  1744. + }
  1745. +}
  1746. +
  1747. +/*** pptp_dispatch_ctrl_packet ************************************************/
  1748. +int ctrlp_disp(PPTP_CONN * conn, void * buffer, size_t size)
  1749. +{
  1750. + struct pptp_header *header = (struct pptp_header *)buffer;
  1751. + u_int8_t close_reason = PPTP_STOP_NONE;
  1752. + assert(conn && conn->call); assert(buffer);
  1753. + assert(ntoh32(header->magic) == PPTP_MAGIC);
  1754. + assert(ntoh16(header->length) == size);
  1755. + assert(ntoh16(header->pptp_type) == PPTP_MESSAGE_CONTROL);
  1756. + if (size < PPTP_CTRL_SIZE(ntoh16(header->ctrl_type))) {
  1757. + warn("Invalid packet received [type: %d; length: %d].",
  1758. + (int) ntoh16(header->ctrl_type), (int) size);
  1759. + return 0;
  1760. + }
  1761. + switch (ntoh16(header->ctrl_type)) {
  1762. + /* ----------- STANDARD Start-Session MESSAGES ------------ */
  1763. + case PPTP_START_CTRL_CONN_RQST:
  1764. + {
  1765. + struct pptp_start_ctrl_conn *packet =
  1766. + (struct pptp_start_ctrl_conn *) buffer;
  1767. + struct pptp_start_ctrl_conn reply = {
  1768. + PPTP_HEADER_CTRL(PPTP_START_CTRL_CONN_RPLY),
  1769. + hton16(PPTP_VERSION), 0, 0,
  1770. + hton32(PPTP_FRAME_CAP), hton32(PPTP_BEARER_CAP),
  1771. + hton16(PPTP_MAX_CHANNELS), hton16(PPTP_FIRMWARE_VERSION),
  1772. + PPTP_HOSTNAME, PPTP_VENDOR };
  1773. + int idx, rc;
  1774. + dbglog("Received Start Control Connection Request");
  1775. + /* fix this packet, if necessary */
  1776. + idx = get_quirk_index();
  1777. + if (idx != -1 && pptp_fixups[idx].start_ctrl_conn) {
  1778. + if ((rc = pptp_fixups[idx].start_ctrl_conn(&reply)))
  1779. + warn("calling the start_ctrl_conn hook failed (%d)", rc);
  1780. + }
  1781. + if (conn->conn_state == CONN_IDLE) {
  1782. + if (ntoh16(packet->version) < PPTP_VERSION) {
  1783. + /* Can't support this (earlier) PPTP_VERSION */
  1784. + reply.version = packet->version;
  1785. + /* protocol version not supported */
  1786. + reply.result_code = hton8(5);
  1787. + pptp_send_ctrl_packet(conn, &reply, sizeof(reply));
  1788. + pptp_reset_timer(); /* give sender a chance for a retry */
  1789. + } else { /* same or greater version */
  1790. + if (pptp_send_ctrl_packet(conn, &reply, sizeof(reply))) {
  1791. + conn->conn_state = CONN_ESTABLISHED;
  1792. + dbglog("server connection ESTABLISHED.");
  1793. + pptp_reset_timer();
  1794. + }
  1795. + }
  1796. + }
  1797. + break;
  1798. + }
  1799. + case PPTP_START_CTRL_CONN_RPLY:
  1800. + {
  1801. + struct pptp_start_ctrl_conn *packet =
  1802. + (struct pptp_start_ctrl_conn *) buffer;
  1803. + dbglog("Received Start Control Connection Reply");
  1804. + if (conn->conn_state == CONN_WAIT_CTL_REPLY) {
  1805. + /* XXX handle collision XXX [see rfc] */
  1806. + if (ntoh16(packet->version) != PPTP_VERSION) {
  1807. + if (conn->callback != NULL)
  1808. + conn->callback(conn, CONN_OPEN_FAIL);
  1809. + close_reason = PPTP_STOP_PROTOCOL;
  1810. + goto pptp_conn_close;
  1811. + }
  1812. + if (ntoh8(packet->result_code) != 1 &&
  1813. + /* J'ai change le if () afin que la connection ne se ferme
  1814. + * pas pour un "rien" :p adel@cybercable.fr -
  1815. + *
  1816. + * Don't close the connection if the result code is zero
  1817. + * (feature found in certain ADSL modems)
  1818. + */
  1819. + ntoh8(packet->result_code) != 0) {
  1820. + dbglog("Negative reply received to our Start Control "
  1821. + "Connection Request");
  1822. + ctrlp_error(packet->result_code, packet->error_code,
  1823. + -1, pptp_start_ctrl_conn_rply,
  1824. + MAX_START_CTRL_CONN_REPLY);
  1825. + if (conn->callback != NULL)
  1826. + conn->callback(conn, CONN_OPEN_FAIL);
  1827. + close_reason = PPTP_STOP_PROTOCOL;
  1828. + goto pptp_conn_close;
  1829. + }
  1830. + conn->conn_state = CONN_ESTABLISHED;
  1831. + /* log session properties */
  1832. + conn->version = ntoh16(packet->version);
  1833. + conn->firmware_rev = ntoh16(packet->firmware_rev);
  1834. + memcpy(conn->hostname, packet->hostname, sizeof(conn->hostname));
  1835. + memcpy(conn->vendor, packet->vendor, sizeof(conn->vendor));
  1836. + pptp_reset_timer(); /* 60 seconds until keep-alive */
  1837. + dbglog("Client connection established.");
  1838. + if (conn->callback != NULL)
  1839. + conn->callback(conn, CONN_OPEN_DONE);
  1840. + } /* else goto pptp_conn_close; */
  1841. + break;
  1842. + }
  1843. + /* ----------- STANDARD Stop-Session MESSAGES ------------ */
  1844. + case PPTP_STOP_CTRL_CONN_RQST:
  1845. + {
  1846. + /* conn_state should be CONN_ESTABLISHED, but it could be
  1847. + * something else */
  1848. + struct pptp_stop_ctrl_conn reply = {
  1849. + PPTP_HEADER_CTRL(PPTP_STOP_CTRL_CONN_RPLY),
  1850. + hton8(1), hton8(PPTP_GENERAL_ERROR_NONE), 0
  1851. + };
  1852. + dbglog("Received Stop Control Connection Request.");
  1853. + if (conn->conn_state == CONN_IDLE) break;
  1854. + if (pptp_send_ctrl_packet(conn, &reply, sizeof(reply))) {
  1855. + if (conn->callback != NULL)
  1856. + conn->callback(conn, CONN_CLOSE_RQST);
  1857. + conn->conn_state = CONN_IDLE;
  1858. + return -1;
  1859. + }
  1860. + break;
  1861. + }
  1862. + case PPTP_STOP_CTRL_CONN_RPLY:
  1863. + {
  1864. + dbglog("Received Stop Control Connection Reply.");
  1865. + /* conn_state should be CONN_WAIT_STOP_REPLY, but it
  1866. + * could be something else */
  1867. + if (conn->conn_state == CONN_IDLE) break;
  1868. + conn->conn_state = CONN_IDLE;
  1869. + return -1;
  1870. + }
  1871. + /* ----------- STANDARD Echo/Keepalive MESSAGES ------------ */
  1872. + case PPTP_ECHO_RPLY:
  1873. + {
  1874. + struct pptp_echo_rply *packet =
  1875. + (struct pptp_echo_rply *) buffer;
  1876. + logecho( PPTP_ECHO_RPLY);
  1877. + if ((conn->ka_state == KA_OUTSTANDING) &&
  1878. + (ntoh32(packet->identifier) == conn->ka_id)) {
  1879. + conn->ka_id++;
  1880. + conn->ka_state = KA_NONE;
  1881. + pptp_reset_timer();
  1882. + }
  1883. + break;
  1884. + }
  1885. + case PPTP_ECHO_RQST:
  1886. + {
  1887. + struct pptp_echo_rqst *packet =
  1888. + (struct pptp_echo_rqst *) buffer;
  1889. + struct pptp_echo_rply reply = {
  1890. + PPTP_HEADER_CTRL(PPTP_ECHO_RPLY),
  1891. + packet->identifier, /* skip hton32(ntoh32(id)) */
  1892. + hton8(1), hton8(PPTP_GENERAL_ERROR_NONE), 0
  1893. + };
  1894. + logecho( PPTP_ECHO_RQST);
  1895. + pptp_send_ctrl_packet(conn, &reply, sizeof(reply));
  1896. + pptp_reset_timer();
  1897. + break;
  1898. + }
  1899. + /* ----------- OUTGOING CALL MESSAGES ------------ */
  1900. + case PPTP_OUT_CALL_RQST:
  1901. + {
  1902. + struct pptp_out_call_rqst *packet =
  1903. + (struct pptp_out_call_rqst *)buffer;
  1904. + struct pptp_out_call_rply reply = {
  1905. + PPTP_HEADER_CTRL(PPTP_OUT_CALL_RPLY),
  1906. + 0 /* callid */, packet->call_id, 1, PPTP_GENERAL_ERROR_NONE, 0,
  1907. + hton32(PPTP_CONNECT_SPEED),
  1908. + hton16(PPTP_WINDOW), hton16(PPTP_DELAY), 0
  1909. + };
  1910. + dbglog("Received Outgoing Call Request.");
  1911. + /* XXX PAC: eventually this should make an outgoing call. XXX */
  1912. + reply.result_code = hton8(7); /* outgoing calls verboten */
  1913. + pptp_send_ctrl_packet(conn, &reply, sizeof(reply));
  1914. + break;
  1915. + }
  1916. + case PPTP_OUT_CALL_RPLY:
  1917. + {
  1918. + struct pptp_out_call_rply *packet =
  1919. + (struct pptp_out_call_rply *)buffer;
  1920. + PPTP_CALL * call;
  1921. + u_int16_t callid = ntoh16(packet->call_id_peer);
  1922. + dbglog("Received Outgoing Call Reply.");
  1923. + if (!vector_search(conn->call, (int) callid, &call)) {
  1924. + dbglog("PPTP_OUT_CALL_RPLY received for non-existant call: "
  1925. + "peer call ID (us) %d call ID (them) %d.",
  1926. + callid, ntoh16(packet->call_id));
  1927. + break;
  1928. + }
  1929. + if (call->call_type != PPTP_CALL_PNS) {
  1930. + dbglog("Ack! How did this call_type get here?"); /* XXX? */
  1931. + break;
  1932. + }
  1933. + if (call->state.pns != PNS_WAIT_REPLY) {
  1934. + warn("Unexpected(?) Outgoing Call Reply will be ignored.");
  1935. + break;
  1936. + }
  1937. + /* check for errors */
  1938. + if (packet->result_code != 1) {
  1939. + /* An error. Log it verbosely. */
  1940. + dbglog("Our outgoing call request [callid %d] has not been "
  1941. + "accepted.", (int) callid);
  1942. + ctrlp_error(packet->result_code, packet->error_code,
  1943. + packet->cause_code, pptp_out_call_reply_result,
  1944. + MAX_OUT_CALL_REPLY_RESULT);
  1945. + call->state.pns = PNS_IDLE;
  1946. + if (call->callback != NULL)
  1947. + call->callback(conn, call, CALL_OPEN_FAIL);
  1948. + pptp_call_destroy(conn, call);
  1949. + } else {
  1950. + /* connection established */
  1951. + call->state.pns = PNS_ESTABLISHED;
  1952. + call->peer_call_id = ntoh16(packet->call_id);
  1953. + call->speed = ntoh32(packet->speed);
  1954. + pptp_reset_timer();
  1955. + /* call pptp_set_link. unless the user specified a quirk
  1956. + and this quirk has a set_link hook, this is a noop */
  1957. + pptp_set_link(conn, call->peer_call_id);
  1958. + if (call->callback != NULL)
  1959. + call->callback(conn, call, CALL_OPEN_DONE);
  1960. + dbglog("Outgoing call established (call ID %u, peer's "
  1961. + "call ID %u).\n", call->call_id, call->peer_call_id);
  1962. + }
  1963. + break;
  1964. + }
  1965. + /* ----------- INCOMING CALL MESSAGES ------------ */
  1966. + /* XXX write me XXX */
  1967. + /* ----------- CALL CONTROL MESSAGES ------------ */
  1968. + case PPTP_CALL_CLEAR_RQST:
  1969. + {
  1970. + struct pptp_call_clear_rqst *packet =
  1971. + (struct pptp_call_clear_rqst *)buffer;
  1972. + struct pptp_call_clear_ntfy reply = {
  1973. + PPTP_HEADER_CTRL(PPTP_CALL_CLEAR_NTFY), packet->call_id,
  1974. + 1, PPTP_GENERAL_ERROR_NONE, 0, 0, {0}
  1975. + };
  1976. + dbglog("Received Call Clear Request.");
  1977. + if (vector_contains(conn->call, ntoh16(packet->call_id))) {
  1978. + PPTP_CALL * call;
  1979. + vector_search(conn->call, ntoh16(packet->call_id), &call);
  1980. + if (call->callback != NULL)
  1981. + call->callback(conn, call, CALL_CLOSE_RQST);
  1982. + pptp_send_ctrl_packet(conn, &reply, sizeof(reply));
  1983. + pptp_call_destroy(conn, call);
  1984. + dbglog("Call closed (RQST) (call id %d)", (int) call->call_id);
  1985. + }
  1986. + break;
  1987. + }
  1988. + case PPTP_CALL_CLEAR_NTFY:
  1989. + {
  1990. + struct pptp_call_clear_ntfy *packet =
  1991. + (struct pptp_call_clear_ntfy *)buffer;
  1992. + dbglog("Call disconnect notification received (call id %d)",
  1993. + ntoh16(packet->call_id));
  1994. + if (vector_contains(conn->call, ntoh16(packet->call_id))) {
  1995. + PPTP_CALL * call;
  1996. + ctrlp_error(packet->result_code, packet->error_code,
  1997. + packet->cause_code, pptp_call_disc_ntfy,
  1998. + MAX_CALL_DISC_NTFY);
  1999. + vector_search(conn->call, ntoh16(packet->call_id), &call);
  2000. + pptp_call_destroy(conn, call);
  2001. + }
  2002. + /* XXX we could log call stats here XXX */
  2003. + /* XXX not all servers send this XXX */
  2004. + break;
  2005. + }
  2006. + case PPTP_SET_LINK_INFO:
  2007. + {
  2008. + /* I HAVE NO CLUE WHAT TO DO IF send_accm IS NOT 0! */
  2009. + /* this is really dealt with in the HDLC deencapsulation, anyway. */
  2010. + struct pptp_set_link_info *packet =
  2011. + (struct pptp_set_link_info *)buffer;
  2012. + /* log it. */
  2013. + dbglog("PPTP_SET_LINK_INFO received from peer_callid %u",
  2014. + (unsigned int) ntoh16(packet->call_id_peer));
  2015. + dbglog(" send_accm is %08lX, recv_accm is %08lX",
  2016. + (unsigned long) ntoh32(packet->send_accm),
  2017. + (unsigned long) ntoh32(packet->recv_accm));
  2018. + if (!(ntoh32(packet->send_accm) == 0 &&
  2019. + ntoh32(packet->recv_accm) == 0))
  2020. + warn("Non-zero Async Control Character Maps are not supported!");
  2021. + break;
  2022. + }
  2023. + default:
  2024. + dbglog("Unrecognized Packet %d received.",
  2025. + (int) ntoh16(((struct pptp_header *)buffer)->ctrl_type));
  2026. + /* goto pptp_conn_close; */
  2027. + break;
  2028. + }
  2029. + return 0;
  2030. +pptp_conn_close:
  2031. + warn("pptp_conn_close(%d)", (int) close_reason);
  2032. + pptp_conn_close(conn, close_reason);
  2033. + return 0;
  2034. +}
  2035. +
  2036. +/*** pptp_set_link **************************************************************/
  2037. +void pptp_set_link(PPTP_CONN* conn, int peer_call_id)
  2038. +{
  2039. + int idx, rc;
  2040. + /* if we need to send a set_link packet because of buggy
  2041. + hardware or pptp server, do it now */
  2042. + if ((idx = get_quirk_index()) != -1 && pptp_fixups[idx].set_link_hook) {
  2043. + struct pptp_set_link_info packet;
  2044. + if ((rc = pptp_fixups[idx].set_link_hook(&packet, peer_call_id)))
  2045. + warn("calling the set_link hook failed (%d)", rc);
  2046. + if (pptp_send_ctrl_packet(conn, &packet, sizeof(packet))) {
  2047. + pptp_reset_timer();
  2048. + }
  2049. + }
  2050. +}
  2051. +
  2052. +/*** Get info from call structure *********************************************/
  2053. +/* NOTE: The peer_call_id is undefined until we get a server response. */
  2054. +void pptp_call_get_ids(PPTP_CONN * conn, PPTP_CALL * call,
  2055. + u_int16_t * call_id, u_int16_t * peer_call_id)
  2056. +{
  2057. + assert(conn != NULL); assert(call != NULL);
  2058. + *call_id = call->call_id;
  2059. + *peer_call_id = call->peer_call_id;
  2060. +}
  2061. +
  2062. +/*** pptp_call_closure_put ****************************************************/
  2063. +void pptp_call_closure_put(PPTP_CONN * conn, PPTP_CALL * call, void *cl)
  2064. +{
  2065. + assert(conn != NULL); assert(call != NULL);
  2066. + call->closure = cl;
  2067. +}
  2068. +
  2069. +/*** pptp_call_closure_get ****************************************************/
  2070. +void * pptp_call_closure_get(PPTP_CONN * conn, PPTP_CALL * call)
  2071. +{
  2072. + assert(conn != NULL); assert(call != NULL);
  2073. + return call->closure;
  2074. +}
  2075. +
  2076. +/*** pptp_conn_closure_put ****************************************************/
  2077. +void pptp_conn_closure_put(PPTP_CONN * conn, void *cl)
  2078. +{
  2079. + assert(conn != NULL);
  2080. + conn->closure = cl;
  2081. +}
  2082. +
  2083. +/*** pptp_conn_closure_get ****************************************************/
  2084. +void * pptp_conn_closure_get(PPTP_CONN * conn)
  2085. +{
  2086. + assert(conn != NULL);
  2087. + return conn->closure;
  2088. +}
  2089. +
  2090. +/*** Reset keep-alive timer ***************************************************/
  2091. +static void pptp_reset_timer(void)
  2092. +{
  2093. + const struct itimerval tv = { { 0, 0 }, /* stop on time-out */
  2094. + { idle_wait, 0 } };
  2095. + if (idle_wait) setitimer(ITIMER_REAL, &tv, NULL);
  2096. +}
  2097. +
  2098. +
  2099. +/*** Handle keep-alive timer **************************************************/
  2100. +static void pptp_handle_timer()
  2101. +{
  2102. + int i;
  2103. + /* "Keep Alives and Timers, 1": check connection state */
  2104. + if (global.conn->conn_state != CONN_ESTABLISHED) {
  2105. + if (global.conn->conn_state == CONN_WAIT_STOP_REPLY)
  2106. + /* hard close. */
  2107. + pptp_conn_destroy(global.conn);
  2108. + else /* soft close */
  2109. + pptp_conn_close(global.conn, PPTP_STOP_NONE);
  2110. + }
  2111. + /* "Keep Alives and Timers, 2": check echo status */
  2112. + if (global.conn->ka_state == KA_OUTSTANDING) {
  2113. + /* no response to keep-alive */
  2114. + info("closing control connection due to missing echo reply");
  2115. + pptp_conn_close(global.conn, PPTP_STOP_NONE);
  2116. + } else { /* ka_state == NONE */ /* send keep-alive */
  2117. + struct pptp_echo_rqst rqst = {
  2118. + PPTP_HEADER_CTRL(PPTP_ECHO_RQST), hton32(global.conn->ka_id) };
  2119. + pptp_send_ctrl_packet(global.conn, &rqst, sizeof(rqst));
  2120. + global.conn->ka_state = KA_OUTSTANDING;
  2121. + }
  2122. + /* check incoming/outgoing call states for !IDLE && !ESTABLISHED */
  2123. + for (i = 0; i < vector_size(global.conn->call); i++) {
  2124. + PPTP_CALL * call = vector_get_Nth(global.conn->call, i);
  2125. + if (call->call_type == PPTP_CALL_PNS) {
  2126. + if (call->state.pns == PNS_WAIT_REPLY) {
  2127. + /* send close request */
  2128. + pptp_call_close(global.conn, call);
  2129. + assert(call->state.pns == PNS_WAIT_DISCONNECT);
  2130. + } else if (call->state.pns == PNS_WAIT_DISCONNECT) {
  2131. + /* hard-close the call */
  2132. + pptp_call_destroy(global.conn, call);
  2133. + }
  2134. + } else if (call->call_type == PPTP_CALL_PAC) {
  2135. + if (call->state.pac == PAC_WAIT_REPLY) {
  2136. + /* XXX FIXME -- drop the PAC connection XXX */
  2137. + } else if (call->state.pac == PAC_WAIT_CS_ANS) {
  2138. + /* XXX FIXME -- drop the PAC connection XXX */
  2139. + }
  2140. + }
  2141. + }
  2142. + pptp_reset_timer();
  2143. +}
  2144. --- /dev/null
  2145. +++ b/pppd/plugins/pptp/pptp_ctrl.h
  2146. @@ -0,0 +1,57 @@
  2147. +/* pptp_ctrl.h ... handle PPTP control connection.
  2148. + * C. Scott Ananian <cananian@alumni.princeton.edu>
  2149. + *
  2150. + * $Id: pptp_ctrl.h,v 1.5 2004/11/09 01:42:32 quozl Exp $
  2151. + */
  2152. +
  2153. +#ifndef INC_PPTP_CTRL_H
  2154. +#define INC_PPTP_CTRL_H
  2155. +#include <sys/types.h>
  2156. +
  2157. +typedef struct PPTP_CONN PPTP_CONN;
  2158. +typedef struct PPTP_CALL PPTP_CALL;
  2159. +
  2160. +enum call_state { CALL_OPEN_RQST, CALL_OPEN_DONE, CALL_OPEN_FAIL,
  2161. + CALL_CLOSE_RQST, CALL_CLOSE_DONE };
  2162. +enum conn_state { CONN_OPEN_RQST, CONN_OPEN_DONE, CONN_OPEN_FAIL,
  2163. + CONN_CLOSE_RQST, CONN_CLOSE_DONE };
  2164. +
  2165. +typedef void (*pptp_call_cb)(PPTP_CONN*, PPTP_CALL*, enum call_state);
  2166. +typedef void (*pptp_conn_cb)(PPTP_CONN*, enum conn_state);
  2167. +
  2168. +/* if 'isclient' is true, then will send 'conn open' packet to other host.
  2169. + * not necessary if this is being opened by a server process after
  2170. + * receiving a conn_open packet from client.
  2171. + */
  2172. +PPTP_CONN * pptp_conn_open(int inet_sock, int isclient,
  2173. + pptp_conn_cb callback);
  2174. +PPTP_CALL * pptp_call_open(PPTP_CONN * conn, int call_id,
  2175. + pptp_call_cb callback, char *phonenr,int window);
  2176. +int pptp_conn_established(PPTP_CONN * conn);
  2177. +/* soft close. Will callback on completion. */
  2178. +void pptp_call_close(PPTP_CONN * conn, PPTP_CALL * call);
  2179. +/* hard close. */
  2180. +void pptp_call_destroy(PPTP_CONN *conn, PPTP_CALL *call);
  2181. +/* soft close. Will callback on completion. */
  2182. +void pptp_conn_close(PPTP_CONN * conn, u_int8_t close_reason);
  2183. +/* hard close */
  2184. +void pptp_conn_destroy(PPTP_CONN * conn);
  2185. +
  2186. +/* Add file descriptors used by pptp to fd_set. */
  2187. +void pptp_fd_set(PPTP_CONN * conn, fd_set * read_set, fd_set * write_set, int *max_fd);
  2188. +/* handle any pptp file descriptors set in fd_set, and clear them */
  2189. +int pptp_dispatch(PPTP_CONN * conn, fd_set * read_set, fd_set * write_set);
  2190. +
  2191. +/* Get info about connection, call */
  2192. +void pptp_call_get_ids(PPTP_CONN * conn, PPTP_CALL * call,
  2193. + u_int16_t * call_id, u_int16_t * peer_call_id);
  2194. +/* Arbitrary user data about this call/connection.
  2195. + * It is the caller's responsibility to free this data before calling
  2196. + * pptp_call|conn_close()
  2197. + */
  2198. +void * pptp_conn_closure_get(PPTP_CONN * conn);
  2199. +void pptp_conn_closure_put(PPTP_CONN * conn, void *cl);
  2200. +void * pptp_call_closure_get(PPTP_CONN * conn, PPTP_CALL * call);
  2201. +void pptp_call_closure_put(PPTP_CONN * conn, PPTP_CALL * call, void *cl);
  2202. +
  2203. +#endif /* INC_PPTP_CTRL_H */
  2204. --- /dev/null
  2205. +++ b/pppd/plugins/pptp/pptp_msg.h
  2206. @@ -0,0 +1,303 @@
  2207. +/* pptp.h: packet structures and magic constants for the PPTP protocol
  2208. + * C. Scott Ananian <cananian@alumni.princeton.edu>
  2209. + *
  2210. + * $Id: pptp_msg.h,v 1.3 2003/02/15 10:37:21 quozl Exp $
  2211. + */
  2212. +
  2213. +#ifndef INC_PPTP_H
  2214. +#define INC_PPTP_H
  2215. +
  2216. +/* Grab definitions of int16, int32, etc. */
  2217. +#include <sys/types.h>
  2218. +/* define "portable" htons, etc. */
  2219. +#define hton8(x) (x)
  2220. +#define ntoh8(x) (x)
  2221. +#define hton16(x) htons(x)
  2222. +#define ntoh16(x) ntohs(x)
  2223. +#define hton32(x) htonl(x)
  2224. +#define ntoh32(x) ntohl(x)
  2225. +
  2226. +/* PPTP magic numbers: ----------------------------------------- */
  2227. +
  2228. +#define PPTP_MAGIC 0x1A2B3C4D /* Magic cookie for PPTP datagrams */
  2229. +#define PPTP_PORT 1723 /* PPTP TCP port number */
  2230. +#define PPTP_PROTO 47 /* PPTP IP protocol number */
  2231. +
  2232. +/* Control Connection Message Types: --------------------------- */
  2233. +
  2234. +#define PPTP_MESSAGE_CONTROL 1
  2235. +#define PPTP_MESSAGE_MANAGE 2
  2236. +
  2237. +/* Control Message Types: -------------------------------------- */
  2238. +
  2239. +/* (Control Connection Management) */
  2240. +#define PPTP_START_CTRL_CONN_RQST 1
  2241. +#define PPTP_START_CTRL_CONN_RPLY 2
  2242. +#define PPTP_STOP_CTRL_CONN_RQST 3
  2243. +#define PPTP_STOP_CTRL_CONN_RPLY 4
  2244. +#define PPTP_ECHO_RQST 5
  2245. +#define PPTP_ECHO_RPLY 6
  2246. +
  2247. +/* (Call Management) */
  2248. +#define PPTP_OUT_CALL_RQST 7
  2249. +#define PPTP_OUT_CALL_RPLY 8
  2250. +#define PPTP_IN_CALL_RQST 9
  2251. +#define PPTP_IN_CALL_RPLY 10
  2252. +#define PPTP_IN_CALL_CONNECT 11
  2253. +#define PPTP_CALL_CLEAR_RQST 12
  2254. +#define PPTP_CALL_CLEAR_NTFY 13
  2255. +
  2256. +/* (Error Reporting) */
  2257. +#define PPTP_WAN_ERR_NTFY 14
  2258. +
  2259. +/* (PPP Session Control) */
  2260. +#define PPTP_SET_LINK_INFO 15
  2261. +
  2262. +/* PPTP version information: --------------------------------------*/
  2263. +#define PPTP_VERSION_STRING "1.00"
  2264. +#define PPTP_VERSION 0x100
  2265. +#define PPTP_FIRMWARE_STRING "0.01"
  2266. +#define PPTP_FIRMWARE_VERSION 0x001
  2267. +
  2268. +/* PPTP capabilities: ---------------------------------------------*/
  2269. +
  2270. +/* (Framing capabilities for msg sender) */
  2271. +#define PPTP_FRAME_ASYNC 1
  2272. +#define PPTP_FRAME_SYNC 2
  2273. +#define PPTP_FRAME_ANY 3
  2274. +
  2275. +/* (Bearer capabilities for msg sender) */
  2276. +#define PPTP_BEARER_ANALOG 1
  2277. +#define PPTP_BEARER_DIGITAL 2
  2278. +#define PPTP_BEARER_ANY 3
  2279. +
  2280. +#define PPTP_RESULT_GENERAL_ERROR 2
  2281. +
  2282. +/* (Reasons to close a connection) */
  2283. +#define PPTP_STOP_NONE 1 /* no good reason */
  2284. +#define PPTP_STOP_PROTOCOL 2 /* can't support peer's protocol version */
  2285. +#define PPTP_STOP_LOCAL_SHUTDOWN 3 /* requester is being shut down */
  2286. +
  2287. +/* PPTP datagram structures (all data in network byte order): ----------*/
  2288. +
  2289. +struct pptp_header {
  2290. + u_int16_t length; /* message length in octets, including header */
  2291. + u_int16_t pptp_type; /* PPTP message type. 1 for control message. */
  2292. + u_int32_t magic; /* this should be PPTP_MAGIC. */
  2293. + u_int16_t ctrl_type; /* Control message type (0-15) */
  2294. + u_int16_t reserved0; /* reserved. MUST BE ZERO. */
  2295. +};
  2296. +
  2297. +struct pptp_start_ctrl_conn { /* for control message types 1 and 2 */
  2298. + struct pptp_header header;
  2299. +
  2300. + u_int16_t version; /* PPTP protocol version. = PPTP_VERSION */
  2301. + u_int8_t result_code; /* these two fields should be zero on rqst msg*/
  2302. + u_int8_t error_code; /* 0 unless result_code==2 (General Error) */
  2303. + u_int32_t framing_cap; /* Framing capabilities */
  2304. + u_int32_t bearer_cap; /* Bearer Capabilities */
  2305. + u_int16_t max_channels; /* Maximum Channels (=0 for PNS, PAC ignores) */
  2306. + u_int16_t firmware_rev; /* Firmware or Software Revision */
  2307. + u_int8_t hostname[64]; /* Host Name (64 octets, zero terminated) */
  2308. + u_int8_t vendor[64]; /* Vendor string (64 octets, zero term.) */
  2309. + /* MS says that end of hostname/vendor fields should be filled with */
  2310. + /* octets of value 0, but Win95 PPTP driver doesn't do this. */
  2311. +};
  2312. +
  2313. +struct pptp_stop_ctrl_conn { /* for control message types 3 and 4 */
  2314. + struct pptp_header header;
  2315. +
  2316. + u_int8_t reason_result; /* reason for rqst, result for rply */
  2317. + u_int8_t error_code; /* MUST be 0, unless rply result==2 (general err)*/
  2318. + u_int16_t reserved1; /* MUST be 0 */
  2319. +};
  2320. +
  2321. +struct pptp_echo_rqst { /* for control message type 5 */
  2322. + struct pptp_header header;
  2323. + u_int32_t identifier; /* arbitrary value set by sender which is used */
  2324. + /* to match up reply and request */
  2325. +};
  2326. +
  2327. +struct pptp_echo_rply { /* for control message type 6 */
  2328. + struct pptp_header header;
  2329. + u_int32_t identifier; /* should correspond to id of rqst */
  2330. + u_int8_t result_code;
  2331. + u_int8_t error_code; /* =0, unless result_code==2 (general error) */
  2332. + u_int16_t reserved1; /* MUST BE ZERO */
  2333. +};
  2334. +
  2335. +struct pptp_out_call_rqst { /* for control message type 7 */
  2336. + struct pptp_header header;
  2337. + u_int16_t call_id; /* Call ID (unique id used to multiplex data) */
  2338. + u_int16_t call_sernum; /* Call Serial Number (used for logging) */
  2339. + u_int32_t bps_min; /* Minimum BPS (lowest acceptable line speed) */
  2340. + u_int32_t bps_max; /* Maximum BPS (highest acceptable line speed) */
  2341. + u_int32_t bearer; /* Bearer type */
  2342. + u_int32_t framing; /* Framing type */
  2343. + u_int16_t recv_size; /* Recv. Window Size (no. of buffered packets) */
  2344. + u_int16_t delay; /* Packet Processing Delay (in 1/10 sec) */
  2345. + u_int16_t phone_len; /* Phone Number Length (num. of valid digits) */
  2346. + u_int16_t reserved1; /* MUST BE ZERO */
  2347. + u_int8_t phone_num[64]; /* Phone Number (64 octets, null term.) */
  2348. + u_int8_t subaddress[64]; /* Subaddress (64 octets, null term.) */
  2349. +};
  2350. +
  2351. +struct pptp_out_call_rply { /* for control message type 8 */
  2352. + struct pptp_header header;
  2353. + u_int16_t call_id; /* Call ID (used to multiplex data over tunnel)*/
  2354. + u_int16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/
  2355. + u_int8_t result_code; /* Result Code (1 is no errors) */
  2356. + u_int8_t error_code; /* Error Code (=0 unless result_code==2) */
  2357. + u_int16_t cause_code; /* Cause Code (addt'l failure information) */
  2358. + u_int32_t speed; /* Connect Speed (in BPS) */
  2359. + u_int16_t recv_size; /* Recv. Window Size (no. of buffered packets) */
  2360. + u_int16_t delay; /* Packet Processing Delay (in 1/10 sec) */
  2361. + u_int32_t channel; /* Physical Channel ID (for logging) */
  2362. +};
  2363. +
  2364. +struct pptp_in_call_rqst { /* for control message type 9 */
  2365. + struct pptp_header header;
  2366. + u_int16_t call_id; /* Call ID (unique id used to multiplex data) */
  2367. + u_int16_t call_sernum; /* Call Serial Number (used for logging) */
  2368. + u_int32_t bearer; /* Bearer type */
  2369. + u_int32_t channel; /* Physical Channel ID (for logging) */
  2370. + u_int16_t dialed_len; /* Dialed Number Length (# of valid digits) */
  2371. + u_int16_t dialing_len; /* Dialing Number Length (# of valid digits) */
  2372. + u_int8_t dialed_num[64]; /* Dialed Number (64 octets, zero term.) */
  2373. + u_int8_t dialing_num[64]; /* Dialing Number (64 octets, zero term.) */
  2374. + u_int8_t subaddress[64]; /* Subaddress (64 octets, zero term.) */
  2375. +};
  2376. +
  2377. +struct pptp_in_call_rply { /* for control message type 10 */
  2378. + struct pptp_header header;
  2379. + u_int16_t call_id; /* Call ID (used to multiplex data over tunnel)*/
  2380. + u_int16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/
  2381. + u_int8_t result_code; /* Result Code (1 is no errors) */
  2382. + u_int8_t error_code; /* Error Code (=0 unless result_code==2) */
  2383. + u_int16_t recv_size; /* Recv. Window Size (no. of buffered packets) */
  2384. + u_int16_t delay; /* Packet Processing Delay (in 1/10 sec) */
  2385. + u_int16_t reserved1; /* MUST BE ZERO */
  2386. +};
  2387. +
  2388. +struct pptp_in_call_connect { /* for control message type 11 */
  2389. + struct pptp_header header;
  2390. + u_int16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/
  2391. + u_int16_t reserved1; /* MUST BE ZERO */
  2392. + u_int32_t speed; /* Connect Speed (in BPS) */
  2393. + u_int16_t recv_size; /* Recv. Window Size (no. of buffered packets) */
  2394. + u_int16_t delay; /* Packet Processing Delay (in 1/10 sec) */
  2395. + u_int32_t framing; /* Framing type */
  2396. +};
  2397. +
  2398. +struct pptp_call_clear_rqst { /* for control message type 12 */
  2399. + struct pptp_header header;
  2400. + u_int16_t call_id; /* Call ID (used to multiplex data over tunnel)*/
  2401. + u_int16_t reserved1; /* MUST BE ZERO */
  2402. +};
  2403. +
  2404. +struct pptp_call_clear_ntfy { /* for control message type 13 */
  2405. + struct pptp_header header;
  2406. + u_int16_t call_id; /* Call ID (used to multiplex data over tunnel)*/
  2407. + u_int8_t result_code; /* Result Code */
  2408. + u_int8_t error_code; /* Error Code (=0 unless result_code==2) */
  2409. + u_int16_t cause_code; /* Cause Code (for ISDN, is Q.931 cause code) */
  2410. + u_int16_t reserved1; /* MUST BE ZERO */
  2411. + u_int8_t call_stats[128]; /* Call Statistics: 128 octets, ascii, 0-term */
  2412. +};
  2413. +
  2414. +struct pptp_wan_err_ntfy { /* for control message type 14 */
  2415. + struct pptp_header header;
  2416. + u_int16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/
  2417. + u_int16_t reserved1; /* MUST BE ZERO */
  2418. + u_int32_t crc_errors; /* CRC errors */
  2419. + u_int32_t frame_errors; /* Framing errors */
  2420. + u_int32_t hard_errors; /* Hardware overruns */
  2421. + u_int32_t buff_errors; /* Buffer overruns */
  2422. + u_int32_t time_errors; /* Time-out errors */
  2423. + u_int32_t align_errors; /* Alignment errors */
  2424. +};
  2425. +
  2426. +struct pptp_set_link_info { /* for control message type 15 */
  2427. + struct pptp_header header;
  2428. + u_int16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst) */
  2429. + u_int16_t reserved1; /* MUST BE ZERO */
  2430. + u_int32_t send_accm; /* Send ACCM (for PPP packets; default 0xFFFFFFFF)*/
  2431. + u_int32_t recv_accm; /* Receive ACCM (for PPP pack.;default 0xFFFFFFFF)*/
  2432. +};
  2433. +
  2434. +/* helpful #defines: -------------------------------------------- */
  2435. +#define pptp_isvalid_ctrl(header, type, length) \
  2436. + (!( ( ntoh16(((struct pptp_header *)header)->length) < (length) ) || \
  2437. + ( ntoh16(((struct pptp_header *)header)->pptp_type) !=(type) ) || \
  2438. + ( ntoh32(((struct pptp_header *)header)->magic) !=PPTP_MAGIC) || \
  2439. + ( ntoh16(((struct pptp_header *)header)->ctrl_type) > PPTP_SET_LINK_INFO) || \
  2440. + ( ntoh16(((struct pptp_header *)header)->reserved0) !=0 ) ))
  2441. +
  2442. +#define PPTP_HEADER_CTRL(type) \
  2443. +{ hton16(PPTP_CTRL_SIZE(type)), \
  2444. + hton16(PPTP_MESSAGE_CONTROL), \
  2445. + hton32(PPTP_MAGIC), \
  2446. + hton16(type), 0 }
  2447. +
  2448. +#define PPTP_CTRL_SIZE(type) ( \
  2449. +(type==PPTP_START_CTRL_CONN_RQST)?sizeof(struct pptp_start_ctrl_conn): \
  2450. +(type==PPTP_START_CTRL_CONN_RPLY)?sizeof(struct pptp_start_ctrl_conn): \
  2451. +(type==PPTP_STOP_CTRL_CONN_RQST )?sizeof(struct pptp_stop_ctrl_conn): \
  2452. +(type==PPTP_STOP_CTRL_CONN_RPLY )?sizeof(struct pptp_stop_ctrl_conn): \
  2453. +(type==PPTP_ECHO_RQST )?sizeof(struct pptp_echo_rqst): \
  2454. +(type==PPTP_ECHO_RPLY )?sizeof(struct pptp_echo_rply): \
  2455. +(type==PPTP_OUT_CALL_RQST )?sizeof(struct pptp_out_call_rqst): \
  2456. +(type==PPTP_OUT_CALL_RPLY )?sizeof(struct pptp_out_call_rply): \
  2457. +(type==PPTP_IN_CALL_RQST )?sizeof(struct pptp_in_call_rqst): \
  2458. +(type==PPTP_IN_CALL_RPLY )?sizeof(struct pptp_in_call_rply): \
  2459. +(type==PPTP_IN_CALL_CONNECT )?sizeof(struct pptp_in_call_connect): \
  2460. +(type==PPTP_CALL_CLEAR_RQST )?sizeof(struct pptp_call_clear_rqst): \
  2461. +(type==PPTP_CALL_CLEAR_NTFY )?sizeof(struct pptp_call_clear_ntfy): \
  2462. +(type==PPTP_WAN_ERR_NTFY )?sizeof(struct pptp_wan_err_ntfy): \
  2463. +(type==PPTP_SET_LINK_INFO )?sizeof(struct pptp_set_link_info): \
  2464. +0)
  2465. +#define max(a,b) (((a)>(b))?(a):(b))
  2466. +#define PPTP_CTRL_SIZE_MAX ( \
  2467. +max(sizeof(struct pptp_start_ctrl_conn), \
  2468. +max(sizeof(struct pptp_echo_rqst), \
  2469. +max(sizeof(struct pptp_echo_rply), \
  2470. +max(sizeof(struct pptp_out_call_rqst), \
  2471. +max(sizeof(struct pptp_out_call_rply), \
  2472. +max(sizeof(struct pptp_in_call_rqst), \
  2473. +max(sizeof(struct pptp_in_call_rply), \
  2474. +max(sizeof(struct pptp_in_call_connect), \
  2475. +max(sizeof(struct pptp_call_clear_rqst), \
  2476. +max(sizeof(struct pptp_call_clear_ntfy), \
  2477. +max(sizeof(struct pptp_wan_err_ntfy), \
  2478. +max(sizeof(struct pptp_set_link_info), 0)))))))))))))
  2479. +
  2480. +
  2481. +/* gre header structure: -------------------------------------------- */
  2482. +
  2483. +#define PPTP_GRE_PROTO 0x880B
  2484. +#define PPTP_GRE_VER 0x1
  2485. +
  2486. +#define PPTP_GRE_FLAG_C 0x80
  2487. +#define PPTP_GRE_FLAG_R 0x40
  2488. +#define PPTP_GRE_FLAG_K 0x20
  2489. +#define PPTP_GRE_FLAG_S 0x10
  2490. +#define PPTP_GRE_FLAG_A 0x80
  2491. +
  2492. +#define PPTP_GRE_IS_C(f) ((f)&PPTP_GRE_FLAG_C)
  2493. +#define PPTP_GRE_IS_R(f) ((f)&PPTP_GRE_FLAG_R)
  2494. +#define PPTP_GRE_IS_K(f) ((f)&PPTP_GRE_FLAG_K)
  2495. +#define PPTP_GRE_IS_S(f) ((f)&PPTP_GRE_FLAG_S)
  2496. +#define PPTP_GRE_IS_A(f) ((f)&PPTP_GRE_FLAG_A)
  2497. +
  2498. +struct pptp_gre_header {
  2499. + u_int8_t flags; /* bitfield */
  2500. + u_int8_t ver; /* should be PPTP_GRE_VER (enhanced GRE) */
  2501. + u_int16_t protocol; /* should be PPTP_GRE_PROTO (ppp-encaps) */
  2502. + u_int16_t payload_len; /* size of ppp payload, not inc. gre header */
  2503. + u_int16_t call_id; /* peer's call_id for this session */
  2504. + u_int32_t seq; /* sequence number. Present if S==1 */
  2505. + u_int32_t ack; /* seq number of highest packet recieved by */
  2506. + /* sender in this session */
  2507. +};
  2508. +
  2509. +#endif /* INC_PPTP_H */
  2510. --- /dev/null
  2511. +++ b/pppd/plugins/pptp/pptp_options.h
  2512. @@ -0,0 +1,41 @@
  2513. +/* pptp_options.h ...... various constants used in the PPTP protocol.
  2514. + * #define STANDARD to emulate NT 4.0 exactly.
  2515. + * C. Scott Ananian <cananian@alumni.princeton.edu>
  2516. + *
  2517. + * $Id: pptp_options.h,v 1.3 2004/11/09 01:42:32 quozl Exp $
  2518. + */
  2519. +
  2520. +#ifndef INC_PPTP_OPTIONS_H
  2521. +#define INC_PPTP_OPTIONS_H
  2522. +
  2523. +#undef PPTP_FIRMWARE_STRING
  2524. +#undef PPTP_FIRMWARE_VERSION
  2525. +#define PPTP_BUF_MAX 65536
  2526. +#define PPTP_TIMEOUT 60 /* seconds */
  2527. +extern int idle_wait;
  2528. +extern int max_echo_wait;
  2529. +#define PPTP_CONNECT_SPEED 1000000000
  2530. +#define PPTP_WINDOW 3
  2531. +#define PPTP_DELAY 0
  2532. +#define PPTP_BPS_MIN 2400
  2533. +#define PPTP_BPS_MAX 1000000000
  2534. +
  2535. +#ifndef STANDARD
  2536. +#define PPTP_MAX_CHANNELS 65535
  2537. +#define PPTP_FIRMWARE_STRING "0.01"
  2538. +#define PPTP_FIRMWARE_VERSION 0x001
  2539. +#define PPTP_HOSTNAME {'l','o','c','a','l',0}
  2540. +#define PPTP_VENDOR {'c','a','n','a','n','i','a','n',0}
  2541. +#define PPTP_FRAME_CAP PPTP_FRAME_ANY
  2542. +#define PPTP_BEARER_CAP PPTP_BEARER_ANY
  2543. +#else
  2544. +#define PPTP_MAX_CHANNELS 5
  2545. +#define PPTP_FIRMWARE_STRING "0.01"
  2546. +#define PPTP_FIRMWARE_VERSION 0
  2547. +#define PPTP_HOSTNAME {'l','o','c','a','l',0}
  2548. +#define PPTP_VENDOR {'N','T',0}
  2549. +#define PPTP_FRAME_CAP 2
  2550. +#define PPTP_BEARER_CAP 1
  2551. +#endif
  2552. +
  2553. +#endif /* INC_PPTP_OPTIONS_H */
  2554. --- /dev/null
  2555. +++ b/pppd/plugins/pptp/pptp_quirks.c
  2556. @@ -0,0 +1,54 @@
  2557. +/* pptp_quirks.c ...... various options to fix quirks found in buggy adsl modems
  2558. + * mulix <mulix@actcom.co.il>
  2559. + *
  2560. + * $Id: pptp_quirks.c,v 1.2 2001/11/23 03:42:51 quozl Exp $
  2561. + */
  2562. +
  2563. +#include <string.h>
  2564. +#include "orckit_quirks.h"
  2565. +#include "pptp_quirks.h"
  2566. +
  2567. +static int quirk_index = -1;
  2568. +
  2569. +struct pptp_fixup pptp_fixups[] = {
  2570. + {BEZEQ_ISRAEL, ORCKIT, ORCKIT_ATUR3,
  2571. + orckit_atur3_build_hook,
  2572. + orckit_atur3_start_ctrl_conn_hook,
  2573. + orckit_atur3_set_link_hook}
  2574. +};
  2575. +
  2576. +static int fixups_sz = sizeof(pptp_fixups)/sizeof(pptp_fixups[0]);
  2577. +
  2578. +/* return 0 on success, non 0 otherwise */
  2579. +int set_quirk_index(int index)
  2580. +{
  2581. + if (index >= 0 && index < fixups_sz) {
  2582. + quirk_index = index;
  2583. + return 0;
  2584. + }
  2585. +
  2586. + return -1;
  2587. +}
  2588. +
  2589. +int get_quirk_index()
  2590. +{
  2591. + return quirk_index;
  2592. +}
  2593. +
  2594. +/* return the index for this isp in the quirks table, -1 if not found */
  2595. +int find_quirk(const char* isp_name)
  2596. +{
  2597. + int i = 0;
  2598. + if (isp_name) {
  2599. + while (i < fixups_sz && pptp_fixups[i].isp) {
  2600. + if (!strcmp(pptp_fixups[i].isp, isp_name)) {
  2601. + return i;
  2602. + }
  2603. + ++i;
  2604. + }
  2605. + }
  2606. +
  2607. + return -1;
  2608. +}
  2609. +
  2610. +
  2611. --- /dev/null
  2612. +++ b/pppd/plugins/pptp/pptp_quirks.h
  2613. @@ -0,0 +1,59 @@
  2614. +/* pptp_quirks.h ...... various options to fix quirks found in buggy adsl modems
  2615. + * mulix <mulix@actcom.co.il>
  2616. + *
  2617. + * $Id: pptp_quirks.h,v 1.1 2001/11/20 06:30:10 quozl Exp $
  2618. + */
  2619. +
  2620. +#ifndef INC_PPTP_QUIRKS_H
  2621. +#define INC_PPTP_QUIRKS_H
  2622. +
  2623. +/* isp defs - correspond to slots in the fixups table */
  2624. +#define BEZEQ_ISRAEL "BEZEQ_ISRAEL"
  2625. +
  2626. +/* vendor defs */
  2627. +
  2628. +#define ORCKIT 1
  2629. +#define ALCATEL 2
  2630. +
  2631. +/* device defs */
  2632. +
  2633. +#define ORCKIT_ATUR2 1
  2634. +#define ORCKIT_ATUR3 2
  2635. +
  2636. +#include "pptp_msg.h"
  2637. +#include "pptp_ctrl.h"
  2638. +
  2639. +struct pptp_fixup {
  2640. + const char* isp; /* which isp? e.g. Bezeq in Israel */
  2641. + int vendor; /* which vendor? e.g. Orckit */
  2642. + int device; /* which device? e.g. Orckit Atur3 */
  2643. +
  2644. + /* use this hook to build your own out call request packet */
  2645. + int (*out_call_rqst_hook)(struct pptp_out_call_rqst* packet);
  2646. +
  2647. + /* use this hook to build your own start control connection packet */
  2648. + /* note that this hook is called from two different places, depending
  2649. + on whether this is a request or reply */
  2650. + int (*start_ctrl_conn)(struct pptp_start_ctrl_conn* packet);
  2651. +
  2652. + /* use this hook if you need to send a 'set_link' packet once
  2653. + the connection is established */
  2654. + int (*set_link_hook)(struct pptp_set_link_info* packet,
  2655. + int peer_call_id);
  2656. +};
  2657. +
  2658. +extern struct pptp_fixup pptp_fixups[];
  2659. +
  2660. +/* find the index for this isp in the quirks table */
  2661. +/* return the index on success, -1 if not found */
  2662. +int find_quirk(const char* isp_name);
  2663. +
  2664. +/* set the global quirk index. return 0 on success, non 0 otherwise */
  2665. +int set_quirk_index(int index);
  2666. +
  2667. +/* get the global quirk index. return the index on success,
  2668. + -1 if no quirk is defined */
  2669. +int get_quirk_index();
  2670. +
  2671. +
  2672. +#endif /* INC_PPTP_QUIRKS_H */
  2673. --- /dev/null
  2674. +++ b/pppd/plugins/pptp/util.c
  2675. @@ -0,0 +1,109 @@
  2676. +/* util.c ....... error message utilities.
  2677. + * C. Scott Ananian <cananian@alumni.princeton.edu>
  2678. + *
  2679. + * $Id: util.c,v 1.11 2005/08/22 00:49:48 quozl Exp $
  2680. + */
  2681. +
  2682. +#include <stdio.h>
  2683. +#include <stdarg.h>
  2684. +#include <syslog.h>
  2685. +#include <unistd.h>
  2686. +#include <stdlib.h>
  2687. +#include "util.h"
  2688. +
  2689. +#define MAKE_STRING(label) \
  2690. +va_list ap; \
  2691. +char buf[256], string[256]; \
  2692. +va_start(ap, format); \
  2693. +vsnprintf(buf, sizeof(buf), format, ap); \
  2694. +snprintf(string, sizeof(string), "%s %s[%s:%s:%d]: %s", \
  2695. + log_string, label, func, file, line, buf); \
  2696. +va_end(ap)
  2697. +
  2698. +/*** connect a file to a file descriptor **************************************/
  2699. +int file2fd(const char *path, const char *mode, int fd)
  2700. +{
  2701. + int ok = 0;
  2702. + FILE *file = NULL;
  2703. + file = fopen(path, mode);
  2704. + if (file != NULL && dup2(fileno(file), fd) != -1)
  2705. + ok = 1;
  2706. + if (file) fclose(file);
  2707. + return ok;
  2708. +}
  2709. +
  2710. +/* signal to pipe delivery implementation */
  2711. +#include <unistd.h>
  2712. +#include <fcntl.h>
  2713. +#include <signal.h>
  2714. +#include <string.h>
  2715. +
  2716. +/* pipe private to process */
  2717. +static int sigpipe[2];
  2718. +
  2719. +/* create a signal pipe, returns 0 for success, -1 with errno for failure */
  2720. +int sigpipe_create()
  2721. +{
  2722. + int rc;
  2723. +
  2724. + rc = pipe(sigpipe);
  2725. + if (rc < 0) return rc;
  2726. +
  2727. + fcntl(sigpipe[0], F_SETFD, FD_CLOEXEC);
  2728. + fcntl(sigpipe[1], F_SETFD, FD_CLOEXEC);
  2729. +
  2730. +#ifdef O_NONBLOCK
  2731. +#define FLAG_TO_SET O_NONBLOCK
  2732. +#else
  2733. +#ifdef SYSV
  2734. +#define FLAG_TO_SET O_NDELAY
  2735. +#else /* BSD */
  2736. +#define FLAG_TO_SET FNDELAY
  2737. +#endif
  2738. +#endif
  2739. +
  2740. + rc = fcntl(sigpipe[1], F_GETFL);
  2741. + if (rc != -1)
  2742. + rc = fcntl(sigpipe[1], F_SETFL, rc | FLAG_TO_SET);
  2743. + if (rc < 0) return rc;
  2744. + return 0;
  2745. +#undef FLAG_TO_SET
  2746. +}
  2747. +
  2748. +/* generic handler for signals, writes signal number to pipe */
  2749. +void sigpipe_handler(int signum)
  2750. +{
  2751. + write(sigpipe[1], &signum, sizeof(signum));
  2752. + signal(signum, sigpipe_handler);
  2753. +}
  2754. +
  2755. +/* assign a signal number to the pipe */
  2756. +void sigpipe_assign(int signum)
  2757. +{
  2758. + struct sigaction sa;
  2759. +
  2760. + memset(&sa, 0, sizeof(sa));
  2761. + sa.sa_handler = sigpipe_handler;
  2762. + sigaction(signum, &sa, NULL);
  2763. +}
  2764. +
  2765. +/* return the signal pipe read file descriptor for select(2) */
  2766. +int sigpipe_fd()
  2767. +{
  2768. + return sigpipe[0];
  2769. +}
  2770. +
  2771. +/* read and return the pending signal from the pipe */
  2772. +int sigpipe_read()
  2773. +{
  2774. + int signum;
  2775. + read(sigpipe[0], &signum, sizeof(signum));
  2776. + return signum;
  2777. +}
  2778. +
  2779. +void sigpipe_close()
  2780. +{
  2781. + close(sigpipe[0]);
  2782. + close(sigpipe[1]);
  2783. +}
  2784. +
  2785. --- /dev/null
  2786. +++ b/pppd/plugins/pptp/util.h
  2787. @@ -0,0 +1,31 @@
  2788. +/* util.h ....... error message utilities.
  2789. + * C. Scott Ananian <cananian@alumni.princeton.edu>
  2790. + *
  2791. + * $Id: util.h,v 1.6 2005/03/10 01:18:20 quozl Exp $
  2792. + */
  2793. +
  2794. +#ifndef INC_UTIL_H
  2795. +#define INC_UTIL_H
  2796. +
  2797. +int file2fd(const char *path, const char *mode, int fd);
  2798. +
  2799. +/* signal to pipe delivery implementation */
  2800. +
  2801. +/* create a signal pipe, returns 0 for success, -1 with errno for failure */
  2802. +int sigpipe_create();
  2803. +
  2804. +/* generic handler for signals, writes signal number to pipe */
  2805. +void sigpipe_handler(int signum);
  2806. +
  2807. +/* assign a signal number to the pipe */
  2808. +void sigpipe_assign(int signum);
  2809. +
  2810. +/* return the signal pipe read file descriptor for select(2) */
  2811. +int sigpipe_fd();
  2812. +
  2813. +/* read and return the pending signal from the pipe */
  2814. +int sigpipe_read();
  2815. +
  2816. +void sigpipe_close();
  2817. +
  2818. +#endif /* INC_UTIL_H */
  2819. --- /dev/null
  2820. +++ b/pppd/plugins/pptp/vector.c
  2821. @@ -0,0 +1,209 @@
  2822. +/* vector.c ..... store a vector of PPTP_CALL information and search it
  2823. + * efficiently.
  2824. + * C. Scott Ananian <cananian@alumni.princeton.edu>
  2825. + *
  2826. + * $Id: vector.c,v 1.3 2003/06/17 10:12:55 reink Exp $
  2827. + */
  2828. +
  2829. +#include <stdlib.h>
  2830. +#include <string.h>
  2831. +#include <assert.h>
  2832. +#include "pptp_ctrl.h"
  2833. +#include "vector.h"
  2834. +/* #define VECTOR_DEBUG */
  2835. +#ifndef TRUE
  2836. +#define TRUE 1
  2837. +#endif
  2838. +#ifndef FALSE
  2839. +#define FALSE 0
  2840. +#endif
  2841. +
  2842. +struct vector_item {
  2843. + int key;
  2844. + PPTP_CALL *call;
  2845. +};
  2846. +
  2847. +struct vector_struct {
  2848. + struct vector_item *item;
  2849. + int size;
  2850. + int alloc;
  2851. +#ifdef VECTOR_DEBUG
  2852. + int key_max;
  2853. +#endif
  2854. +};
  2855. +
  2856. +static struct vector_item *binary_search(VECTOR *v, int key);
  2857. +
  2858. +/*** vector_create ************************************************************/
  2859. +VECTOR *vector_create()
  2860. +{
  2861. + const int INITIAL_SIZE = 4;
  2862. +
  2863. + VECTOR *v = malloc(sizeof(*v));
  2864. + if (v == NULL) return v;
  2865. +
  2866. + v->size = 0;
  2867. + v->alloc = INITIAL_SIZE;
  2868. + v->item = malloc(sizeof(*(v->item)) * (v->alloc));
  2869. +#ifdef VECTOR_DEBUG
  2870. + v->key_max = -1;
  2871. +#endif
  2872. + if (v->item == NULL) { free(v); return NULL; }
  2873. + else return v;
  2874. +}
  2875. +
  2876. +/*** vector_destroy ***********************************************************/
  2877. +void vector_destroy(VECTOR *v)
  2878. +{
  2879. + free(v->item);
  2880. +#ifdef VECTOR_DEBUG
  2881. + v->item = NULL;
  2882. +#endif
  2883. + free(v);
  2884. +}
  2885. +
  2886. +/*** vector_size **************************************************************/
  2887. +int vector_size(VECTOR *v)
  2888. +{
  2889. + assert(v != NULL);
  2890. + return v->size;
  2891. +}
  2892. +
  2893. +/*** vector_insert*************************************************************
  2894. + * nice thing about file descriptors is that we are assured by POSIX
  2895. + * that they are monotonically increasing.
  2896. + */
  2897. +int vector_insert(VECTOR *v, int key, PPTP_CALL * call)
  2898. +{
  2899. + int i;
  2900. + assert(v != NULL && call != NULL);
  2901. + assert(!vector_contains(v, key));
  2902. +#ifdef VECTOR_DEBUG
  2903. + assert(v->key_max < key);
  2904. +#endif
  2905. + if (!(v->size < v->alloc)) {
  2906. + void *tmp = realloc(v->item, sizeof(*(v->item)) * 2 * v->alloc);
  2907. + if (tmp != NULL) {
  2908. + v->alloc *= 2;
  2909. + v->item = tmp;
  2910. + } else return FALSE; /* failed to alloc memory. */
  2911. + }
  2912. + assert(v->size < v->alloc);
  2913. + /* for safety, we make this work in the general case;
  2914. + * but this is optimized for adding call to the end of the vector.
  2915. + */
  2916. + for(i = v->size - 1; i >= 0; i--)
  2917. + if (v->item[i].key < key)
  2918. + break;
  2919. + /* insert after item i */
  2920. + memmove(&v->item[i + 2], &v->item[i + 1],
  2921. + (v->size - i - 1) * sizeof(*(v->item)));
  2922. + v->item[i + 1].key = key;
  2923. + v->item[i + 1].call = call;
  2924. + v->size++;
  2925. +#ifdef VECTOR_DEBUG
  2926. + if (v->key_max < key) /* ie, always. */
  2927. + v->key_max = key;
  2928. +#endif
  2929. + return TRUE;
  2930. +}
  2931. +
  2932. +/*** vector_remove ************************************************************/
  2933. +int vector_remove(VECTOR *v, int key)
  2934. +{
  2935. + struct vector_item *tmp;
  2936. + assert(v != NULL);
  2937. + if ((tmp =binary_search(v,key)) == NULL) return FALSE;
  2938. + assert(tmp >= v->item && tmp < v->item + v->size);
  2939. + memmove(tmp, tmp + 1, (v->size - (v->item - tmp) - 1) * sizeof(*(v->item)));
  2940. + v->size--;
  2941. + return TRUE;
  2942. +}
  2943. +
  2944. +/*** vector_search ************************************************************/
  2945. +int vector_search(VECTOR *v, int key, PPTP_CALL **call)
  2946. +{
  2947. + struct vector_item *tmp;
  2948. + assert(v != NULL);
  2949. + tmp = binary_search(v, key);
  2950. + if (tmp ==NULL) return FALSE;
  2951. + *call = tmp->call;
  2952. + return TRUE;
  2953. +}
  2954. +
  2955. +/*** vector_contains **********************************************************/
  2956. +int vector_contains(VECTOR *v, int key)
  2957. +{
  2958. + assert(v != NULL);
  2959. + return (binary_search(v, key) != NULL);
  2960. +}
  2961. +
  2962. +/*** vector_item **************************************************************/
  2963. +static struct vector_item *binary_search(VECTOR *v, int key)
  2964. +{
  2965. + int l,r,x;
  2966. + l = 0;
  2967. + r = v->size - 1;
  2968. + while (r >= l) {
  2969. + x = (l + r)/2;
  2970. + if (key < v->item[x].key) r = x - 1; else l = x + 1;
  2971. + if (key == v->item[x].key) return &(v->item[x]);
  2972. + }
  2973. + return NULL;
  2974. +}
  2975. +
  2976. +/*** vector_scan ***************************************************************
  2977. + * Hmm. Let's be fancy and use a binary search for the first
  2978. + * unused key, taking advantage of the list is stored sorted; ie
  2979. + * we can look at pointers and keys at two different locations,
  2980. + * and if (ptr1 - ptr2) = (key1 - key2) then all the slots
  2981. + * between ptr1 and ptr2 are filled. Note that ptr1-ptr2 should
  2982. + * never be greater than key1-key2 (no duplicate keys!)... we
  2983. + * check for this.
  2984. + */
  2985. +int vector_scan(VECTOR *v, int lo, int hi, int *key)
  2986. +{
  2987. + int l,r,x;
  2988. + assert(v != NULL);
  2989. + assert(key != NULL);
  2990. + if ((v->size<1) || (lo < v->item[0].key)) { *key = lo; return TRUE; }
  2991. + /* our array bounds */
  2992. + l = 0; r = v->size - 1;
  2993. + while (r > l) {
  2994. + /* check for a free spot right after l */
  2995. + if (v->item[l].key + 1 < v->item[l + 1].key) { /* found it! */
  2996. + *key = v->item[l].key + 1;
  2997. + return TRUE;
  2998. + }
  2999. + /* no dice. Let's see if the free spot is before or after the midpoint */
  3000. + x = (l + r)/2;
  3001. + /* Okay, we have right (r), left (l) and the probe (x). */
  3002. + assert(x - l <= v->item[x].key - v->item[l].key);
  3003. + assert(r - x <= v->item[r].key - v->item[x].key);
  3004. + if (x - l < v->item[x].key - v->item[l].key)
  3005. + /* room between l and x */
  3006. + r = x;
  3007. + else /* no room between l and x */
  3008. + if (r - x < v->item[r].key - v->item[x].key)
  3009. + /* room between x and r */
  3010. + l = x;
  3011. + else /* no room between x and r, either */
  3012. + break; /* game over, man. */
  3013. + }
  3014. + /* no room found in already allocated space. Check to see if
  3015. + * there's free space above allocated entries. */
  3016. + if (v->item[v->size - 1].key < hi) {
  3017. + *key = v->item[v->size - 1].key + 1;
  3018. + return TRUE;
  3019. + }
  3020. + /* outta luck */
  3021. + return FALSE;
  3022. +}
  3023. +
  3024. +/*** vector_get_Nth ***********************************************************/
  3025. +PPTP_CALL * vector_get_Nth(VECTOR *v, int n)
  3026. +{
  3027. + assert(v != NULL);
  3028. + assert(0 <= n && n < vector_size(v));
  3029. + return v->item[n].call;
  3030. +}
  3031. --- /dev/null
  3032. +++ b/pppd/plugins/pptp/vector.h
  3033. @@ -0,0 +1,31 @@
  3034. +/* vector.h ..... store a vector of PPTP_CALL information and search it
  3035. + * efficiently.
  3036. + * C. Scott Ananian <cananian@alumni.princeton.edu>
  3037. + *
  3038. + * $Id: vector.h,v 1.1.1.1 2000/12/23 08:19:51 scott Exp $
  3039. + */
  3040. +
  3041. +#ifndef INC_VECTOR_H
  3042. +#define INC_VECTOR_H
  3043. +
  3044. +#include "pptp_ctrl.h" /* for definition of PPTP_CALL */
  3045. +
  3046. +typedef struct vector_struct VECTOR;
  3047. +
  3048. +VECTOR *vector_create();
  3049. +void vector_destroy(VECTOR *v);
  3050. +
  3051. +int vector_size(VECTOR *v);
  3052. +
  3053. +/* vector_insert and vector_search return TRUE on success, FALSE on failure. */
  3054. +int vector_insert(VECTOR *v, int key, PPTP_CALL * call);
  3055. +int vector_remove(VECTOR *v, int key);
  3056. +int vector_search(VECTOR *v, int key, PPTP_CALL ** call);
  3057. +/* vector_contains returns FALSE if not found, TRUE if found. */
  3058. +int vector_contains(VECTOR *v, int key);
  3059. +/* find first unused key. Returns TRUE on success, FALSE if no. */
  3060. +int vector_scan(VECTOR *v, int lo, int hi, int *key);
  3061. +/* get a specific PPTP_CALL ... useful only when iterating. */
  3062. +PPTP_CALL * vector_get_Nth(VECTOR *v, int n);
  3063. +
  3064. +#endif /* INC_VECTOR_H */