1: #ifndef lint 2: static char sccsid[] = "@(#)client.c 4.2 (Berkeley) 7/7/83"; 3: #endif 4: 5: #include <stdio.h> 6: #include <sys/types.h> 7: #include <sys/socket.h> 8: #include <netinet/in.h> 9: #include <netdb.h> 10: #include "courier.h" 11: 12: #if DEBUG 13: int CourierClientDebuggingFlag = 0; 14: #endif 15: 16: SendCallMessage(f, procedure, nwords, arguments) 17: int f; 18: Cardinal procedure, nwords; 19: Unspecified *arguments; 20: { 21: Cardinal p, n; 22: 23: #if DEBUG 24: if (CourierClientDebuggingFlag) 25: fprintf(stderr, "[SendCallMessage %d %d]\n", procedure, nwords); 26: #endif 27: PackCardinal(&procedure, &p, 1); 28: PackCardinal(&nwords, &n, 1); 29: write(f, &p, sizeof(Cardinal)); 30: write(f, &n, sizeof(Cardinal)); 31: write(f, arguments, nwords*sizeof(Unspecified)); 32: } 33: 34: Unspecified * 35: ReceiveReturnMessage(f) 36: int f; 37: { 38: Cardinal nwords, n; 39: Unspecified *bp; 40: 41: if (ClientRead(f, &nwords, 1) == 0) 42: goto eof; 43: UnpackCardinal(&n, &nwords); 44: #if DEBUG 45: if (CourierClientDebuggingFlag) 46: fprintf(stderr, "[ReceiveReturnMessage %d]\n", n); 47: #endif 48: bp = Allocate(n); 49: if (ClientRead(f, bp, n) == 0) 50: goto eof; 51: return (bp); 52: eof: 53: fprintf(stderr, "\n\r\7Lost connection to server.\n"); 54: exit(1); 55: } 56: 57: static 58: ClientRead(f, addr, nwords) 59: int f; 60: char *addr; 61: int nwords; 62: { 63: register int nbytes, n; 64: register char *p; 65: 66: for (p = addr, nbytes = 2*nwords; nbytes > 0; nbytes -= n, p += n) { 67: n = read(f, p, nbytes); 68: if (n <= 0) 69: return (0); 70: } 71: return (1); 72: } 73: 74: CourierActivate(program_name, host) 75: String program_name, host; 76: { 77: struct hostent *hp; 78: struct servent *srvp; 79: int f; 80: struct sockaddr_in sin; 81: Unspecified buf[50]; 82: Cardinal n; 83: char c; 84: 85: hp = gethostbyname(host); 86: if (hp == 0) { 87: fprintf(stderr, "%s: unknown host\n", host); 88: exit(1); 89: } 90: srvp = getservbyname("courier", "tcp"); 91: if (srvp == 0) { 92: fprintf(stderr, "tcp/courier: unknown service\n"); 93: exit(1); 94: } 95: f = socket(AF_INET, SOCK_STREAM, 0, 0); 96: if (f < 0) { 97: perror("socket"); 98: exit(1); 99: } 100: sin.sin_family = AF_INET; 101: sin.sin_port = 0; 102: sin.sin_addr.s_addr = 0; 103: if (bind(f, (caddr_t)&sin, sizeof (sin), 0) < 0) { 104: perror("bind"); 105: exit(1); 106: } 107: sin.sin_family = hp->h_addrtype; 108: sin.sin_addr = *(struct in_addr *) hp->h_addr; 109: sin.sin_port = srvp->s_port; 110: if (connect(f, (caddr_t)&sin, sizeof(sin), 0) < 0) { 111: perror(hp->h_name); 112: exit(1); 113: } 114: #if DEBUG 115: if (CourierClientDebuggingFlag) 116: fprintf(stderr, "[CourierActivate: connected to %s]\n", hp->h_name); 117: #endif 118: n = PackString(&program_name, buf, 1); 119: write(f, buf, n*sizeof(Unspecified)); 120: if (read(f, &c, 1) != 1) { 121: perror(host); 122: exit(1); 123: } 124: if (c != 0) { 125: do write(fileno(stderr), &c, 1); 126: while (read(f, &c, 1) == 1 && c != 0); 127: exit(1); 128: } 129: #if DEBUG 130: if (CourierClientDebuggingFlag) 131: fprintf(stderr, "[CourierActivate: running %s]\n", program_name); 132: #endif 133: return (f); 134: }