static void test_time(void *_) { //str_check(run_time("U:1401010215Z"), "2014-01-01 02:15:00 GMT"); str_check(run_time("U:140101021500Z"), "2014-01-01 02:15:00 GMT"); //str_check(run_time("U:1401010215"), "2014-01-01 02:15:00 GMT"); //str_check(run_time("U:140101021500"), "2014-01-01 02:15:00 GMT"); str_check(run_time("U:14010102150"), "Invalid asn1 time"); str_check(run_time("U:"), "Invalid asn1 time"); str_check(run_time("X:"), "Invalid time object type: 88"); str_check(run_time("G:20140101021544Z"), "2014-01-01 02:15:44 GMT"); end:; }
static int /* O - 0 on success or -1 on error */ try_connect(http_addr_t *addr, /* I - Socket address */ const char *addrname, /* I - Hostname or IP address */ int port) /* I - Port number */ { int fd; /* Socket */ int status; /* Connection status */ debug_printf("DEBUG: %.3f Trying %s://%s:%d...\n", run_time(), port == 515 ? "lpd" : "socket", addrname, port); if ((fd = socket(httpAddrFamily(addr), SOCK_STREAM, 0)) < 0) { fprintf(stderr, "ERROR: Unable to create socket: %s\n", strerror(errno)); return (-1); } _httpAddrSetPort(addr, port); alarm(1); status = connect(fd, (void *)addr, (socklen_t)httpAddrLength(addr)); close(fd); alarm(0); return (status); }
int main (int, char *[]) { Timeout_Handler handler; // Register a 3 second timer. ACE_Time_Value bar_tv (3); ACE_Reactor::instance ()->schedule_timer (&handler, (void *) "Bar", bar_tv, bar_tv); // Register a 2 second timer. ACE_Time_Value foo_tv (2); ACE_Reactor::instance ()->schedule_timer (&handler, (void *) "Foo", foo_tv, foo_tv); // Handle events for 12 seconds. ACE_Time_Value run_time (12); if (ACE_Reactor::run_event_loop(run_time) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p.\n", "main"), -1); return 0; }
void clock_init() { int i; #ifdef THINK_C /* kludge for mac: see run_time */ long l; l = run_time(); #endif for (i=0; i<MAX_CLOCKS; i++) clock_reset(i); send_sato_message(); } /* clock_init */
int svc (void) { // Handle events for 13 seconds. ACE_Time_Value run_time (13); ACE_DEBUG ((LM_DEBUG, "(%t):Starting svc routine\n")); if (this->proactor_->handle_events (run_time) == -1) ACE_ERROR_RETURN ((LM_ERROR, "(%t):%p.\n", "Worker::svc"), -1); ACE_DEBUG ((LM_DEBUG, "(%t) work complete\n")); return 0; }
// Thread fuction. int svc (void) { ACE_DEBUG ((LM_DEBUG, "(%t) Worker started\n")); // Handle events for 13 seconds. ACE_Time_Value run_time (13); // Try to become the owner ACE_Reactor::instance ()->owner (ACE_Thread::self ()); if (ACE_Reactor::run_event_loop (run_time) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p.\n", "Worker::svc"), -1); else ACE_DEBUG ((LM_DEBUG, "(%t) work complete\n")); return 0; }
int ACE_TMAIN (int, ACE_TCHAR *[]) { Timeout_Handler handler; // Register a 2 second timer. ACE_Time_Value foo_tv (2); if (ACE_Proactor::instance ()->schedule_timer (handler, (void *) "Foo", ACE_Time_Value::zero, foo_tv) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "schedule_timer"), -1); // Register a 3 second timer. ACE_Time_Value bar_tv (3); if (ACE_Proactor::instance ()->schedule_timer (handler, (void *) "Bar", ACE_Time_Value::zero, bar_tv) == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "schedule_timer"), -1); // Handle events for 13 seconds. ACE_Time_Value run_time (13); ACE_DEBUG ((LM_DEBUG, "Starting event loop\n")); // Run the event loop. if (ACE_Proactor::run_event_loop(run_time) == -1) ACE_ERROR_RETURN ((LM_ERROR, "(%t):%p.\n", "Worker::svc"), 1); ACE_DEBUG ((LM_DEBUG, "Ending event loop\n")); return 0; }
static void read_snmp_response(int fd) /* I - SNMP socket file descriptor */ { char addrname[256]; /* Source address name */ cups_snmp_t packet; /* Decoded packet */ snmp_cache_t key, /* Search key */ *device; /* Matching device */ /* * Read the response data... */ if (!_cupsSNMPRead(fd, &packet, -1.0)) { fprintf(stderr, "ERROR: Unable to read data from socket: %s\n", strerror(errno)); return; } if (HostNameLookups) httpAddrLookup(&(packet.address), addrname, sizeof(addrname)); else httpAddrString(&(packet.address), addrname, sizeof(addrname)); debug_printf("DEBUG: %.3f Received data from %s...\n", run_time(), addrname); /* * Look for the response status code in the SNMP message header... */ if (packet.error) { fprintf(stderr, "ERROR: Bad SNMP packet from %s: %s\n", addrname, packet.error); return; } debug_printf("DEBUG: community=\"%s\"\n", packet.community); debug_printf("DEBUG: request-id=%d\n", packet.request_id); debug_printf("DEBUG: error-status=%d\n", packet.error_status); if (packet.error_status && packet.request_id != DEVICE_TYPE) return; /* * Find a matching device in the cache... */ key.addrname = addrname; device = (snmp_cache_t *)cupsArrayFind(Devices, &key); /* * Process the message... */ switch (packet.request_id) { case DEVICE_TYPE : /* * Got the device type response... */ if (device) { debug_printf("DEBUG: Discarding duplicate device type for \"%s\"...\n", addrname); return; } /* * Add the device and request the device data... */ add_cache(&(packet.address), addrname, NULL, NULL, NULL); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_DESCRIPTION, DescriptionOID); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_ID, DeviceIdOID); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_URI, UriOID); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_LOCATION, LocationOID); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_PRODUCT, LexmarkProductOID); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_PRODUCT, LexmarkProductOID2); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_ID, LexmarkDeviceIdOID); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_PRODUCT, XeroxProductOID); break; case DEVICE_DESCRIPTION : if (device && packet.object_type == CUPS_ASN1_OCTET_STRING) { /* * Update an existing cache entry... */ char make_model[256]; /* Make and model */ if (strchr((char *)packet.object_value.string.bytes, ':') && strchr((char *)packet.object_value.string.bytes, ';')) { /* * Description is the IEEE-1284 device ID... */ char *ptr; /* Pointer into device ID */ for (ptr = (char *)packet.object_value.string.bytes; *ptr; ptr ++) if (*ptr == '\n') *ptr = ';'; /* A lot of bad printers put a newline */ if (!device->id) device->id = strdup((char *)packet.object_value.string.bytes); backendGetMakeModel((char *)packet.object_value.string.bytes, make_model, sizeof(make_model)); if (device->info) free(device->info); device->info = strdup(make_model); } else { /* * Description is plain text... */ fix_make_model(make_model, (char *)packet.object_value.string.bytes, sizeof(make_model)); if (device->info) free(device->info); device->info = strdup((char *)packet.object_value.string.bytes); } if (!device->make_and_model) device->make_and_model = strdup(make_model); } break; case DEVICE_ID : if (device && packet.object_type == CUPS_ASN1_OCTET_STRING && (!device->id || strlen(device->id) < packet.object_value.string.num_bytes)) { /* * Update an existing cache entry... */ char make_model[256]; /* Make and model */ char *ptr; /* Pointer into device ID */ for (ptr = (char *)packet.object_value.string.bytes; *ptr; ptr ++) if (*ptr == '\n') *ptr = ';'; /* A lot of bad printers put a newline */ if (device->id) free(device->id); device->id = strdup((char *)packet.object_value.string.bytes); /* * Convert the ID to a make and model string... */ backendGetMakeModel((char *)packet.object_value.string.bytes, make_model, sizeof(make_model)); if (device->make_and_model) free(device->make_and_model); device->make_and_model = strdup(make_model); } break; case DEVICE_LOCATION : if (device && packet.object_type == CUPS_ASN1_OCTET_STRING && !device->location) device->location = strdup((char *)packet.object_value.string.bytes); break; case DEVICE_PRODUCT : if (device && packet.object_type == CUPS_ASN1_OCTET_STRING && !device->id) { /* * Update an existing cache entry... */ if (!device->info) device->info = strdup((char *)packet.object_value.string.bytes); if (device->make_and_model) free(device->make_and_model); device->make_and_model = strdup((char *)packet.object_value.string.bytes); } break; case DEVICE_URI : if (device && packet.object_type == CUPS_ASN1_OCTET_STRING && !device->uri && packet.object_value.string.num_bytes > 3) { /* * Update an existing cache entry... */ char scheme[32], /* URI scheme */ userpass[256], /* Username:password in URI */ hostname[256], /* Hostname in URI */ resource[1024]; /* Resource path in URI */ int port; /* Port number in URI */ if (!strncmp((char *)packet.object_value.string.bytes, "lpr:", 4)) { /* * We want "lpd://..." for the URI... */ packet.object_value.string.bytes[2] = 'd'; } if (httpSeparateURI(HTTP_URI_CODING_ALL, (char *)packet.object_value.string.bytes, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource)) >= HTTP_URI_OK) device->uri = strdup((char *)packet.object_value.string.bytes); } break; } }
static void probe_device(snmp_cache_t *device) /* I - Device */ { char uri[1024], /* Full device URI */ *uriptr, /* Pointer into URI */ *format; /* Format string for device */ device_uri_t *device_uri; /* Current DeviceURI match */ debug_printf("DEBUG: %.3f Probing %s...\n", run_time(), device->addrname); #ifdef __APPLE__ /* * If the printer supports Bonjour/mDNS, don't report it from the SNMP backend. */ if (!try_connect(&(device->address), device->addrname, 5353)) { debug_printf("DEBUG: %s supports mDNS, not reporting!\n", device->addrname); return; } #endif /* __APPLE__ */ /* * Lookup the device in the match table... */ for (device_uri = (device_uri_t *)cupsArrayFirst(DeviceURIs); device_uri; device_uri = (device_uri_t *)cupsArrayNext(DeviceURIs)) if (device->make_and_model && !regexec(&(device_uri->re), device->make_and_model, 0, NULL, 0)) { /* * Found a match, add the URIs... */ for (format = (char *)cupsArrayFirst(device_uri->uris); format; format = (char *)cupsArrayNext(device_uri->uris)) { for (uriptr = uri; *format && uriptr < (uri + sizeof(uri) - 1);) if (*format == '%' && format[1] == 's') { /* * Insert hostname/address... */ strlcpy(uriptr, device->addrname, sizeof(uri) - (size_t)(uriptr - uri)); uriptr += strlen(uriptr); format += 2; } else *uriptr++ = *format++; *uriptr = '\0'; update_cache(device, uri, NULL, NULL); } return; } /* * Then try the standard ports... */ if (!try_connect(&(device->address), device->addrname, 9100)) { debug_printf("DEBUG: %s supports AppSocket!\n", device->addrname); snprintf(uri, sizeof(uri), "socket://%s", device->addrname); update_cache(device, uri, NULL, NULL); } else if (!try_connect(&(device->address), device->addrname, 515)) { debug_printf("DEBUG: %s supports LPD!\n", device->addrname); snprintf(uri, sizeof(uri), "lpd://%s/", device->addrname); update_cache(device, uri, NULL, NULL); } }
static void scan_devices(int ipv4, /* I - SNMP IPv4 socket */ int ipv6) /* I - SNMP IPv6 socket */ { int fd, /* File descriptor for this address */ busy; /* Are we busy processing something? */ char *address, /* Current address */ *community; /* Current community */ fd_set input; /* Input set for select() */ struct timeval timeout; /* Timeout for select() */ time_t endtime; /* End time for scan */ http_addrlist_t *addrs, /* List of addresses */ *addr; /* Current address */ snmp_cache_t *device; /* Current device */ char temp[1024]; /* Temporary address string */ gettimeofday(&StartTime, NULL); /* * First send all of the broadcast queries... */ for (address = (char *)cupsArrayFirst(Addresses); address; address = (char *)cupsArrayNext(Addresses)) { if (!strcmp(address, "@LOCAL")) addrs = get_interface_addresses(NULL); else if (!strncmp(address, "@IF(", 4)) { char ifname[255]; /* Interface name */ strlcpy(ifname, address + 4, sizeof(ifname)); if (ifname[0]) ifname[strlen(ifname) - 1] = '\0'; addrs = get_interface_addresses(ifname); } else addrs = httpAddrGetList(address, AF_UNSPEC, NULL); if (!addrs) { fprintf(stderr, "ERROR: Unable to scan \"%s\"!\n", address); continue; } for (community = (char *)cupsArrayFirst(Communities); community; community = (char *)cupsArrayNext(Communities)) { debug_printf("DEBUG: Scanning for devices in \"%s\" via \"%s\"...\n", community, address); for (addr = addrs; addr; addr = addr->next) { #ifdef AF_INET6 if (httpAddrFamily(&(addr->addr)) == AF_INET6) fd = ipv6; else #endif /* AF_INET6 */ fd = ipv4; debug_printf("DEBUG: Sending get request to %s...\n", httpAddrString(&(addr->addr), temp, sizeof(temp))); _cupsSNMPWrite(fd, &(addr->addr), CUPS_SNMP_VERSION_1, community, CUPS_ASN1_GET_REQUEST, DEVICE_TYPE, DeviceTypeOID); } } httpAddrFreeList(addrs); } /* * Then read any responses that come in over the next 3 seconds... */ endtime = time(NULL) + MaxRunTime; FD_ZERO(&input); while (time(NULL) < endtime) { timeout.tv_sec = 2; timeout.tv_usec = 0; FD_SET(ipv4, &input); if (ipv6 >= 0) FD_SET(ipv6, &input); fd = ipv4 > ipv6 ? ipv4 : ipv6; if (select(fd + 1, &input, NULL, NULL, &timeout) < 0) { fprintf(stderr, "ERROR: %.3f select() for %d/%d failed: %s\n", run_time(), ipv4, ipv6, strerror(errno)); break; } busy = 0; if (FD_ISSET(ipv4, &input)) { read_snmp_response(ipv4); busy = 1; } if (ipv6 >= 0 && FD_ISSET(ipv6, &input)) { read_snmp_response(ipv6); busy = 1; } if (!busy) { /* * List devices with complete information... */ int sent_something = 0; for (device = (snmp_cache_t *)cupsArrayFirst(Devices); device; device = (snmp_cache_t *)cupsArrayNext(Devices)) if (!device->sent && device->info && device->make_and_model) { if (device->uri) list_device(device); else probe_device(device); device->sent = sent_something = 1; } if (!sent_something) break; } } debug_printf("DEBUG: %.3f Scan complete!\n", run_time()); }