Example #1
0
int main ( void )
/************************************************************************
 * GRPHGD								*
 *									*
 * This program runs the graph-to-grid algorithm.			*
 *									*
 **									*
 * Log:									*
 * D.W.Plummer/NCEP	 2/05	Re-write in C based on grphgd.f		*
 * R. Tian/SAIC		 3/05	Added dg_intl				*
 * M. Li/SAIC		 4/05	Modified ggdriv				*
 * D.W.Plummer/NCEP	 5/05	Move dg_intl into gg_driv driver	*
 * m.gamazaychikov/SAIC	12/05	Add ces_gtrtbl				*
 * T. Piper/SAIC        01/08   Added GD_INIT; removed from IN_BDTA     *
 ***********************************************************************/
{
int	respnd, iperr, done, one=1, ier, iret, rspflg=G_TRUE;
int	kx=0, ky=0, npoints;
float	*grid, *grid1, *hist, *work1, *work2, *work3, *buffer;
/*---------------------------------------------------------------------*/
/*
 *  Initialize TAE.
 */
    ip_init ( &respnd, &iperr );
    if ( iperr == G_NORMAL )  {
	ip_idnt ( "GRPHGD", &ier, strlen("GRPHGD") );

/*
 *  Initialize grid library common area grdcmn.cmn
 */
        gd_init  ( &ier );
	done = G_FALSE;

    }
    else  {

	iperr = -1;
	done = G_TRUE;

    }

/*
 *  Initialize GEMPLT in order to set grid navigation later.
 */
    if ( done == G_FALSE )  {

	gg_init ( &one, &iret );

	if ( iret != G_NORMAL )  {
	    iperr = -3;
	    done = G_TRUE;
	}
    }

/*
 *  Initialize device.
 */
    if ( done == G_FALSE )  {

	gg_sdev ( "GN", &ier, strlen("GN") );

	if ( ier != G_NORMAL )  {
	    done = G_TRUE;
	}
    }

/*
 *  Initialize the _grpTbl structure.
 */
    if ( done == G_FALSE )  {
                                                                                                        
        ces_gtrtbl( &ier );
                                                                                                        
        if ( ier != G_NORMAL )  {
            done = G_TRUE;
        }
    }
    
/*
 * Main loop.
 */
    while ( done == G_FALSE ) {

	npoints = LLMXTG;
	G_MALLOC ( grid,	float, npoints, "Error allocating grid" );
	G_MALLOC ( grid1,	float, npoints, "Error allocating grid1" );
	G_MALLOC ( hist,	float, npoints, "Error allocating hist" );
	G_MALLOC ( work1,	float, npoints, "Error allocating work1" );
	G_MALLOC ( work2,	float, npoints, "Error allocating work2" );
	G_MALLOC ( work3,	float, npoints, "Error allocating work3" );
	G_MALLOC ( buffer,	float, npoints, "Error allocating buffer" );

	ggdriv ( grid, grid1, &kx, &ky, hist, work1, work2, work3, 
		 buffer, &rspflg, &ier );

	G_FREE ( hist, float );
	G_FREE ( grid, float );
	G_FREE ( grid1, float );
	G_FREE ( work1, float );
	G_FREE ( work2, float );
	G_FREE ( work3, float );
	G_FREE ( buffer, float );

/*
 * Call dynamic tutor.
 */
	ip_dynm ( &done, &ier );

    }

/*
 * Final error messages.
 */
    if ( iperr != G_NORMAL )  {
	er_wmsg ( "GRPHGD", &iperr, " ", &ier, strlen("GRPHGD"), strlen(" ") );
    }

    ip_exit ( &iret );

    return 0;

}
Example #2
0
File: ip.c Project: dbruenig/inadyn
/* Sets up the object. */
int ip_init(ip_sock_t *ip)
{
	int rc = 0;
	struct ifreq ifr;
	struct sockaddr_in *addrp = NULL;

	ASSERT(ip);

	if (ip->initialized == 1)
		return 0;

	do {
		TRY(os_ip_support_startup());

		/* local bind, to interface */
		if (ip->ifname) {
			int sd = socket(PF_INET, SOCK_DGRAM, 0);

			if (sd < 0) {
				int code = os_get_socket_error();

				logit(LOG_WARNING, "Failed opening network socket: %s", strerror(code));
				rc = RC_IP_OS_SOCKET_INIT_FAILED;
				break;
			}

			memset(&ifr, 0, sizeof(struct ifreq));
			strlcpy(ifr.ifr_name, ip->ifname, IFNAMSIZ);
			if (ioctl(sd, SIOCGIFADDR, &ifr) != -1) {
				ip->local_addr.sin_family = AF_INET;
				ip->local_addr.sin_port = htons(0);
				addrp = (struct sockaddr_in *)&(ifr.ifr_addr);
				ip->local_addr.sin_addr.s_addr = addrp->sin_addr.s_addr;
				ip->bound = 1;

				logit(LOG_INFO, "Bound to interface %s (IP# %s)",
				      ip->ifname, inet_ntoa(ip->local_addr.sin_addr));
			} else {
				int code = os_get_socket_error();

				logit(LOG_ERR, "Failed reading IP address of interface %s: %s",
				      ip->ifname, strerror(code));
				ip->bound = 0;
			}
			close(sd);
		}

		/* remote address */
		if (ip->p_remote_host_name) {
			int s;
			char port[10];
			struct addrinfo hints, *result;

			/* Clear DNS cache before calling getaddrinfo(). */
			res_init();

			/* Obtain address(es) matching host/port */
			memset(&hints, 0, sizeof(struct addrinfo));
			hints.ai_family = AF_INET;	/* Use AF_UNSPEC to allow IPv4 or IPv6 */
			hints.ai_socktype = SOCK_DGRAM;	/* Datagram socket */
			snprintf(port, sizeof(port), "%d", ip->port);

			s = getaddrinfo(ip->p_remote_host_name, port, &hints, &result);
			if (s != 0 || !result) {
				logit(LOG_WARNING, "Failed resolving hostname %s: %s",
				      ip->p_remote_host_name, gai_strerror(s));
				rc = RC_IP_INVALID_REMOTE_ADDR;
				break;
			}

			/* XXX: Here we should iterate over all of the records returned by
			 * getaddrinfo(), but with this code here in ip.c and connect() being
			 * in tcp.c that's hardly feasible.  Needs refactoring!  --Troglobit */
			ip->remote_addr = *result->ai_addr;
			ip->remote_len = result->ai_addrlen;

			freeaddrinfo(result);	/* No longer needed */
		}
	}
	while (0);

	if (rc) {
		ip_exit(ip);
		return rc;
	}

	ip->initialized = 1;

	return 0;
}
Example #3
0
int main ( void )
/************************************************************************
 * GDGRIB2                                                              *
 *                                                                      *
 * This program encodes a selected GEMPAK grid into a GRIB2 message.    *
 *                                                                      *
 *  Command line:                                                       *
 *  gdgrib2                                                             *
 **                                                                     *
 * Log:                                                                 *
 * S. Gilbert/NCEP           5/2005   Orig                              *
 * S. Gilbert/NCEP           3/2006   Replaced dg_clal with dg_nend     *
 * T. Piper/SAIC        01/08   Added GD_INIT; removed from IN_BDTA     *
 ***********************************************************************/
{

    int    respond, ret, ier, mode=1, done, skip, j;
    float  minval, maxval;

    int    g2len;                     /* Length of GRIB2 message  */
    unsigned char  *g2msg;            /* GRIB2 message            */
    char  cur_gbfile[LLMXLN]="";
    char  chdr[22];          /* WMO Header               */
    FILE  *gbfptr=0;

    GDG2_input    input;                  /* user input variables */
    GDG2_gemgrid  gemgrid;                /* GEMPAK grid and info */
/*---------------------------------------------------------------------*/
/*
 *  Initialize TAE. 
 */
    ip_init ( &respond, &ret );
    if ( ret == 0 ) {
	ip_idnt ( "GDGRIB2", &done, 7 );

/*
 *  Initialize GEMPLT.
 */
	gg_init ( &mode, &ret );
	if ( ret == 0 ) {

/*
 *  Initialize grid library common area grdcmn.cmn
 */
	    gd_init ( &ier );

/*
 *  Initialize the DG library
 */
	    dg_intl ( &ret );
	    done = 0;
	    skip = 1;
	}
	else {
	    done = 1;
	}
    }
    else {
	done = 1;
    }

/*
 *  Process next request, if user has one.
 */
    while ( done == 0 ) {

/*
 *  Wait for user input, if not first time through this loop
 */
        if ( skip == 0 ) ip_dynm( &done, &ret );
        skip = 0;
        if ( done != 0 ) break;          /*  Exit out of interactive loop  */

/*
 *  Get user input info
 */
        gdg2in( &input, &ret );
        if ( ret != 0 ) {
            er_wmsg("GDGRIB2",&ret," ",&ier,7,1);
            continue;
        }
        if ( strlen(input.g2file) == (size_t)0 ) {
            ret=-28;
            er_wmsg("GDGRIB2",&ret," ",&ier,7,1);
            continue;
        }

/*
 *  Get requested grid
 */
        gdgetgrid( &input, &gemgrid, &ret );
        if ( ret != 0 ) {
            er_wmsg("GDGRIB2",&ret," ",&ier,7,1);
            continue;
        }

        maxval=gemgrid.grid[0];
        minval=gemgrid.grid[0];
        for ( j=1; j < gemgrid.kx*gemgrid.ky; j++ ) {
            if ( gemgrid.grid[j] < minval ) minval=gemgrid.grid[j];
            if ( gemgrid.grid[j] > maxval ) maxval=gemgrid.grid[j];
        }

/*
 *  Make GRIB2 field
 */
        gdmakeg2( &input, &gemgrid, &g2msg, &g2len, &ret );
        if ( ret != 0 ) {
            er_wmsg("GDGRIB2",&ret," ",&ier,7,1);
            continue;
        }

/*
 *  Open GRIB2 file, if not already open.
 */
        if ( strncmp(input.g2file, cur_gbfile, LLMXLN) != 0 ) {

/*
 *  If output GRIB file is different, must close previous one first
 */
            if ( strlen(cur_gbfile) != (size_t)0 ) {
                cfl_clos( gbfptr, &ret);
                gbfptr=0;
            }

/*
 *  Open GRIB file
 */
            gbfptr = cfl_aopn( input.g2file, &ret);
            if ( ret == 0 ) {
                strncpy( cur_gbfile, input.g2file, LLMXLN);
            }
            else {
                er_wmsg("CFL",&ret,input.g2file,&ier,7,strlen(input.g2file));
                ret = -24;
                er_wmsg("GDGRIB2",&ret," ",&ier,7,1);
                gbfptr = 0;
                continue;
            }
        }

/*
 *  Write out WMO Header, if requested
 */
        gdmakewmo( &input, &gemgrid, chdr, &ret );
        if ( strlen(chdr) == (size_t)21 ) {
           cfl_writ( gbfptr, strlen(chdr), (unsigned char*)chdr, &ret );
        }

/*
 *  Write out GRIB2 message.
 */
        cfl_writ( gbfptr, g2len, g2msg, &ret );
        if ( ret != 0 ) {
            er_wmsg("CFL",&ret,input.g2file,&ier,7,strlen(input.g2file));
            ret = -25;
            er_wmsg("GDGRIB2",&ret," ",&ier,7,1);
            continue;
        }

/*
 *  Free no longer needed allocated space
 */
        if ( gemgrid.grid != 0 ) free(gemgrid.grid);
        if ( g2msg != 0 ) free(g2msg);

    }

/*
 *  Clean up files
 */
    dg_nend( &ret );
    if ( gbfptr != 0 ) cfl_clos( gbfptr, &ret);

/*
 *  Exit the GEMPAK user interface
 */
    ip_exit( &ret );
 
    return(0);
}
Example #4
0
static void ip_cmdline(int argc, char **argv)
{
    int opt, i;
    u_int32_t addr_tmp[6];
    char *ip_options;
    extern char *optarg;
    extern int optind;

#if defined(ENABLE_PCAPOUTPUT)
  #if defined(WIN32)
    ip_options = "d:D:F:H:I:M:O:p:P:S:t:T:vWZ?";
  #else
    ip_options = "d:D:F:H:I:M:O:p:P:S:t:T:vW?";
  #endif
#else
  #if defined(WIN32)
    ip_options = "d:D:F:H:I:M:O:p:P:S:t:T:vZ?";
  #else
    ip_options = "d:D:F:H:I:M:O:p:P:S:t:T:v?";
  #endif
#endif

    while ((opt = getopt(argc, argv, ip_options)) != -1)
    {
        switch (opt)
        {
            case 'd':    /* Ethernet device */
#if defined(WIN32)
                if (nemesis_getdev(atoi(optarg), &device) < 0)
                {
                    fprintf(stderr, "ERROR: Unable to lookup device: '%d'.\n", 
                            atoi(optarg));
                    ip_exit(1);
                }
#else
                if (strlen(optarg) < 256)
                {
                    device = strdup(optarg);
                    got_link = 1;
                }
                else
                {
                    fprintf(stderr, "ERROR: device %s > 256 characters\n",
                            optarg);
                    ip_exit(1);
                }
#endif
                break;
            case 'D':    /* destination IP address */
                if ((nemesis_name_resolve(optarg, 
                        (u_int32_t *)&iphdr.ip_dst.s_addr )) < 0)
                {
                    fprintf(stderr, "ERROR: Invalid destination IP address: "
                            "\"%s\".\n", optarg);
                    ip_exit(1);
                }
                break;
            case 'F':    /* IP fragmentation options */
                if (parsefragoptions(&iphdr, optarg) < 0)
                    ip_exit(1);
                break;
            case 'H':    /* Ethernet source address */
                memset(addr_tmp, 0, sizeof(addr_tmp));
                sscanf(optarg, "%02X:%02X:%02X:%02X:%02X:%02X", &addr_tmp[0],
                        &addr_tmp[1], &addr_tmp[2], &addr_tmp[3], &addr_tmp[4],
                        &addr_tmp[5]);
                for (i = 0; i < 6; i++)
                    etherhdr.ether_shost[i] = (u_int8_t)addr_tmp[i];
                break;
            case 'I':   /* IP ID */
                iphdr.ip_id = xgetint16(optarg);
                break;
            case 'M':    /* Ethernet destination address */
                memset(addr_tmp, 0, sizeof(addr_tmp));
                sscanf(optarg, "%02X:%02X:%02X:%02X:%02X:%02X", &addr_tmp[0],
                        &addr_tmp[1], &addr_tmp[2], &addr_tmp[3], &addr_tmp[4],
                        &addr_tmp[5]);
                for (i = 0; i < 6; i++)
                    etherhdr.ether_dhost[i] = (u_int8_t)addr_tmp[i];
                break;
            case 'O':   /* IP options file */
                if (strlen(optarg) < 256)
                {
                    ipoptionsfile = strdup(optarg);
                    got_ipoptions = 1;
                }
                else
                {
                    fprintf(stderr, "ERROR: IP options file %s > 256 "
                            "characters.\n", optarg);
                    ip_exit(1);
                }
                break;
            case 'p':   /* IP protocol */
                iphdr.ip_p = xgetint8(optarg);
                break;
            case 'P':   /* payload file */
                if (strlen(optarg) < 256)
                {
                    payloadfile = strdup(optarg);
                    got_payload = 1;
                }
                else
                {
                    fprintf(stderr, "ERROR: payload file %s > 256 characters\n",
                            optarg);
                    ip_exit(1);
                }
                break;
            case 'S':    /* source IP address */
                if ((nemesis_name_resolve(optarg, 
                        (u_int32_t *)&iphdr.ip_src.s_addr )) < 0)
                {
                    fprintf(stderr, "ERROR: Invalid source IP address: \"%s\"."
                            "\n", optarg);
                    ip_exit(1);
                }
                break;
            case 't':   /* IP type of service */
                iphdr.ip_tos = xgetint8(optarg);
                break;
            case 'T':   /* IP time to live */
                iphdr.ip_ttl = xgetint8(optarg);
                break;
            case 'v':
                verbose++;
                if (verbose == 1)
                    nemesis_printtitle((const char *)title);
                break;
#if defined(WIN32)
            case 'Z':
                if ((ifacetmp = pcap_lookupdev(errbuf)) == NULL)
                    perror(errbuf);

                PrintDeviceList(ifacetmp);
                ip_exit(1);
#endif
            case '?':    /* FALLTHROUGH */
            default:
                ip_usage(argv[0]);
                break;
        }    
    }
    argc -= optind;
    argv += optind;
    return;
}