|
|
#include "libjwgc.h"
Go to the source code of this file.
Functions | |
int | get_netport () |
int | make_netsocket (u_short port, char *host, int type) |
in_addr * | make_addr (char *host) |
int | set_fd_close_on_exec (int fd, int flag) |
Variables | |
int | __Xode_port = -1 |
|
Definition at line 57 of file JNetSock.c. 00058 {
00059 return __Xode_port;
00060 }
|
|
Definition at line 141 of file JNetSock.c. References MAXHOSTNAMELEN, and NULL. Referenced by make_netsocket(). 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 }
|
|
Definition at line 63 of file JNetSock.c. References __Xode_port, make_addr(), and NULL. Referenced by jab_start(). 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 }
|
|
Definition at line 172 of file JNetSock.c. 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 }
|
|
Definition at line 54 of file JNetSock.c. Referenced by make_netsocket(). |
Last updated at Tue Dec 18 21:07:42 PST 2007. | This site and project hosted by... |