Connecting to Oracle with olog() at specific hostname:port

566 views Asked by At

The C-program I'm writing tries to connect to Oracle database with olog():

olog(&lda, (ub1 *)hda, dbparam_keys[DbUser].value, -1,
        dbparam_keys[DbPassword].value, -1, NULL, -1, (ub4)OCI_LM_DEF);

This fails with ORA-12545, because I specify neither the server's hostname, nor the port number anywhere. I do not, because I do not know, how to do that...

The "demos" bundled with the client seem to presume a valid tnsnames.ora -- is having that file mandatory for OCI API, or a can a client program, that knows the necessary values, specify them to the library (such as with OCIAttrSet()) all by itself?

1

There are 1 answers

0
Mikhail T. On

There, likely, exists a better way, but the following works. The connection-specifier -- the third string passed to olog() -- normally refers to an entry found in tnsnames.ora. But, instead of being such a shortcut, it can also be the entire entry, which is what I want in this case.

So, I'm using the complete syntax normally used in tnsnames.ora to describe the server in my code. I then pass the text as the connection-specifier to olog():

bzero(&lda, sizeof(lda));
bzero(&hda, sizeof(hda));

connlen = snprintf(buf, sizeof buf,
    "(DESCRIPTION =\n"
    "   (ADDRESS_LIST =\n"
    "       (ADDRESS =\n"
    "           (PROTOCOL = TCP)\n"
    "           (HOST = %s)\n"
    "           (PORT = %s)\n"
    "       )\n"
    "   )\n"
    "   (CONNECT_DATA =\n"
    "       (SID = %s)\n"
    "   )\n"
    ")",
    dbparam_keys[DbHostName].value,
    dbparam_keys[DbServerPortNumber].value,
    dbparam_keys[DbServerOrServiceName].value);

if (connlen >= sizeof buf)
    errx(EX_SOFTWARE, "Internal error: buffer not big enough: "
        "need %zd, have %zd bytes", connlen, sizeof buf);
if (verbose)
    warnx("Connecting to %.*s", connlen, buf);

if (olog(&lda, (ub1 *)hda,
    dbparam_keys[DbUser].value, -1,
    dbparam_keys[DbPassword].value, -1,
    buf, connlen,
    OCI_LM_DEF)) {
    errx(EX_NOPERM, "Logging into Oracle failed: %s",
        oraerr(&lda, buf, sizeof(buf)));
}