00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <pcap.h>
00025 #include <string.h>
00026 #include "nethelp.h"
00027 #include "berkeleyapi.h"
00028 #include "debug.h"
00029
00030 errorcode resolveIP(char *ip_or_name, ip_t *ip) {
00031
00032
00033 struct hostent *hp;
00034
00035
00036 CHECK_NOT_NULL(ip_or_name,ERROR_NULL_ARG_1);
00037 CHECK_NOT_NULL(ip,ERROR_NULL_ARG_2);
00038
00039
00040 hp = gethostbyname(ip_or_name);
00041
00042 if (hp==NULL)
00043 return ERROR_HOST_NAME_LOOKUP;
00044
00045 if (hp->h_addr==NULL)
00046 return ERROR_1;
00047
00048 memcpy(ip,hp->h_addr,sizeof(ip));
00049 if (*ip==-1)
00050 return ERROR_2;
00051
00052 return SUCCESS;
00053 }
00054
00055 errorcode bindSocket(port_t port_to_bind, sock_t *sd) {
00056
00057
00058 int buddy_sd;
00059 struct sockaddr_in server;
00060
00061
00062 CHECK_NOT_NULL(sd,ERROR_NULL_ARG_2);
00063
00064
00065
00066
00067 if( (buddy_sd=socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
00068 return ERROR_SOCKET_CREATE;
00069 }
00070
00071
00072 server.sin_family = AF_INET;
00073 server.sin_port = port_to_bind;
00074 server.sin_addr.s_addr = htonl(INADDR_ANY);
00075
00076
00077 if (bind(buddy_sd, (struct sockaddr*)&server, sizeof(server))<0) {
00078 DEBUG(DBG_NETWORK, "NETWORK:couldn't bind port %u\n",
00079 DBG_PORT(port_to_bind));
00080 return ERROR_BIND;
00081 }
00082
00083 *sd = buddy_sd;
00084
00085 return SUCCESS;
00086 }
00087
00088 errorcode tcp_connect(ip_t ip, port_t port, sock_t *sd) {
00089
00090
00091 struct in_addr addr;
00092 struct sockaddr_in con_to;
00093
00094
00095 CHECK_NOT_NULL(sd,ERROR_NULL_ARG_3);
00096
00097
00098
00099 if (*sd <= 0) {
00100 DEBUG(DBG_ALL, "ALL:WARNING: tcp_connect: calling socket (bad b/c sd should already exist)\n");
00101 if ( (*sd=socket(AF_INET,SOCK_STREAM,0)) < 0)
00102 return ERROR_TCP_SOCKET;
00103 }
00104
00105 addr.s_addr = ip;
00106
00107 con_to.sin_family = AF_INET;
00108 con_to.sin_port = port;
00109 con_to.sin_addr = addr;
00110 memset(con_to.sin_zero,0,8);
00111
00112 if (connect(*sd, (struct sockaddr*)&con_to, sizeof(con_to))<0)
00113 return ERROR_TCP_CONNECT;
00114
00115 return SUCCESS;
00116 }
00117