Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

nethelp.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Copyright 2005 Daniel Ferullo                                             *
00003  *                                                                           *
00004  * Licensed under the Apache License, Version 2.0 (the "License");           *
00005  * you may not use this file except in compliance with the License.          *
00006  * You may obtain a copy of the License at                                   *
00007  *                                                                           *
00008  *    http://www.apache.org/licenses/LICENSE-2.0                             *
00009  *                                                                           *
00010  * Unless required by applicable law or agreed to in writing, software       *
00011  * distributed under the License is distributed on an "AS IS" BASIS,         *
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
00013  * See the License for the specific language governing permissions and       *
00014  * limitations under the License.                                            *
00015  *                                                                           *
00016  *****************************************************************************/
00017 
00018 /**
00019  * @file nethelp.c
00020  * @author Daniel Ferullo (ferullo@cmu.edu)
00021  *
00022  * @brief provides helpful basic network functions
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         /* declare local variables */
00033         struct hostent *hp;
00034 
00035         /* error check arguments */
00036         CHECK_NOT_NULL(ip_or_name,ERROR_NULL_ARG_1);
00037         CHECK_NOT_NULL(ip,ERROR_NULL_ARG_2);
00038 
00039         /* do function */
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         /* declare local variables */
00058         int buddy_sd;
00059         struct sockaddr_in server;
00060 
00061         /* error check arguments */
00062         CHECK_NOT_NULL(sd,ERROR_NULL_ARG_2);
00063 
00064         /* do function */
00065 
00066         /* create stream socket using TCP */
00067         if( (buddy_sd=socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
00068                 return ERROR_SOCKET_CREATE;
00069         }
00070 
00071         /* put the clients info in the sockaddr_in struct */
00072         server.sin_family = AF_INET;
00073         server.sin_port = port_to_bind;
00074         server.sin_addr.s_addr = htonl(INADDR_ANY);
00075 
00076         /* bind the port to use for the direct connection*/
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         /* done binding to port for direct connection */
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         /* declare local variables */
00091         struct in_addr addr;
00092         struct sockaddr_in con_to;
00093 
00094         /* error check arguments */
00095         CHECK_NOT_NULL(sd,ERROR_NULL_ARG_3);
00096 
00097         /* do function */
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 

Generated on Wed Mar 30 23:20:47 2005 for NATBLASTER by  doxygen 1.3.9.1