]>
jfr.im git - irc/gameservirc.git/blob - gameserv/sockhelp.cpp
2 * This file is provided for use with the unix-socket-faq. It is public
3 * domain, and may be copied freely. There is no copyright on it. The
4 * original work was by Vic Metcalfe (vic@brutus.tlug.org), and any
5 * modifications made to that work were made with the understanding that
6 * the finished work would be in the public domain.
8 * If you have found a bug, please pass it on to me at the above address
9 * acknowledging that there will be no copyright on your work.
11 * The most recent version of this file, and the unix-socket-faq can be
12 * found at http://www.interlog.com/~vic/sock-faq/.
18 /* Take a service name, and a service type, and return a port number. If
20 service name is not found, it tries it as a decimal number. The number
21 returned is byte ordered for the network. */
22 int atoport(char *service
, char *proto
)
28 lport
= strtol(service
,&errpos
,0);
29 if ( (errpos
[0] != 0) || (lport
< 1) || (lport
> 65535) )
30 return -1; /* Invalid port address */
37 /* Converts ascii text to in_addr struct. NULL is returned if the address
39 struct in_addr
*atoaddr(char *address
)
43 host
= gethostbyname(address
);
45 return (struct in_addr
*) *host
->h_addr_list
;
50 /* This function listens on a port, and returns connections. It forks
51 returns off internally, so your main function doesn't have to worry
52 about that. This can be confusing if you don't know what is going on.
53 The function will create a new process for every incoming connection,
54 so in the listening process, it will never return. Only when a
56 comes in, and we create a new process for it will the function return.
57 This means that your code that calls it should _not_ loop.
59 The parameters are as follows:
60 socket_type: SOCK_STREAM or SOCK_DGRAM (TCP or UDP sockets)
61 port: The port to listen on. Remember that ports < 1024 are
62 reserved for the root user. Must be passed in network byte
63 order (see "man htons").
64 listener: This is a pointer to a variable for holding the file
65 descriptor of the socket which is being used to listen. It
66 is provided so that you can write a signal handler to close
67 it in the event of program termination. If you aren't interested,
68 just pass NULL. Note that all modern unixes will close file
69 descriptors for you on exit, so this is not required. */
70 int get_connection(int socket_type
, u_short port
, int *listener
)
72 struct sockaddr_in address
;
74 int connected_socket
= -1;
78 /* Setup internet address information.
79 This is used with the bind() call */
80 memset((char *) &address
, 0, sizeof(address
));
81 address
.sin_family
= AF_INET
;
82 address
.sin_port
= port
;
83 address
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
85 listening_socket
= socket(AF_INET
, socket_type
, 0);
86 if (listening_socket
< 0) {
92 *listener
= listening_socket
;
94 setsockopt(listening_socket
, SOL_SOCKET
, SO_REUSEADDR
, &reuse_addr
,
97 if (bind(listening_socket
, (struct sockaddr
*) &address
,
98 sizeof(address
)) < 0) {
100 close(listening_socket
);
104 if (socket_type
== SOCK_STREAM
) {
105 listen(listening_socket
, 5); /* Queue up to five connections before
106 having them automatically rejected. */
108 while(connected_socket
< 0) {
109 connected_socket
= accept(listening_socket
, NULL
, NULL
);
110 if (connected_socket
< 0) {
111 /* Either a real error occured, or blocking was interrupted for
112 some reason. Only abort execution if a real error occured. */
113 if (errno
!= EINTR
) {
115 close(listening_socket
);
118 continue; /* don't fork - do the accept again */
122 new_process
= fork();
123 if (new_process
< 0) {
125 close(connected_socket
);
126 connected_socket
= -1;
128 else { /* We have a new process... */
129 if (new_process
== 0) {
130 /* This is the new process. */
131 close(listening_socket
); /* Close our copy of this socket */
132 if (listener
!= NULL
)
133 *listener
= -1; /* Closed in this process. We are not
134 responsible for it. */
137 /* This is the main loop. Close copy of connected socket, and
139 close(connected_socket
);
140 connected_socket
= -1;
144 return connected_socket
;
147 return listening_socket
;
150 /* This is a generic function to make a connection to a given server/port.
151 service is the port name/number,
152 type is either SOCK_STREAM or SOCK_DGRAM, and
153 netaddress is the host name to connect to.
154 The function returns the socket, ready for action.*/
155 int make_connection(char *service
, int type
, char *netaddress
)
157 /* First convert service from a string, to a number... */
159 struct in_addr
*addr
;
161 struct sockaddr_in address
;
163 if (type
== SOCK_STREAM
)
164 port
= atoport(service
, "tcp");
165 if (type
== SOCK_DGRAM
)
166 port
= atoport(service
, "udp");
168 fprintf(stderr
,"make_connection: Invalid socket type.\n");
171 addr
= atoaddr(netaddress
);
173 fprintf(stderr
,"make_connection: Invalid network address.\n");
177 memset((char *) &address
, 0, sizeof(address
));
178 address
.sin_family
= AF_INET
;
179 address
.sin_port
= (port
);
180 address
.sin_addr
.s_addr
= addr
->s_addr
;
182 sock
= socket(AF_INET
, type
, 0);
184 log("Connecting to %s on port %d.",inet_ntoa(*addr
),htons(port
));
186 if (type
== SOCK_STREAM
) {
187 connected
= connect(sock
, (struct sockaddr
*) &address
,
195 /* Otherwise, must be for udp, so bind to address. */
196 if (bind(sock
, (struct sockaddr
*) &address
, sizeof(address
)) < 0) {
203 /* This is just like the read() system call, accept that it will make
204 sure that all your data goes through the socket. */
205 int sock_read(int sockfd
, char *buf
, size_t count
)
207 size_t bytes_read
= 0;
210 while (bytes_read
< count
) {
212 this_read
= read(sockfd
, buf
, count
- bytes_read
);
213 while ( (this_read
< 0) && (errno
== EINTR
) );
216 else if (this_read
== 0)
218 bytes_read
+= this_read
;
224 /* This function reads from a socket, until it recieves a linefeed
225 character. It fills the buffer "str" up to the maximum size "count".
227 This function will return -1 if the socket is closed during the read
230 Note that if a single line exceeds the length of count, the extra data
231 will be read and discarded! You have been warned. */
232 int sock_gets(int sockfd
, char *str
, size_t count
)
235 unsigned int total_count
= 0;
236 char *current_position
;
239 current_position
= str
;
240 while (last_read
!= 10) {
241 bytes_read
= read(sockfd
, &last_read
, 1);
242 if (bytes_read
<= 0) {
243 /* The other side may have closed unexpectedly */
244 return -1; /* Is this effective on other platforms than linux? */
246 if ( (total_count
< count
) && (last_read
!= 10) && (last_read
!=13) )
248 current_position
[0] = last_read
;
254 current_position
[0] = 0;
258 /* This is just like the write() system call, accept that it will
259 make sure that all data is transmitted. */
260 int sock_write(int sockfd
, const char *buf
, size_t count
)
262 size_t bytes_sent
= 0;
265 while (bytes_sent
< count
) {
267 this_write
= write(sockfd
, buf
, count
- bytes_sent
);
268 while ( (this_write
< 0) && (errno
== EINTR
) );
271 bytes_sent
+= this_write
;
277 /* This function writes a character string out to a socket. It will
278 return -1 if the connection is closed while it is trying to write. */
279 int sock_puts(int sockfd
, const char *str
)
281 return sock_write(sockfd
, str
, strlen(str
));
284 /* This ignores the SIGPIPE signal. This is usually a good idea, since
285 the default behaviour is to terminate the application. SIGPIPE is
286 sent when you try to write to an unconnected socket. You should
287 check your return codes to make sure you catch this error! */
288 void ignore_pipe(void)
290 struct sigaction sig
;
292 sig
.sa_handler
= SIG_IGN
;
294 sigemptyset(&sig
.sa_mask
);
295 sigaction(SIGPIPE
,&sig
,NULL
);