merge 64-bit fixes from Fernando Lucas Rodriguez <fernando_lr@terra.es>
[debian/gcpegg] / testudp.c
1 /* PROGRAM:     testudp
2  * FILE:        $Header: /home/egg/src/RCS/testudp.c,v 1.2 1998/07/21 11:34:48 ghn Exp $
3  * PURPOSE:     Testing UDP communications (fake basket)
4  * AUTHOR:      Greg Nelson
5  * DATE:        98-05-09
6  *
7  * REVISED:     $Log: testudp.c,v $
8  * REVISED:     Revision 1.2  1998/07/21 11:34:48  ghn
9  * REVISED:     Testing UDP communications (fake basket)
10  * REVISED:
11  * Copyright 1998 - Greg Nelson
12  * Redistributable under the terms of the GNU Public Licence (GPL)
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <netdb.h>
18 #include <netinet/in.h>
19 #include "global.h"
20 #include "genlib.h"
21 #include "hwapi.h"
22 #include "collect.h"
23 #include "storage.h"
24 #include "network.h"
25
26 int main(int argc, char *argv[]) {
27   ReqHeader             rhdr;
28   struct protoent       *pp;
29   struct hostent        *hp;
30   struct sockaddr_in    rhost;
31   int32                 i;
32   int32                 out_sock;
33
34   pgmname = argv[0];
35
36   if (argc != 2) {
37     printf("Usage: %s <host>\n", pgmname);
38     exit(-1);
39   }
40
41   if ((pp = getprotobyname("udp")) == NULL) {
42     perror("getprotobyname");
43     exit(-1);
44   }
45
46   if ((hp = gethostbyname(argv[1])) == NULL) {
47     fprintf(stderr, "gethostbyname(%s): %s", argv[1], strerror(errno));
48     exit(-1);
49   }
50
51   if (hp->h_addrtype != AF_INET) {
52     fprintf(stderr, "Host is not on the internet!\n");
53     exit(-1);
54   }
55
56   rhost.sin_port = htons(EGGPORT);
57   rhost.sin_family = AF_INET;
58   memcpy(&(rhost.sin_addr), hp->h_addr, hp->h_length);
59
60   rhdr.type = REQ_PACKET;
61   rhdr.pktsize = sizeof(ReqHeader);
62   rhdr.eggid = 30800;   /* halebopp! */
63   rhdr.samp_rec = 10;
64   rhdr.sec_rec = 10;
65   rhdr.rec_pkt = 6;
66   rhdr.trialsz = 200;
67   rhdr.starttm = 0;
68   rhdr.cksum = BlockCRC16((byte *)&rhdr, sizeof(ReqHeader)-2);
69
70   if ((out_sock = socket(AF_INET, SOCK_DGRAM, pp->p_proto)) < 0) {
71     perror("socket");
72     exit(-1);
73   }
74         
75   i = sendto(out_sock, &rhdr, rhdr.pktsize, 
76              0, (struct sockaddr *)&rhost,
77              sizeof(struct sockaddr));
78   if (i == -1) {
79     perror("sendto");
80     exit(-1);
81   }
82   if (i != rhdr.pktsize) {
83     fprintf(stderr, "Failed to send complete packet (%ld/%d)\n",
84             i, rhdr.pktsize);
85     exit(-1);
86   }
87   
88   exit(0);
89 }