====== Command definition: CHKTCPPRT ======= cmd prompt('Check TCP port') parm kwd(ipaddr) type(*char) len(128) prompt('IP + Address') min(1) parm kwd(port) type(*int4) dft(80) prompt('Port') ===== Command processing program: CHKTCPPRTE ========== // Check TCP port status h dftactgrp(*no) h actgrp(*new) d chgtcpprte pr extpgm('CHKTCPPRTE') d inaddress 128a d inport 4a d returnval n options(*omit : *nopass) d chgtcpprte pi d inaddress 128a d inport 4a d returnval n options(*omit : *nopass) // Information on sockets and RPG IV can be found at Scott Klement's // socket tutorial at http://www.scottklement.com/rpg/socktut/index.html // Prototypes for socket APIs required... // Get service by name. d getservbyname pr * extproc('getservbyname') d service_name * value options(*string) d protocol_name * value options(*string) // example: p_servent = getservbyname( 'http' : 'tcp' ); // returns *NULL if service not found d p_servent s * d servent ds based(p_servent) d s_name * d s_aliases * d s_port 10i 0 d s_proto * // Convert dotted decimal IP address to 32-bit network address. d inet_addr pr 10u 0 extproc('inet_addr') d address_str * value options(*string) // example: ip = inet_addr('192.168.1.1'); // if ip = INADDR_NONE; // Conversion failed. d INADDR_NONE c 4294967295 // Convert 32-bit network address to dotted decimal d inet_ntoa pr * extproc('inet_ntoa') d internet_addr 10u 0 value // example: ptr = inet_ntoa(ip); // if ptr <> *null; // dottedip = %str(ptr); // Get host information by DNS lookup. d gethostbyname pr * extproc('gethostbyname') d host_name * value options(*string) d p_hostent s * d hostent ds based(p_hostent) d h_name * d h_aliases * d h_addrtype 10i 0 d h_length 10i 0 d h_addr_list * d p_h_addr s * based(h_addr_list) d h_addr s 10u 0 based(p_h_addr) d socket pr 10i 0 extproc('socket') d addr_family 10i 0 value d type 10i 0 value d protocol 10i 0 value d AF_INET c 2 d SOCK_STREAM c 1 d IPPROTO_IP c 0 // example: s = socket( AF_INET : SOCK_STREAM : IPPROTO_IP ); d connect pr 10i 0 extproc('connect') d sock_desc 10i 0 value d dest_addr * value d addr_len 10i 0 value d p_sockaddr s * d sockaddr ds based(p_sockaddr) d sa_family 5i 0 d sa_data 14a d sockaddr_in ds based(p_sockaddr) d sin_family 5i 0 d sin_port 5u 0 d sin_addr 10u 0 d sin_zero 8a d close pr 10i 0 extproc('close') d sock_desc 10i 0 value // Local variables. d ip s 10u 0 d msg s 50a d p_connto s * d addrlen s 10i 0 d sock s 10i 0 d usereturnval s n inz(*off) d workport s 5u 0 d ds d binportx 1 4 d binport 1 4b 0 /free usereturnval = *off; if %parms() > 2; usereturnval = *on; endif; if usereturnval; returnval = *off; // Not connected by default. endif; // Convert port from input format to required format by APIs. evalr binportx = inport; if binport <= 0; binport = 80; endif; workport = binport; // Determine if we have an IP address or name. If name, try to resolve // to IP address. ip = inet_addr(%trim(inaddress)); if ip = INADDR_NONE; p_hostent = gethostbyname(%trim(inaddress)); if p_hostent <> *null; ip = h_addr; endif; endif; msg = 'In host: ' + inaddress; dsply msg; msg = 'In port: ' + %char(workport); dsply msg; if ip = INADDR_NONE; msg = 'Host not found.'; else; msg = 'IP address: ' + %str(inet_ntoa(ip)); endif; dsply msg; sock = socket( AF_INET : SOCK_STREAM : IPPROTO_IP ); // Get some memory to store the socket address information. addrlen = %size(sockaddr); p_connto = %alloc( addrlen ); // Point the socket address structure at the newly allocated memory. p_sockaddr = p_connto; // Populate socket connect structure. sin_family = AF_INET; sin_addr = ip; sin_port = workport; sin_zero = *allx'00'; if connect( sock : p_connto : addrlen ) < 0; msg = 'Connect failed.'; else; if usereturnval; returnval = *on; endif; msg = 'Connected.'; endif; dsply msg; callp close( sock ); // Need callp to distinguish from RPG close file. dealloc(n) p_connto; msg = 'Connection closed.'; dsply msg; *inlr = *on; return; /end-free