sq_socket.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. #include "squirrel.h"
  2. #include "sqstdblobimpl.h"
  3. #include <string.h>
  4. #include <stdio.h>
  5. SQ_OPT_STRING_STRLEN();
  6. #include "lua_socket.h"
  7. #ifdef _WIN32
  8. #include <windows.h>
  9. #else
  10. #include <time.h>
  11. #include <sys/time.h>
  12. #endif
  13. /* option registry */
  14. typedef struct t_opt {
  15. const char *name;
  16. int (*func)(HSQUIRRELVM v, p_socket ps);
  17. } t_opt;
  18. typedef t_opt *p_opt;
  19. /*=========================================================================*\
  20. * Internal functions prototypes
  21. \*=========================================================================*/
  22. static int opt_setmembership(HSQUIRRELVM v, p_socket ps, int level, int name);
  23. static int opt_setboolean(HSQUIRRELVM v, p_socket ps, int level, int name);
  24. static int opt_set(HSQUIRRELVM v, p_socket ps, int level, int name,
  25. void *val, int len);
  26. /*=========================================================================*\
  27. * Exported functions
  28. \*=========================================================================*/
  29. /*-------------------------------------------------------------------------*\
  30. * Calls appropriate option handler
  31. \*-------------------------------------------------------------------------*/
  32. static int opt_meth_setoption(HSQUIRRELVM v, p_opt opt, p_socket ps)
  33. {
  34. SQ_FUNC_VARS_NO_TOP(v);
  35. SQ_GET_STRING(v, 2, name); /* obj, name, ... */
  36. while (opt->name && strcmp(name, opt->name))
  37. opt++;
  38. if (!opt->func) {
  39. return sq_throwerror(v, _SC("unsupported option `%.35s'"), name);
  40. }
  41. return opt->func(v, ps);
  42. }
  43. /* enables reuse of local address */
  44. static int opt_reuseaddr(HSQUIRRELVM v, p_socket ps)
  45. {
  46. return opt_setboolean(v, ps, SOL_SOCKET, SO_REUSEADDR);
  47. }
  48. /* disables the Naggle algorithm */
  49. static int opt_tcp_nodelay(HSQUIRRELVM v, p_socket ps)
  50. {
  51. return opt_setboolean(v, ps, IPPROTO_TCP, TCP_NODELAY);
  52. }
  53. static int opt_keepalive(HSQUIRRELVM v, p_socket ps)
  54. {
  55. return opt_setboolean(v, ps, SOL_SOCKET, SO_KEEPALIVE);
  56. }
  57. static int opt_dontroute(HSQUIRRELVM v, p_socket ps)
  58. {
  59. return opt_setboolean(v, ps, SOL_SOCKET, SO_DONTROUTE);
  60. }
  61. static int opt_broadcast(HSQUIRRELVM v, p_socket ps)
  62. {
  63. return opt_setboolean(v, ps, SOL_SOCKET, SO_BROADCAST);
  64. }
  65. static int opt_ip_multicast_loop(HSQUIRRELVM v, p_socket ps)
  66. {
  67. return opt_setboolean(v, ps, IPPROTO_IP, IP_MULTICAST_LOOP);
  68. }
  69. static int opt_linger(HSQUIRRELVM v, p_socket ps)
  70. {
  71. SQ_FUNC_VARS_NO_TOP(v);
  72. struct linger li; /* obj, name, table */
  73. if(sq_gettype(v, 3) != OT_TABLE) return sq_throwerror(v, _SC("table expected as parameter 2"));
  74. sq_pushliteral(v, _SC("on"));
  75. sq_get(v, 3);
  76. if (!sq_gettype(v, -1) != OT_BOOL)
  77. return sq_throwerror(v, _SC("boolean 'on' field expected"));
  78. SQ_GET_BOOL(v, -1, bval);
  79. li.l_onoff = (u_short) bval == SQTrue;
  80. sq_pushliteral(v, _SC("timeout"));
  81. sq_get(v, 3);
  82. if (!sq_gettype(v, -1) != OT_INTEGER)
  83. return sq_throwerror(v, _SC("integer 'timeout' field expected"));
  84. SQ_GET_INTEGER(v, -1, ival);
  85. li.l_linger = (u_short) ival;
  86. return opt_set(v, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li));
  87. }
  88. static int opt_ip_multicast_ttl(HSQUIRRELVM v, p_socket ps)
  89. {
  90. SQ_FUNC_VARS_NO_TOP(v);
  91. SQ_GET_INTEGER(v, 3, val); /* obj, name, int */
  92. return opt_set(v, ps, SOL_SOCKET, SO_LINGER, (char *) &val, sizeof(val));
  93. }
  94. static int opt_ip_add_membership(HSQUIRRELVM v, p_socket ps)
  95. {
  96. return opt_setmembership(v, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP);
  97. }
  98. static int opt_ip_drop_membersip(HSQUIRRELVM v, p_socket ps)
  99. {
  100. return opt_setmembership(v, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP);
  101. }
  102. /*=========================================================================*\
  103. * Auxiliar functions
  104. \*=========================================================================*/
  105. /*-------------------------------------------------------------------------*\
  106. * Some systems do not provide this so that we provide our own. It's not
  107. * marvelously fast, but it works just fine.
  108. \*-------------------------------------------------------------------------*/
  109. #ifdef INET_ATON
  110. static int inet_aton(const char *cp, struct in_addr *inp)
  111. {
  112. unsigned int a = 0, b = 0, c = 0, d = 0;
  113. int n = 0, r;
  114. unsigned long int addr = 0;
  115. r = sscanf(cp, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n);
  116. if (r == 0 || n == 0) return 0;
  117. cp += n;
  118. if (*cp) return 0;
  119. if (a > 255 || b > 255 || c > 255 || d > 255) return 0;
  120. if (inp) {
  121. addr += a; addr <<= 8;
  122. addr += b; addr <<= 8;
  123. addr += c; addr <<= 8;
  124. addr += d;
  125. inp->s_addr = htonl(addr);
  126. }
  127. return 1;
  128. }
  129. #endif
  130. #ifdef _WIN32
  131. //#define inet_aton inet_addr
  132. //#define inet_aton(a,b) inet_pton(a,b, AF_INET)
  133. #endif
  134. static SQRESULT socket_open(HSQUIRRELVM v)
  135. {
  136. lua_socket_open();
  137. return 0;
  138. }
  139. static SQRESULT socket_close(HSQUIRRELVM v)
  140. {
  141. lua_socket_close();
  142. return 0;
  143. }
  144. static int opt_setmembership(HSQUIRRELVM v, p_socket ps, int level, int name)
  145. {
  146. SQ_FUNC_VARS_NO_TOP(v);
  147. struct ip_mreq val; /* obj, name, table */
  148. if(sq_gettype(v, 3) != OT_TABLE) return sq_throwerror(v, _SC("table expected as parameter 2"));
  149. sq_pushliteral(v, _SC("multiaddr"));
  150. sq_get(v, 3);
  151. if (!sq_gettype(v, -1) != OT_STRING)
  152. return sq_throwerror(v, _SC("string 'multiaddr' field expected"));
  153. SQ_GET_STRING(v, -1, multiaddr);
  154. if (!inet_aton(multiaddr, &val.imr_multiaddr))
  155. return sq_throwerror(v, _SC("invalid 'multiaddr' ip address"));
  156. sq_pushliteral(v, _SC("interface"));
  157. sq_get(v, 3);
  158. if (!sq_gettype(v, -1) != OT_STRING)
  159. return sq_throwerror(v, _SC("string 'interface' field expected"));
  160. val.imr_interface.s_addr = htonl(INADDR_ANY);
  161. SQ_GET_STRING(v, -1, saddr);
  162. if (strcmp(saddr, "*") &&
  163. !inet_aton(saddr, &val.imr_interface))
  164. return sq_throwerror(v, _SC("invalid 'interface' ip address"));
  165. return opt_set(v, ps, level, name, (char *) &val, sizeof(val));
  166. }
  167. static
  168. int opt_set(HSQUIRRELVM v, p_socket ps, int level, int name, void *val, int len)
  169. {
  170. if (setsockopt(*ps, level, name, (char *) val, len) < 0) {
  171. return sq_throwerror(v, _SC("setsockopt failed"));
  172. }
  173. return 0;
  174. }
  175. static int opt_setboolean(HSQUIRRELVM v, p_socket ps, int level, int name)
  176. {
  177. SQ_FUNC_VARS_NO_TOP(v);
  178. SQ_GET_BOOL(v, 3, bval);
  179. int val = bval == SQTrue; /* obj, name, bool */
  180. return opt_set(v, ps, level, name, (char *) &val, sizeof(val));
  181. }
  182. /*=========================================================================*\
  183. * Global Lua functions
  184. \*=========================================================================*/
  185. /*-------------------------------------------------------------------------*\
  186. * Sets timeout values for IO operations
  187. * Lua Input: base, time [, mode]
  188. * time: time out value in seconds
  189. * mode: "b" for block timeout, "t" for total timeout. (default: b)
  190. \*-------------------------------------------------------------------------*/
  191. static int timeout_meth_settimeout(HSQUIRRELVM v, p_timeout tm) {
  192. SQ_FUNC_VARS(v);
  193. SQ_OPT_INTEGER(v, 2, t, -1);
  194. SQ_OPT_STRING(v, 3, mode, "b");
  195. switch (*mode) {
  196. case _SC('b'):
  197. tm->block = t;
  198. break;
  199. case _SC('r'): case _SC('t'):
  200. tm->total = t;
  201. break;
  202. default:
  203. return sq_throwerror(v, _SC("invalid timeout mode"));
  204. break;
  205. }
  206. return 0;
  207. }
  208. /*=========================================================================*\
  209. * Test support functions
  210. \*=========================================================================*/
  211. /*-------------------------------------------------------------------------*\
  212. * Returns the time the system has been up, in secconds.
  213. \*-------------------------------------------------------------------------*/
  214. static SQRESULT timeout_lua_gettime(HSQUIRRELVM v)
  215. {
  216. sq_pushinteger(v, lua_timeout_gettime());
  217. return 1;
  218. }
  219. /*-------------------------------------------------------------------------*\
  220. * Sleep for n seconds.
  221. \*-------------------------------------------------------------------------*/
  222. static SQRESULT timeout_lua_sleep(HSQUIRRELVM v)
  223. {
  224. SQ_FUNC_VARS_NO_TOP(v);
  225. SQ_GET_FLOAT(v, 2, n);
  226. #ifdef _WIN32
  227. Sleep((int)(n*1000));
  228. #else
  229. struct timespec t, r;
  230. t.tv_sec = (int) n;
  231. n -= t.tv_sec;
  232. t.tv_nsec = (int) (n * 1000000000);
  233. if (t.tv_nsec >= 1000000000) t.tv_nsec = 999999999;
  234. while (nanosleep(&t, &r) != 0) {
  235. t.tv_sec = r.tv_sec;
  236. t.tv_nsec = r.tv_nsec;
  237. }
  238. #endif
  239. return 0;
  240. }
  241. /*-------------------------------------------------------------------------*\
  242. * Sleep for n Milliseconds.
  243. * Clone of the above for murgaLua.
  244. \*-------------------------------------------------------------------------*/
  245. static SQRESULT timeout_lua_sleepMillisecs(HSQUIRRELVM v)
  246. {
  247. SQ_FUNC_VARS_NO_TOP(v);
  248. SQ_GET_FLOAT(v, 2, n);
  249. #ifdef _WIN32
  250. Sleep((int)(n));
  251. #else
  252. struct timespec t, r;
  253. n = n / 1000;
  254. t.tv_sec = (int) n;
  255. n -= t.tv_sec;
  256. t.tv_nsec = (int) (n * 1000000000);
  257. if (t.tv_nsec >= 1000000000) t.tv_nsec = 999999999;
  258. while (nanosleep(&t, &r) != 0) {
  259. t.tv_sec = r.tv_sec;
  260. t.tv_nsec = r.tv_nsec;
  261. }
  262. #endif
  263. return 0;
  264. }
  265. /*=========================================================================*\
  266. * Internal functions
  267. \*=========================================================================*/
  268. /*-------------------------------------------------------------------------*\
  269. * Passes all resolver information to Lua as a table
  270. \*-------------------------------------------------------------------------*/
  271. static void inet_pushresolved(HSQUIRRELVM v, struct hostent *hp)
  272. {
  273. char **alias;
  274. struct in_addr **addr;
  275. int i, resolved;
  276. sq_newtable(v); resolved = sq_gettop(v);
  277. sq_pushliteral(v, _SC("name"));
  278. sq_pushstring(v, hp->h_name, -1);
  279. sq_rawset(v, resolved);
  280. sq_pushliteral(v, _SC("ip"));
  281. sq_pushliteral(v, _SC("alias"));
  282. i = 1;
  283. alias = hp->h_aliases;
  284. sq_newarray(v, 0);
  285. if (alias) {
  286. while (*alias) {
  287. sq_pushstring(v, *alias, -1);
  288. sq_arrayappend(v, -2);
  289. i++; alias++;
  290. }
  291. }
  292. sq_rawset(v, resolved);
  293. i = 1;
  294. sq_newarray(v, 0);
  295. addr = (struct in_addr **) hp->h_addr_list;
  296. if (addr) {
  297. while (*addr) {
  298. sq_pushstring(v, inet_ntoa(**addr), -1);
  299. sq_arrayappend(v, -2);
  300. i++; addr++;
  301. }
  302. }
  303. sq_rawset(v, resolved);
  304. }
  305. /*-------------------------------------------------------------------------*\
  306. * Returns all information provided by the resolver given a host name
  307. * or ip address
  308. \*-------------------------------------------------------------------------*/
  309. static int inet_gethost(const char *address, struct hostent **hp) {
  310. struct in_addr addr;
  311. if (inet_aton(address, &addr))
  312. return lua_socket_gethostbyaddr((char *) &addr, sizeof(addr), hp);
  313. else
  314. return lua_socket_gethostbyname(address, hp);
  315. }
  316. /*-------------------------------------------------------------------------*\
  317. * Returns all information provided by the resolver given a host name
  318. * or ip address
  319. \*-------------------------------------------------------------------------*/
  320. static SQRESULT inet_global_tohostname(HSQUIRRELVM v) {
  321. SQ_FUNC_VARS_NO_TOP(v);
  322. SQ_GET_STRING(v, 2, address);
  323. struct hostent *hp = NULL;
  324. int err = inet_gethost(address, &hp);
  325. if (err != IO_DONE) return sq_throwerror(v, lua_socket_hoststrerror(err));
  326. sq_newtable(v);
  327. sq_pushliteral(v, _SC("name"));
  328. sq_pushstring(v, hp->h_name, -1);
  329. sq_rawset(v, -3);
  330. sq_pushliteral(v, _SC("resolved"));
  331. inet_pushresolved(v, hp);
  332. sq_rawset(v, -3);
  333. return 1;
  334. }
  335. /*-------------------------------------------------------------------------*\
  336. * Returns all information provided by the resolver given a host name
  337. * or ip address
  338. \*-------------------------------------------------------------------------*/
  339. static SQRESULT inet_global_toip(HSQUIRRELVM v)
  340. {
  341. SQ_FUNC_VARS_NO_TOP(v);
  342. SQ_GET_STRING(v, 2, address);
  343. struct hostent *hp = NULL;
  344. int err = inet_gethost(address, &hp);
  345. if (err != IO_DONE) return sq_throwerror(v, lua_socket_hoststrerror(err));
  346. sq_newtable(v);
  347. sq_pushliteral(v, _SC("address"));
  348. sq_pushstring(v, inet_ntoa(*((struct in_addr *) hp->h_addr)), -1);
  349. sq_rawset(v, -3);
  350. sq_pushliteral(v, _SC("resolved"));
  351. inet_pushresolved(v, hp);
  352. sq_rawset(v, -3);
  353. return 1;
  354. }
  355. /*-------------------------------------------------------------------------*\
  356. * Gets the host name
  357. \*-------------------------------------------------------------------------*/
  358. static SQRESULT inet_global_gethostname(HSQUIRRELVM v)
  359. {
  360. char name[257];
  361. name[256] = '\0';
  362. if (gethostname(name, 256) < 0) return sq_throwerror(v, _SC("gethostname failed"));
  363. sq_pushstring(v, name, -1);
  364. return 1;
  365. }
  366. /*=========================================================================*\
  367. * Lua methods
  368. \*=========================================================================*/
  369. /*-------------------------------------------------------------------------*\
  370. * Retrieves socket peer name
  371. \*-------------------------------------------------------------------------*/
  372. static int inet_meth_getpeername(HSQUIRRELVM v, p_socket ps)
  373. {
  374. struct sockaddr_in peer;
  375. socklen_t peer_len = sizeof(peer);
  376. if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) return sq_throwerror(v, _SC("getpeername failed"));
  377. sq_newtable(v);
  378. sq_pushliteral(v, _SC("address"));
  379. sq_pushstring(v, inet_ntoa(peer.sin_addr), -1);
  380. sq_rawset(v, -3);
  381. sq_pushliteral(v, _SC("port"));
  382. sq_pushinteger(v, ntohs(peer.sin_port));
  383. sq_rawset(v, -3);
  384. return 1;
  385. }
  386. /*-------------------------------------------------------------------------*\
  387. * Retrieves socket local name
  388. \*-------------------------------------------------------------------------*/
  389. static int inet_meth_getsockname(HSQUIRRELVM v, p_socket ps)
  390. {
  391. struct sockaddr_in local;
  392. socklen_t local_len = sizeof(local);
  393. if (getsockname(*ps, (SA *) &local, &local_len) < 0) return sq_throwerror(v, _SC("getsockname failed"));
  394. sq_newtable(v);
  395. sq_pushliteral(v, _SC("address"));
  396. sq_pushstring(v, inet_ntoa(local.sin_addr), -1);
  397. sq_rawset(v, -3);
  398. sq_pushliteral(v, _SC("port"));
  399. sq_pushinteger(v, ntohs(local.sin_port));
  400. sq_rawset(v, -3);
  401. return 1;
  402. }
  403. /*-------------------------------------------------------------------------*\
  404. * Tries to create a new inet socket
  405. \*-------------------------------------------------------------------------*/
  406. static const char *inet_trycreate(p_socket ps, int type) {
  407. return lua_socket_strerror(lua_socket_create(ps, AF_INET, type, 0));
  408. }
  409. /*-------------------------------------------------------------------------*\
  410. * Tries to connect to remote address (address, port)
  411. \*-------------------------------------------------------------------------*/
  412. static const char *inet_tryconnect(p_socket ps, const char *address,
  413. unsigned short port, p_timeout tm)
  414. {
  415. struct sockaddr_in remote;
  416. int err;
  417. memset(&remote, 0, sizeof(remote));
  418. remote.sin_family = AF_INET;
  419. remote.sin_port = htons(port);
  420. if (strcmp(address, "*")) {
  421. if (!inet_aton(address, &remote.sin_addr)) {
  422. struct hostent *hp = NULL;
  423. struct in_addr **addr;
  424. err = lua_socket_gethostbyname(address, &hp);
  425. if (err != IO_DONE) return lua_socket_hoststrerror(err);
  426. addr = (struct in_addr **) hp->h_addr_list;
  427. memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));
  428. }
  429. } else remote.sin_family = AF_UNSPEC;
  430. err = lua_socket_connect(ps, (SA *) &remote, sizeof(remote), tm);
  431. return lua_socket_strerror(err);
  432. }
  433. /*-------------------------------------------------------------------------*\
  434. * Tries to bind socket to (address, port)
  435. \*-------------------------------------------------------------------------*/
  436. static const char *inet_trybind(p_socket ps, const char *address, unsigned short port)
  437. {
  438. struct sockaddr_in local;
  439. int err;
  440. memset(&local, 0, sizeof(local));
  441. /* address is either wildcard or a valid ip address */
  442. local.sin_addr.s_addr = htonl(INADDR_ANY);
  443. local.sin_port = htons(port);
  444. local.sin_family = AF_INET;
  445. if (strcmp(address, "*") && !inet_aton(address, &local.sin_addr)) {
  446. struct hostent *hp = NULL;
  447. struct in_addr **addr;
  448. err = lua_socket_gethostbyname(address, &hp);
  449. if (err != IO_DONE) return lua_socket_hoststrerror(err);
  450. addr = (struct in_addr **) hp->h_addr_list;
  451. memcpy(&local.sin_addr, *addr, sizeof(struct in_addr));
  452. }
  453. err = lua_socket_bind(ps, (SA *) &local, sizeof(local));
  454. if (err != IO_DONE) lua_socket_destroy(ps);
  455. return lua_socket_strerror(err);
  456. }
  457. /*=========================================================================*\
  458. * Input/Output interface for Lua programs
  459. * LuaSocket toolkit
  460. *
  461. * Line patterns require buffering. Reading one character at a time involves
  462. * too many system calls and is very slow. This module implements the
  463. * LuaSocket interface for input/output on connected objects, as seen by
  464. * Lua programs.
  465. *
  466. * Input is buffered. Output is *not* buffered because there was no simple
  467. * way of making sure the buffered output data would ever be sent.
  468. *
  469. * The module is built on top of the I/O abstraction defined in io.h and the
  470. * timeout management is done with the timeout.h interface.
  471. *
  472. * Adapted to Squirrel by Domingo Alvarez Duarte on 04/01/2013
  473. *
  474. * RCS ID: $Id: buffer.h,v 1.12 2005/10/07 04:40:59 diego Exp $
  475. \*=========================================================================*/
  476. /* buffer size in bytes */
  477. #define BUF_SIZE 8192
  478. /* buffer control structure */
  479. typedef struct t_buffer_ {
  480. SQFloat birthday; /* throttle support info: creation time, */
  481. SQInteger sent, received; /* bytes sent, and bytes received */
  482. p_io io; /* IO driver used for this buffer */
  483. p_timeout tm; /* timeout management for this buffer */
  484. #ifdef LUASOCKET_DEBUG
  485. int elapsed;
  486. #endif
  487. SQInteger first, last; /* index of first and last bytes of stored data */
  488. char data[BUF_SIZE]; /* storage space for buffer data */
  489. } t_buffer;
  490. typedef t_buffer *p_buffer;
  491. /*=========================================================================*\
  492. * Internal function prototypes
  493. \*=========================================================================*/
  494. static int recvraw(p_buffer buf, size_t wanted, SQBlob &b);
  495. static int recvline(p_buffer buf, SQBlob &b);
  496. static int recvall(p_buffer buf, SQBlob &b);
  497. static int buffer_get(p_buffer buf, const char **data, size_t *count);
  498. static void buffer_skip(p_buffer buf, size_t count);
  499. static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent);
  500. /* min and max macros */
  501. #ifndef MIN
  502. #define MIN(x, y) ((x) < (y) ? x : y)
  503. #endif
  504. #ifndef MAX
  505. #define MAX(x, y) ((x) > (y) ? x : y)
  506. #endif
  507. /*=========================================================================*\
  508. * Exported functions
  509. \*=========================================================================*/
  510. /*-------------------------------------------------------------------------*\
  511. * Initializes module
  512. \*-------------------------------------------------------------------------*/
  513. static SQRESULT buffer_open(HSQUIRRELVM v) {
  514. (void) v;
  515. return 0;
  516. }
  517. /*-------------------------------------------------------------------------*\
  518. * Initializes C structure
  519. \*-------------------------------------------------------------------------*/
  520. static void buffer_init(p_buffer buf, p_io io, p_timeout tm) {
  521. buf->first = buf->last = 0;
  522. buf->io = io;
  523. buf->tm = tm;
  524. buf->received = buf->sent = 0;
  525. buf->birthday = lua_timeout_gettime();
  526. }
  527. /*-------------------------------------------------------------------------*\
  528. * object:getstats() interface
  529. \*-------------------------------------------------------------------------*/
  530. static int buffer_meth_getstats(HSQUIRRELVM v, p_buffer buf) {
  531. sq_newtable(v);
  532. sq_pushliteral(v, _SC("received"));
  533. sq_pushinteger(v, buf->received);
  534. sq_rawset(v, -3);
  535. sq_pushliteral(v, _SC("sent"));
  536. sq_pushinteger(v, buf->sent);
  537. sq_rawset(v, -3);
  538. sq_pushliteral(v, _SC("age"));
  539. sq_pushinteger(v, lua_timeout_gettime() - buf->birthday);
  540. sq_rawset(v, -3);
  541. return 1;
  542. }
  543. /*-------------------------------------------------------------------------*\
  544. * object:setstats() interface
  545. \*-------------------------------------------------------------------------*/
  546. static int buffer_meth_setstats(HSQUIRRELVM v, p_buffer buf) {
  547. SQ_FUNC_VARS(v);
  548. if(sq_getinteger(v, 2, &buf->received) < 0
  549. || sq_getinteger(v, 3, &buf->sent) < 0) return SQ_ERROR;
  550. if(_top_ > 3){
  551. SQ_GET_INTEGER(v, 4, age);
  552. buf->birthday = lua_timeout_gettime() - age;
  553. }
  554. return 0;
  555. }
  556. /*-------------------------------------------------------------------------*\
  557. * object:send() interface
  558. \*-------------------------------------------------------------------------*/
  559. static int buffer_meth_send(HSQUIRRELVM v, p_buffer buf) {
  560. SQ_FUNC_VARS(v);
  561. int err = IO_DONE;
  562. size_t sent = 0;
  563. SQ_GET_STRING(v, 2, data);
  564. SQ_OPT_INTEGER(v, 3, start, 0);
  565. SQ_OPT_INTEGER(v, 4, end, -1);
  566. #ifdef LUASOCKET_DEBUG
  567. p_timeout tm =
  568. #endif
  569. lua_timeout_markstart(buf->tm);
  570. if (start < 0) start = (data_size+start);
  571. if (end < 0) end = (data_size+end+1);
  572. if (start < 0) start = 0;
  573. if (end > data_size) end = data_size;
  574. if (start <= end) err = sendraw(buf, data+start, end-start, &sent);
  575. /* check if there was an error */
  576. if (err != IO_DONE) {
  577. return sq_throwerror(v, buf->io->error(buf->io->ctx, err));
  578. } else {
  579. sq_pushinteger(v, sent+start);
  580. }
  581. #ifdef LUASOCKET_DEBUG
  582. /* push time elapsed during operation as the last return value */
  583. buf->elapsed = timeout_gettime() - timeout_getstart(tm);
  584. #endif
  585. return 1;
  586. }
  587. /*-------------------------------------------------------------------------*\
  588. * object:receive() interface
  589. \*-------------------------------------------------------------------------*/
  590. static int buffer_meth_receive(HSQUIRRELVM v, p_buffer buf) {
  591. SQ_FUNC_VARS(v);
  592. int err = IO_DONE;
  593. SQBlob b(0, 8192);
  594. SQ_OPT_STRING(v, 3, prefix, NULL);
  595. #ifdef LUASOCKET_DEBUG
  596. p_timeout tm =
  597. #endif
  598. lua_timeout_markstart(buf->tm);
  599. /* initialize buffer with optional extra prefix
  600. * (useful for concatenating previous partial results) */
  601. b.Write(prefix, prefix_size);
  602. /* receive new patterns */
  603. SQObjectType ptype = sq_gettype(v, 2);
  604. if (ptype == OT_STRING) {
  605. SQ_GET_STRING(v, 2, p);
  606. if (p[0] == _SC('*') && p[1] == _SC('l')) err = recvline(buf, b);
  607. else if (p[0] == _SC('*') && p[1] == _SC('a')) err = recvall(buf, b);
  608. else return sq_throwerror(v, _SC("invalid receive pattern"));
  609. /* get a fixed number of bytes (minus what was already partially
  610. * received) */
  611. }
  612. else
  613. {
  614. SQ_GET_INTEGER(v, 2, recv_size);
  615. err = recvraw(buf, (size_t) recv_size-prefix_size, b);
  616. }
  617. sq_newarray(v,2);
  618. sq_pushinteger(v, 0);
  619. sq_pushstring(v, (const SQChar*)b.GetBuf(), b.Len());
  620. sq_rawset(v, -3);
  621. sq_pushinteger(v, 1);
  622. sq_pushinteger(v, err);
  623. sq_rawset(v, -3);
  624. /* check if there was an error */
  625. //if (err != IO_DONE) return sq_throwerror(v, buf->io->error(buf->io->ctx, err));
  626. #ifdef LUASOCKET_DEBUG
  627. /* push time elapsed during operation as the last return value */
  628. buf->elapsed = timeout_gettime() - timeout_getstart(tm);
  629. #endif
  630. return 1;
  631. }
  632. /*-------------------------------------------------------------------------*\
  633. * Determines if there is any data in the read buffer
  634. \*-------------------------------------------------------------------------*/
  635. static int buffer_isempty(p_buffer buf) {
  636. return buf->first >= buf->last;
  637. }
  638. /*=========================================================================*\
  639. * Internal functions
  640. \*=========================================================================*/
  641. /*-------------------------------------------------------------------------*\
  642. * Sends a block of data (unbuffered)
  643. \*-------------------------------------------------------------------------*/
  644. #define STEPSIZE 8192
  645. static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent) {
  646. p_io io = buf->io;
  647. p_timeout tm = buf->tm;
  648. size_t total = 0;
  649. int err = IO_DONE;
  650. while (total < count && err == IO_DONE) {
  651. size_t done;
  652. size_t step = (count-total <= STEPSIZE)? count-total: STEPSIZE;
  653. err = io->send(io->ctx, data+total, step, &done, tm);
  654. total += done;
  655. }
  656. *sent = total;
  657. buf->sent += total;
  658. return err;
  659. }
  660. /*-------------------------------------------------------------------------*\
  661. * Reads a fixed number of bytes (buffered)
  662. \*-------------------------------------------------------------------------*/
  663. static int recvraw(p_buffer buf, size_t wanted, SQBlob &b) {
  664. int err = IO_DONE;
  665. size_t total = 0;
  666. while (err == IO_DONE) {
  667. size_t count; const char *data;
  668. err = buffer_get(buf, &data, &count);
  669. count = MIN(count, wanted - total);
  670. b.Write(data, count);
  671. buffer_skip(buf, count);
  672. total += count;
  673. if (total >= wanted) break;
  674. }
  675. return err;
  676. }
  677. /*-------------------------------------------------------------------------*\
  678. * Reads everything until the connection is closed (buffered)
  679. \*-------------------------------------------------------------------------*/
  680. static int recvall(p_buffer buf, SQBlob &b) {
  681. int err = IO_DONE;
  682. size_t total = 0;
  683. while (err == IO_DONE) {
  684. const char *data; size_t count;
  685. err = buffer_get(buf, &data, &count);
  686. total += count;
  687. b.Write(data, count);
  688. buffer_skip(buf, count);
  689. }
  690. if (err == IO_CLOSED) {
  691. if (total > 0) return IO_DONE;
  692. else return IO_CLOSED;
  693. } else return err;
  694. }
  695. /*-------------------------------------------------------------------------*\
  696. * Reads a line terminated by a CR LF pair or just by a LF. The CR and LF
  697. * are not returned by the function and are discarded from the buffer
  698. \*-------------------------------------------------------------------------*/
  699. static int recvline(p_buffer buf, SQBlob &b) {
  700. int err = IO_DONE;
  701. while (err == IO_DONE) {
  702. size_t count, pos; const char *data;
  703. err = buffer_get(buf, &data, &count);
  704. pos = 0;
  705. while (pos < count && data[pos] != '\n') {
  706. /* we ignore all \r's */
  707. if (data[pos] != '\r') b.WriteChar(data[pos]);
  708. pos++;
  709. }
  710. if (pos < count) { /* found '\n' */
  711. buffer_skip(buf, pos+1); /* skip '\n' too */
  712. break; /* we are done */
  713. } else /* reached the end of the buffer */
  714. buffer_skip(buf, pos);
  715. }
  716. return err;
  717. }
  718. /*-------------------------------------------------------------------------*\
  719. * Skips a given number of bytes from read buffer. No data is read from the
  720. * transport layer
  721. \*-------------------------------------------------------------------------*/
  722. static void buffer_skip(p_buffer buf, size_t count) {
  723. buf->received += count;
  724. buf->first += count;
  725. if (buffer_isempty(buf))
  726. buf->first = buf->last = 0;
  727. }
  728. /*-------------------------------------------------------------------------*\
  729. * Return any data available in buffer, or get more data from transport layer
  730. * if buffer is empty
  731. \*-------------------------------------------------------------------------*/
  732. static int buffer_get(p_buffer buf, const char **data, size_t *count) {
  733. int err = IO_DONE;
  734. p_io io = buf->io;
  735. p_timeout tm = buf->tm;
  736. if (buffer_isempty(buf)) {
  737. size_t got;
  738. err = io->recv(io->ctx, buf->data, BUF_SIZE, &got, tm);
  739. buf->first = 0;
  740. buf->last = got;
  741. }
  742. *count = buf->last - buf->first;
  743. *data = buf->data + buf->first;
  744. return err;
  745. }
  746. /*=========================================================================*\
  747. * Lua methods
  748. \*=========================================================================*/
  749. #define TCP_TYPE_ANY 0
  750. #define TCP_TYPE_CLIENT 1
  751. #define TCP_TYPE_SERVER 2
  752. #define TCP_TYPE_MASTER 3
  753. typedef struct t_tcp_ {
  754. t_socket sock;
  755. t_io io;
  756. t_buffer buf;
  757. t_timeout tm;
  758. int type;
  759. } t_tcp;
  760. typedef t_tcp *p_tcp;
  761. static const SQChar SQ_tcp_TAG[] = _SC("sq_tcp");
  762. #define GET_tcp_INSTANCE_AT(v, idx) \
  763. p_tcp tcp; \
  764. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)&tcp,(void*)SQ_tcp_TAG)) < 0) return _rc_;
  765. #define GET_tcp_client_INSTANCE_AT(v, idx) GET_tcp_INSTANCE_AT(v, idx)\
  766. if(tcp->type != TCP_TYPE_CLIENT) return sq_throwerror(v, _SC("tcp client expected"));
  767. #define GET_tcp_server_INSTANCE_AT(v, idx) GET_tcp_INSTANCE_AT(v, idx)\
  768. if(tcp->type != TCP_TYPE_SERVER) return sq_throwerror(v, _SC("tcp server expected"));
  769. #define GET_tcp_master_INSTANCE_AT(v, idx) GET_tcp_INSTANCE_AT(v, idx)\
  770. if(tcp->type != TCP_TYPE_MASTER) return sq_throwerror(v, _SC("tcp master expected"));
  771. /*-------------------------------------------------------------------------*\
  772. * Just call buffered IO methods
  773. \*-------------------------------------------------------------------------*/
  774. /* socket tcp option handlers */
  775. static t_opt tcp_opt[] = {
  776. {"keepalive", opt_keepalive},
  777. {"reuseaddr", opt_reuseaddr},
  778. {"tcp-nodelay", opt_tcp_nodelay},
  779. {"linger", opt_linger},
  780. {NULL, NULL}
  781. };
  782. static SQRESULT tcp_meth_send(HSQUIRRELVM v) {
  783. SQ_FUNC_VARS_NO_TOP(v);
  784. GET_tcp_client_INSTANCE_AT(v, 1);
  785. return buffer_meth_send(v, &tcp->buf);
  786. }
  787. static SQRESULT tcp_meth_receive(HSQUIRRELVM v) {
  788. SQ_FUNC_VARS_NO_TOP(v);
  789. GET_tcp_client_INSTANCE_AT(v, 1);
  790. return buffer_meth_receive(v, &tcp->buf);
  791. }
  792. static SQRESULT tcp_meth_getstats(HSQUIRRELVM v) {
  793. SQ_FUNC_VARS_NO_TOP(v);
  794. GET_tcp_client_INSTANCE_AT(v, 1);
  795. return buffer_meth_getstats(v, &tcp->buf);
  796. }
  797. static SQRESULT tcp_meth_setstats(HSQUIRRELVM v) {
  798. SQ_FUNC_VARS_NO_TOP(v);
  799. GET_tcp_client_INSTANCE_AT(v, 1);
  800. return buffer_meth_setstats(v, &tcp->buf);
  801. }
  802. /*-------------------------------------------------------------------------*\
  803. * Just call option handler
  804. \*-------------------------------------------------------------------------*/
  805. static SQRESULT tcp_meth_setoption(HSQUIRRELVM v)
  806. {
  807. SQ_FUNC_VARS_NO_TOP(v);
  808. GET_tcp_INSTANCE_AT(v, 1);
  809. return opt_meth_setoption(v, tcp_opt, &tcp->sock);
  810. }
  811. /*-------------------------------------------------------------------------*\
  812. * Select support methods
  813. \*-------------------------------------------------------------------------*/
  814. static SQRESULT tcp_meth_getfd(HSQUIRRELVM v)
  815. {
  816. SQ_FUNC_VARS_NO_TOP(v);
  817. GET_tcp_INSTANCE_AT(v, 1);
  818. sq_pushinteger(v, (int) tcp->sock);
  819. return 1;
  820. }
  821. /* this is very dangerous, but can be handy for those that are brave enough */
  822. static SQRESULT tcp_meth_setfd(HSQUIRRELVM v)
  823. {
  824. SQ_FUNC_VARS_NO_TOP(v);
  825. GET_tcp_INSTANCE_AT(v, 1);
  826. SQ_GET_INTEGER(v, 2, fd);
  827. tcp->sock = (t_socket) fd;
  828. return 0;
  829. }
  830. static SQRESULT tcp_meth_dirty(HSQUIRRELVM v)
  831. {
  832. SQ_FUNC_VARS_NO_TOP(v);
  833. GET_tcp_INSTANCE_AT(v, 1);
  834. sq_pushbool(v, !buffer_isempty(&tcp->buf));
  835. return 1;
  836. }
  837. /*=========================================================================*\
  838. * Library functions
  839. \*=========================================================================*/
  840. /*-------------------------------------------------------------------------*\
  841. * Creates a master tcp object
  842. \*-------------------------------------------------------------------------*/
  843. static SQRESULT tcp_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  844. {
  845. p_tcp tcp = (p_tcp)p;
  846. if(tcp) {
  847. lua_socket_destroy(&tcp->sock);
  848. sq_free(tcp, sizeof(t_tcp));
  849. }
  850. return 0;
  851. }
  852. static int tcp_constructor_for_socket(HSQUIRRELVM v, int idx, t_socket sock, int type)
  853. {
  854. /* allocate tcp object */
  855. p_tcp tcp = (p_tcp) sq_malloc(sizeof(t_tcp));
  856. /* set its type as master object */
  857. tcp->type = type;
  858. /* initialize remaining structure fields */
  859. lua_socket_setnonblocking(&sock);
  860. tcp->sock = sock;
  861. lua_io_init(&tcp->io, (p_send) lua_socket_send, (p_recv) lua_socket_recv,
  862. (p_error) lua_socket_ioerror, &tcp->sock);
  863. lua_timeout_init(&tcp->tm, -1, -1);
  864. buffer_init(&tcp->buf, &tcp->io, &tcp->tm);
  865. sq_setinstanceup(v, idx, tcp);
  866. sq_setreleasehook(v, idx, tcp_releasehook);
  867. return 1;
  868. }
  869. static SQRESULT tcp_constructor(HSQUIRRELVM v)
  870. {
  871. t_socket sock;
  872. const char *err = inet_trycreate(&sock, SOCK_STREAM);
  873. /* try to allocate a system socket */
  874. if (!err) {
  875. return tcp_constructor_for_socket(v, 1, sock, TCP_TYPE_MASTER);
  876. }
  877. return sq_throwerror(v, err);
  878. }
  879. /*-------------------------------------------------------------------------*\
  880. * Waits for and returns a client object attempting connection to the
  881. * server object
  882. \*-------------------------------------------------------------------------*/
  883. static SQRESULT tcp_meth_accept(HSQUIRRELVM v)
  884. {
  885. SQ_FUNC_VARS_NO_TOP(v);
  886. GET_tcp_server_INSTANCE_AT(v, 1);
  887. p_timeout tm = lua_timeout_markstart(&tcp->tm);
  888. t_socket sock;
  889. int err = lua_socket_accept(&tcp->sock, &sock, NULL, NULL, tm);
  890. /* if successful, push client socket */
  891. if (err == IO_DONE) {
  892. sq_pushstring(v,_SC("socket"),-1);
  893. int rc = sq_getonroottable(v);
  894. if(rc < 0) return rc;
  895. sq_pushstring(v,_SC("tcp"),-1);
  896. rc = sq_rawget(v, -2);
  897. if(rc < 0) return rc;
  898. rc = sq_createinstance(v, -1);
  899. if(rc < 0) return rc;
  900. tcp_constructor_for_socket(v, sq_gettop(v), sock, TCP_TYPE_CLIENT);
  901. } else {
  902. return sq_throwerror(v, lua_socket_strerror(err));
  903. }
  904. return 1;
  905. }
  906. /*-------------------------------------------------------------------------*\
  907. * Binds an object to an address
  908. \*-------------------------------------------------------------------------*/
  909. static SQRESULT tcp_meth_bind(HSQUIRRELVM v)
  910. {
  911. SQ_FUNC_VARS_NO_TOP(v);
  912. GET_tcp_master_INSTANCE_AT(v, 1);
  913. SQ_GET_STRING(v, 2, address);
  914. SQ_GET_INTEGER(v, 3, port);
  915. const char *err = inet_trybind(&tcp->sock, address, port);
  916. if (err) return sq_throwerror(v, err);
  917. return 0;
  918. }
  919. /*-------------------------------------------------------------------------*\
  920. * Turns a master tcp object into a client object.
  921. \*-------------------------------------------------------------------------*/
  922. static SQRESULT tcp_meth_connect(HSQUIRRELVM v)
  923. {
  924. SQ_FUNC_VARS_NO_TOP(v);
  925. GET_tcp_INSTANCE_AT(v, 1);
  926. SQ_GET_STRING(v, 2, address);
  927. SQ_GET_INTEGER(v, 3, port);
  928. p_timeout tm = lua_timeout_markstart(&tcp->tm);
  929. const char *err = inet_tryconnect(&tcp->sock, address, port, tm);
  930. /* have to set the class even if it failed due to non-blocking connects */
  931. if (err) return sq_throwerror(v, err);
  932. tcp->type = TCP_TYPE_CLIENT;
  933. return 0;
  934. }
  935. /*-------------------------------------------------------------------------*\
  936. * Closes socket used by object
  937. \*-------------------------------------------------------------------------*/
  938. static SQRESULT tcp_meth_close(HSQUIRRELVM v)
  939. {
  940. SQ_FUNC_VARS_NO_TOP(v);
  941. GET_tcp_INSTANCE_AT(v, 1);
  942. lua_socket_destroy(&tcp->sock);
  943. return 0;
  944. }
  945. /*-------------------------------------------------------------------------*\
  946. * Puts the sockt in listen mode
  947. \*-------------------------------------------------------------------------*/
  948. static SQRESULT tcp_meth_listen(HSQUIRRELVM v)
  949. {
  950. SQ_FUNC_VARS(v);
  951. GET_tcp_master_INSTANCE_AT(v, 1);
  952. SQ_OPT_INTEGER(v, 2, backlog, 32);
  953. int err = lua_socket_listen(&tcp->sock, backlog);
  954. if (err != IO_DONE) return sq_throwerror(v, lua_socket_strerror(err));
  955. /* turn master object into a server object */
  956. tcp->type = TCP_TYPE_SERVER;
  957. return 0;
  958. }
  959. /*-------------------------------------------------------------------------*\
  960. * Shuts the connection down partially
  961. \*-------------------------------------------------------------------------*/
  962. static SQRESULT tcp_meth_shutdown(HSQUIRRELVM v)
  963. {
  964. SQ_FUNC_VARS(v);
  965. GET_tcp_client_INSTANCE_AT(v, 1);
  966. SQ_OPT_STRING(v, 2, how, _SC("both"));
  967. switch (how[0]) {
  968. case _SC('b'):
  969. if (strcmp(how, "both")) goto error;
  970. lua_socket_shutdown(&tcp->sock, 2);
  971. break;
  972. case _SC('s'):
  973. if (strcmp(how, "send")) goto error;
  974. lua_socket_shutdown(&tcp->sock, 1);
  975. break;
  976. case _SC('r'):
  977. if (strcmp(how, "receive")) goto error;
  978. lua_socket_shutdown(&tcp->sock, 0);
  979. break;
  980. }
  981. return 0;
  982. error:
  983. return sq_throwerror(v, _SC("invalid shutdown method"));
  984. }
  985. /*-------------------------------------------------------------------------*\
  986. * Just call inet methods
  987. \*-------------------------------------------------------------------------*/
  988. static SQRESULT tcp_meth_getpeername(HSQUIRRELVM v)
  989. {
  990. SQ_FUNC_VARS_NO_TOP(v);
  991. GET_tcp_INSTANCE_AT(v, 1);
  992. return inet_meth_getpeername(v, &tcp->sock);
  993. }
  994. static SQRESULT tcp_meth_getsockname(HSQUIRRELVM v)
  995. {
  996. SQ_FUNC_VARS_NO_TOP(v);
  997. GET_tcp_INSTANCE_AT(v, 1);
  998. return inet_meth_getsockname(v, &tcp->sock);
  999. }
  1000. /*-------------------------------------------------------------------------*\
  1001. * Just call tm methods
  1002. \*-------------------------------------------------------------------------*/
  1003. static SQRESULT tcp_meth_settimeout(HSQUIRRELVM v)
  1004. {
  1005. SQ_FUNC_VARS_NO_TOP(v);
  1006. GET_tcp_INSTANCE_AT(v, 1);
  1007. return timeout_meth_settimeout(v, &tcp->tm);
  1008. }
  1009. /*=========================================================================*\
  1010. * Lua udp methods
  1011. \*=========================================================================*/
  1012. /* can't be larger than wsocket.c MAXCHUNK!!! */
  1013. #define UDP_DATAGRAMSIZE 8192
  1014. #define UDP_TYPE_ANY 0
  1015. #define UDP_TYPE_CONNECTED 1
  1016. #define UDP_TYPE_UNCONNECTED 2
  1017. typedef struct t_udp_ {
  1018. t_socket sock;
  1019. t_timeout tm;
  1020. int type;
  1021. } t_udp;
  1022. typedef t_udp *p_udp;
  1023. /* socket udp options */
  1024. static t_opt udp_opt[] = {
  1025. {"dontroute", opt_dontroute},
  1026. {"broadcast", opt_broadcast},
  1027. {"reuseaddr", opt_reuseaddr},
  1028. {"ip-multicast-ttl", opt_ip_multicast_ttl},
  1029. {"ip-multicast-loop", opt_ip_multicast_loop},
  1030. {"ip-add-membership", opt_ip_add_membership},
  1031. {"ip-drop-membership", opt_ip_drop_membersip},
  1032. {NULL, NULL}
  1033. };
  1034. static const SQChar SQ_udp_TAG[] = _SC("sq_udp");
  1035. #define GET_udp_INSTANCE_AT(v, idx) \
  1036. p_udp udp; \
  1037. if((_rc_ = sq_getinstanceup(v,idx,(SQUserPointer*)&udp,(void*)SQ_udp_TAG)) < 0) return _rc_;
  1038. #define GET_udp_connected_INSTANCE_AT(v, idx) GET_udp_INSTANCE_AT(v, idx)\
  1039. if(udp->type != UDP_TYPE_CONNECTED) return sq_throwerror(v, _SC("udp connected expected"));
  1040. #define GET_udp_unconnected_INSTANCE_AT(v, idx) GET_udp_INSTANCE_AT(v, idx)\
  1041. if(udp->type != UDP_TYPE_UNCONNECTED) return sq_throwerror(v, _SC("udp unconnected expected"));
  1042. static const char *udp_strerror(int err) {
  1043. /* a 'closed' error on an unconnected means the target address was not
  1044. * accepted by the transport layer */
  1045. if (err == IO_CLOSED) return "refused";
  1046. else return lua_socket_strerror(err);
  1047. }
  1048. /*-------------------------------------------------------------------------*\
  1049. * Send data through connected udp socket
  1050. \*-------------------------------------------------------------------------*/
  1051. static SQRESULT udp_meth_send(HSQUIRRELVM v) {
  1052. SQ_FUNC_VARS_NO_TOP(v);
  1053. GET_udp_connected_INSTANCE_AT(v, 1);
  1054. p_timeout tm = &udp->tm;
  1055. size_t sent = 0;
  1056. int err;
  1057. SQ_GET_STRING(v, 2, data);
  1058. lua_timeout_markstart(tm);
  1059. err = lua_socket_send(&udp->sock, data, data_size, &sent, tm);
  1060. if (err != IO_DONE) return sq_throwerror(v, udp_strerror(err));
  1061. sq_pushinteger(v, sent);
  1062. return 1;
  1063. }
  1064. /*-------------------------------------------------------------------------*\
  1065. * Send data through unconnected udp socket
  1066. \*-------------------------------------------------------------------------*/
  1067. static SQRESULT udp_meth_sendto(HSQUIRRELVM v) {
  1068. SQ_FUNC_VARS_NO_TOP(v);
  1069. GET_udp_unconnected_INSTANCE_AT(v, 1);
  1070. size_t sent = 0;
  1071. SQ_GET_STRING(v, 2, data);
  1072. SQ_GET_STRING(v, 3, ip);
  1073. SQ_GET_INTEGER(v, 4, port);
  1074. p_timeout tm = &udp->tm;
  1075. struct sockaddr_in addr;
  1076. int err;
  1077. memset(&addr, 0, sizeof(addr));
  1078. if (!inet_aton(ip, &addr.sin_addr))
  1079. return sq_throwerror(v, _SC("invalid ip address"));
  1080. addr.sin_family = AF_INET;
  1081. addr.sin_port = htons(port);
  1082. lua_timeout_markstart(tm);
  1083. err = lua_socket_sendto(&udp->sock, data, data_size, &sent,
  1084. (SA *) &addr, sizeof(addr), tm);
  1085. if (err != IO_DONE) return sq_throwerror(v, udp_strerror(err));
  1086. sq_pushinteger(v, sent);
  1087. return 1;
  1088. }
  1089. /*-------------------------------------------------------------------------*\
  1090. * Receives data from a UDP socket
  1091. \*-------------------------------------------------------------------------*/
  1092. static SQRESULT udp_meth_receive(HSQUIRRELVM v) {
  1093. SQ_FUNC_VARS(v);
  1094. GET_udp_INSTANCE_AT(v, 1);
  1095. char buffer[UDP_DATAGRAMSIZE];
  1096. SQ_OPT_INTEGER(v, 2, count, sizeof(buffer));
  1097. if(count <= 0) return sq_throwerror(v, _SC("invalid size for receive (%d)"), count);
  1098. size_t got;
  1099. int err;
  1100. p_timeout tm = &udp->tm;
  1101. count = MIN(count, sizeof(buffer));
  1102. lua_timeout_markstart(tm);
  1103. err = lua_socket_recv(&udp->sock, buffer, count, &got, tm);
  1104. if (err != IO_DONE) return sq_throwerror(v, udp_strerror(err));
  1105. sq_pushstring(v, buffer, got);
  1106. return 1;
  1107. }
  1108. /*-------------------------------------------------------------------------*\
  1109. * Receives data and sender from a UDP socket
  1110. \*-------------------------------------------------------------------------*/
  1111. static SQRESULT udp_meth_receivefrom(HSQUIRRELVM v) {
  1112. SQ_FUNC_VARS(v);
  1113. GET_udp_unconnected_INSTANCE_AT(v, 1);
  1114. struct sockaddr_in addr;
  1115. socklen_t addr_len = sizeof(addr);
  1116. char buffer[UDP_DATAGRAMSIZE];
  1117. size_t got;
  1118. SQ_OPT_INTEGER(v, 2, count, sizeof(buffer));
  1119. if(count <= 0) return sq_throwerror(v, _SC("invalid size for receive (%d)"), count);
  1120. int err;
  1121. p_timeout tm = &udp->tm;
  1122. lua_timeout_markstart(tm);
  1123. count = MIN(count, sizeof(buffer));
  1124. err = lua_socket_recvfrom(&udp->sock, buffer, count, &got,
  1125. (SA *) &addr, &addr_len, tm);
  1126. if (err == IO_DONE) {
  1127. sq_newtable(v);
  1128. sq_pushliteral(v, _SC("data"));
  1129. sq_pushstring(v, buffer, got);
  1130. sq_rawset(v, -3);
  1131. sq_pushliteral(v, _SC("address"));
  1132. sq_pushstring(v, inet_ntoa(addr.sin_addr), -1);
  1133. sq_rawset(v, -3);
  1134. sq_pushliteral(v, _SC("port"));
  1135. sq_pushinteger(v, ntohs(addr.sin_port));
  1136. sq_rawset(v, -3);
  1137. return 1;
  1138. }
  1139. else return sq_throwerror(v, udp_strerror(err));
  1140. }
  1141. /*-------------------------------------------------------------------------*\
  1142. * Select support methods
  1143. \*-------------------------------------------------------------------------*/
  1144. static SQRESULT udp_meth_getfd(HSQUIRRELVM v) {
  1145. SQ_FUNC_VARS_NO_TOP(v);
  1146. GET_udp_INSTANCE_AT(v, 1);
  1147. sq_pushinteger(v, (SQInteger) udp->sock);
  1148. return 1;
  1149. }
  1150. /* this is very dangerous, but can be handy for those that are brave enough */
  1151. static SQRESULT udp_meth_setfd(HSQUIRRELVM v) {
  1152. SQ_FUNC_VARS_NO_TOP(v);
  1153. GET_udp_INSTANCE_AT(v, 1);
  1154. SQ_GET_INTEGER(v, 2, fd);
  1155. udp->sock = (t_socket) fd;
  1156. return 0;
  1157. }
  1158. static SQRESULT udp_meth_dirty(HSQUIRRELVM v) {
  1159. SQ_FUNC_VARS_NO_TOP(v);
  1160. GET_udp_INSTANCE_AT(v, 1);
  1161. (void) udp;
  1162. sq_pushbool(v, SQFalse);
  1163. return 1;
  1164. }
  1165. /*-------------------------------------------------------------------------*\
  1166. * Just call inet methods
  1167. \*-------------------------------------------------------------------------*/
  1168. static SQRESULT udp_meth_getpeername(HSQUIRRELVM v) {
  1169. SQ_FUNC_VARS_NO_TOP(v);
  1170. GET_udp_connected_INSTANCE_AT(v, 1);
  1171. return inet_meth_getpeername(v, &udp->sock);
  1172. }
  1173. static SQRESULT udp_meth_getsockname(HSQUIRRELVM v) {
  1174. SQ_FUNC_VARS_NO_TOP(v);
  1175. GET_udp_INSTANCE_AT(v, 1);
  1176. return inet_meth_getsockname(v, &udp->sock);
  1177. }
  1178. /*-------------------------------------------------------------------------*\
  1179. * Just call option handler
  1180. \*-------------------------------------------------------------------------*/
  1181. static SQRESULT udp_meth_setoption(HSQUIRRELVM v) {
  1182. SQ_FUNC_VARS_NO_TOP(v);
  1183. GET_udp_INSTANCE_AT(v, 1);
  1184. return opt_meth_setoption(v, udp_opt, &udp->sock);
  1185. }
  1186. /*-------------------------------------------------------------------------*\
  1187. * Just call tm methods
  1188. \*-------------------------------------------------------------------------*/
  1189. static SQRESULT udp_meth_settimeout(HSQUIRRELVM v) {
  1190. SQ_FUNC_VARS_NO_TOP(v);
  1191. GET_udp_INSTANCE_AT(v, 1);
  1192. return timeout_meth_settimeout(v, &udp->tm);
  1193. }
  1194. /*-------------------------------------------------------------------------*\
  1195. * Turns a master udp object into a client object.
  1196. \*-------------------------------------------------------------------------*/
  1197. static SQRESULT udp_meth_setpeername(HSQUIRRELVM v) {
  1198. SQ_FUNC_VARS_NO_TOP(v);
  1199. GET_udp_INSTANCE_AT(v, 1);
  1200. p_timeout tm = &udp->tm;
  1201. SQ_GET_STRING(v, 2, address);
  1202. int connecting = scstrcmp(address, _SC("*"));
  1203. SQInteger port;
  1204. if(connecting) sq_getinteger(v, 3, &port);
  1205. else sq_optinteger(v, 3, &port, 0);
  1206. const char *err = inet_tryconnect(&udp->sock, address, port, tm);
  1207. if (err) return sq_throwerror(v, err);
  1208. /* change class to connected or unconnected depending on address */
  1209. if (connecting)
  1210. udp->type = connecting ? UDP_TYPE_CONNECTED : UDP_TYPE_UNCONNECTED;
  1211. return 0;
  1212. }
  1213. /*-------------------------------------------------------------------------*\
  1214. * Closes socket used by object
  1215. \*-------------------------------------------------------------------------*/
  1216. static SQRESULT udp_meth_close(HSQUIRRELVM v) {
  1217. SQ_FUNC_VARS_NO_TOP(v);
  1218. GET_udp_INSTANCE_AT(v, 1);
  1219. lua_socket_destroy(&udp->sock);
  1220. return 0;
  1221. }
  1222. /*-------------------------------------------------------------------------*\
  1223. * Turns a master object into a server object
  1224. \*-------------------------------------------------------------------------*/
  1225. static SQRESULT udp_meth_setsockname(HSQUIRRELVM v) {
  1226. SQ_FUNC_VARS_NO_TOP(v);
  1227. GET_udp_unconnected_INSTANCE_AT(v, 1);
  1228. SQ_GET_STRING(v, 2, address);
  1229. SQ_GET_INTEGER(v, 3, port);
  1230. const char *err = inet_trybind(&udp->sock, address, port);
  1231. if (err) return sq_throwerror(v, err);
  1232. return 0;
  1233. }
  1234. /*=========================================================================*\
  1235. * Library functions
  1236. \*=========================================================================*/
  1237. /*-------------------------------------------------------------------------*\
  1238. * Creates a master udp object
  1239. \*-------------------------------------------------------------------------*/
  1240. static SQRESULT udp_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  1241. {
  1242. p_udp udp = (p_udp)p;
  1243. if(udp) {
  1244. lua_socket_destroy(&udp->sock);
  1245. sq_free(udp, sizeof(p_udp));
  1246. }
  1247. return 0;
  1248. }
  1249. static SQRESULT udp_constructor(HSQUIRRELVM v) {
  1250. t_socket sock;
  1251. const char *err = inet_trycreate(&sock, SOCK_DGRAM);
  1252. /* try to allocate a system socket */
  1253. if (!err) {
  1254. /* allocate tcp object */
  1255. p_udp udp = (p_udp) sq_malloc(sizeof(t_udp));
  1256. udp->type = UDP_TYPE_UNCONNECTED;
  1257. /* initialize remaining structure fields */
  1258. lua_socket_setnonblocking(&sock);
  1259. udp->sock = sock;
  1260. lua_timeout_init(&udp->tm, -1, -1);
  1261. sq_setinstanceup(v, 1, udp);
  1262. sq_setreleasehook(v, 1, udp_releasehook);
  1263. return 1;
  1264. }
  1265. return sq_throwerror(v, err);
  1266. }
  1267. /*=========================================================================*\
  1268. * Internal Select function prototypes.
  1269. \*=========================================================================*/
  1270. static t_socket getfd(HSQUIRRELVM v);
  1271. static SQRESULT dirty(HSQUIRRELVM v);
  1272. static t_socket collect_fd(HSQUIRRELVM v, int tab, t_socket max_fd,
  1273. int itab, fd_set *set);
  1274. static int check_dirty(HSQUIRRELVM v, int tab, int dtab, fd_set *set);
  1275. static void return_fd(HSQUIRRELVM v, fd_set *set, t_socket max_fd,
  1276. int itab, int tab, int start);
  1277. static void make_assoc(HSQUIRRELVM v, int tab);
  1278. /*-------------------------------------------------------------------------*\
  1279. * Waits for a set of sockets until a condition is met or timeout.
  1280. \*-------------------------------------------------------------------------*/
  1281. static SQRESULT global_select(HSQUIRRELVM v) {
  1282. SQ_FUNC_VARS(v);
  1283. int rtab, wtab, itab, ret, ndirty;
  1284. t_socket max_fd;
  1285. fd_set rset, wset;
  1286. t_timeout tm;
  1287. SQ_OPT_FLOAT(v, 4, t, -1);
  1288. FD_ZERO(&rset); FD_ZERO(&wset);
  1289. sq_settop(v, 4);
  1290. sq_newtable(v); itab = sq_gettop(v);
  1291. sq_newarray(v, 0); rtab = sq_gettop(v);
  1292. sq_newarray(v, 0); wtab = sq_gettop(v);
  1293. max_fd = collect_fd(v, 2, SOCKET_INVALID, itab, &rset);
  1294. ndirty = check_dirty(v, 2, rtab, &rset);
  1295. t = ndirty > 0 ? 0.0: t;
  1296. lua_timeout_init(&tm, t, -1);
  1297. lua_timeout_markstart(&tm);
  1298. max_fd = collect_fd(v, 3, max_fd, itab, &wset);
  1299. ret = lua_socket_select(max_fd+1, &rset, &wset, NULL, &tm);
  1300. if (ret > 0 || ndirty > 0) {
  1301. return_fd(v, &rset, max_fd+1, itab, rtab, ndirty);
  1302. return_fd(v, &wset, max_fd+1, itab, wtab, 0);
  1303. sq_newarray(v, 2);
  1304. sq_pushinteger(v, 0);
  1305. sq_push(v, rtab);
  1306. sq_rawset(v, -3);
  1307. sq_pushinteger(v, 1);
  1308. sq_push(v, wtab);
  1309. sq_rawset(v, -3);
  1310. return 1;
  1311. } else if (ret == 0) {
  1312. return sq_throwerror(v, _SC("timeout"));
  1313. }
  1314. return sq_throwerror(v, _SC("select error %d"), ret);
  1315. }
  1316. /*=========================================================================*\
  1317. * Internal functions
  1318. \*=========================================================================*/
  1319. static t_socket getfd(HSQUIRRELVM v) {
  1320. t_socket fd = SOCKET_INVALID;
  1321. SQInteger top = sq_gettop(v);
  1322. sq_pushliteral(v, _SC("getfd"));
  1323. if(sq_get(v, -2) == SQ_OK){
  1324. SQObjectType ptype = sq_gettype(v, -1);
  1325. if (ptype == OT_CLOSURE || ptype == OT_NATIVECLOSURE) {
  1326. sq_push(v, -2);
  1327. if(sq_call(v, 1, SQTrue, SQFalse) == SQ_OK){
  1328. if (sq_gettype(v, -1) == OT_INTEGER){
  1329. SQInteger ifd;
  1330. sq_getinteger(v, -1, &ifd);
  1331. fd = (t_socket) ifd;
  1332. }
  1333. }
  1334. }
  1335. }
  1336. sq_settop(v, top);
  1337. return fd;
  1338. }
  1339. static SQRESULT dirty(HSQUIRRELVM v) {
  1340. int is = 0;
  1341. SQInteger top = sq_gettop(v);
  1342. sq_pushliteral(v, _SC("dirty"));
  1343. if(sq_get(v, -2) == SQ_OK){
  1344. SQObjectType ptype = sq_gettype(v, -1);
  1345. if (ptype == OT_CLOSURE || ptype == OT_NATIVECLOSURE) {
  1346. sq_push(v, -2);
  1347. if(sq_call(v, 1, SQTrue, SQFalse) == SQ_OK){
  1348. if (sq_gettype(v, -1) == OT_INTEGER){
  1349. SQBool bval;
  1350. sq_getbool(v, -1, &bval);
  1351. is = bval == SQTrue;
  1352. }
  1353. }
  1354. }
  1355. sq_pop(v, 1);
  1356. }
  1357. sq_settop(v, top);
  1358. return is;
  1359. }
  1360. static t_socket collect_fd(HSQUIRRELVM v, int tab, t_socket max_fd,
  1361. int itab, fd_set *set) {
  1362. int i = 0;
  1363. if (sq_gettype(v, tab) == OT_NULL) return max_fd;
  1364. while (1) {
  1365. t_socket fd;
  1366. sq_pushinteger(v, i);
  1367. if(sq_rawget(v, tab) == SQ_ERROR) break;
  1368. fd = getfd(v);
  1369. if (fd != SOCKET_INVALID) {
  1370. FD_SET(fd, set);
  1371. if (max_fd == SOCKET_INVALID || max_fd < fd)
  1372. max_fd = fd;
  1373. sq_pushinteger(v, fd);
  1374. sq_push(v, -2);
  1375. sq_rawset(v, itab);
  1376. }
  1377. sq_pop(v, 1);
  1378. ++i;
  1379. }
  1380. return max_fd;
  1381. }
  1382. static int check_dirty(HSQUIRRELVM v, int tab, int dtab, fd_set *set) {
  1383. int ndirty = 0, i = 0;
  1384. if (sq_gettype(v, tab) == OT_NULL)
  1385. return 0;
  1386. while (1) {
  1387. t_socket fd;
  1388. sq_pushinteger(v, i);
  1389. if(sq_rawget(v, tab) == SQ_ERROR) break;
  1390. fd = getfd(v);
  1391. if (fd != SOCKET_INVALID && dirty(v)) {
  1392. sq_pushinteger(v, ++ndirty);
  1393. sq_push(v, -2);
  1394. sq_rawset(v, dtab);
  1395. FD_CLR(fd, set);
  1396. }
  1397. sq_pop(v, 1);
  1398. ++i;
  1399. }
  1400. return ndirty;
  1401. }
  1402. static void return_fd(HSQUIRRELVM v, fd_set *set, t_socket max_fd,
  1403. int itab, int tab, int start) {
  1404. t_socket fd;
  1405. for (fd = 0; fd < max_fd; fd++) {
  1406. if (FD_ISSET(fd, set)) {
  1407. sq_pushinteger(v, fd);
  1408. sq_rawget(v, itab);
  1409. sq_arrayappend(v, tab);
  1410. }
  1411. }
  1412. }
  1413. #define INT_CONST(v,num) sq_pushstring(v,_SC(#num),-1);sq_pushinteger(v,num);sq_newslot(v,-3,SQTrue);
  1414. #ifdef __cplusplus
  1415. extern "C" {
  1416. #endif
  1417. SQRESULT sqext_register_sq_socket(HSQUIRRELVM v)
  1418. {
  1419. sq_pushliteral(v,_SC("socket"));
  1420. sq_newtable(v);
  1421. INT_CONST(v, IO_DONE);
  1422. INT_CONST(v, IO_TIMEOUT);
  1423. INT_CONST(v, IO_CLOSED);
  1424. INT_CONST(v, IO_UNKNOWN);
  1425. INT_CONST(v, IO_SSL);
  1426. sq_insertfunc(v, _SC("open"), socket_open, 1, _SC("."), SQTrue);
  1427. sq_insertfunc(v, _SC("close"), socket_close, 1, _SC("."), SQTrue);
  1428. sq_insertfunc(v, _SC("gethostname"), inet_global_gethostname, 1, _SC("."), SQTrue);
  1429. sq_insertfunc(v, _SC("gettime"), timeout_lua_gettime, 1, _SC("."), SQTrue);
  1430. sq_insertfunc(v, _SC("select"), global_select, -3, _SC(".aan"), SQTrue);
  1431. sq_insertfunc(v, _SC("sleep"), timeout_lua_sleep, 2, _SC(".i"), SQTrue);
  1432. sq_insertfunc(v, _SC("sleepMillisecs"), timeout_lua_sleepMillisecs, 2, _SC(".i"), SQTrue);
  1433. sq_insertfunc(v, _SC("tohostname"), inet_global_tohostname, 2, _SC(".s"), SQTrue);
  1434. sq_insertfunc(v, _SC("toip"), inet_global_toip, 2, _SC(".s"), SQTrue);
  1435. sq_pushliteral(v, _SC("tcp"));
  1436. sq_newclass(v, SQFalse);
  1437. sq_settypetag(v,-1,(void*)SQ_tcp_TAG);
  1438. sq_insertfunc(v, _SC("constructor"), tcp_constructor, 1, _SC("x"), SQFalse);
  1439. sq_insertfunc(v, _SC("accept"), tcp_meth_accept, 1, _SC("x"), SQFalse);
  1440. sq_insertfunc(v, _SC("bind"), tcp_meth_bind, 3, _SC("xsi"), SQFalse);
  1441. sq_insertfunc(v, _SC("close"), tcp_meth_close, 1, _SC("x"), SQFalse);
  1442. sq_insertfunc(v, _SC("connect"), tcp_meth_connect, 3, _SC("xsi"), SQFalse);
  1443. sq_insertfunc(v, _SC("dirty"), tcp_meth_dirty, 1, _SC("x"), SQFalse);
  1444. sq_insertfunc(v, _SC("getfd"), tcp_meth_getfd, 1, _SC("x"), SQFalse);
  1445. sq_insertfunc(v, _SC("getpeername"), tcp_meth_getpeername, 1, _SC("x"), SQFalse);
  1446. sq_insertfunc(v, _SC("getsockname"), tcp_meth_getsockname, 1, _SC("x"), SQFalse);
  1447. sq_insertfunc(v, _SC("getstats"), tcp_meth_getstats, 1, _SC("x"), SQFalse);
  1448. sq_insertfunc(v, _SC("listen"), tcp_meth_listen, -1, _SC("xi"), SQFalse);
  1449. sq_insertfunc(v, _SC("receive"), tcp_meth_receive, -1, _SC("x i|s s"), SQFalse);
  1450. sq_insertfunc(v, _SC("send"), tcp_meth_send, -2, _SC("xsii"), SQFalse);
  1451. sq_insertfunc(v, _SC("setfd"), tcp_meth_setfd, 2, _SC("xi"), SQFalse);
  1452. sq_insertfunc(v, _SC("setoption"), tcp_meth_setoption, -2, _SC("xs s|n|b"), SQFalse);
  1453. sq_insertfunc(v, _SC("setstats"), tcp_meth_setstats, 4, _SC("xiii"), SQFalse);
  1454. sq_insertfunc(v, _SC("settimeout"), tcp_meth_settimeout, -2, _SC("xns"), SQFalse);
  1455. sq_insertfunc(v, _SC("shutdown"), tcp_meth_shutdown, 2, _SC("xs"), SQFalse);
  1456. sq_newslot(v,-3,SQTrue);
  1457. sq_pushliteral(v, _SC("udp"));
  1458. sq_newclass(v, SQFalse);
  1459. sq_settypetag(v,-1,(void*)SQ_udp_TAG);
  1460. sq_insertfunc(v, _SC("constructor"), udp_constructor, 1, _SC("x"), SQFalse);
  1461. sq_insertfunc(v, _SC("close"), udp_meth_close, 1, _SC("x"), SQFalse);
  1462. sq_insertfunc(v, _SC("dirty"), udp_meth_dirty, 1, _SC("x"), SQFalse);
  1463. sq_insertfunc(v, _SC("getfd"), udp_meth_getfd, 1, _SC("x"), SQFalse);
  1464. sq_insertfunc(v, _SC("getpeername"), udp_meth_getpeername, 1, _SC("x"), SQFalse);
  1465. sq_insertfunc(v, _SC("getsockname"), udp_meth_getsockname, 1, _SC("x"), SQFalse);
  1466. sq_insertfunc(v, _SC("receive"), udp_meth_receive, -1, _SC("xi"), SQFalse);
  1467. sq_insertfunc(v, _SC("receivefrom"), udp_meth_receivefrom, -1, _SC("xi"), SQFalse);
  1468. sq_insertfunc(v, _SC("send"), udp_meth_send, 2, _SC("xs"), SQFalse);
  1469. sq_insertfunc(v, _SC("sendto"), udp_meth_sendto, 4, _SC("xssi"), SQFalse);
  1470. sq_insertfunc(v, _SC("setfd"), udp_meth_setfd, 2, _SC("xi"), SQFalse);
  1471. sq_insertfunc(v, _SC("setpeername"), udp_meth_setpeername, -1, _SC("xsi"), SQFalse);
  1472. sq_insertfunc(v, _SC("setsockname"), udp_meth_setsockname, 3, _SC("xsi"), SQFalse);
  1473. sq_insertfunc(v, _SC("setoption"), udp_meth_setoption, -2, _SC("xs s|n|b"), SQFalse);
  1474. sq_insertfunc(v, _SC("settimeout"), udp_meth_settimeout, 2, _SC("xn"), SQFalse);
  1475. sq_newslot(v,-3,SQTrue);
  1476. sq_newslot(v,-3,SQTrue); //push socket table
  1477. return 0;
  1478. }
  1479. #ifdef __cplusplus
  1480. }
  1481. #endif