1: /* 2: ** ident.c High-level calls to the ident lib 3: ** 4: ** Author: Pdr Emanuelsson <pell@lysator.liu.se> 5: ** Hacked by: Peter Eriksson <pen@lysator.liu.se> 6: */ 7: 8: #ifdef NeXT3 9: # include <libc.h> 10: #endif 11: 12: #include <stdio.h> 13: 14: #ifdef HAVE_ANSIHEADERS 15: # include <stdlib.h> 16: # include <string.h> 17: #endif 18: 19: #include <errno.h> 20: 21: #include <sys/types.h> 22: #include <sys/socket.h> 23: 24: #define IN_LIBIDENT_SRC 25: #include "ident.h" 26: 27: #include <arpa/inet.h> 28: 29: 30: /* Do a complete ident query and return result */ 31: 32: IDENT *ident_lookup __P2(int, fd, 33: int, timeout) 34: { 35: struct sockaddr_in localaddr, remoteaddr; 36: int len; 37: 38: len = sizeof(remoteaddr); 39: if (getpeername(fd, (struct sockaddr*) &remoteaddr, &len) < 0) 40: return 0; 41: 42: len = sizeof(localaddr); 43: if (getsockname(fd, (struct sockaddr *) &localaddr, &len) < 0) 44: return 0; 45: 46: return ident_query( &localaddr.sin_addr, &remoteaddr.sin_addr, 47: ntohs(localaddr.sin_port), ntohs(remoteaddr.sin_port), 48: timeout); 49: } 50: 51: 52: IDENT *ident_query __P5(struct in_addr *, laddr, 53: struct in_addr *, raddr, 54: int, lport, 55: int, rport, 56: int, timeout) 57: { 58: int res; 59: ident_t *id; 60: struct timeval timout; 61: IDENT *ident=0; 62: 63: 64: timout.tv_sec = timeout; 65: timout.tv_usec = 0; 66: 67: if (timeout) 68: id = id_open( laddr, raddr, &timout); 69: else 70: id = id_open( laddr, raddr, (struct timeval *)0); 71: 72: if (!id) 73: { 74: errno = EINVAL; 75: return 0; 76: } 77: 78: if (timeout) 79: res = id_query(id, rport, lport, &timout); 80: else 81: res = id_query(id, rport, lport, (struct timeval *) 0); 82: 83: if (res < 0) 84: { 85: id_close(id); 86: return 0; 87: } 88: 89: ident = (IDENT *) malloc(sizeof(IDENT)); 90: if (!ident) { 91: id_close(id); 92: return 0; 93: } 94: 95: if (timeout) 96: res = id_parse(id, &timout, 97: &ident->lport, 98: &ident->fport, 99: &ident->identifier, 100: &ident->opsys, 101: &ident->charset); 102: else 103: res = id_parse(id, (struct timeval *) 0, 104: &ident->lport, 105: &ident->fport, 106: &ident->identifier, 107: &ident->opsys, 108: &ident->charset); 109: 110: if (res != 1) 111: { 112: free(ident); 113: id_close(id); 114: return 0; 115: } 116: 117: id_close(id); 118: return ident; /* At last! */ 119: } 120: 121: 122: char *ident_id __P2(int, fd, 123: int, timeout) 124: { 125: IDENT *ident; 126: char *id=0; 127: 128: ident = ident_lookup(fd, timeout); 129: if (ident && ident->identifier && *ident->identifier) 130: { 131: id = id_strdup(ident->identifier); 132: if (id == NULL) 133: return NULL; 134: } 135: 136: ident_free(ident); 137: return id; 138: } 139: 140: 141: void ident_free __P1(IDENT *, id) 142: { 143: if (!id) 144: return; 145: if (id->identifier) 146: free(id->identifier); 147: if (id->opsys) 148: free(id->opsys); 149: if (id->charset) 150: free(id->charset); 151: free(id); 152: }