Thu, 21 Dec 2000 14:54:13 +0000
[gaim-migrate @ 1357]
jabber for those not fortunate enough to have libjabber and libxode on their systems
| 1347 | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify | |
| 3 | * it under the terms of the GNU General Public License as published by | |
| 4 | * the Free Software Foundation; either version 2 of the License, or | |
| 5 | * (at your option) any later version. | |
| 6 | * | |
| 7 | * This program is distributed in the hope that it will be useful, | |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 10 | * GNU General Public License for more details. | |
| 11 | * | |
| 12 | * You should have received a copy of the GNU General Public License | |
| 13 | * along with this program; if not, write to the Free Software | |
| 14 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 15 | * | |
| 16 | * Jabber | |
| 17 | * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/ | |
| 18 | */ | |
| 19 | ||
| 20 | #include "libxode.h" | |
| 21 | ||
| 22 | /* socket.c | |
| 23 | * | |
| 24 | * Simple wrapper to make socket creation easy. | |
| 25 | * type = NETSOCKET_SERVER is local listening socket | |
| 26 | * type = NETSOCKET_CLIENT is connection socket | |
| 27 | * type = NETSOCKET_UDP | |
| 28 | */ | |
| 29 | ||
| 30 | int make_netsocket(u_short port, char *host, int type) | |
| 31 | { | |
| 32 | int s, flag = 1; | |
| 33 | struct sockaddr_in sa; | |
| 34 | struct in_addr *saddr; | |
| 35 | int socket_type; | |
| 36 | ||
| 37 | /* is this a UDP socket or a TCP socket? */ | |
| 38 | socket_type = (type == NETSOCKET_UDP)?SOCK_DGRAM:SOCK_STREAM; | |
| 39 | ||
| 40 | bzero((void *)&sa,sizeof(struct sockaddr_in)); | |
| 41 | ||
| 42 | if((s = socket(AF_INET,socket_type,0)) < 0) | |
| 43 | return(-1); | |
| 44 | if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) < 0) | |
| 45 | return(-1); | |
| 46 | ||
| 47 | saddr = make_addr(host); | |
| 48 | if(saddr == NULL) | |
| 49 | return(-1); | |
| 50 | sa.sin_family = AF_INET; | |
| 51 | sa.sin_port = htons(port); | |
| 52 | ||
| 53 | if(type == NETSOCKET_SERVER) | |
| 54 | { | |
| 55 | /* bind to specific address if specified */ | |
| 56 | if(host != NULL) | |
| 57 | sa.sin_addr.s_addr = saddr->s_addr; | |
| 58 | ||
| 59 | if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0) | |
| 60 | { | |
| 61 | close(s); | |
| 62 | return(-1); | |
| 63 | } | |
| 64 | } | |
| 65 | if(type == NETSOCKET_CLIENT) | |
| 66 | { | |
| 67 | sa.sin_addr.s_addr = saddr->s_addr; | |
| 68 | if(connect(s,(struct sockaddr*)&sa,sizeof sa) < 0) | |
| 69 | { | |
| 70 | close(s); | |
| 71 | return(-1); | |
| 72 | } | |
| 73 | } | |
| 74 | if(type == NETSOCKET_UDP) | |
| 75 | { | |
| 76 | /* bind to all addresses for now */ | |
| 77 | if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0) | |
| 78 | { | |
| 79 | close(s); | |
| 80 | return(-1); | |
| 81 | } | |
| 82 | ||
| 83 | /* specify default recipient for read/write */ | |
| 84 | sa.sin_addr.s_addr = saddr->s_addr; | |
| 85 | if(connect(s,(struct sockaddr*)&sa,sizeof sa) < 0) | |
| 86 | { | |
| 87 | close(s); | |
| 88 | return(-1); | |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | ||
| 93 | return(s); | |
| 94 | } | |
| 95 | ||
| 96 | ||
| 97 | struct in_addr *make_addr(char *host) | |
| 98 | { | |
| 99 | struct hostent *hp; | |
| 100 | static struct in_addr addr; | |
| 101 | char myname[MAXHOSTNAMELEN + 1]; | |
| 102 | ||
| 103 | if(host == NULL || strlen(host) == 0) | |
| 104 | { | |
| 105 | gethostname(myname,MAXHOSTNAMELEN); | |
| 106 | hp = gethostbyname(myname); | |
| 107 | if(hp != NULL) | |
| 108 | { | |
| 109 | return (struct in_addr *) *hp->h_addr_list; | |
| 110 | } | |
| 111 | }else{ | |
| 112 | addr.s_addr = inet_addr(host); | |
| 113 | if(addr.s_addr != -1) | |
| 114 | { | |
| 115 | return &addr; | |
| 116 | } | |
| 117 | hp = gethostbyname(host); | |
| 118 | if(hp != NULL) | |
| 119 | { | |
| 120 | return (struct in_addr *) *hp->h_addr_list; | |
| 121 | } | |
| 122 | } | |
| 123 | return NULL; | |
| 124 | } | |
| 125 | ||
| 126 | /* Sets a file descriptor to close on exec. "flag" is 1 to close on exec, 0 to | |
| 127 | * leave open across exec. | |
| 128 | * -- EJB 7/31/2000 | |
| 129 | */ | |
| 130 | int set_fd_close_on_exec(int fd, int flag) | |
| 131 | { | |
| 132 | int oldflags = fcntl(fd,F_GETFL); | |
| 133 | int newflags; | |
| 134 | ||
| 135 | if(flag) | |
| 136 | newflags = oldflags | FD_CLOEXEC; | |
| 137 | else | |
| 138 | newflags = oldflags & (~FD_CLOEXEC); | |
| 139 | ||
| 140 | if(newflags==oldflags) | |
| 141 | return 0; | |
| 142 | return fcntl(fd,F_SETFL,(long)newflags); | |
| 143 | } | |
| 144 |