Mercurial > pidgin
annotate src/protocols/jabber/socket.c @ 4569:7f2de19d052d
[gaim-migrate @ 4850]
Bug fix. Gaim would crash if using the IM convo window slider bars after plugin removal.
committer: Tailor Script <tailor@pidgin.im>
| author | Herman Bloggs <hermanator12002@yahoo.com> |
|---|---|
| date | Tue, 11 Feb 2003 00:58:28 +0000 |
| parents | 988485669631 |
| children | 5239a3b4ab33 |
| rev | line source |
|---|---|
| 3127 | 1 /* -------------------------------------------------------------------------- |
| 2 * | |
| 3 * License | |
| 4 * | |
| 5 * The contents of this file are subject to the Jabber Open Source License | |
| 6 * Version 1.0 (the "JOSL"). You may not copy or use this file, in either | |
| 7 * source code or executable form, except in compliance with the JOSL. You | |
| 8 * may obtain a copy of the JOSL at http://www.jabber.org/ or at | |
| 9 * http://www.opensource.org/. | |
| 10 * | |
| 11 * Software distributed under the JOSL is distributed on an "AS IS" basis, | |
| 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the JOSL | |
| 13 * for the specific language governing rights and limitations under the | |
| 14 * JOSL. | |
| 15 * | |
| 16 * Copyrights | |
| 17 * | |
| 18 * Portions created by or assigned to Jabber.com, Inc. are | |
| 19 * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact | |
| 20 * information for Jabber.com, Inc. is available at http://www.jabber.com/. | |
| 2086 | 21 * |
| 3127 | 22 * Portions Copyright (c) 1998-1999 Jeremie Miller. |
| 23 * | |
| 24 * Acknowledgements | |
| 25 * | |
| 26 * Special thanks to the Jabber Open Source Contributors for their | |
| 27 * suggestions and support of Jabber. | |
| 28 * | |
| 29 * Alternatively, the contents of this file may be used under the terms of the | |
| 30 * GNU General Public License Version 2 or later (the "GPL"), in which case | |
| 31 * the provisions of the GPL are applicable instead of those above. If you | |
| 32 * wish to allow use of your version of this file only under the terms of the | |
| 33 * GPL and not to allow others to use your version of this file under the JOSL, | |
| 34 * indicate your decision by deleting the provisions above and replace them | |
| 35 * with the notice and other provisions required by the GPL. If you do not | |
| 36 * delete the provisions above, a recipient may use your version of this file | |
| 37 * under either the JOSL or the GPL. | |
| 38 * | |
| 39 * | |
| 40 * --------------------------------------------------------------------------*/ | |
| 2086 | 41 |
| 3127 | 42 #include "lib.h" |
| 2086 | 43 |
|
3717
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
44 #ifdef _WIN32 |
|
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
45 #include "win32dep.h" |
|
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
46 #endif |
|
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
47 |
| 2086 | 48 /* socket.c |
| 49 * | |
| 50 * Simple wrapper to make socket creation easy. | |
| 51 * type = NETSOCKET_SERVER is local listening socket | |
| 52 * type = NETSOCKET_CLIENT is connection socket | |
| 3127 | 53 * type = NETSOCKET_UDP is a UDP connection socket |
| 2086 | 54 */ |
| 55 | |
| 56 int make_netsocket(u_short port, char *host, int type) | |
| 57 { | |
| 58 int s, flag = 1; | |
| 59 struct sockaddr_in sa; | |
| 60 struct in_addr *saddr; | |
| 61 int socket_type; | |
| 62 | |
| 63 /* is this a UDP socket or a TCP socket? */ | |
| 64 socket_type = (type == NETSOCKET_UDP)?SOCK_DGRAM:SOCK_STREAM; | |
| 65 | |
| 66 bzero((void *)&sa,sizeof(struct sockaddr_in)); | |
| 67 | |
| 68 if((s = socket(AF_INET,socket_type,0)) < 0) | |
| 69 return(-1); | |
| 70 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) < 0) | |
| 71 return(-1); | |
| 72 | |
| 73 saddr = make_addr(host); | |
| 3127 | 74 if(saddr == NULL && type != NETSOCKET_UDP) |
| 2086 | 75 return(-1); |
| 76 sa.sin_family = AF_INET; | |
| 77 sa.sin_port = htons(port); | |
| 78 | |
| 79 if(type == NETSOCKET_SERVER) | |
| 80 { | |
| 81 /* bind to specific address if specified */ | |
| 82 if(host != NULL) | |
| 83 sa.sin_addr.s_addr = saddr->s_addr; | |
| 84 | |
| 85 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0) | |
| 86 { | |
| 87 close(s); | |
| 88 return(-1); | |
| 89 } | |
| 90 } | |
| 91 if(type == NETSOCKET_CLIENT) | |
| 92 { | |
| 93 sa.sin_addr.s_addr = saddr->s_addr; | |
| 94 if(connect(s,(struct sockaddr*)&sa,sizeof sa) < 0) | |
| 95 { | |
| 96 close(s); | |
| 97 return(-1); | |
| 98 } | |
| 99 } | |
| 100 if(type == NETSOCKET_UDP) | |
| 101 { | |
| 102 /* bind to all addresses for now */ | |
| 103 if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0) | |
| 104 { | |
| 105 close(s); | |
| 106 return(-1); | |
| 107 } | |
| 108 | |
| 3127 | 109 /* if specified, use a default recipient for read/write */ |
| 110 if(host != NULL && saddr != NULL) | |
| 2086 | 111 { |
| 3127 | 112 sa.sin_addr.s_addr = saddr->s_addr; |
| 113 if(connect(s,(struct sockaddr*)&sa,sizeof sa) < 0) | |
| 114 { | |
| 115 close(s); | |
| 116 return(-1); | |
| 117 } | |
| 2086 | 118 } |
| 119 } | |
| 120 | |
| 121 | |
| 122 return(s); | |
| 123 } | |
| 124 | |
| 125 | |
| 126 struct in_addr *make_addr(char *host) | |
| 127 { | |
| 128 struct hostent *hp; | |
| 129 static struct in_addr addr; | |
| 130 char myname[MAXHOSTNAMELEN + 1]; | |
| 131 | |
| 132 if(host == NULL || strlen(host) == 0) | |
| 133 { | |
| 134 gethostname(myname,MAXHOSTNAMELEN); | |
| 135 hp = gethostbyname(myname); | |
| 136 if(hp != NULL) | |
| 137 { | |
| 138 return (struct in_addr *) *hp->h_addr_list; | |
| 139 } | |
| 140 }else{ | |
| 141 addr.s_addr = inet_addr(host); | |
| 142 if(addr.s_addr != -1) | |
| 143 { | |
| 144 return &addr; | |
| 145 } | |
| 146 hp = gethostbyname(host); | |
| 147 if(hp != NULL) | |
| 148 { | |
| 149 return (struct in_addr *) *hp->h_addr_list; | |
| 150 } | |
| 151 } | |
| 152 return NULL; | |
| 153 } | |
| 154 | |
| 3630 | 155 #ifndef _WIN32 |
| 2086 | 156 /* Sets a file descriptor to close on exec. "flag" is 1 to close on exec, 0 to |
| 157 * leave open across exec. | |
| 158 * -- EJB 7/31/2000 | |
| 159 */ | |
| 160 int set_fd_close_on_exec(int fd, int flag) | |
| 161 { | |
| 162 int oldflags = fcntl(fd,F_GETFL); | |
| 163 int newflags; | |
| 164 | |
| 165 if(flag) | |
| 166 newflags = oldflags | FD_CLOEXEC; | |
| 167 else | |
| 168 newflags = oldflags & (~FD_CLOEXEC); | |
| 169 | |
| 170 if(newflags==oldflags) | |
| 171 return 0; | |
| 172 return fcntl(fd,F_SETFL,(long)newflags); | |
| 173 } | |
| 3630 | 174 #endif |
| 2086 | 175 |
