Jabber WindowGram Client (JWGC)

Introduction Screenshots Installation Downloads
Documentation Browse Source Resources Project Site

Stable Version
-none-

Latest Version
beta5



Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

JNetSock.c

Go to the documentation of this file.
00001 /*
00002  * --------------------------------------------------------------------------
00003  * 
00004  * License
00005  * 
00006  * The contents of this file are subject to the Jabber Open Source License
00007  * Version 1.0 (the "JOSL").  You may not copy or use this file, in either
00008  * source code or executable form, except in compliance with the JOSL. You
00009  * may obtain a copy of the JOSL at http://www.jabber.org/ or at
00010  * http://www.opensource.org/.
00011  * 
00012  * Software distributed under the JOSL is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the JOSL for
00014  * the specific language governing rights and limitations under the JOSL.
00015  * 
00016  * Copyrights
00017  * 
00018  * Portions created by or assigned to Jabber.com, Inc. are Copyright (c)
00019  * 1999-2002 Jabber.com, Inc.  All Rights Reserved.  Contact information for
00020  * Jabber.com, Inc. is available at http://www.jabber.com/.
00021  * 
00022  * Portions Copyright (c) 1998-1999 Jeremie Miller.
00023  * 
00024  * Acknowledgements
00025  * 
00026  * Special thanks to the Jabber Open Source Contributors for their suggestions
00027  * and support of Jabber.
00028  * 
00029  * Alternatively, the contents of this file may be used under the terms of the
00030  * GNU General Public License Version 2 or later (the "GPL"), in which case
00031  * the provisions of the GPL are applicable instead of those above.  If you
00032  * wish to allow use of your version of this file only under the terms of the
00033  * GPL and not to allow others to use your version of this file under the
00034  * JOSL, indicate your decision by deleting the provisions above and replace
00035  * them with the notice and other provisions required by the GPL.  If you do
00036  * not delete the provisions above, a recipient may use your version of this
00037  * file under either the JOSL or the GPL.
00038  * 
00039  * --------------------------------------------------------------------------
00040  */
00041 
00042 /* $Id: JNetSock.c,v 1.2 2003/09/27 02:47:38 jadestorm Exp $ */
00043 
00044 #include "libjwgc.h"
00045 
00046 /*
00047  * socket.c
00048  * 
00049  * Simple wrapper to make socket creation easy. type = NETSOCKET_SERVER is local
00050  * listening socket type = NETSOCKET_CLIENT is connection socket type =
00051  * NETSOCKET_UDP is a UDP connection socket
00052  */
00053 
00054 int __Xode_port = -1;
00055 
00056 int 
00057 get_netport()
00058 {
00059         return __Xode_port;
00060 }
00061 
00062 int 
00063 make_netsocket(u_short port, char *host, int type)
00064 {
00065         int s, flag = 1;
00066         struct sockaddr_in sa;
00067         struct in_addr *saddr;
00068         int socket_type, len;
00069 
00070         /* is this a UDP socket or a TCP socket? */
00071         socket_type = (type == NETSOCKET_UDP) ? SOCK_DGRAM : SOCK_STREAM;
00072 
00073         bzero((void *) &sa, sizeof(struct sockaddr_in));
00074 
00075         if ((s = socket(AF_INET, socket_type, 0)) < 0)
00076                 return (-1);
00077         if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &flag, sizeof(flag)) < 0)
00078                 return (-1);
00079 
00080         saddr = make_addr(host);
00081         if (saddr == NULL && type != NETSOCKET_UDP)
00082                 return (-1);
00083         sa.sin_family = AF_INET;
00084         sa.sin_port = htons(port);
00085 
00086         if (type == NETSOCKET_SERVER) {
00087                 /* bind to specific address if specified */
00088                 if (host != NULL)
00089                         sa.sin_addr.s_addr = saddr->s_addr;
00090 
00091                 if (bind(s, (struct sockaddr *) & sa, sizeof sa) < 0) {
00092                         close(s);
00093                         return (-1);
00094                 }
00095 
00096                 if (!sa.sin_port) {
00097                         len = sizeof(sa);
00098                         if (getsockname(s, (struct sockaddr *) & sa, &len) < 0) {
00099                                 close(s);
00100                                 return (-1);
00101                         }
00102                 }
00103 
00104                 if (listen(s, 10) < 0) {
00105                         close(s);
00106                         return (-1);
00107                 }
00108 
00109                 __Xode_port = ntohs(sa.sin_port);
00110         }
00111         if (type == NETSOCKET_CLIENT) {
00112                 sa.sin_addr.s_addr = saddr->s_addr;
00113                 if (connect(s, (struct sockaddr *) & sa, sizeof sa) < 0) {
00114                         close(s);
00115                         return (-1);
00116                 }
00117         }
00118         if (type == NETSOCKET_UDP) {
00119                 /* bind to all addresses for now */
00120                 if (bind(s, (struct sockaddr *) & sa, sizeof sa) < 0) {
00121                         close(s);
00122                         return (-1);
00123                 }
00124 
00125                 /* if specified, use a default recipient for read/write */
00126                 if (host != NULL && saddr != NULL) {
00127                         sa.sin_addr.s_addr = saddr->s_addr;
00128                         if (connect(s, (struct sockaddr *) & sa, sizeof sa) < 0) {
00129                                 close(s);
00130                                 return (-1);
00131                         }
00132                 }
00133         }
00134 
00135 
00136         return (s);
00137 }
00138 
00139 
00140 struct in_addr *
00141 make_addr(char *host)
00142 {
00143         struct hostent *hp;
00144         static struct in_addr addr;
00145         char myname[MAXHOSTNAMELEN + 1];
00146 
00147         if (host == NULL || strlen(host) == 0) {
00148                 gethostname(myname, MAXHOSTNAMELEN);
00149                 hp = gethostbyname(myname);
00150                 if (hp != NULL) {
00151                         return (struct in_addr *) * hp->h_addr_list;
00152                 }
00153         }
00154         else {
00155                 addr.s_addr = inet_addr(host);
00156                 if (addr.s_addr != -1) {
00157                         return &addr;
00158                 }
00159                 hp = gethostbyname(host);
00160                 if (hp != NULL) {
00161                         return (struct in_addr *) * hp->h_addr_list;
00162                 }
00163         }
00164         return NULL;
00165 }
00166 
00167 /*
00168  * Sets a file descriptor to close on exec.  "flag" is 1 to close on exec, 0
00169  * to leave open across exec. -- EJB 7/31/2000
00170  */
00171 int 
00172 set_fd_close_on_exec(int fd, int flag)
00173 {
00174         int oldflags = fcntl(fd, F_GETFL);
00175         int newflags;
00176 
00177         if (flag)
00178                 newflags = oldflags | FD_CLOEXEC;
00179         else
00180                 newflags = oldflags & (~FD_CLOEXEC);
00181 
00182         if (newflags == oldflags)
00183                 return 0;
00184         return fcntl(fd, F_SETFL, (long) newflags);
00185 }


Last updated at Tue Dec 18 21:07:42 PST 2007. This site and project hosted by...SourceForge.net Logo
Source Perspective by Fisheye