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 /* O - 1 on success, 0 on error */ _cupsSNMPWrite( int fd, /* I - SNMP socket */ http_addr_t *address, /* I - Address to send to */ int version, /* I - SNMP version */ const char *community, /* I - Community name */ cups_asn1_t request_type, /* I - Request type */ const unsigned request_id, /* I - Request ID */ const int *oid) /* I - OID */ { int i; /* Looping var */ cups_snmp_t packet; /* SNMP message packet */ unsigned char buffer[CUPS_SNMP_MAX_PACKET]; /* SNMP message buffer */ int bytes; /* Size of message */ http_addr_t temp; /* Copy of address */ /* * Range check input... */ DEBUG_printf(("4_cupsSNMPWrite(fd=%d, address=%p, version=%d, " "community=\"%s\", request_type=%d, request_id=%u, oid=%p)", fd, address, version, community, request_type, request_id, oid)); if (fd < 0 || !address || version != CUPS_SNMP_VERSION_1 || !community || (request_type != CUPS_ASN1_GET_REQUEST && request_type != CUPS_ASN1_GET_NEXT_REQUEST) || request_id < 1 || !oid) { DEBUG_puts("5_cupsSNMPWrite: Returning 0 (bad arguments)"); return (0); } /* * Create the SNMP message... */ memset(&packet, 0, sizeof(packet)); packet.version = version; packet.request_type = request_type; packet.request_id = request_id; packet.object_type = CUPS_ASN1_NULL_VALUE; strlcpy(packet.community, community, sizeof(packet.community)); for (i = 0; oid[i] >= 0 && i < (CUPS_SNMP_MAX_OID - 1); i ++) packet.object_name[i] = oid[i]; packet.object_name[i] = -1; if (oid[i] >= 0) { DEBUG_puts("5_cupsSNMPWrite: Returning 0 (OID too big)"); errno = E2BIG; return (0); } bytes = asn1_encode_snmp(buffer, sizeof(buffer), &packet); if (bytes < 0) { DEBUG_puts("5_cupsSNMPWrite: Returning 0 (request too big)"); errno = E2BIG; return (0); } asn1_debug("DEBUG: OUT ", buffer, bytes, 0); /* * Send the message... */ temp = *address; _httpAddrSetPort(&temp, CUPS_SNMP_PORT); return (sendto(fd, buffer, bytes, 0, (void *)&temp, httpAddrLength(&temp)) == bytes); }
static int /* O - Socket or -1 on error */ rresvport_af(int *port, /* IO - Port number to bind to */ int family) /* I - Address family */ { http_addr_t addr; /* Socket address */ int fd; /* Socket file descriptor */ /* * Try to create an IPv4 socket... */ if ((fd = socket(family, SOCK_STREAM, 0)) < 0) return (-1); /* * Initialize the address buffer... */ memset(&addr, 0, sizeof(addr)); addr.addr.sa_family = family; /* * Try to bind the socket to a reserved port... */ while (*port > 511) { /* * Set the port number... */ _httpAddrSetPort(&addr, *port); /* * Try binding the port to the socket; return if all is OK... */ if (!bind(fd, (struct sockaddr *)&addr, sizeof(addr))) return (fd); /* * Stop if we have any error other than "address already in use"... */ if (errno != EADDRINUSE) { # ifdef WIN32 closesocket(fd); # else close(fd); # endif /* WIN32 */ return (-1); } /* * Try the next port... */ (*port)--; } /* * Wasn't able to bind to a reserved port, so close the socket and return * -1... */ # ifdef WIN32 closesocket(fd); # else close(fd); # endif /* WIN32 */ return (-1); }