00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include "libjwgc.h"
00045
00046
00047
00048
00049
00050
00051
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
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
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
00120 if (bind(s, (struct sockaddr *) & sa, sizeof sa) < 0) {
00121 close(s);
00122 return (-1);
00123 }
00124
00125
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
00169
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 }