Changeset 49a6c7b61d5cac352916722a03d88dc0a53bffee

Show
Ignore:
Timestamp:
09/13/09 04:04:12 (5 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1252814652 +0000
git-parent:

[72c5b0be5471b0ebbb782fba28a4f6f1cd00a6f1]

git-author:
Theo Schlossnagle <jesus@omniti.com> 1252814652 +0000
Message:

added SSL upgrade support into lua, need to expose context metadata. refs #174

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/modules/lua.c

    r88a7178 r49a6c7b  
    260260static int 
    261261noit_lua_set_status(lua_State *L) { 
     262  const char *ns; 
    262263  noit_check_t *check; 
    263264  noit_lua_check_info_t *ci; 
     
    267268  /* strdup here... but free later */ 
    268269  if(ci->current.status) free(ci->current.status); 
    269   ci->current.status = strdup(lua_tostring(L, 1)); 
     270  ns = lua_tostring(L, 1); 
     271  ci->current.status = ns ? strdup(ns) : NULL; 
    270272  return 0; 
    271273} 
  • src/modules/lua_noit.c

    rb9a4230 r49a6c7b  
    7979  noit_lua_check_info_t *ci; 
    8080  struct nl_slcl *cl = vcl; 
    81   int args = 0; 
     81  int args = 0, aerrno; 
     82  socklen_t aerrno_len = sizeof(aerrno); 
    8283 
    8384  ci = get_ci(cl->L); 
     
    8889  memcpy(*cl->eptr, e, sizeof(*e)); 
    8990  noit_lua_check_register_event(ci, *cl->eptr); 
     91 
     92  if(getsockopt(e->fd,SOL_SOCKET,SO_ERROR, &aerrno, &aerrno_len) == 0) 
     93    if(aerrno != 0) goto connerr; 
    9094 
    9195  if(!(mask & EVENTER_EXCEPTION) && 
     
    96100  } 
    97101  else { 
     102    aerrno = errno; 
     103   connerr: 
    98104    lua_pushinteger(cl->L, -1); 
    99     lua_pushstring(cl->L, strerror(errno)); 
     105    lua_pushstring(cl->L, strerror(aerrno)); 
    100106    args = 2; 
    101107  } 
     
    163169  lua_pushstring(L, strerror(errno)); 
    164170  return 2; 
     171} 
     172static int 
     173noit_lua_ssl_upgrade(eventer_t e, int mask, void *vcl, 
     174                     struct timeval *now) { 
     175  noit_lua_check_info_t *ci; 
     176  struct nl_slcl *cl = vcl; 
     177  int rv; 
     178   
     179  rv = eventer_SSL_connect(e, &mask); 
     180  if(rv <= 0 && errno == EAGAIN) return mask | EVENTER_EXCEPTION; 
     181 
     182  ci = get_ci(cl->L); 
     183  assert(ci); 
     184  noit_lua_check_deregister_event(ci, e, 0); 
     185   
     186  *(cl->eptr) = eventer_alloc(); 
     187  memcpy(*cl->eptr, e, sizeof(*e)); 
     188  noit_lua_check_register_event(ci, *cl->eptr); 
     189 
     190  /* Upgrade completed successfully */ 
     191  lua_pushinteger(cl->L, (rv > 0) ? 0 : -1); 
     192  noit_lua_resume(ci, 1); 
     193  return 0; 
     194} 
     195static int 
     196noit_lua_socket_connect_ssl(lua_State *L) { 
     197  const char *ca, *ciphers, *cert, *key; 
     198  eventer_ssl_ctx_t *sslctx; 
     199  noit_lua_check_info_t *ci; 
     200  eventer_t e, *eptr; 
     201  struct timeval now; 
     202 
     203  ci = get_ci(L); 
     204  assert(ci); 
     205 
     206  eptr = lua_touserdata(L, lua_upvalueindex(1)); 
     207  e = *eptr; 
     208  ca = lua_tostring(L, 1); 
     209  ciphers = lua_tostring(L, 2); 
     210  cert = lua_tostring(L, 3); 
     211  key = lua_tostring(L, 4); 
     212 
     213  sslctx = eventer_ssl_ctx_new(SSL_CLIENT, cert, key, ca, ciphers); 
     214  if(!sslctx) { 
     215    lua_pushinteger(L, -1); 
     216    return 1; 
     217  } 
     218 
     219  eventer_ssl_ctx_set_verify(sslctx, eventer_ssl_verify_cert, NULL); 
     220  EVENTER_ATTACH_SSL(e, sslctx); 
     221  e->callback = noit_lua_ssl_upgrade; 
     222  gettimeofday(&now, NULL); 
     223  e->mask = e->callback(e, EVENTER_READ|EVENTER_WRITE, e->closure, &now); 
     224  if(e->mask & (EVENTER_READ|EVENTER_WRITE)) { 
     225    /* Need completion */ 
     226    eventer_add(e); 
     227    return noit_lua_yield(ci, 0); 
     228  } 
     229  lua_pushinteger(L, 0); 
     230  return 1; 
    165231} 
    166232 
     
    414480     } 
    415481     break; 
     482    case 's': 
     483     if(!strcmp(k, "ssl_upgrade_socket")) { 
     484       lua_pushlightuserdata(L, udata); 
     485       lua_pushcclosure(L, noit_lua_socket_connect_ssl, 1); 
     486       return 1; 
     487     } 
     488     break; 
    416489    case 'w': 
    417490     if(!strcmp(k, "write")) {