lm_ssl.c
changeset 6 90073cbb535d
parent 4 5770be2d5f3f
child 9 50f55d494efb
equal deleted inserted replaced
5:e617c9cf6dd3 6:90073cbb535d
     3 #include <lauxlib.h>
     3 #include <lauxlib.h>
     4 #include <glib.h>
     4 #include <glib.h>
     5 #include <loudmouth/loudmouth.h>
     5 #include <loudmouth/loudmouth.h>
     6 #include <stdio.h>
     6 #include <stdio.h>
     7 
     7 
       
     8 #include "config.h"
     8 #include "util.h"
     9 #include "util.h"
     9 #include "lm_types.h"
    10 #include "lm_types.h"
    10 
    11 
    11 /// lm.ssl
    12 /// lm.ssl
    12 /// Object, containing information about ssl abilities for connection.
    13 /// Object, containing information about ssl abilities for connection.
    38 	lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
    39 	lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
    39 	llm_ssl_bless (cb->L, ssl);
    40 	llm_ssl_bless (cb->L, ssl);
    40 	// XXX lm_ssl_unref (ssl);
    41 	// XXX lm_ssl_unref (ssl);
    41 	luaL_pushenum (cb->L, status, llm_ssl_status);
    42 	luaL_pushenum (cb->L, status, llm_ssl_status);
    42 	if (lua_pcall (cb->L, 2, 0, 0)) {
    43 	if (lua_pcall (cb->L, 2, 0, 0)) {
    43 		// XXX lua_error (cb->L);
    44 		E ("SSL callback error: %s", lua_tostring (cb->L, -1));
    44 		lua_pop (cb->L, 1);
    45 		lua_pop (cb->L, 1);
    45 		return LM_SSL_RESPONSE_CONTINUE;
    46 		return LM_SSL_RESPONSE_CONTINUE;
    46 	}
    47 	}
    47 	ret = lua_toboolean (cb->L, -1);
    48 	ret = lua_toboolean (cb->L, -1);
    48 	lua_pop (cb->L, 1);
    49 	lua_pop (cb->L, 1);
    80 		const char *fingerprint = luaL_checkstring (L, 1);
    81 		const char *fingerprint = luaL_checkstring (L, 1);
    81 
    82 
    82 		if (lua_objlen (L, 1) > 46)
    83 		if (lua_objlen (L, 1) > 46)
    83 			string2fingerprint (fingerprint, buffer);
    84 			string2fingerprint (fingerprint, buffer);
    84 		ssl = lm_ssl_new (buffer, NULL, NULL, NULL);
    85 		ssl = lm_ssl_new (buffer, NULL, NULL, NULL);
    85 		lua_pop (L, 1);
       
    86 	} else {
    86 	} else {
    87 		llm_callback_t *cb;
    87 		llm_callback_t *cb;
    88 		gchar buffer[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
    88 		gchar buffer[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
    89 
    89 
    90 		if (args > 1) {
    90 		if (args > 1) {
    99 		cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
    99 		cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
   100 		cb->L = L;
   100 		cb->L = L;
   101 
   101 
   102 		ssl = lm_ssl_new ((args > 1) ? buffer : NULL, (LmSSLFunction)llm_ssl_callback,
   102 		ssl = lm_ssl_new ((args > 1) ? buffer : NULL, (LmSSLFunction)llm_ssl_callback,
   103 							cb, (GDestroyNotify)llm_callback_destroy);
   103 							cb, (GDestroyNotify)llm_callback_destroy);
   104 		lua_pop (L, 1);
       
   105 	}
   104 	}
   106 	llm_ssl_bless (L, ssl);
   105 	llm_ssl_bless (L, ssl);
   107 	lm_ssl_unref (ssl); // XXX
   106 	lm_ssl_unref (ssl); // XXX
       
   107 	D ("SSL %X created", (int) ssl);
   108 	return 1;
   108 	return 1;
   109 }
   109 }
   110 
   110 
   111 /// lm.ssl.bless
   111 /// lm.ssl.bless
   112 /// Blesses given pointer to lm ssl object.
   112 /// Blesses given pointer to lm ssl object.
   114 /// R: lm ssl object
   114 /// R: lm ssl object
   115 static int llm_ssl_bless_lua (lua_State *L)
   115 static int llm_ssl_bless_lua (lua_State *L)
   116 {
   116 {
   117 	luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "lm ssl lightuserdata expected");
   117 	luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "lm ssl lightuserdata expected");
   118 	llm_ssl_bless (L, lua_touserdata (L, 1));
   118 	llm_ssl_bless (L, lua_touserdata (L, 1));
   119 	lua_remove (L, -2);
       
   120 	return 1;
   119 	return 1;
   121 }
   120 }
   122 
   121 
   123 /// lm.ssl.supported
   122 /// lm.ssl.supported
   124 /// Indicates if SSL is supported by loudmouth library.
   123 /// Indicates if SSL is supported by loudmouth library.
   147 			  fingerprint[4], fingerprint[5], fingerprint[6], fingerprint[7],
   146 			  fingerprint[4], fingerprint[5], fingerprint[6], fingerprint[7],
   148 			  fingerprint[8], fingerprint[9], fingerprint[10], fingerprint[11],
   147 			  fingerprint[8], fingerprint[9], fingerprint[10], fingerprint[11],
   149 			  fingerprint[12], fingerprint[13], fingerprint[14], fingerprint[15]);
   148 			  fingerprint[12], fingerprint[13], fingerprint[14], fingerprint[15]);
   150 		lua_pushlstring (L, buffer, 47);
   149 		lua_pushlstring (L, buffer, 47);
   151 	}
   150 	}
   152 	lua_remove (L, -2);
       
   153 	return 1;
   151 	return 1;
   154 }
   152 }
   155 
   153 
   156 /// ssl:pointer
   154 /// ssl:pointer
   157 /// Returns pointer to underlying C structure.
   155 /// Returns pointer to underlying C structure.
   158 /// R: lightuserdata
   156 /// R: lightuserdata
   159 static int llm_ssl_pointer (lua_State *L)
   157 static int llm_ssl_pointer (lua_State *L)
   160 {
   158 {
   161 	llm_ssl_t *object = luaL_checklm_ssl (L, 1);
   159 	llm_ssl_t *object = luaL_checklm_ssl (L, 1);
   162 	lua_pushlightuserdata (L, object->ssl);
   160 	lua_pushlightuserdata (L, object->ssl);
   163 	lua_remove (L, -2);
       
   164 	return 1;
   161 	return 1;
   165 }
   162 }
   166 
   163 
   167 static int llm_ssl_gc (lua_State *L)
   164 static int llm_ssl_gc (lua_State *L)
   168 {
   165 {
   169 	llm_ssl_t *object = luaL_checklm_ssl (L, 1);
   166 	llm_ssl_t *object = luaL_checklm_ssl (L, 1);
       
   167 	D ("SSL %X gc called", (int) object);
   170 	lm_ssl_unref (object->ssl);
   168 	lm_ssl_unref (object->ssl);
   171 	lua_pop (L, 1);
       
   172 	return 0;
   169 	return 0;
   173 }
   170 }
   174 
   171 
   175 const static luaL_Reg llm_ssl_reg_f[] = {
   172 const static luaL_Reg llm_ssl_reg_f[] = {
   176 	{ "new",       llm_ssl_new       },
   173 	{ "new",       llm_ssl_new       },