~ubuntu-branches/ubuntu/wily/net-snmp/wily-proposed

« back to all changes in this revision

Viewing changes to python/netsnmp/client_intf.c

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2010-06-28 14:59:36 UTC
  • mfrom: (1.2.3 upstream) (1.1.12 sid)
  • Revision ID: james.westby@ubuntu.com-20100628145936-cbiallic69pn044g
Tags: 5.4.3~dfsg-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Set Ubuntu maintainer address.
  - net-snmp-config: Use bash. (LP: #104738)
  - Removed multiuser option when calling update-rc.d. (LP: #254261)
  - debian/snmpd.init: LSBify the init script.
  - debian/patches/52_fix_snmpcmd_1_typo.patch: Adjust a typo in snmpcmd.1
    (LP: #250459)
  - debian/snmpd.postinst: source debconf before doing work, LP: #589056
  - debian/snmp.preinst, debian/snmp.prerm: kill any/all processes owned by
    snmp user before install/uninstall, LP: #573391
  - Add apport hook (LP: #533603):
  - debian/{snmp,snmpd}.apport: Added.
  - debian/control: Build-depends on dh-apport.
  - debian/rules: 
    + Add --with apport.
    + override_dh_apport to install hook on snmpd package only.
 * Dropped patches:
   - debian/patches/99-fix-ubuntu-div0.patch: Fix dvision by zero.. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
40
40
#define STRLEN(x) (x ? strlen(x) : 0)
41
41
 
 
42
/* from perl/SNMP/perlsnmp.h: */
 
43
#ifndef timeradd
 
44
#define timeradd(a, b, result)                                                \
 
45
  do {                                                                        \
 
46
    (result)->tv_sec = (a)->tv_sec + (b)->tv_sec;                             \
 
47
    (result)->tv_usec = (a)->tv_usec + (b)->tv_usec;                          \
 
48
    if ((result)->tv_usec >= 1000000)                                         \
 
49
      {                                                                       \
 
50
        ++(result)->tv_sec;                                                   \
 
51
        (result)->tv_usec -= 1000000;                                         \
 
52
      }                                                                       \
 
53
  } while (0)
 
54
#endif
 
55
 
 
56
#ifndef timersub
 
57
#define timersub(a, b, result)                                                \
 
58
  do {                                                                        \
 
59
    (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;                             \
 
60
    (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;                          \
 
61
    if ((result)->tv_usec < 0) {                                              \
 
62
      --(result)->tv_sec;                                                     \
 
63
      (result)->tv_usec += 1000000;                                           \
 
64
    }                                                                         \
 
65
  } while (0)
 
66
#endif
 
67
 
42
68
typedef netsnmp_session SnmpSession;
43
69
typedef struct tree SnmpMibNode;
44
70
static void __recalc_timeout (struct timeval*,struct timeval*,
1081
1107
  return PyObject_CallFunction(callable, "");
1082
1108
}
1083
1109
 
1084
 
static char *
1085
 
py_netsnmp_attr_string(PyObject *obj, char * attr_name)
 
1110
static int
 
1111
py_netsnmp_attr_string(PyObject *obj, char * attr_name, char **val,
 
1112
    Py_ssize_t *len)
1086
1113
{
1087
 
  char *val = NULL;
1088
 
 
 
1114
  *val = NULL;
1089
1115
  if (obj && attr_name && PyObject_HasAttrString(obj, attr_name)) {
1090
1116
    PyObject *attr = PyObject_GetAttrString(obj, attr_name);
1091
1117
    if (attr) {
1092
 
      val = PyString_AsString(attr);
 
1118
      int retval;
 
1119
      retval = PyString_AsStringAndSize(attr, val, len);
1093
1120
      Py_DECREF(attr);
 
1121
      return retval;
1094
1122
    }
1095
1123
  }
1096
1124
 
1097
 
  return val;
 
1125
  return -1;
1098
1126
}
1099
1127
 
1100
 
static long
 
1128
static long long
1101
1129
py_netsnmp_attr_long(PyObject *obj, char * attr_name)
1102
1130
{
1103
 
  long val = -1;
 
1131
  long long val = -1;
1104
1132
 
1105
1133
  if (obj && attr_name  && PyObject_HasAttrString(obj, attr_name)) {
1106
1134
    PyObject *attr = PyObject_GetAttrString(obj, attr_name);
1196
1224
      printf("error:snmp_new_session: Couldn't open SNMP session");
1197
1225
  }
1198
1226
 end:
1199
 
  return Py_BuildValue("i", (int)ss);
 
1227
  return Py_BuildValue("L", (long long)ss);
1200
1228
}
1201
1229
 
1202
1230
static PyObject *
1232
1260
    return NULL;
1233
1261
 
1234
1262
  __libraries_init("python");
 
1263
  snmp_sess_init(&session);
1235
1264
 
1236
1265
  if (version == 3) {
1237
1266
    session.version = SNMP_VERSION_3;
1330
1359
    }
1331
1360
  }
1332
1361
  
1333
 
  ss = snmp_open(&session);
 
1362
  ss = snmp_sess_open(&session);
1334
1363
 
1335
1364
 end:
1336
1365
  if (ss == NULL) {
1395
1424
  int err_ind;
1396
1425
  int err_num;
1397
1426
  char err_str[STR_BUF_SIZE];
 
1427
  char *tmpstr;
 
1428
  Py_ssize_t tmplen;
1398
1429
           
1399
1430
  oid_arr = calloc(MAX_OID_LEN, sizeof(oid));
1400
1431
 
1406
1437
 
1407
1438
    ss = (SnmpSession *)py_netsnmp_attr_long(session, "sess_ptr");
1408
1439
 
1409
 
    strcpy(err_str, py_netsnmp_attr_string(session, "ErrorStr"));
 
1440
    if (py_netsnmp_attr_string(session, "ErrorStr", &tmpstr, &tmplen) < 0) {
 
1441
      goto done;
 
1442
    }
 
1443
    memcpy(&err_str, tmpstr, tmplen);
1410
1444
    err_num = py_netsnmp_attr_long(session, "ErrorNum");
1411
1445
    err_ind = py_netsnmp_attr_long(session, "ErrorInd");
1412
1446
 
1427
1461
      PyObject *varlist_iter = PyObject_GetIter(varlist);
1428
1462
 
1429
1463
      while (varlist_iter && (varbind = PyIter_Next(varlist_iter))) {
1430
 
        tag = py_netsnmp_attr_string(varbind, "tag");
1431
 
        iid = py_netsnmp_attr_string(varbind, "iid");
1432
 
 
1433
 
 
1434
 
        tp = __tag2oid(tag, iid, oid_arr, &oid_arr_len, 
1435
 
                       NULL, best_guess);
 
1464
        if (py_netsnmp_attr_string(varbind, "tag", &tag, NULL) < 0 ||
 
1465
            py_netsnmp_attr_string(varbind, "iid", &iid, NULL) < 0)
 
1466
        {
 
1467
          oid_arr_len = 0;
 
1468
        } else {
 
1469
          tp = __tag2oid(tag, iid, oid_arr, &oid_arr_len, NULL, best_guess);
 
1470
        }
1436
1471
 
1437
1472
        if (oid_arr_len) {
1438
1473
          snmp_add_null_var(pdu, oid_arr, oid_arr_len);
1600
1635
  int err_ind;
1601
1636
  int err_num;
1602
1637
  char err_str[STR_BUF_SIZE];
 
1638
  char *tmpstr;
 
1639
  Py_ssize_t tmplen;
1603
1640
           
1604
1641
  oid_arr = calloc(MAX_OID_LEN, sizeof(oid));
1605
1642
 
1611
1648
 
1612
1649
    ss = (SnmpSession *)py_netsnmp_attr_long(session, "sess_ptr");
1613
1650
 
1614
 
    strcpy(err_str, py_netsnmp_attr_string(session, "ErrorStr"));
 
1651
    if (py_netsnmp_attr_string(session, "ErrorStr", &tmpstr, &tmplen) < 0) {
 
1652
      goto done;
 
1653
    }
 
1654
    memcpy(&err_str, tmpstr, tmplen);
1615
1655
    err_num = py_netsnmp_attr_long(session, "ErrorNum");
1616
1656
    err_ind = py_netsnmp_attr_long(session, "ErrorInd");
1617
1657
 
1632
1672
      PyObject *varlist_iter = PyObject_GetIter(varlist);
1633
1673
 
1634
1674
      while (varlist_iter && (varbind = PyIter_Next(varlist_iter))) {
1635
 
        tag = py_netsnmp_attr_string(varbind, "tag");
1636
 
        iid = py_netsnmp_attr_string(varbind, "iid");
1637
 
 
1638
 
        tp = __tag2oid(tag, iid, oid_arr, &oid_arr_len, 
1639
 
                       NULL, best_guess);
 
1675
        if (py_netsnmp_attr_string(varbind, "tag", &tag, NULL) < 0 ||
 
1676
            py_netsnmp_attr_string(varbind, "iid", &iid, NULL) < 0)
 
1677
        {
 
1678
          oid_arr_len = 0;
 
1679
        } else {
 
1680
          tp = __tag2oid(tag, iid, oid_arr, &oid_arr_len, NULL, best_guess);
 
1681
        }
1640
1682
 
1641
1683
        if (_debug_level) 
1642
1684
          printf("netsnmp_getnext: filling request: %s:%s:%d:%d\n", 
1779
1821
  PyObject *varlist_iter;
1780
1822
  PyObject *varbind;
1781
1823
  PyObject *val_tuple = NULL;
1782
 
  PyObject *varbinds;
 
1824
  PyObject *varbinds  = NULL;
1783
1825
  int varlist_len = 0;
1784
1826
  int varlist_ind;
1785
1827
  netsnmp_session *ss;
1809
1851
  char err_str[STR_BUF_SIZE];
1810
1852
  int notdone = 1;
1811
1853
  int result_count = 0;
 
1854
  char *tmpstr;
 
1855
  Py_ssize_t tmplen;
1812
1856
           
1813
1857
  oid_arr = calloc(MAX_OID_LEN, sizeof(oid));
1814
1858
 
1827
1871
    }
1828
1872
    ss = (SnmpSession *)py_netsnmp_attr_long(session, "sess_ptr");
1829
1873
 
1830
 
    strcpy(err_str, py_netsnmp_attr_string(session, "ErrorStr"));
 
1874
    if (py_netsnmp_attr_string(session, "ErrorStr", &tmpstr, &tmplen) < 0) {
 
1875
      goto done;
 
1876
    }
 
1877
    memcpy(&err_str, tmpstr, tmplen);
1831
1878
    err_num = py_netsnmp_attr_long(session, "ErrorNum");
1832
1879
    err_ind = py_netsnmp_attr_long(session, "ErrorInd");
1833
1880
 
1848
1895
    
1849
1896
    /* get the initial starting oids*/
1850
1897
    while (varlist_iter && (varbind = PyIter_Next(varlist_iter))) {
1851
 
      tag = py_netsnmp_attr_string(varbind, "tag");
1852
 
      iid = py_netsnmp_attr_string(varbind, "iid");
1853
 
 
1854
 
      tp = __tag2oid(tag, iid, oid_arr, &oid_arr_len, 
1855
 
               NULL, best_guess);
 
1898
      if (py_netsnmp_attr_string(varbind, "tag", &tag, NULL) < 0 ||
 
1899
         py_netsnmp_attr_string(varbind, "iid", &iid, NULL) < 0)
 
1900
      {
 
1901
        oid_arr_len = 0;
 
1902
      } else {
 
1903
        tp = __tag2oid(tag, iid, oid_arr, &oid_arr_len, NULL, best_guess);
 
1904
      }
1856
1905
 
1857
1906
      if (_debug_level) 
1858
1907
        printf("netsnmp_walk: filling request: %s:%s:%d:%d\n", 
2004
2053
                          (len ? Py_BuildValue("s#", str_buf, len) :
2005
2054
                           Py_BuildValue("")));
2006
2055
            
2007
 
          Py_DECREF(varbind);
2008
2056
 
2009
2057
          } else {
2010
2058
            /* Return None for this variable. */
2012
2060
            PyTuple_SetItem(val_tuple, result_count++, Py_BuildValue(""));
2013
2061
            printf("netsnmp_walk: bad varbind (%d)\n", varlist_ind);
2014
2062
          }     
 
2063
          Py_XDECREF(varbind);
2015
2064
        }
2016
2065
        /* reuse the response as the next pdu to send */
2017
2066
        pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
2037
2086
  }
2038
2087
 
2039
2088
 done:
 
2089
  Py_XDECREF(varbinds);
2040
2090
  SAFE_FREE(oid_arr);
2041
2091
  return (val_tuple ? val_tuple : Py_BuildValue(""));
2042
2092
}
2079
2129
  int err_ind;
2080
2130
  int err_num;
2081
2131
  char err_str[STR_BUF_SIZE];
 
2132
  char *tmpstr;
 
2133
  Py_ssize_t tmplen;
2082
2134
           
2083
2135
  oid_arr = calloc(MAX_OID_LEN, sizeof(oid));
2084
2136
 
2093
2145
      
2094
2146
      ss = (SnmpSession *)py_netsnmp_attr_long(session, "sess_ptr");
2095
2147
 
2096
 
      strcpy(err_str, py_netsnmp_attr_string(session, "ErrorStr"));
 
2148
      if (py_netsnmp_attr_string(session, "ErrorStr", &tmpstr, &tmplen) < 0) {
 
2149
        goto done;
 
2150
      }
 
2151
      memcpy(&err_str, tmpstr, tmplen);
2097
2152
      err_num = py_netsnmp_attr_long(session, "ErrorNum");
2098
2153
      err_ind = py_netsnmp_attr_long(session, "ErrorInd");
2099
2154
 
2116
2171
      varbinds_iter = PyObject_GetIter(varbinds);
2117
2172
 
2118
2173
      while (varbinds_iter && (varbind = PyIter_Next(varbinds_iter))) {
2119
 
        tag = py_netsnmp_attr_string(varbind, "tag");
2120
 
        iid = py_netsnmp_attr_string(varbind, "iid");
2121
 
 
2122
 
        tp = __tag2oid(tag, iid, oid_arr, &oid_arr_len, 
2123
 
                       NULL, best_guess);
 
2174
        if (py_netsnmp_attr_string(varbind, "tag", &tag, NULL) < 0 ||
 
2175
          py_netsnmp_attr_string(varbind, "iid", &iid, NULL) < 0)
 
2176
        {
 
2177
          oid_arr_len = 0;
 
2178
        } else {
 
2179
          tp = __tag2oid(tag, iid, oid_arr, &oid_arr_len, NULL, best_guess);
 
2180
        }
2124
2181
 
2125
2182
        if (oid_arr_len) {
2126
2183
          snmp_add_null_var(pdu, oid_arr, oid_arr_len);
2303
2360
  int err_ind;
2304
2361
  int err_num;
2305
2362
  char err_str[STR_BUF_SIZE];
 
2363
  char *tmpstr;
 
2364
  Py_ssize_t tmplen;
2306
2365
           
2307
2366
  oid_arr = calloc(MAX_OID_LEN, sizeof(oid));
2308
2367
 
2315
2374
    ss = (SnmpSession *)py_netsnmp_attr_long(session, "sess_ptr");
2316
2375
 
2317
2376
    /* PyObject_SetAttrString(); */
2318
 
    strcpy(err_str, py_netsnmp_attr_string(session, "ErrorStr"));
 
2377
    if (py_netsnmp_attr_string(session, "ErrorStr", &tmpstr, &tmplen) < 0) {
 
2378
      goto done;
 
2379
    }
 
2380
    memcpy(&err_str, tmpstr, tmplen);
2319
2381
    err_num = py_netsnmp_attr_long(session, "ErrorNum");
2320
2382
    err_ind = py_netsnmp_attr_long(session, "ErrorInd");
2321
2383
 
2329
2391
      PyObject *varlist_iter = PyObject_GetIter(varlist);
2330
2392
 
2331
2393
      while (varlist_iter && (varbind = PyIter_Next(varlist_iter))) {
2332
 
        tag = py_netsnmp_attr_string(varbind, "tag");
2333
 
        iid = py_netsnmp_attr_string(varbind, "iid");
2334
 
 
2335
 
        tp = __tag2oid(tag, iid, oid_arr, &oid_arr_len, 
2336
 
                       &type, best_guess);
 
2394
        if (py_netsnmp_attr_string(varbind, "tag", &tag, NULL) < 0 ||
 
2395
          py_netsnmp_attr_string(varbind, "iid", &iid, NULL) < 0)
 
2396
        {
 
2397
          oid_arr_len = 0;
 
2398
        } else {
 
2399
          tp = __tag2oid(tag, iid, oid_arr, &oid_arr_len, &type, best_guess);
 
2400
        }
2337
2401
 
2338
2402
        if (oid_arr_len==0) {
2339
2403
          if (verbose)
2344
2408
        }
2345
2409
 
2346
2410
        if (type == TYPE_UNKNOWN) {
2347
 
          type_str = py_netsnmp_attr_string(varbind, "type");
 
2411
          if (py_netsnmp_attr_string(varbind, "type", &type_str, NULL) < 0) {
 
2412
            snmp_free_pdu(pdu);
 
2413
            goto done;
 
2414
          }
2348
2415
          type = __translate_appl_type(type_str);
2349
2416
          if (type == TYPE_UNKNOWN) {
2350
2417
            if (verbose)
2354
2421
          }
2355
2422
        }
2356
2423
 
2357
 
        val = py_netsnmp_attr_string(varbind, "val");
2358
 
        strcpy(tmp_val_str, val);
 
2424
        if (py_netsnmp_attr_string(varbind, "val", &val, &tmplen) < 0) {
 
2425
          snmp_free_pdu(pdu);
 
2426
          goto done;
 
2427
        }
 
2428
        memcpy(tmp_val_str, val, tmplen);
2359
2429
        if (type==TYPE_INTEGER && use_enums && tp && tp->enums) {
2360
2430
          for(ep = tp->enums; ep; ep = ep->next) {
2361
2431
            if (val && !strcmp(ep->label, val)) {
2364
2434
            }
2365
2435
          }
2366
2436
        }
2367
 
        len = STRLEN(tmp_val_str);
 
2437
        len = (int)tmplen;
2368
2438
        status = __add_var_val_str(pdu, oid_arr, oid_arr_len,
2369
2439
                                tmp_val_str, len, type);
2370
2440