int qini_ParseFile ( FILE *f, tree_sTable *ntp, int *warnings, int *errors, int *fatals ) { pwr_tStatus sts = 1; int n; char *s; char buffer[256]; int error = 0; char name[80]; char s_nid[80]; char s_naddr[80]; char s_port[80]; char s_connection[80]; char s_min_resend_time[80]; char s_max_resend_time[80]; pwr_tNodeId nid; struct in_addr naddr; qini_sNode *nep; struct arpreq arpreq; while ((s = fgets(buffer, sizeof(buffer) - 1, f)) != NULL) { if (*s == '#' || *s == '!') { s++; continue; } n = sscanf(s, "%s %s %s %s %s %s %s", name, s_nid, s_naddr, s_port, s_connection, s_min_resend_time, s_max_resend_time); if (n < 3) { errh_Error("error in line, <wrong number of arguments>, skip to next line.\n>> %s", s); (*errors)++; continue; } sts = cdh_StringToVolumeId(s_nid, (pwr_tVolumeId *)&nid); if (EVEN(sts)) { errh_Error("error in line, <node identity>, skip to next line.\n>> %s", s); (*errors)++; continue; } sts = net_StringToAddr( s_naddr, &naddr); if ( EVEN(sts)) { errh_Error("error in line, <network address>, skip to next line.\n>> %s", s); (*errors)++; continue; } #if 0 naddr.s_addr = inet_network(s_naddr); #if defined(OS_VMS) if (naddr.s_addr == (in_addr_t)-1) { #else if (naddr.s_addr == (unsigned int)-1) { #endif /* Try name instead */ struct addrinfo hints; struct addrinfo *res; int err; memset((void*)&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_STREAM; err = getaddrinfo(s_naddr, 0, &hints, &res); if ( err < 0) { errh_Error("error in line, <network address>, skip to next line.\n>> %s", s); (*errors)++; continue; } switch ( res->ai_family) { case AF_INET: // memcpy( &naddr.s_addr, (char *)&res->ai_addr->sa_data + 2, 4); memcpy( &naddr.s_addr, &((struct sock_addr_in *)res->ai_addr)->sin_addr, 4); naddr.s_addr = ntohl( naddr.s_addr); break; case AF_INET6: break; } freeaddrinfo( res); } #endif nep = tree_Find(&sts, ntp, &nid); if (nep != NULL) { errh_Warning("node is allready defined: %s, skip to next line", s); (*warnings)++; continue; } else { nep = tree_Insert(&sts, ntp, &nid); } strcpy(nep->name, name); nep->naddr.s_addr = htonl(naddr.s_addr); if (n > 3) nep->port = htons(atoi(s_port)); if (n > 4) nep->connection = atoi(s_connection); if (n > 5) nep->min_resend_time = atoi(s_min_resend_time); if (n > 6) nep->max_resend_time = atoi(s_max_resend_time); memset(&arpreq, 0, sizeof(arpreq)); memcpy(&arpreq.arp_pa.sa_data, &naddr, sizeof(naddr)); inet_GetArpEntry(&sts, 0, &arpreq); } return error; } pwr_tBoolean qini_BuildDb ( pwr_tStatus *sts, tree_sTable *nodes, qini_sNode *me, #if 0 /* change when new class NetConfig is deined */ pwr_sNetConfig *cp, #else void *cp, #endif qcom_tBus bus ) { qdb_sInit init; qdb_sNode *np; qini_sNode *nep; void *p; qdb_sAppl *ap; memset(&init, 0, sizeof(init)); init.nid = me->nid; init.bus = bus; init.nodes = nodes->nNode; #if 0 /* change when new class NetConfig is deined */ init.queues = cp->Queues; init.applications = cp->Applications; init.sbufs = cp->SmallCount; init.mbufs = cp->MediumCount; init.lbufs = cp->LargeCount; init.size_sbuf = cp->SmallSize; init.size_mbuf = cp->MediumSize; init.size_lbuf = cp->LargeSize; #endif p = qdb_CreateDb(sts, &init); if (p == NULL) return NO; qdb_ScopeLock { for (nep = tree_Minimum(sts, nodes); nep != NULL; nep = tree_Successor(sts, nodes, nep)) { np = addNode(nep); } ap = qdb_AddAppl(NULL, YES); qdb->exportque = addQueue(NULL, qdb_cIexport, "export", qdb_eQue_private, qdb_mQue_system); addQueue(ap, qcom_cInetEvent, "netEvent", qdb_eQue_forward, qdb_mQue_system); addQueue(ap, qcom_cIapplEvent, "applEvent", qdb_eQue_forward, qdb_mQue_system); addQueue(ap, qcom_cImhAllHandlers, "allHandlers", qdb_eQue_forward, qdb_mQue_broadcast); addQueue(ap, qcom_cImhAllOutunits, "allOutunits", qdb_eQue_forward, qdb_mQue_broadcast); addQueue(ap, qcom_cIhdServer, "hdServer", qdb_eQue_forward, qdb_mQue_broadcast); addQueue(ap, qcom_cIhdClient, "hdClient", qdb_eQue_forward, qdb_mQue_broadcast); #if !defined OS_CYGWIN addQueue(NULL, qcom_cInacp, "nacp", qdb_eQue_private, qdb_mQue_system); #endif addQueue(ap, qcom_cIini, "ini", qdb_eQue_forward, qdb_mQue_system | qdb_mQue_event); } qdb_ScopeUnlock; return (YES); }
static int connect_slave( io_sRackLocal *local, io_sRack *rp) { pwr_tStatus sts; pwr_sClass_Modbus_TCP_Slave *op; int buffsize = 10000; int flags; fd_set fdr; /* For select call */ fd_set fdw; /* For select call */ struct timeval tv; unsigned short port; time_GetTimeMonotonic( &local->last_try_connect_time); op = (pwr_sClass_Modbus_TCP_Slave *) rp->op; /* Create socket, store in local struct */ port = op->Port == 0 ? 502 : op->Port; local->s = socket(AF_INET, SOCK_STREAM, 0); if (local->s < 0) { errh_Error( "Error creating socket for IO modbus tcp slave %s, %d", rp->Name, local->s); return local->s; } sts = setsockopt(local->s, SOL_SOCKET, SO_RCVBUF, &buffsize, sizeof(buffsize)); if (sts < 0) { printf( "Error setting receive buffer size \n"); } /* Initialize remote address structure */ local->rem_addr.sin_family = AF_INET; local->rem_addr.sin_port = htons(port); // local->rem_addr.sin_addr.s_addr = inet_addr((char *) &(op->Address)); sts = net_StringToAddr( op->Address, &local->rem_addr.sin_addr); if ( EVEN(sts)) { errh_Error( "Address error for IO modbus tcp slave %s %s", rp->Name, op->Address); return sts; } local->rem_addr.sin_addr.s_addr = ntohl(local->rem_addr.sin_addr.s_addr); /* Connect to remote address */ fcntl(local->s, F_SETFL, (flags = fcntl(local->s, F_GETFL)) | O_NONBLOCK); sts = connect(local->s, (struct sockaddr *) &local->rem_addr, sizeof(local->rem_addr)); if (sts < 0) { FD_ZERO(&fdr); FD_ZERO(&fdw); FD_SET(local->s, &fdr); FD_SET(local->s, &fdw); if (op->ReconnectLimit > 200) { tv.tv_sec = 0; tv.tv_usec = 1000 * op->ReconnectLimit; } else { tv.tv_sec = 0; tv.tv_usec = 200000; } sts = select((int)local->s+1, &fdr, &fdw, NULL, &tv); if (sts <= 0) { close(local->s); errh_Error( "Error connecting remote socket for IO modbus slave %s, %d", rp->Name, sts); return -1; } } fcntl(local->s, F_SETFL, (flags = fcntl(local->s, F_GETFL)) ^ O_NONBLOCK); return sts; }