title: Simple snmp trap generator program using Net-SNMP in C?
body: <p>I'm trying to create a simple SNMP trap generator program so that I can understand the Net-SNMP library better. With the following code I keep getting the error</p>
  <blockquote>
  <p>snmptrap: Configuration data found but the transport can't be
  configured</p>
  <p>Session did not open</p>
  </blockquote>
  <pre><code>#include &lt;net-snmp/net-snmp-config.h&gt;
  #include &lt;net-snmp/net-snmp-includes.h&gt;
  #include &lt;net-snmp/agent/net-snmp-agent-includes.h&gt;
  
  #include &quot;snmptrap_simple.h&quot;
  
  int main (int argc, char **argv) {
    oid             objid_enterprise[] = { 1, 3, 6, 1, 4, 1, 3, 1, 1 };
    oid             objid_sysdescr[] = { 1, 3, 6, 1, 2, 1, 1, 1, 0 };
    oid             objid_sysuptime[] = { 1, 3, 6, 1, 2, 1, 1, 3, 0 };
    oid             objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
  
    netsnmp_session session, *ss;
    netsnmp_pdu    *pdu, *response;
    oid             name[MAX_OID_LEN];
    size_t          name_length;
    int             arg;
    int             status;
    char           *trap = NULL;
    char           *prognam;
    int             exitval = 0;
  
    session.version       = SNMP_VERSION_1;
    session.retries       = 2;
    char address[]        = &quot;127.0.0.1&quot;;
    char *ptrAddress      = address;
    session.peername      = ptrAddress;
    u_char comm[]         = &quot;public&quot;;
    const u_char *community = comm;
    session.community     = community;
    session.community_len = strlen(session.community);
  
    /* windows32 specific initialization (is a noop on unix) */
    SOCK_STARTUP;
    // open snmp session
    ss = snmp_open(&amp;session);
    if (!ss) {
      snmp_perror(&quot;ack&quot;);
      snmp_sess_perror(&quot;snmptrap&quot;, &amp;session);
      snmp_log(LOG_DEBUG, &quot;Session did not open\n&quot;);
      exit(2);
    }
    snmp_log(LOG_DEBUG, &quot;DEBUG OUTPUT&quot;);
    // create the PDU
    pdu = snmp_pdu_create(SNMP_MSG_TRAP);
    if ( !pdu ) {
        fprintf(stderr, &quot;Failed to create trap PDU\n&quot;);
        SOCK_CLEANUP;
        exit(1);
    }
  
    // add variables to the PDU
    char *value;
    *value = '42';
    if(snmp_add_var (pdu, name, name_length, 'i', value)){
      //void snmp_perror(const char *prog_string)
      snmp_perror(&quot;add variable&quot;);
      SOCK_CLEANUP;
      exit(1);
    };
  
    send_trap_to_sess (&amp;session, pdu);
  
    return 0;
  }
  </code></pre>
  <p>What am I doing wrong?</p>
  <p>I have a github repo of the code here: <a href="https://github.com/EhevuTov/tcp2snmp/blob/master/test/snmptrap_simple.c" rel="nofollow noreferrer">https://github.com/EhevuTov/tcp2snmp/blob/master/test/snmptrap_simple.c</a></p>