500-add-pptp-plugin.patch 106 KB

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