/*
 * MG_KEYEXCH.pgc - mg 모듈 서비스 MG_KEYEXCH (one service = one file).
 * Split out of the former inline mg_svr.pgc; runs on the caller's XA
 * branch (no EXEC SQL CONNECT). Advertised by mg_svr at tpsvrinit.
 */
#include "MG_KEYEXCH.h"

/* 19. MG_KEYEXCH - 키 교환: 키버전 채번 + 세션키 갱신. */
void MG_KEYEXCH(TPSVCINFO *p)
{
    UBFH *b = (UBFH *)p->data;
    char chan[16], key[64];
    long ver, stan;
    gets_(b, T_CHANNEL, chan, sizeof(chan)); if (chan[0] == 0) strcpy(chan, "CH01");
    gets_(b, T_KEY1, key, sizeof(key));      if (key[0] == 0) strcpy(key, "NEW-KEY");
    ver = mgdb_seq_bump("KEYVER"); if (ver < 0) FAIL(b);
    stan = mgdb_next_stan();       if (stan < 0) FAIL(b);
    if (mgdb_set_channel_session(chan, key, stan) < 0) FAIL(b);
    setl(b, T_ARG2, ver);
    userlog("MG_KEYEXCH chan=%s keyver=%ld", chan, ver);
    OK(b);
}
