Lines Matching +full:1 +full:- +full:port

11  * 1. Redistributions of source code must retain the above copyright
104 struct inet_port *port; member
141 /** common i/o port stuff (must be first) */
156 /** port number in dns case; network byte order */
159 /** create a port */
162 /** activate a port */
165 /** deactivate port */
225 * Deactivate SNMP port.
227 * \param tp port to close
232 p->deactivate(p); in deactivate_port()
236 * This function activates a port. For ports opened via the config files
241 * \param tp transport port
248 struct inet_port *port = (struct inet_port *)tp; in inet_activate() local
250 return (port->activate(port)); in inet_activate()
254 * Close the SNMP port if it is open and destroy it.
256 * \param tp port to close
261 struct inet_port *port = (struct inet_port *)tp; in inet_destroy_port() local
263 deactivate_port(port); in inet_destroy_port()
267 free(port->dns_addr); in inet_destroy_port()
268 free(port); in inet_destroy_port()
277 * \return -1 if allocation fails, 0 otherwise
284 if (pi->buf == NULL) { in inet_alloc_buf()
285 if ((pi->buf = buf_alloc(0)) == NULL) { in inet_alloc_buf()
286 (void)recvfrom(pi->fd, drop_buf, sizeof(drop_buf), in inet_alloc_buf()
288 return (-1); in inet_alloc_buf()
290 pi->buflen = buf_size(0); in inet_alloc_buf()
304 * \return -1 when something goes wrong, 0 othersise
309 struct iovec iov[1]; in inet_read_msg()
311 iov[0].iov_base = pi->buf; in inet_read_msg()
312 iov[0].iov_len = pi->buflen; in inet_read_msg()
314 msg->msg_name = pi->peer; in inet_read_msg()
315 msg->msg_namelen = pi->peerlen; in inet_read_msg()
316 msg->msg_iov = iov; in inet_read_msg()
317 msg->msg_iovlen = 1; in inet_read_msg()
318 msg->msg_control = cbuf; in inet_read_msg()
319 msg->msg_controllen = RECV_CBUF_SIZE; in inet_read_msg()
320 msg->msg_flags = 0; in inet_read_msg()
324 const ssize_t len = recvmsg(pi->fd, msg, 0); in inet_read_msg()
326 if (len == -1 || len == 0) in inet_read_msg()
328 return (-1); in inet_read_msg()
330 if (msg->msg_flags & MSG_TRUNC) { in inet_read_msg()
331 /* truncated - drop */ in inet_read_msg()
334 return (-1); in inet_read_msg()
337 pi->length = (size_t)len; in inet_read_msg()
345 * \param tp transport port
353 struct inet_port *port = __containerof(tp, struct inet_port, tport); in inet_recv() local
356 assert(port->proto == BegemotSnmpdTransportProto_udp); in inet_recv()
358 if (inet_alloc_buf(pi) == -1) in inet_recv()
359 return (-1); in inet_recv()
364 if (inet_read_msg(pi, &msg, cbuf) == -1) in inet_recv()
365 return (-1); in inet_recv()
367 sock->parse_ctrl(sock, &msg); in inet_recv()
375 * \param tp port
388 struct port_sock *s = (pi == NULL) ? TAILQ_FIRST(&p->socks) : in inet_send2()
400 msg.msg_iovlen = 1; in inet_send2()
401 msg.msg_name = (void *)pi->peer; in inet_send2()
402 msg.msg_namelen = pi->peerlen; in inet_send2()
405 if (s->set_ret_source) { in inet_send2()
406 s->setsrc(s, &msg, cbuf); in inet_send2()
412 return (sendmsg(s->input.fd, &msg, 0)); in inet_send2()
441 /** port number */
442 uint32_t port; member
452 * \param port the port to create
458 ipv4_create(struct inet_port *port, struct inet_port_params *params) in ipv4_create() argument
461 if (params->addr_len != 4) in ipv4_create()
464 struct port_sock *sock = calloc(1, sizeof(struct port_sock)); in ipv4_create()
468 snmpd_input_init(&sock->input); in ipv4_create()
470 TAILQ_INSERT_HEAD(&port->socks, sock, link); in ipv4_create()
473 (struct sockaddr_in *)&sock->bind_addr; in ipv4_create()
475 sin->sin_len = sizeof(struct sockaddr_in); in ipv4_create()
476 sin->sin_family = AF_INET; in ipv4_create()
477 sin->sin_port = htons(params->port); in ipv4_create()
478 memcpy(&sin->sin_addr, params->addr, 4); /* network byte order */ in ipv4_create()
480 sock->port = port; in ipv4_create()
486 * An IPv4 inet port is ready. Delegate to the generic code to read the data
497 sock->input.peerlen = sizeof(struct sockaddr_in); in ipv4_input()
498 snmpd_input(&sock->input, &sock->port->tport); in ipv4_input()
511 if ((sock->input.fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { in ipv4_activate_sock()
517 (const struct sockaddr_in *)&sock->bind_addr; in ipv4_activate_sock()
519 if (sin->sin_addr.s_addr == INADDR_ANY) { in ipv4_activate_sock()
521 static const int on = 1; in ipv4_activate_sock()
523 if (setsockopt(sock->input.fd, IPPROTO_IP, IP_RECVDSTADDR, &on, in ipv4_activate_sock()
524 sizeof(on)) == -1) { in ipv4_activate_sock()
526 (void)close(sock->input.fd); in ipv4_activate_sock()
527 sock->input.fd = -1; in ipv4_activate_sock()
530 sock->set_ret_source = true; in ipv4_activate_sock()
533 if (bind(sock->input.fd, (const struct sockaddr *)sin, sizeof(*sin))) { in ipv4_activate_sock()
535 (void)close(sock->input.fd); in ipv4_activate_sock()
536 sock->input.fd = -1; in ipv4_activate_sock()
539 syslog(LOG_ERR, "bind: %s:%u %m", inet_ntoa(sin->sin_addr), in ipv4_activate_sock()
540 ntohs(sin->sin_port)); in ipv4_activate_sock()
541 (void)close(sock->input.fd); in ipv4_activate_sock()
542 sock->input.fd = -1; in ipv4_activate_sock()
546 if ((sock->input.id = fd_select(sock->input.fd, ipv4_input, in ipv4_activate_sock()
548 (void)close(sock->input.fd); in ipv4_activate_sock()
549 sock->input.fd = -1; in ipv4_activate_sock()
552 sock->input.peer = (struct sockaddr *)&sock->ret_dest; in ipv4_activate_sock()
554 sock->parse_ctrl = ipv4_parse_ctrl; in ipv4_activate_sock()
555 sock->setsrc = ipv4_setsrc; in ipv4_activate_sock()
564 * \param p inet port
571 struct port_sock *sock = TAILQ_FIRST(&p->socks); in ipv4_activate()
577 p->row_status = RowStatus_active; in ipv4_activate()
585 * \param p inet port
590 struct port_sock *sock = TAILQ_FIRST(&p->socks); in ipv4_deactivate()
594 snmpd_input_close(&sock->input); in ipv4_deactivate()
596 p->row_status = RowStatus_notInService; in ipv4_deactivate()
616 if (cmsg->cmsg_level == IPPROTO_IP && in ipv4_parse_ctrl()
617 cmsg->cmsg_type == IP_RECVDSTADDR) { in ipv4_parse_ctrl()
618 memcpy(&sock->ret_source.a4, CMSG_DATA(cmsg), in ipv4_parse_ctrl()
621 } else if (cmsg->cmsg_level == SOL_SOCKET && in ipv4_parse_ctrl()
622 cmsg->cmsg_type == SCM_CREDS) { in ipv4_parse_ctrl()
627 sock->input.priv = 0; in ipv4_parse_ctrl()
628 if (sock->input.cred && cred) in ipv4_parse_ctrl()
630 sock->input.priv = (cred->sc_euid == 0); in ipv4_parse_ctrl()
644 msg->msg_control = cbuf; in ipv4_setsrc()
645 msg->msg_controllen = CMSG_SPACE(sizeof(struct in_addr)); in ipv4_setsrc()
649 cmsg->cmsg_level = IPPROTO_IP; in ipv4_setsrc()
650 cmsg->cmsg_type = IP_SENDSRCADDR; in ipv4_setsrc()
651 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr)); in ipv4_setsrc()
652 memcpy(CMSG_DATA(cmsg), &sock->ret_source.a4, in ipv4_setsrc()
660 * \param port the table row
664 * \return SNMP_ERR_NOERROR if port has been created, error code otherwise
667 ipv6_create_common(struct inet_port *port, struct inet_port_params *params, in ipv6_create_common() argument
670 struct port_sock *sock = calloc(1, sizeof(struct port_sock)); in ipv6_create_common()
675 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)&sock->bind_addr; in ipv6_create_common()
677 sin->sin6_len = sizeof(struct sockaddr_in6); in ipv6_create_common()
678 sin->sin6_family = AF_INET6; in ipv6_create_common()
679 sin->sin6_port = htons(params->port); in ipv6_create_common()
680 sin->sin6_flowinfo = 0; in ipv6_create_common()
681 sin->sin6_scope_id = scope_id; in ipv6_create_common()
683 memcpy(sin->sin6_addr.s6_addr, params->addr, 16); in ipv6_create_common()
685 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) && scope_id == 0) { in ipv6_create_common()
689 &sin->sin6_addr, buf, sizeof(buf))); in ipv6_create_common()
694 sock->port = port; in ipv6_create_common()
696 snmpd_input_init(&sock->input); in ipv6_create_common()
697 TAILQ_INSERT_HEAD(&port->socks, sock, link); in ipv6_create_common()
706 * \param port the port to create
712 ipv6_create(struct inet_port *port, struct inet_port_params *params) in ipv6_create() argument
714 if (params->addr_len != 16) in ipv6_create()
717 const int ret = ipv6_create_common(port, params, 0); in ipv6_create()
725 * An IPv6 inet port is ready. Delegate to the generic code to read the data
736 sock->input.peerlen = sizeof(struct sockaddr_in6); in ipv6_input()
737 snmpd_input(&sock->input, &sock->port->tport); in ipv6_input()
750 if ((sock->input.fd = socket(PF_INET6, SOCK_DGRAM, 0)) == -1) { in ipv6_activate_sock()
756 (const struct sockaddr_in6 *)&sock->bind_addr; in ipv6_activate_sock()
758 if (memcmp(&sin->sin6_addr, &in6addr_any, sizeof(in6addr_any)) == 0) { in ipv6_activate_sock()
760 static const int on = 1; in ipv6_activate_sock()
762 if (setsockopt(sock->input.fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, in ipv6_activate_sock()
763 &on, sizeof(on)) == -1) { in ipv6_activate_sock()
765 (void)close(sock->input.fd); in ipv6_activate_sock()
766 sock->input.fd = -1; in ipv6_activate_sock()
769 sock->set_ret_source = true; in ipv6_activate_sock()
772 if (bind(sock->input.fd, (const struct sockaddr *)sin, sizeof(*sin))) { in ipv6_activate_sock()
774 (void)close(sock->input.fd); in ipv6_activate_sock()
775 sock->input.fd = -1; in ipv6_activate_sock()
780 &sin->sin6_addr, buf, sizeof(buf)), sin->sin6_scope_id, in ipv6_activate_sock()
781 ntohs(sin->sin6_port)); in ipv6_activate_sock()
782 (void)close(sock->input.fd); in ipv6_activate_sock()
783 sock->input.fd = -1; in ipv6_activate_sock()
786 if ((sock->input.id = fd_select(sock->input.fd, ipv6_input, in ipv6_activate_sock()
788 (void)close(sock->input.fd); in ipv6_activate_sock()
789 sock->input.fd = -1; in ipv6_activate_sock()
792 sock->input.peer = (struct sockaddr *)&sock->ret_dest; in ipv6_activate_sock()
794 sock->parse_ctrl = ipv6_parse_ctrl; in ipv6_activate_sock()
795 sock->setsrc = ipv6_setsrc; in ipv6_activate_sock()
803 * \param port inet port
810 struct port_sock *sock = TAILQ_FIRST(&p->socks); in ipv6_activate()
816 p->row_status = RowStatus_active; in ipv6_activate()
823 * \param p inet port
828 struct port_sock *sock = TAILQ_FIRST(&p->socks); in ipv6_deactivate()
832 snmpd_input_close(&sock->input); in ipv6_deactivate()
834 p->row_status = RowStatus_notInService; in ipv6_deactivate()
853 if (cmsg->cmsg_level == IPPROTO_IPV6 && in ipv6_parse_ctrl()
854 cmsg->cmsg_type == IPV6_PKTINFO) { in ipv6_parse_ctrl()
858 sock->ret_source.a6.ipi6_addr = info->ipi6_addr; in ipv6_parse_ctrl()
859 sock->ret_source.a6.ipi6_ifindex = in ipv6_parse_ctrl()
860 !IN6_IS_ADDR_LINKLOCAL(&info->ipi6_addr) ? 0: in ipv6_parse_ctrl()
861 info->ipi6_ifindex; in ipv6_parse_ctrl()
863 } else if (cmsg->cmsg_level == SOL_SOCKET && in ipv6_parse_ctrl()
864 cmsg->cmsg_type == SCM_CREDS) { in ipv6_parse_ctrl()
869 sock->input.priv = 0; in ipv6_parse_ctrl()
870 if (sock->input.cred && cred) in ipv6_parse_ctrl()
872 sock->input.priv = (cred->sc_euid == 0); in ipv6_parse_ctrl()
886 msg->msg_control = cbuf; in ipv6_setsrc()
887 msg->msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo)); in ipv6_setsrc()
891 cmsg->cmsg_level = IPPROTO_IPV6; in ipv6_setsrc()
892 cmsg->cmsg_type = IPV6_PKTINFO; in ipv6_setsrc()
893 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); in ipv6_setsrc()
894 memcpy(CMSG_DATA(cmsg), &sock->ret_source.a6, in ipv6_setsrc()
902 * \param port the port to create
908 ipv6z_create(struct inet_port *port, struct inet_port_params *params) in ipv6z_create() argument
910 if (params->addr_len != 20) in ipv6z_create()
916 scope_id |= params->addr[i]; in ipv6z_create()
919 const int ret = ipv6_create_common(port, params, scope_id); in ipv6z_create()
930 * \param port the port to create
936 dns_create(struct inet_port *port, struct inet_port_params *params) in dns_create() argument
938 if (params->addr_len > 64) in dns_create()
941 if (strnlen(params->addr, params->addr_len) != in dns_create()
942 params->addr_len) in dns_create()
945 if ((port->dns_addr = realloc(params->addr, in dns_create()
946 params->addr_len + 1)) == NULL) in dns_create()
949 port->dns_addr[params->addr_len] = '\0'; in dns_create()
950 params->addr = NULL; in dns_create()
952 port->dns_port = htons(params->port); in dns_create()
961 * \param port inet port
966 dns_activate(struct inet_port *port) in dns_activate() argument
971 hints.ai_socktype = SOCK_DGRAM; // XXX udp-only in dns_activate()
975 sprintf(portbuf, "%hu", ntohs(port->dns_port)); in dns_activate()
978 int error = getaddrinfo(port->dns_addr[0] == '\0' in dns_activate()
979 ? NULL : port->dns_addr, in dns_activate()
984 port->dns_addr, gai_strerror(error)); in dns_activate()
988 for (struct addrinfo *res = res0; res != NULL; res = res->ai_next) { in dns_activate()
989 if (res->ai_family != AF_INET && res->ai_family != AF_INET6) in dns_activate()
992 struct port_sock *sock = calloc(1, sizeof(struct port_sock)); in dns_activate()
996 snmpd_input_init(&sock->input); in dns_activate()
997 sock->port = port; in dns_activate()
1001 if (res->ai_family == AF_INET) { in dns_activate()
1002 *(struct sockaddr_in *)&sock->bind_addr = in dns_activate()
1003 *(struct sockaddr_in *)(void *)res->ai_addr; in dns_activate()
1006 *(struct sockaddr_in6 *)&sock->bind_addr = in dns_activate()
1007 *(struct sockaddr_in6 *)(void *)res->ai_addr; in dns_activate()
1014 TAILQ_INSERT_HEAD(&port->socks, sock, link); in dns_activate()
1017 if (!TAILQ_EMPTY(&port->socks)) in dns_activate()
1018 port->row_status = RowStatus_active; in dns_activate()
1027 * \param port inet port
1030 dns_deactivate(struct inet_port *port) in dns_deactivate() argument
1032 while (!TAILQ_EMPTY(&port->socks)) { in dns_deactivate()
1033 struct port_sock *sock = TAILQ_FIRST(&port->socks); in dns_deactivate()
1034 TAILQ_REMOVE(&port->socks, sock, link); in dns_deactivate()
1035 snmpd_input_close(&sock->input); in dns_deactivate()
1038 port->row_status = RowStatus_notInService; in dns_deactivate()
1045 struct inet_port *port = NULL; in inet_create() local
1047 if (params->port > 0xffff) { in inet_create()
1052 if ((port = malloc(sizeof(*port))) == NULL) { in inet_create()
1056 memset(port, 0, sizeof(*port)); in inet_create()
1057 TAILQ_INIT(&port->socks); in inet_create()
1059 port->proto = params->proto; in inet_create()
1060 port->tport.index = params->index; in inet_create()
1062 switch (params->type) { in inet_create()
1065 port->create = ipv4_create; in inet_create()
1066 port->activate = ipv4_activate; in inet_create()
1067 port->deactivate = ipv4_deactivate; in inet_create()
1071 port->create = ipv6_create; in inet_create()
1072 port->activate = ipv6_activate; in inet_create()
1073 port->deactivate = ipv6_deactivate; in inet_create()
1077 port->create = ipv6z_create; in inet_create()
1078 port->activate = ipv6_activate; in inet_create()
1079 port->deactivate = ipv6_deactivate; in inet_create()
1083 port->create = dns_create; in inet_create()
1084 port->activate = dns_activate; in inet_create()
1085 port->deactivate = dns_deactivate; in inet_create()
1093 if ((err = port->create(port, params)) != SNMP_ERR_NOERROR) in inet_create()
1096 *pp = port; in inet_create()
1097 trans_insert_port(my_trans, &port->tport); in inet_create()
1101 free(port->dns_addr); in inet_create()
1102 free(port); in inet_create()
1110 struct inet_port *port; in create_and_go() local
1112 if ((err = inet_create(params, &port)) != SNMP_ERR_NOERROR) in create_and_go()
1115 port->row_status = RowStatus_createAndGo; in create_and_go()
1116 ctx->scratch->ptr1 = port; in create_and_go()
1121 return (inet_activate(&port->tport)); in create_and_go()
1128 struct inet_port *port; in create_and_wait() local
1130 if ((err = inet_create(params, &port)) != SNMP_ERR_NOERROR) in create_and_wait()
1133 port->row_status = RowStatus_createAndWait; in create_and_wait()
1134 ctx->scratch->ptr1 = port; in create_and_wait()
1140 * This is called to set a RowStatus value in the port table during
1144 * to CreateAndGo, the port is actually not activated. This is done when
1148 inet_port_set(struct snmp_context *ctx, struct inet_port *port, in inet_port_set() argument
1154 if (port != NULL) in inet_port_set()
1156 ctx->scratch->int1 = SET_CREATED; in inet_port_set()
1160 if (port != NULL) in inet_port_set()
1162 ctx->scratch->int1 = SET_CREATED; in inet_port_set()
1166 if (port == NULL) in inet_port_set()
1169 switch (port->row_status) { in inet_port_set()
1176 ctx->scratch->int1 = SET_ACTIVATED; in inet_port_set()
1177 return (inet_activate(&port->tport)); in inet_port_set()
1190 if (port == NULL) in inet_port_set()
1193 switch (port->row_status) { in inet_port_set()
1204 ctx->scratch->int1 = SET_DEACTIVATE; in inet_port_set()
1216 ctx->scratch->int1 = SET_DESTROY; in inet_port_set()
1226 * Port table
1232 asn_subid_t which = value->var.subs[sub - 1]; in op_snmp_trans_inet()
1233 struct inet_port *port; in op_snmp_trans_inet() local
1240 if ((port = (struct inet_port *)trans_next_port(my_trans, in op_snmp_trans_inet()
1241 &value->var, sub)) == NULL) in op_snmp_trans_inet()
1243 index_append(&value->var, sub, &port->tport.index); in op_snmp_trans_inet()
1247 if ((port = (struct inet_port *)trans_find_port(my_trans, in op_snmp_trans_inet()
1248 &value->var, sub)) == NULL) in op_snmp_trans_inet()
1253 port = (struct inet_port *)trans_find_port(my_trans, in op_snmp_trans_inet()
1254 &value->var, sub); in op_snmp_trans_inet()
1258 if (!isok_RowStatus(value->v.integer)) in op_snmp_trans_inet()
1261 if (index_decode(&value->var, sub, iidx, &params.type, in op_snmp_trans_inet()
1262 &params.addr, &params.addr_len, &params.port, in op_snmp_trans_inet()
1266 asn_slice_oid(&params.index, &value->var, sub, value->var.len); in op_snmp_trans_inet()
1268 ret = inet_port_set(ctx, port, &params, in op_snmp_trans_inet()
1269 (enum RowStatus)value->v.integer); in op_snmp_trans_inet()
1275 if ((port = (struct inet_port *)trans_find_port(my_trans, in op_snmp_trans_inet()
1276 &value->var, sub)) == NULL) in op_snmp_trans_inet()
1280 switch (ctx->scratch->int1) { in op_snmp_trans_inet()
1284 assert(port != NULL); in op_snmp_trans_inet()
1285 inet_destroy_port(&port->tport); in op_snmp_trans_inet()
1290 assert(port != NULL); in op_snmp_trans_inet()
1294 deactivate_port(port); in op_snmp_trans_inet()
1303 if ((port = (struct inet_port *)trans_find_port(my_trans, in op_snmp_trans_inet()
1304 &value->var, sub)) == NULL) in op_snmp_trans_inet()
1308 switch (ctx->scratch->int1) { in op_snmp_trans_inet()
1312 assert(port != NULL); in op_snmp_trans_inet()
1317 assert(port != NULL); in op_snmp_trans_inet()
1318 inet_destroy_port(&port->tport); in op_snmp_trans_inet()
1325 deactivate_port(port); in op_snmp_trans_inet()
1336 value->v.integer = port->row_status; in op_snmp_trans_inet()